import { 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 implements PromiseLike { 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( onfulfilled?: ((value: T) => T1 | PromiseLike) | null, onrejected?: ((reason: any) => T2 | PromiseLike) | null, ): Promise { return runOnDedicated(this.id, this.args).then(onfulfilled, onrejected); } }