import { runStreamOnDedicated } from './dedicated-runner.ts'; import type { 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 implements AsyncIterable { 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 a dedicated worker. @returns An iterator of yielded values. */ [Symbol.asyncIterator](): AsyncIterator { const iterable = runStreamOnDedicated(this.id, this.args); return iterable[Symbol.asyncIterator](); } }