patient-stream
v1.0.0
Published
a pass-through stream that pauses while you pipe
Downloads
17
Readme
patient-stream
A stream that can be piped to many destinations over several ticks, so long as you know in advance how many destinations you'll need. Why? Because sometimes it's a drag to wrap everything in Passthrough streams in the same tick.
Usage
Pause your input stream (important). Construct a Patient
with the number of destinations you expect to add. At your leisure, bearing in mind that you've now got backpressure on your input, add all your destinations. When you've added as many as your Patient
expects, the input will be resumed for you. The Patient
will emit end
when all its destinations have emitted finish
.
var fs = require('fs'),
Request = require('request'),
Patient = require('patient-stream');
var input = Request.get('http://placekitten.com/400/400');
input.pause(); // required
var out1 = new fs.createWriteStream('kitten_copy1.jpg');
var out2 = new fs.createWriteStream('kitten_copy2.jpg');
tee.on('end', function()
{
// do something with the two lovely kitten jpgs
});
var tee = new Patient(2);
input.pipe(tee);
tee.pipe(out1);
setTimeout(function()
{
tee.pipe(out2);
}, 1000);
API
PatientStream
is a passthrough stream, so it has the same API as a duplex stream.
License
ISC