Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add signal, asyncDispose, and WorkerOptions to Runner type

+25 -1
+11 -1
src/runner.ts
··· 4 4 5 5 type TaskResults<T extends Task<any>[]> = { [K in keyof T]: T[K] extends Task<infer R> ? R : never }; 6 6 7 + /** Options for configuring a worker pool. */ 8 + export interface WorkerOptions { 9 + /** Maximum time in ms to wait for in-flight tasks during async dispose. If exceeded, workers are force-terminated. */ 10 + shutdownTimeout?: number; 11 + } 12 + 7 13 /** 8 14 * A callable that dispatches tasks to a worker pool. Disposable via `using` or `[Symbol.dispose]()`. 9 15 * ··· 17 23 <T extends Task<any>[]>(tasks: [...T]): Promise<TaskResults<T>>; 18 24 /** Dispatches a streaming task and returns an async iterable of yielded values. */ 19 25 <T>(task: StreamTask<T>, opts?: ChannelOptions): AsyncIterable<T>; 20 - /** Terminates all workers in the pool. */ 26 + /** AbortSignal that fires when the pool starts disposing. */ 27 + readonly signal: AbortSignal; 28 + /** Terminates all workers immediately. */ 21 29 [Symbol.dispose](): void; 30 + /** Aborts signal, waits for in-flight tasks to settle, then terminates workers. */ 31 + [Symbol.asyncDispose](): Promise<void>; 22 32 };
+14
src/worker-pool.ts
··· 25 25 let next = 0; 26 26 let disposed = false; 27 27 28 + const abortController = new AbortController(); 29 + 28 30 function dispatch<T>(task: Task<T>): Promise<T> { 29 31 if (disposed) return Promise.reject(new Error('Worker pool is disposed')); 30 32 const worker = pool[next % pool.length]; ··· 46 48 return dispatch(taskOrTasks); 47 49 }, 48 50 { 51 + get signal() { 52 + return abortController.signal; 53 + }, 49 54 [Symbol.dispose]() { 55 + disposed = true; 56 + abortController.abort(); 57 + for (const worker of pool) { 58 + worker.terminate(); 59 + } 60 + pool.length = 0; 61 + }, 62 + async [Symbol.asyncDispose]() { 63 + abortController.abort(); 50 64 disposed = true; 51 65 for (const worker of pool) { 52 66 worker.terminate();