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 types script and workers() overloads

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+11 -5
+1
package.json
··· 25 25 "build": "tsc -p tsconfig.build.json", 26 26 "changeset": "changeset", 27 27 "release": "changeset version && changeset tag && git add -A && git commit -m \"chore: version $(node -p \"require('./package.json').version\")\" && git push --follow-tags", 28 + "types": "tsc", 28 29 "lint": "prettier --check .", 29 30 "lint:fix": "prettier --write .", 30 31 "test": "node --test 'test/**/*.test.ts'"
+10 -5
src/worker-pool.ts
··· 16 16 * @param opts - Optional configuration including shutdown timeout and balancer. 17 17 * @returns A disposable {@link Runner} for dispatching tasks. 18 18 */ 19 + export function workers(): Runner; 20 + export function workers(size: number, opts?: WorkerOptions): Runner; 21 + export function workers(opts: WorkerOptions): Runner; 19 22 export function workers(sizeOrOpts?: number | WorkerOptions, opts?: WorkerOptions): Runner { 20 23 let size: number; 21 24 if (typeof sizeOrOpts === 'object') { ··· 43 46 function track<T>(handle: WorkerHandle, promise: Promise<T>): Promise<T> { 44 47 inflight.add(promise); 45 48 activeCounts.set(handle, (activeCounts.get(handle) ?? 0) + 1); 46 - promise.finally(() => { 47 - inflight.delete(promise); 48 - activeCounts.set(handle, (activeCounts.get(handle) ?? 1) - 1); 49 - }).catch(() => {}); 49 + promise 50 + .finally(() => { 51 + inflight.delete(promise); 52 + activeCounts.set(handle, (activeCounts.get(handle) ?? 1) - 1); 53 + }) 54 + .catch(() => {}); 50 55 return promise; 51 56 } 52 57 ··· 75 80 76 81 function resolveWorkerAndHandle(task: Task<any> | StreamTask<any>): { worker: Worker; handle: WorkerHandle } { 77 82 if (task.worker != null) { 78 - const idx = workerHandles.indexOf(task.worker as WorkerHandle); 83 + const idx = workerHandles.indexOf(task.worker); 79 84 if (idx !== -1) return { worker: pool[idx], handle: workerHandles[idx] }; 80 85 } 81 86 const handle = balancer.select(workerHandles, task);