import { ProcessType } from '../../types'; import { Observable } from 'rxjs'; export { ProcessType }; /** * Global refs in [main] process. */ export type IpcHandlerRef = { type: IpcMessage['type']; clients: IpcIdentifier[]; }; export type IpcHandlerRefs = { [key: string]: IpcHandlerRef }; export type IpcGlobalMainRefs = { // NB: Internal use only. DO NOT TOUCH THESE. _ipcRefs: { client?: IpcClient; handlers: IpcHandlerRefs; }; }; /** * Message types/events. */ export type IpcMessageType = string; export type IpcPayload = object; export type IpcMessage = { type: string; payload: { [key: string]: any }; }; export type IpcIdentifier = { id: number; process: ProcessType }; export type IpcEvent = { eid: string; // Unique "event id". type: M['type']; payload: M['payload']; sender: IpcIdentifier; targets: number[]; // BrowserWindow ID, or `0` for MAIN process. }; export type IpcEventObservable = Observable>; export type IpcFilter = ( e: IpcEvent, index: number, ) => boolean; /** * Definition of the IPC client. */ export type IpcClient = { /** * [Properties] */ readonly MAIN: number; // The ID of the main process. readonly process: ProcessType; readonly events$: IpcEventObservable; readonly id: number; /** * [Methods] */ dispose: () => void; filter: ( criteria: IpcFilter | T['type'], ) => IpcEventObservable; on: (type: T['type']) => IpcEventObservable; send: ( type: T['type'], payload: T['payload'], options?: IpcClientSendOptions, ) => IpcSend; handle: ( type: T['type'], handler: IpcEventHandler, ) => IpcClient; }; export type IpcClientSendOptions = { target?: number | number[]; // Target window-id, or `0` for MAIN process. timeout?: number; // Msecs. Default taken from `client.timeout`. }; /** * The response object returned by a `send` operation. */ export type IpcSend = { eid: string; // The unique event-id. type: M['type']; results$: Observable>; timeout$: Observable<{}>; cancel$: Observable<{}>; results: Array<{ sender: IpcIdentifier; data?: D }>; cancel: () => IpcSend; isCancelled: boolean; }; /** * Response data sent from an event handler. */ export type ISendResponse = { elapsed: number; data: D; type: M['type']; sender: IpcIdentifier; }; /** * The event passed to an event-handler with methods for responding. */ export type IpcEventHandlerArgs = IpcEvent & {}; export type IpcEventHandler = ( e: IpcEventHandlerArgs, ) => Promise; /** * [EVENTS] */ /** * The response fired back from a registered handler. */ export type IpcHandlerResponseEvent = { type: './SYS/IPC/handler/response'; payload: { eid: string; data?: any; }; }; /** * A notification that an event-handler has been registered. */ export type IpcRegisterHandlerEvent = { type: './SYS/IPC/register-handler'; payload: { type: IpcMessage['type']; stage: 'CREATE' | 'DISPOSE'; }; };