Coder Social home page Coder Social logo

anx-bridge's Introduction

anx-bridge

Overview

This is utility AIR Native Extension that allows to add asynchronous calls to other native extension.

Setup

Use com.github.airext.Bridge.swc library and corresponded headers to build ANE and com.github.airext.Bridge.ane for application that uses ANE based on anx-bridge.

Setup ActionScript project

Just link com.github.airext.Bridge.swc as external library.

Setup Objective-C project

  • link corresponded headers to your Xcode bin/headers/ios for iOS project and bin/headers/osx for OSX project;
  • in ContextInitializer function right after function initialization call [ANXBridge setup:functions:]; as showed next:
void TSTContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToSet, const FRENamedFunction** functionsToSet)
{
    *numFunctionsToSet = 1;
    
    FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * (*numFunctionsToSet));
    
    func[0].name = (const uint8_t*) "isSupported";
    func[0].functionData = NULL;
    func[0].function = &ANXTwitterIsSupported;
    
    [ANXBridge setup:numFunctionsToSet functions:&func];

    *functionsToSet = func;
}

this registers additional functions used by anx-bridge.

Setup Application project

<extensions> 
    <extensionID>com.github.airext.Bridge</extensionID>
</extensions>

Usage

ActionScript

Using anx-bridge from ActionScript in one line:

bridge(context).call("someMethod", arg1, arg2).callback(callback);

where:

  • context is ExtensionContext;
  • someMethod is registered function that takes two arguments arg1 and arg2;
  • callback is function with signature function(error:Error = null, value:Object = null):void.

Objective-C

On objective-c part you use [ANXBridge] class for create async calls and [ANXBridgeCall] class for notify failure or result:

// creates call
ANXBridgeCall* call = [ANXBridge call:context];

// notify success where value is FREObject received as second argument in callback on AIR side
[call result: value];

// notify failure where error is NSError that will be converted to ActionScript' Error object
[call reject:error];

// each async method should retrun ANXBridgeCall as FREObject
return [call toFREObject];

Example

Asume we implement Twitter login method (this is real example from fabric/twitter kit):

ActionScript part contains one method login:

public function login(callback:Function):void
{
  bridge(context).call("login").callback(callback);
}

Objective-C context initializer:

void ANXTwitterContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{
    *numFunctionsToTest = 1;
    
    FRENamedFunction* func = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * (*numFunctionsToTest));
    
    func[0].name = (const uint8_t*) "login";
    func[0].functionData = NULL;
    func[0].function = &ANXTwitterLogin;
    
    [ANXBridge setup:numFunctionsToTest functions:&func];

    *functionsToSet = func;
}

Objective-C context login method:

FREObject ANXTwitterLogin(FREContext context, void* functionData, uint32_t argc, FREObject argv[])
{
    ANXBridgeCall* call = [ANXBridge call:context];
    
    [[Twitter sharedInstance] logInWithCompletion:
        ^(TWTRSession *session, NSError *error)
        {
            if (session)
            {
              // assume there is method that converts TWTRSession to FREObject
              FREObject freSession = [self convertSessionToFREObject:session];
            
              [call result:freSession];
            }
            else
            {
                [call reject:error];
            }
        }];
    
    return [call toFREObject];
}

ActionScript usage in end application:

Twitter.sharedInstance().login(
  function(error:Error = null, session:Object = null):void
  {
    if (session != null)
    {
      trace("Logged in as:", session.userName);
    }
    else
    {
      trace("Login failed:", String(error));
    }
  }
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.