trimble-connect-workspace-api
Version:
Trimble Connect Workspace API
62 lines (61 loc) • 2.48 kB
TypeScript
/**
* Dispatcher that can post messages across process boundaries.
*/
export interface IDispatcher {
/**
* Posts a message to the target process.
* @param message The message object.
* @param targetOrigin The target origin, or "*" if any origin is allowed.
*/
postMessage(message: unknown, targetOrigin: string): void;
}
/**
* Handler that processes the incoming messages.
*/
export interface IMessageHandler {
/**
* Handles an event.
* @param dispatcher The event source dispatcher.
* @param origin The event source origin.
* @param event The event identifier.
* @param data The event data.
*/
event?: (dispatcher: IDispatcher, origin: string, event: string, data: unknown) => void;
/**
* Handles an API request.
* @param dispatcher The request source dispatcher.
* @param origin The request source origin.
* @param api The requested API entry point.
* @param args The request arguments.
* @returns A promise that resolves into a result, or nothing if the handler cannot handle the request.
*/
request?: (dispatcher: IDispatcher, origin: string, api: string, args: unknown[]) => Promise<unknown> | undefined;
}
/**
* Callback for stopping a listener.
*/
export declare type StopListening = () => void;
/**
* Begins listening for incoming messages.
* @param handler The message handler.
* @returns A callback that stops the listener.
*/
export declare function listen(handler: IMessageHandler): StopListening;
/**
* Sends a request through a dispatcher and returns a promise that resolves into a result.
* @param dispatcher The dispatcher.
* @param origin The expected target origin. Use "*" to allow any origin.
* @param api The requested API entry point.
* @param args The request arguments.
* @param timeout The optional timeout in milliseconds. Zero disables the timeout completely.
*/
export declare function sendRequest(dispatcher: IDispatcher, origin: string, api: string, args: unknown[], timeout?: number): Promise<unknown>;
/**
* Sends an event through the dispatcher.
* @param dispatcher The dispatcher.
* @param origin The expected target origin. Use "*" to allow any origin.
* @param event The event identifier.
* @param data The event data.
*/
export declare function sendEvent(dispatcher: IDispatcher, origin: string, event: string, data: unknown): void;
export declare function dispatcherEventListener(e: MessageEvent): Promise<void>;