Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132import { getRuntime } from './runtime.ts';import type { Task, WorkerHandle } from './runner.ts';
let nextUid = 0;
/** * A deferred streaming computation. When dispatched via a {@link Runner} or iterated directly, * returns an `AsyncIterable` of yielded values instead of a `Promise`. * Created by calling an `async function*` wrapped with {@link mo}. */export class AsyncIterableTask<T> implements AsyncIterable<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 generator function. */ constructor(id: string, args: unknown[]) { this.uid = nextUid++; this.id = id; this.args = args; }
/** Enables `for await...of` by dispatching to the global runtime. @returns An iterator of yielded values. */ [Symbol.asyncIterator](): AsyncIterator<T> { // cast carries T: the result brand is type-only, so inference from `this` yields Task<unknown> const iterable = getRuntime()(this as Task<AsyncIterable<T>>); return iterable[Symbol.asyncIterator](); }}