UNPKG

@ziziyi/invoker

Version:
140 lines (134 loc) 4.39 kB
type FuncName = string | number; interface InvokeReq<T = any[]> { func: FuncName; args?: T; reply?: boolean; timeout?: number; signal?: AbortSignal; name?: string; [key: string]: any; } interface InvokeReqMsg<T = any[]> extends InvokeReq { _key: string | number; _id: string; } interface InvokeRes { key: string | number; func?: FuncName; success: boolean; value: any; name: string; } interface IInvoker { readonly name: string; readonly IGNORE: Symbol; add(func: FuncName, service: Function): this; remove(func: string): this; invoke<T = any>(req: InvokeReq): Promise<T | null>; waitInvoke<T extends any[]>({ func, timeout, }: { func: FuncName; timeout?: number; }): Promise<{ args: T; }>; } declare abstract class Invoker<Req extends InvokeReq = InvokeReq> implements IInvoker { readonly name: string; protected readonly uniqueId: string; private count; private services; private responsePromises; private waitingPromises; private pendingInvokers; readonly IGNORE: symbol; currentSender: any; constructor(name: string); protected get key(): string; get pendingReqs(): number; abstract send(msg: InvokeReqMsg, req: InvokeReq): PromiseLike<void | { res?: any; }>; abstract sendRes(res: InvokeRes, sender: any): PromiseLike<void>; invoke<T = any>(req: Req): Promise<T>; add(func: FuncName, service: Function): this; remove(func: FuncName): this; waitInvoke<T extends any[]>({ func, timeout, }: { func: FuncName; timeout?: number; }): Promise<{ args: T; }>; protected getReturnValue<T = any>(key: string | number, req: InvokeReq, receipt?: { res?: any; } | void): Promise<T>; handleResMsg(message: InvokeRes): void; handleReqMsg(req: Req, sender?: any): Promise<InvokeRes | null | undefined>; } interface ExtInvokerReq extends InvokeReq { tabId?: number; } declare class ExtMsgInvoker extends Invoker<ExtInvokerReq> { readonly invokeMsgType: string; readonly resMsgType: string; currentSender: chrome.runtime.MessageSender | null; constructor(name: string, options?: { invokeMsgType: string; resMsgType: string; }); send(msg: InvokeReqMsg, req: ExtInvokerReq): Promise<void>; sendRes(res: InvokeRes, sender: chrome.runtime.MessageSender): Promise<void>; listen(): () => void; } type Options$1 = { win?: Window | (() => Window); peer: HTMLIFrameElement | Window | (() => HTMLIFrameElement | Window); peerOrigin?: string; invokeMsgType?: string; resMsgType?: string; }; declare class WindowMsgInvoker extends Invoker { readonly peer: Options$1["peer"]; readonly win: Options$1["win"]; peerOrigin: string; readonly invokeMsgType: string; readonly resMsgType: string; private _unlisten?; constructor(name: string, options: Options$1); send(msg: InvokeReqMsg, req: InvokeReq): Promise<void>; sendRes(res: InvokeRes, sender: Window): Promise<void>; listen(): () => void; unlisten(): void; } type Options = { target?: EventTarget | (() => EventTarget); eventType?: string; invokeMsgType?: string; resMsgType?: string; }; declare class EventInvoker extends Invoker { readonly target: Options["target"]; readonly eventType: string; readonly invokeMsgType: string; readonly resMsgType: string; private _unlisten?; constructor(name: string, options?: Options); send(msg: InvokeReqMsg, req: InvokeReq): Promise<void>; sendRes(res: InvokeRes, sender?: any): Promise<void>; listen(): () => void; unlisten(): void; } declare class ExtConnectInvoker extends Invoker { readonly invokeMsgType: string; readonly resMsgType: string; port: chrome.runtime.Port | null; constructor(name: string, options?: { invokeMsgType: string; resMsgType: string; }); send(msg: InvokeReqMsg, req: InvokeReq): Promise<void>; sendRes(res: InvokeRes, port: chrome.runtime.Port): Promise<void>; listen(onConnect?: (port: chrome.runtime.Port) => void): () => void; connect(tabId?: number): void; get isConnected(): boolean; } export { EventInvoker, ExtConnectInvoker, ExtMsgInvoker, WindowMsgInvoker as FrameMsgInvoker, Invoker, WindowMsgInvoker };