/** @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`. * - `oneArgFunc(arg: U): R` becomes `async oneArgFunc(arg: U, transfer?: Transferable[]): Promise`. * - `multiArgFunc(arg1: U, arg2: V): R` becomes `async multiArgFunc(args: [U, V], transfer?: Transferable[]): Promise`. * @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 = { [P in keyof T]: T[P] extends () => any ? () => Promise> : (T[P] extends (arg: any) => any ? (arg: Parameters[0], transfer?: Transferable[]) => Promise> : (T[P] extends (...args: any) => any ? (args: Parameters, transfer?: Transferable[]) => Promise> : 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 any> = MaybePromise | { result: Awaited>; 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`. * - `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 = { [P in keyof T]: T[P] extends () => any ? () => WorkerReturnType : (T[P] extends (arg: any) => any ? (arg: Parameters[0]) => WorkerReturnType : (T[P] extends (...args: any) => any ? (args: Parameters) => WorkerReturnType : 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 = WorkerInterface & { /** 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(workerJsPath: string): WorkerProxy; //# sourceMappingURL=WorkerProxy.d.ts.map