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 }; /** * Message types/events. */ export type IpcMessageType = string; export type IpcPayload = Record; 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, ) => IpcSending; handle: (type: T['type'], handler: IpcEventHandler) => IpcClient; handlers: (type: M['type'], options?: { exclude?: number | number[] }) => IpcIdentifier[]; }; 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 IpcSending = { eid: string; // The unique event-id. type: M['type']; elapsed: number; $: Observable>; promise: Promise>; timeout$: Observable; cancel$: Observable; results: IpcHandlerResult[]; resultFrom: (sender: number | ProcessType) => IpcHandlerResult | undefined; dataFrom: (sender: number | ProcessType) => D | undefined; cancel: () => IpcSending; isCancelled: boolean; isComplete: boolean; isTimedOut: boolean; }; /** * Response data sent from an event handler. */ export type IpcSendResponse = { elapsed: number; eid: string; // The initial event-id that the response pertains to. data: D; type: M['type']; sender: IpcIdentifier; }; export type IpcHandlerResult = { data?: D; elapsed: number; eid: string; // The initial event-id that the response pertains to. 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: '@platform/IPC/handler/response'; payload: { eid: string; data?: any; }; }; /** * A notification that an event-handler has been registered. */ export type IpcRegisterHandlerEvent = { type: '@platform/IPC/register-handler'; payload: { type: IpcMessage['type']; stage: 'CREATE' | 'DISPOSE'; }; };