Skip to main content

React Native Host Service

info

This documentation is for React Native SDK 2.3.x

Host Service lets the host application register named JavaScript functions that journey JavaScript can call with await HostService.invokeFunction(name, args, callbacks).

Functions​

registerFunction​

registerFunction(
name: string,
handler: HostFunctionHandler
): Promise<void>

Registers a named host function callable from journey JavaScript as await HostService.invokeFunction(name, args, callbacks). Re-registering the same name replaces the previous handler and logs a warning. Call it after DataSapien.initialize has completed; the native Host Service is not available before that.

Parameters:

  • name: string — Unique function name as seen from journey JavaScript.
  • handler: HostFunctionHandler — Invoked when a script calls this function, see HostFunctionHandler.
await DataSapien.getHostService().registerFunction("getShoppingCart", async (args, callbacks) => {
const cart = await CartStore.load();
return { items: cart.items, total: cart.total };
});

unregisterFunction​

unregisterFunction(name: string): Promise<void>

Removes a previously registered function. No-op if it is not present.

Parameters:

  • name: string — Name of the function to remove.

cancelAllFunctions​

cancelAllFunctions(reason?: string): Promise<void>

Rejects all in-flight host-function calls with the given reason. Typically called when the journey hosting the JavaScript runtime is dismissed or cancelled.

Parameters:

  • reason?: string — Rejection reason delivered to the scripts. Defaults to "journey_cancelled".

Classes​

HostFunctionHandler​

type HostFunctionHandler = (
args: HostFunctionArgs,
callbacks: HostCallbacks
) => unknown | Promise<unknown>;

Handler signature registered with Host Service. The returned or resolved value fulfills the script's promise; throwing rejects it with the error's message. Stream intermediate values back to the script with HostCallbacks.call before returning. Returning undefined or null resolves the script's promise with null.

HostFunctionArgs​

class HostFunctionArgs {
readonly raw: Record<string, any>;

get(key: string): any;
value<T>(key: string): T | undefined;
require<T>(key: string): T;
}

Wraps the JSON args object delivered to a host function handler. value returns the value typed as T, or undefined if it is absent or null; require throws invalid_args: missing '<key>' in that case.

HostCallbacks​

class HostCallbacks {
call(name: string, value?: unknown): void;
has(name: string): boolean;
}

Proxy passed to host handlers for the callback functions the script supplied. call fires a named callback with an optional value (a safe no-op if the script did not provide it). has is meant to return whether the script provided a callback with this name; the native bridge does not yet forward the script's callback names, so it currently always returns true.