streambattery

Creates an array of writeable streams, then calls a callback when they're all done with a keyed object of the streams' results.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
streambattery
000.1.06 years ago6 years agoMinified + gzip package size for streambattery in KB

Readme

StreamBattery
Creates an array of writeable streams, then calls a callback when they're all done with a keyed object of the streams' results.
A class to make working with multiple readable streams not such a horrendous pain.
Originally created to work with Dockerode's demuxer modem, but could be applied elsewhere.

- [`streams`](#streams)   
- [`exit()`, `close()`, or `end()`](#exit-close-or-end)   

Usage

Create a StreamBattery and give it an array of keys, then a callback. It will create a writeable stream for each key you specified, and expose them in an array via the .streams property. When all streams have finished writing, callback will be called. Alternately if you decide you want to stop, you can call .exit().
const StreamBattery = require('streambattery');

const battery = new StreamBattery(['streamOne', 'streamTwo'], callback);

// Now you can use battery.streams to expose Writeables to anything.

fetch('https://jsonplaceholder.typicode.com/posts/1').then(r => r.body.pipe(battery.streams[0]));
fetch('https://jsonplaceholder.typicode.com/posts/2').then(r => r.body.pipe(battery.streams[1]));

// When they're all finished, callback will be called with an object that has keys of the original names you passed in.

// {
//   streamOne: '<a string of JSON data>',
//   streamTwo: '<a string of JSON data>'
// }

Properties and Methods

streams

An Array of objects that act like writeable streams. Specifically, WriteableValueProxy objects. They are in the order that you originally specified your keys.

exit(), close(), or end()

Call this to trigger the callback early if you want to for some reason. It won't complain if it's called multiple times, but it won't do anything on subsequent calls. Triggering this method will also emit close on all of the streams.