figwire
Version:
Bidirectional IPC communication between UI and core in Figma plugins. Lightweight and typed.
44 lines (41 loc) • 1.19 kB
TypeScript
type Method = (...args: any[]) => any;
interface Methods {
[k: string]: Method;
}
type Message = RequestMessage | ResponseMessage | ErrorMessage;
interface RequestMessage<A = any[]> {
type: 'request';
name: string;
args: A;
}
interface ResponseMessage<R = void> {
type: 'response';
name: string;
return?: R;
}
interface ErrorMessage {
type: 'error';
name: string;
message: string;
}
interface APIOptions {
silentError: boolean;
}
type MethodPromises<T extends Record<string, (...args: any[]) => any>> = {
[K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : never;
};
declare class API {
private name;
private postFn;
private receiveFn;
private options;
private requestQueue;
private callbacks;
hasher: {
create(): number;
};
constructor(name: string, postFn: (message: Message) => void, receiveFn: (callback: (message: Message) => void) => void, options?: APIOptions);
request(name: string, args?: any[]): Promise<unknown>;
registerMethod(name: string, callback: Method): void;
}
export { API as A, type Methods as M, type MethodPromises as a };