web-stream-map
Version:
A map function for Web Streams with concurrency support. Like node's ReadableStream.map(), but for Web Streams.
26 lines (23 loc) • 584 B
JavaScript
import sCompose from "s-compose";
import pLimit from "p-limit";
export default function sMap(fn, { concurrency = 1 } = {}) {
const limit = pLimit(concurrency);
return sCompose(
new TransformStream(
{
async transform(chunk, controller) {
const p = limit(() => fn(chunk));
controller.enqueue(p);
},
},
{},
{ highWaterMark: concurrency * 2 - 1 }
),
new TransformStream({
async transform(chunk, controller) {
const result = await chunk;
controller.enqueue(result);
},
})
);
}