@push-stream-std/push-interleave (0.0.3)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-interleave@0.0.3"@push-stream-std/push-interleave": "0.0.3"About this package
push-interleave
Alternate elements from multiple streams.
Published as @push-stream-std/push-interleave.
Synopsis
import { interleave } from '@push-stream-std/push-interleave';
import { values } from '@push-stream-std/push-values';
import { collect } from '@push-stream-std/push-collect';
const a = values([1, 2, 3]);
const b = values(['a', 'b', 'c']);
const c = values([true, false]);
interleave([a, b, c])
.pipe(collect((err, results) => {
console.log(results);
// [1, 'a', true, 2, 'b', false, 3, 'c']
}));
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-interleave
Description
Takes multiple push-stream sources and interleaves their output in round-robin order, taking one item from each source per cycle. This is useful for fair scheduling across concurrent data producers, multiplexing multiple input streams into a single output, or load balancing stream processing. Internally buffers items per source using an O(1) amortized head pointer pattern.
API
interleave(sources, options)
Creates a push-stream source that interleaves items from multiple source streams.
Parameters:
sources(Array) — An array of push-stream source objects.options.endOnFirst(boolean, defaultfalse) — Iftrue, ends the interleaved stream as soon as the first source ends.
Returns: A push-stream source.
Tests
npm test
Tests cover round-robin ordering, handling of sources with different lengths, graceful source completion, and endOnFirst termination.
Requirements
Node.js 18 or later. ESM only.
License
MIT