LCMicroService

@interface LCMicroService : NSObject

An LCMicroService is the basic building block of LiquidCore. It encapsulates the runtime environment for a client-side micro app. An LCMicroService is a complete virtual machine whose operation is defined by the code referenced by the service URI. When an LCMicroService is instantiated, its Node.js environment is set up, its code downloaded (or fetched from cache) from the URI, and is executed in a VM. The host may interact with the VM via a simple message-based API.

  • Each LCMicroService instance is mapped to a unique string id. This id can be serialized in UIs and the instance retrieved by a call to +serviceFromInstanceId:

    Declaration

    Objective-C

    @property (readonly, copy, nonatomic, nonnull) NSString *instanceId;

    Swift

    var instanceId: UnsafeMutablePointer<Int32> { get }
  • The Node.js Process object.

    Declaration

    Objective-C

    @property (readonly, atomic, nullable) LCProcess *process;

    Swift

    var process: LCProcess? { get }
  • The URI from which the service was started.

    Declaration

    Objective-C

    @property (readonly, nonatomic, nonnull) NSURL *serviceURI;

    Swift

    var serviceURI: UnsafeMutablePointer<Int32> { get }
  • Each LCMicroService instance is mapped to a unique string id. This id can be serialized in UIs and the instance retrieved by a call to this method.

    Declaration

    Objective-C

    + (id _Nullable)serviceFromInstanceId:(NSString *_Nonnull)instanceId;

    Swift

    class func service(fromInstanceId instanceId: Any!) -> Any?

    Parameters

    instanceId

    An id returned by the instanceId property

    Return Value

    The associated LCMicroService or nil if no such service is active.

  • Uninstalls the LCMicroService from this host, and removes any global data associated with the service.

    Declaration

    Objective-C

    + (void)uninstall:(NSURL *_Nonnull)serviceURI;

    Swift

    class func uninstall(_ serviceURI: Any!)

    Parameters

    serviceURI

    The URI of the service (should be the same URI that the service was started with).

  • Generates a URL for fetching from a development server on the loopback address (localhost).

    Declaration

    Objective-C

    + (NSURL *_Nonnull)devServer:(NSString *_Nullable)fileName
                            port:(NSNumber *_Nullable)port;

    Swift

    class func devServer(_ fileName: Any!, port: Any!) -> Any!

    Parameters

    fileName

    The name of the bundled javascript file to fetch (default: liquid.js)

    port

    The server’s port (default: 8082)

    Return Value

    A service URL for use in the LCMicroService constructor

  • Generates a URL for fetching from a development server on the loopback address (localhost). Assumes the entry js file is liquid.js and is served from port 8082 on the emulator’s host machine.

    Declaration

    Objective-C

    + (NSURL *_Nonnull)devServer;

    Swift

    class func devServer() -> Any!

    Return Value

    A service URL for use in the LCMicroService constructor

  • Creates a new instance of the micro service referenced by serviceURI.

    Declaration

    Objective-C

    - (id _Nullable)initWithURL:(NSURL *_Nonnull)serviceURI;

    Swift

    init?(url serviceURI: Any!)

    Parameters

    serviceURI

    The URI (can be a network URL or local file/resource) of the micro service code

  • Creates a new instance of the micro service referenced by serviceURI

    Declaration

    Objective-C

    - (id _Nullable)initWithURL:(NSURL *_Nonnull)serviceURI
                       delegate:(id<LCMicroServiceDelegate> _Nullable)delegate;

    Swift

    init?(url serviceURI: Any!, delegate: LCMicroServiceDelegate?)

    Parameters

    serviceURI

    The URI (can be a network URL or local file/resource) of the MicroService code

    delegate

    The LCMicroServiceDelegate for this service

  • Adds an event listener for an event triggered by ‘LiquidCore.emit(event, payload)’ in JavaScript. Example:

         LiquidCore.emit('my_event', { stringData: 'foo', bar : 6 });
    

    This will trigger the listener added here, with the JavaScript object as a Swift/Objective-C dictionary

    Declaration

    Objective-C

    - (void)addEventListener:(NSString *_Nonnull)event
                    listener:(id<LCMicroServiceEventListener> _Nonnull)listener;

    Swift

    func addEventListener(_ event: Any!, listener: LCMicroServiceEventListener)

    Parameters

    event

    The String event id

    listener

    The event listener

  • Removes an EventListener previously added with -addEventListener:listener:.

    Declaration

    Objective-C

    - (void)removeEventListener:(NSString *_Nonnull)event
                       listener:(id<LCMicroServiceEventListener> _Nonnull)listener;

    Swift

    func removeEventListener(_ event: Any!, listener: LCMicroServiceEventListener)

    Parameters

    event

    The event for which to unregister the listener

    listener

    The listener to unregister

  • Emits an event that can be received by the JavaScript code, if the LCMicroService has registered a listener. Example:

    LiquidCore.on('my_event', function() {
       console.log('Received my_event');
    });
    

    On the iOS (Swift) side:

    myService.emit("my_event")
    

    Declaration

    Objective-C

    - (void)emit:(NSString *_Nonnull)event;

    Swift

    func emit(_ event: Any!)

    Parameters

    event

    The event to trigger

  • Emits an event that can be received by the JavaScript code, if the LCMicroService has registered a listener. Example:

    LiquidCore.on('my_event', function(payload) {
       // Do something with the payload data
       console.log(payload.hello);
    });
    

    On the iOS (Swift) side:

    let foo = [ "hello": "world" ]
    myService.emitObject("my_event", object:foo)
    

    Declaration

    Objective-C

    - (void)emitObject:(NSString *_Nonnull)event object:(id _Nullable)object;

    Swift

    func emitObject(_ event: Any!, object: Any?)

    Parameters

    event

    The event to trigger

    object

    The Swift/Objective-C object to emit

  • Emits an event that can be received by the JavaScript code, if the LCMicroService has registered a listener. Example:

    LiquidCore.on('my_event', function(number) {
        // Do something with the payload data
        console.log(payload.number + 2);
    });
    

    On the iOS (Swift) side:

    myService.emitNumber("my_event", number:40)
    

    Declaration

    Objective-C

    - (void)emitNumber:(NSString *_Nonnull)event number:(NSNumber *_Nonnull)number;

    Swift

    func emitNumber(_ event: Any!, number: Any!)

    Parameters

    event

    The event to trigger

    number

    The number to emit

  • Emits an event that can be received by the JavaScript code, if the LCMicroService has registered a listener. Example:

    LiquidCore.on('my_event', function(string) {
        // Do something with the string data
        console.log(string + " are belong to us.");
    });
    

    On the iOS (Swift) side:

    myService.emitString("my_event", string: "All your base")
    

    Declaration

    Objective-C

    - (void)emitString:(NSString *_Nonnull)event string:(NSString *_Nonnull)string;

    Swift

    func emitString(_ event: Any!, string: Any!)

    Parameters

    event

    The event to trigger

    string

    The string to emit

  • Emits an event that can be received by the JavaScript code, if the LCMicroService has registered a listener. Example:

    LiquidCore.on('my_event', function(bool) {
        // Do something with the string data
        console.log(bool === true ? "YUP" : "NOPE");
    });
    

    On the iOS (Swift) side:

    myService.emitBoolean("my_event", boolean:true)
    

    Declaration

    Objective-C

    - (void)emitBoolean:(NSString *_Nonnull)event boolean:(BOOL)boolean;

    Swift

    func emitBoolean(_ event: Any!, boolean: Any!)

    Parameters

    event

    The event to trigger

    boolean

    The boolean to emit

  • Starts the LCMicroService. This method will return immediately and initialization and startup will occur asynchronously in a separate thread. It will download the code from the service URI (if not cached), set the arguments in process.argv and execute the script.

    Declaration

    Objective-C

    - (void)start;

    Swift

    func start()
  • Starts the LCMicroService. This method will return immediately and initialization and startup will occur asynchronously in a separate thread. It will download the code from the service URI (if not cached), set the arguments in process.argv and execute the script.

    Declaration

    Objective-C

    - (void)startWithArguments:(NSArray *_Nullable)argv;

    Swift

    func start(withArguments argv: Any!)

    Parameters

    argv

    The list of arguments to sent to the LCMicroService. This is similar to running node from a command line. The first two arguments will be the application (node) followed by the local module code (/home/module/[service.js]. argv arguments will then be appended in process.argv[2:]