@xylabs/threads
Version:
Web workers & worker threads as simple as a function call
32 lines (28 loc) • 1.86 kB
TypeScript
import { F as FunctionThread, M as ModuleThread, S as StripAsync, W as Worker$1, B as BlobWorker$1, a as WorkerImplementation } from './master-C8XAGDqb.js';
import './master/pool-browser.js';
import { W as WorkerFunction, a as WorkerModule } from './worker-04t9iwDh.js';
import './master/implementation.browser.js';
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$1, options?: {
timeout?: number;
}): Promise<ExposedToThreadType<Exposed>>;
type BlobWorker = typeof BlobWorker$1;
/** Separate class to spawn workers from source code blobs or strings. */
declare const BlobWorker: typeof BlobWorker$1;
type Worker = Worker$1;
/** Worker implementation. Either web worker or a node.js Worker class. */
declare const Worker: typeof WorkerImplementation;
export { BlobWorker as B, type ExposedToThreadType as E, Worker as W, spawn as s };