TL DR How can I read some data from a stream and then return it so that other users can receive the same event data?
Here's a readable stream that passes streams 1 ... Infinity:
var Readable = require('stream').Readable;
var readable = new Readable();
var c = 0;
readable._read = function () {
var self = this;
setTimeout(function () {
self.push((++c).toString());
}, 500);
};
I want to read the first event data, look at the data, and then "reset" the stream to its original state and allow another listener to datause the first event, as if this had not happened. I thought that unshift()would be the correct method, as he says in the docs:
readable.unshift (fragment) #
chunk Buffer | , , " " , , .
, , :
...
readable.once('data', function (d) {
console.log(d.toString());
readable.unshift(d);
readable.on('data', function (d) {
console.log(d.toString());
});
});