@xylabs/threads
Version:
Web workers & worker threads as simple as a function call
29 lines (25 loc) • 1.51 kB
TypeScript
import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker } from '../master-zgnSDvrP.js';
import 'observable-fns';
import '../observable-promise.js';
type WorkerFunction = ((...args: any[]) => any) | (() => any);
type WorkerModule<Keys extends string> = {
[key in Keys]: WorkerFunction;
};
type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
somekeythatisneverusedinproductioncode123: 'magicmarker123';
};
type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>;
type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
/**
* Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
* abstraction layer to provide the transparent API and verifies that
* the worker has initialized successfully.
*
* @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
* @param [options]
* @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
*/
declare function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(worker: Worker, options?: {
timeout?: number;
}): Promise<ExposedToThreadType<Exposed>>;
export { type ExposedToThreadType, spawn };