@push-stream-std/push-share (0.0.6)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-share@0.0.6"@push-stream-std/push-share": "0.0.6"About this package
push-share
Share a single push-stream source subscription among multiple sinks.
Published as @push-stream-std/push-share.
Synopsis
import { share } from '@push-stream-std/push-share';
import { values } from '@push-stream-std/push-values';
import { collect } from '@push-stream-std/push-collect';
const source = values([1, 2, 3]);
const shared = share(source);
collect({ stream: shared, result: false }, (err, results) => {
console.log('sink 1:', results); // [1, 2, 3]
});
collect({ stream: shared, result: false }, (err, results) => {
console.log('sink 2:', results); // [1, 2, 3]
});
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-share
Description
A push-stream multicasting operator that takes a single source stream and fans out its values to multiple sinks. The underlying source is subscribed only once, avoiding duplicate work or side effects. Values are broadcast to all connected sinks. Sinks can be dynamically added and removed via pipe() and unpipe(). When all sinks are unsubscribed, the source subscription is ended. Each sink receives the same sequence of values from the shared source.
API
share(source)
source— A push-stream source object (withpipe, optionallyresume).
Returns a push-stream through with pipe(), unpipe(), end(), abort(), and resume().
stream.pipe(sink)
Adds a sink to the shared subscription. If this is the first sink, the source is subscribed. Returns the sink.
stream.unpipe(sink)
Removes a sink from the shared subscription. When all sinks are removed, the source is ended.
Tests
npm test
Tests cover multiple sinks receiving all values, dynamic sink addition, sink removal with unpiped, source auto-subscription on first pipe, and source auto-end when all sinks disconnect.
Requirements
Node.js 18 or later. ESM only.
License
MIT