@ziziyi/invoker
Version:
130 lines (124 loc) • 4.14 kB
TypeScript
type FuncName = string | number;
interface InvokeReqMsg<T = any[]> {
key?: string | number;
func: FuncName;
args?: T;
reply?: boolean;
invoker?: string;
}
interface InvokeReq<T = any[]> extends InvokeReqMsg<T> {
timeout?: number;
signal?: AbortSignal;
[key: string]: any;
}
interface InvokeRes {
key: string | number;
success: boolean;
value: any;
func?: FuncName;
}
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>;
protected setReturnValue(key: string | number, success: boolean, value: any): void;
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 = {
peer: HTMLIFrameElement | Window | (() => HTMLIFrameElement);
peerOrigin?: string;
invokeMsgType?: string;
resMsgType?: string;
};
declare class FrameMsgInvoker extends Invoker {
readonly peer: Options["peer"] | null;
peerOrigin: string;
readonly invokeMsgType: string;
readonly resMsgType: string;
constructor(name: string, options: Options);
send(msg: InvokeReqMsg, req: InvokeReq): Promise<void>;
sendRes(res: InvokeRes, sender: Window): Promise<void>;
listen(): () => void;
}
declare class EventInvoker extends Invoker {
readonly eventType: string;
readonly invokeMsgType: string;
readonly resMsgType: string;
constructor(name: string, options?: {
eventType: string;
invokeMsgType: string;
resMsgType: string;
});
send(msg: InvokeReqMsg, req: InvokeReq): Promise<void>;
sendRes(res: InvokeRes, sender?: any): Promise<void>;
listen(): () => 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, FrameMsgInvoker, Invoker };