@push-stream-std/push-switch-map (0.0.8)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-switch-map@0.0.8"@push-stream-std/push-switch-map": "0.0.8"About this package
@push-stream-std/push-switch-map
Map each value to an inner push-stream source, canceling the previous inner stream on each new outer value.
Published as @push-stream-std/push-switch-map.
Synopsis
import switchMap from '@push-stream-std/push-switch-map';
const innerA = values(['A1', 'A2', 'A3']);
const innerB = values(['B1', 'B2']);
const switcher = switchMap(function (streamId) {
return streamId === 'A' ? innerA : innerB;
});
values(['A', 'B']).pipe(switcher).pipe(sink);
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-switch-map
Why
A push-stream through that applies a mapper function to each incoming value. The mapper must return a push-stream source (inner). When a new outer value arrives, the previous inner source is aborted and the new inner source is piped to the sink via an internal proxy. Values from the inner source are forwarded downstream. If the mapper throws, the stream ends with the thrown error.
API
switchMap(mapper)
Creates a switch-map through stream.
- mapper
(outerValue) => innerSource– Function that takes an outer value and returns a push-stream source. The returned source should havepipe(),abort(), and optionallyresume(). - Returns A through object with
pipe(),write(),end(),abort(),resume(),source,paused,ended.
Tests
npm test
Tests cover canceling previous inner on new outer value, forwarding inner values, handling mapper errors, normal end, abort propagation to current inner, and empty/undefined inner source handling.
Requirements
Node.js 22 or later. ESM only.