import { Readable } from 'node:stream'; import type { Task, Runner } from './runner.ts'; type TaskResult = I extends Task ? 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 | Task`) 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>( run: Runner, items: Iterable | AsyncIterable, opts?: MapOptions, ): AsyncIterable> { return Readable.from(items).map(run, opts) as AsyncIterable>; }