Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132import { runOnDedicated } from './dedicated-runner.ts';import type { WorkerHandle } from './runner.ts';
let nextUid = 0;
/** * A deferred computation that runs on a worker thread when awaited or dispatched via a {@link Runner}. * Created by calling a function wrapped with {@link mo}. */export class PromiseLikeTask<T> implements PromiseLike<T> { readonly uid: number; readonly id: string; readonly args: unknown[]; worker?: WorkerHandle;
/** @param id - The moroutine identifier (module URL + index). * @param args - The arguments to pass to the worker function. */ constructor(id: string, args: unknown[]) { this.uid = nextUid++; this.id = id; this.args = args; }
/** Enables `await task` by dispatching to a dedicated worker. @returns The worker function's result. */ then<T1 = T, T2 = never>( onfulfilled?: ((value: T) => T1 | PromiseLike<T1>) | null, onrejected?: ((reason: any) => T2 | PromiseLike<T2>) | null, ): Promise<T1 | T2> { return runOnDedicated<T>(this.id, this.args).then(onfulfilled, onrejected); }}