@interopio/gateway
Version:
[](https://www.npmjs.com/package/@interopio/gateway)
53 lines (44 loc) • 1.79 kB
TypeScript
export type MeshEndpointType = 'cluster' | 'node' | 'peer';
export type MeshMessage<T = string> = {
source: { type: MeshEndpointType };
receiver: { type: MeshEndpointType };
body: { type: T };
}
export type MeshEvent<T = string>
= { type: 'member-added', node: string }
| { type: 'member-removed', node: string }
| { type: 'message-received', message: MeshMessage<T> };
export type MeshAction<T = string>
= { type: 'publish-message', message: MeshMessage<T> }
| { type: 'add-users', added: readonly string[] }
| { type: 'remove-users', removed: readonly string[] };
export type MeshSubscriber<T = string>
= (event: MeshEvent<T>, node: string, mesh: MeshChannel) => void;
export interface MeshChannel {
/**
* Subscribes to the mesh channel with optional node identifier.
*
* After this call, the subscriber will be notified when a new event is received from the mesh network for this node.
*
* @param node node identifier. If not provided, a random node id will be generated.
* @param subscriber subscriber to be notified when a new event is received from the mesh network for this node.
*
* @returns the node identifier.
*/
subscribe<T = string>(node?: string, subscriber?: MeshSubscriber<T>): string;
/**
* Unsubscribes from the mesh channel for a given node identifier.
*
* @param node node identifier to be unregistered.
*/
unsubscribe(node: string): void;
/**
* Executes an action on behalf of a node.
*
* After this call, the action will be executed on the mesh network.
*
* @param node node identifier.
* @param action action to be executed.
*/
execute<T = string>(node: string, action: MeshAction<T>): void;
}