Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233import { Readable } from 'node:stream';import type { Task, Runner } from './runner.ts';
type TaskResult<I> = I extends Task<infer T> ? T : never;
/** Options controlling how {@link map} dispatches tasks to a {@link Runner}. */export interface MapOptions { /** Maximum number of in-flight tasks at once. Defaults to `1`. */ concurrency?: number; /** Signal that, when aborted, stops pulling from `items` and ends iteration. */ signal?: AbortSignal;}
/** * Dispatches tasks yielded by `items` to a worker pool with bounded concurrency, * emitting results in completion order. * * Accepts a union of task types (e.g. `Task<string> | Task<number>`) and emits * the corresponding union of results. * * @param run - A {@link Runner} returned by {@link workers}. * @param items - Iterable or async iterable of inert tasks to dispatch. Use {@link inert} to yield tasks from an async generator without triggering auto-await. * @param opts - Concurrency and cancellation options. * @returns Async iterable of task results in completion order. */export function map<I extends Task<any>>( run: Runner, items: Iterable<I> | AsyncIterable<I>, opts?: MapOptions,): AsyncIterable<TaskResult<I>> { return Readable.from(items).map(run, opts) as AsyncIterable<TaskResult<I>>;}