@xylabs/threads
Version:
Web workers & worker threads as simple as a function call
119 lines (114 loc) • 5.46 kB
TypeScript
import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker$1, B as BlobWorker$1, b as WorkerImplementation } from './master-DBU-F6eB.js';
import { Observable } from 'observable-fns';
import { T as Thread, P as PoolEvent, a as PoolEventType, b as TaskRunFunction, Q as QueuedTask } from './pool-types-BnrcRDgC.js';
import { W as WorkerFunction, a as WorkerModule } from './worker-04t9iwDh.js';
import './master/implementation.node.js';
interface PoolOptions {
/** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
concurrency?: number;
/** Maximum no. of jobs to be queued for execution before throwing an error. */
maxQueuedJobs?: number;
/** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
name?: string;
/** No. of worker threads to spawn and to be managed by the pool. */
size?: number;
}
declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
static EventType: typeof PoolEventType;
private readonly debug;
private readonly eventObservable;
private readonly options;
private readonly workers;
private readonly eventSubject;
private initErrors;
private isClosing;
private nextTaskID;
private taskQueue;
constructor(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions);
private findIdlingWorker;
private runPoolTask;
private run;
private scheduleWork;
private taskCompletion;
settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
completed(allowResolvingImmediately?: boolean): Promise<void>;
events(): Observable<PoolEvent<ThreadType>>;
queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
terminate(force?: boolean): Promise<void>;
}
/**
* Thread pool constructor. Creates a new pool and spawns its worker threads.
*/
declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
declare namespace Pool {
type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
type EventType = PoolEventType;
}
/**
* Thread pool managing a set of worker threads.
* Use it to queue tasks that are run on those threads with limited
* concurrency.
*/
interface Pool<ThreadType extends Thread> {
/**
* Returns a promise that resolves once the task queue is emptied.
* Promise will be rejected if any task fails.
*
* @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
*/
completed(allowResolvingImmediately?: boolean): Promise<any>;
/**
* Returns a promise that resolves once the task queue is emptied.
* Failing tasks will not cause the promise to be rejected.
*
* @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
*/
settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
/**
* Returns an observable that yields pool events.
*/
events(): Observable<PoolEvent<ThreadType>>;
/**
* Queue a task and return a promise that resolves once the task has been dequeued,
* started and finished.
*
* @param task An async function that takes a thread instance and invokes it.
*/
queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
/**
* Terminate all pool threads.
*
* @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
*/
terminate(force?: boolean): Promise<void>;
}
/**
* Thread pool constructor. Creates a new pool and spawns its worker threads.
*/
declare const Pool: typeof PoolConstructor & {
EventType: typeof PoolEventType;
};
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, Pool as P, Worker as W, spawn as s };