UNPKG

@zlepper/rpc

Version:

Allows RPC from the main thread to a background worker thread (Of any kind), using ES6 classes.

10 lines (9 loc) 913 B
import { WorkerClientConnection } from './worker-client-connection.js'; import { IEventDispatcher, InferEvent, NormalizedEventTarget } from '../shared/normalized-event-target'; export type AsyncProperty<T> = T extends (...args: infer TArgs) => infer TResult ? (...args: TArgs) => TResult extends Promise<any> ? TResult : Promise<TResult> : Promise<T>; export type WrappedObject<T> = T extends IEventDispatcher<infer TEvent> ? { readonly [property in keyof Omit<T, 'dispatchEvent'> as T[property] extends Function ? property : never]: AsyncProperty<T[property]>; } & NormalizedEventTarget<TEvent> : { readonly [property in keyof T as T[property] extends Function ? property : never]: AsyncProperty<T[property]>; }; export declare function wrapBackgroundService<T extends object | IEventDispatcher<TEvent>, TEvent extends object = InferEvent<T>>(workerConnection: WorkerClientConnection): WrappedObject<T>;