sflow
Version:
sflow is a powerful and highly-extensible library designed for processing and manipulating streams of data effortlessly. Inspired by the functional programming paradigm, it provides a rich set of utilities for transforming streams, including chunking, fil
30 lines (27 loc) • 783 B
text/typescript
import type { Awaitable } from "./Awaitable";
type asyncMapOptions = {
concurrency?: number;
};
/* map a stream by parallel, return them in original order */
export const pMaps: {
<T, R>(
fn: (x: T, i: number) => Awaitable<R>,
options?: asyncMapOptions,
): TransformStream<T, R>;
} = <T, R>(
fn: (x: T, i: number) => Awaitable<R>,
options: asyncMapOptions = {},
) => {
let i = 0;
let promises: Awaitable<R>[] = [];
return new TransformStream<T, R>({
transform: async (chunk, ctrl) => {
promises.push(fn(chunk, i++));
if (promises.length >= (options.concurrency ?? Infinity))
ctrl.enqueue(await promises.shift());
},
flush: async (ctrl) => {
while (promises.length) ctrl.enqueue(await promises.shift());
},
});
};