UNPKG

@itwin/core-frontend

Version:
59 lines 4.41 kB
/** @packageDocumentation * @module Utils */ import { MaybePromise } from "@itwin/core-bentley"; /** Given an interface T that defines the operations provided by a worker, produce an interface that can be used to asynchronously invoke those operations * from the main thread, optionally passing an array of values to be transferred from the main thread to the worker. * - Every return type is converted to a `Promise` (i.e., every function becomes `async`)>. * - `zeroArgFunc(): R` becomes `async zeroArgFunc(): Promise<R>`. * - `oneArgFunc(arg: U): R` becomes `async oneArgFunc(arg: U, transfer?: Transferable[]): Promise<R>`. * - `multiArgFunc(arg1: U, arg2: V): R` becomes `async multiArgFunc(args: [U, V], transfer?: Transferable[]): Promise<R>`. * @note All parameters of all methods of `T` must support [structured cloning](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) - * attempts to pass functions, instances of classes, DOM nodes, WebGL resources, and other non-cloneable types will compile but fail at run-time. * @beta */ export type WorkerInterface<T> = { [P in keyof T]: T[P] extends () => any ? () => Promise<ReturnType<T[P]>> : (T[P] extends (arg: any) => any ? (arg: Parameters<T[P]>[0], transfer?: Transferable[]) => Promise<ReturnType<T[P]>> : (T[P] extends (...args: any) => any ? (args: Parameters<T[P]>, transfer?: Transferable[]) => Promise<ReturnType<T[P]>> : never)); }; /** Augments each method of `T` with the ability to specify values to be transferred from the worker thread to the main thread. * Each return type `R` is replaced with `R | { result: R; transfer: Transferable[]; }`, or a promise that resolves to such a type. * @see [[WorkerImplementation]]. * @beta */ export type WorkerReturnType<T extends (...args: any) => any> = MaybePromise<ReturnType<T> | { result: Awaited<ReturnType<T>>; transfer: Transferable[]; }>; /** Given an interface T that defines the operations provided by a worker, produce an interface to which the implementation of those operations must conform. * The return type of each function is enhanced to permit supplying a list of values to be transferred from the worker to the main thread. * Multi-argument functions are converted to functions accepting a single tuple of arguments. * - Every return type `R` is converted to `WorkerReturnType<R>`. * - `zeroArgFunc(): R` becomes `zeroArgFunc(): R | { result: R; transfer: Transferable[]; }`. * - `oneArgFunc(arg: U): R` becomes `oneArgFunc(arg: U): R | { result: R; transfer: Transferable[]; }`. * - `multiArgFunc(arg1: U, arg2: V): R` becomes `multiArgFunc([U, V]): R | { result: R; transfer: Transferable[]; }`. * @note All parameters of all methods of `T` must support [structured cloning](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) - * attempts to pass functions, instances of classes, DOM nodes, WebGL resources, and other non-cloneable types will compile but fail at run-time. * @beta */ export type WorkerImplementation<T> = { [P in keyof T]: T[P] extends () => any ? () => WorkerReturnType<T[P]> : (T[P] extends (arg: any) => any ? (arg: Parameters<T[P]>[0]) => WorkerReturnType<T[P]> : (T[P] extends (...args: any) => any ? (args: Parameters<T[P]>) => WorkerReturnType<T[P]> : never)); }; /** A proxy for a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker) that provides the operations specified by * the methods of `T`. * To use a worker proxy, define a worker script that provides [[registerWorker]] with an implementation of `T`, and obtain a proxy * via [[createWorkerProxy]] on the main thread. The proxy can then be used to asynchronously invoke methods of `T` on the worker. * See [this article]($docs/learning/frontend/WorkerProxy.md) for more details and examples. * @beta */ export type WorkerProxy<T> = WorkerInterface<T> & { /** Terminate the worker. */ terminate(): void; /** Returns true if [[terminate]] has been called. */ readonly isTerminated: boolean; }; /** Create a [[WorkerProxy]] implementing the methods of `T` using the specified worker script. * See [this article]($docs/learning/frontend/WorkerProxy.md) for more details and examples. * @beta */ export declare function createWorkerProxy<T>(workerJsPath: string): WorkerProxy<T>; //# sourceMappingURL=WorkerProxy.d.ts.map