Class: Drmer

core.Drmer

The middleware that native(Android, iOS, Desktop etc) and JS talk to each other. The JS side doesn't need to know which native platform it is running on. Calls to the instance will be bridged to the bond bridge.

new core.Drmer () overrides

Example


 import {Drmer} from "@drmer/core";
 const drmer = new Drmer();
 // 1. bind the bridge
 drmer.bindBridge(bridge);
 // or you can expose a global variable to `window` as name `androidBridge`, `browserBridge`, `desktopBridge`,
 // `macBridge`, `iOSBridge`, `drmer` will automatically bind it.

 // 2. call native functions
 drmer.run("AppService@test");
 drmer.callJson("ProjectService@get", {
   "id": 1,
 }).then((project) => {
  // process project
  console.log(project);
 });
 // or
 (async () => {
   const project = await drmer.callJson("ProjectService@get", {
     "id": 1,
   });
   console.log(project);
 })();

 // 3. native returns the results
 drmer.dequeue("li123", {
   volume: 10,
 });

 //
 // When you needs native to call JS proactively, you can register an listener to
 // an event on JS, then emit that event from native.

 // On JS
 drmer.on("app.pause", () => {
   // do things when app pause
 });

 // On native
 drmer.emit("app.pause");

Extends

Members

bridge core.IBridge | undefined

The current bond bridge

Methods

bindBridge (bridge) void

Bind bridge manually, you need to unbind the bridge first if other bridge is bond first. After the bridge is bond, this instance will turn into the ready state.

Name Type Description
bridge core.IBridge

Bridge that will handle the calls.

call (method, params) Promise<any>

Send commands to native Use this if you need the results. Use run instead if you don't needs the results.

Name Type Attributes Description
method string

Method signature

params Object <optional>

Arguments

Returns:
Type Description
Promise<any>

callJson (method, params) Promise<any>

Send commands to native Use this if you need to deserialize the result by JSON. Use run instead if you don't needs the results.

Name Type Attributes Description
method string

Method signature

params Object <optional>

Arguments

Returns:
Type Description
Promise<any>

dequeue (id, res) void

This is called by the native parts to return results for remote calls.

Name Type Description
id string

Job id

res Object

Results

die (id) void

Unregister for native live jobs

Name Type Description
id string

job id

lazyBindBridge (bridge, timeout) void

Bind the given bridge after some time. This gives us a chance that we can bind other bridges before the given time. For example, we can lazy bind the browser bridge when running on iOS, when the native bridge on iOS is ready before the given time, we will use it. And our programs can also run on browser with the given browser bridge.

import {bridge} from "@drmer/browser";
drmer.lazyBindBridge(bridge);
drmer.lazyBindBridge(bridge, 1000);
Name Type Default Description
bridge core.IBridge

bridge to be bound

timeout number 500

milliseconds to wait to bind given bridge

live (method, params, cbFn) string | null

Call native and get live results

Name Type Attributes Description
method string

Method signature

params Object <optional>

payload

cbFn Function <optional>

callback listener

Returns:
Type Description
string | null job id

onReady (fcn) void overrides

Register for callback when this instance turns into the ready state, will be called directly if it was ready. This will also start waiting for the bridge.

Name Type Description
fcn () => void

run (method, params) void

Send commands to native Use this if you don't need the results. Use call or callJson if you need to get the result.

Name Type Attributes Description
method string

Method signature id

params Object <optional>

Arguments

unbindBridge () void

Unbind the bond bridge

Inherited Properties

From class core.Readily

ready boolean inherited

Whether this instance is ready, set true to this field will mark this instance as ready, and will run the callback functions registered by onReady.

Inherited Methods

From class core.EventEmitter

emit (event, …args) boolean inherited

Fires an event with args, this will call each of the listeners registered for the given event.

Name Type Description
event T

the event id

args any

related arguments

Returns:
Type Description
boolean