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.

at e2cebd539403475e2f4e79d55ca1a84a0ce3510d 31 lines 1.1 kB view raw
1import { runOnDedicated } from './dedicated-runner.ts'; 2import type { WorkerHandle } from './runner.ts'; 3 4let nextUid = 0; 5 6/** 7 * A deferred computation that runs on a worker thread when awaited or dispatched via a {@link Runner}. 8 * Created by calling a function wrapped with {@link mo}. 9 */ 10export class PromiseLikeTask<T> implements PromiseLike<T> { 11 readonly uid: number; 12 readonly id: string; 13 readonly args: unknown[]; 14 worker?: WorkerHandle; 15 16 /** @param id - The moroutine identifier (module URL + index). 17 * @param args - The arguments to pass to the worker function. */ 18 constructor(id: string, args: unknown[]) { 19 this.uid = nextUid++; 20 this.id = id; 21 this.args = args; 22 } 23 24 /** Enables `await task` by dispatching to a dedicated worker. @returns The worker function's result. */ 25 then<T1 = T, T2 = never>( 26 onfulfilled?: ((value: T) => T1 | PromiseLike<T1>) | null, 27 onrejected?: ((reason: any) => T2 | PromiseLike<T2>) | null, 28 ): Promise<T1 | T2> { 29 return runOnDedicated<T>(this.id, this.args).then(onfulfilled, onrejected); 30 } 31}