I am playing around with streams V2 and I was wondering whether it is possible to get the following example working somehow:
//
// Reading stuff
//
var ReadingStuff = function() {
this._data = [1, 2, 3, 4, 5];
stream.Readable.call(this);
};
util.inherits(ReadingStuff, stream.Readable);
ReadingStuff.prototype._read = function() {
if(0 === this._data.length) {
this.push(null);
return;
}
this.push(this._data[0].toString());
this._data.shift();
};
//
// Writing stuff
//
var WritingStuff = function() {
stream.Writable.call(this);
this.on('finish', function() {
console.log('Finished writing stuff!!');
});
};
util.inherits(WritingStuff, stream.Writable);
WritingStuff.prototype._write = function(chunk, encoding, next) {
console.log(chunk.toString(encoding));
next();
};
//
// Application
//
var readingStuff = new ReadingStuff();
var writingStuff = new WritingStuff();
readingStuff.pipe(writingStuff);
process.nextTick(function() {
var readingStuff2 = new ReadingStuff();
readingStuff2.pipe(writingStuff);
});
I have two different read streams that I want to pipe to a single write stream on separate iterations of the event loop (suppose I implement the write stream as a singleton in my application). What I see is that the output of the first read stream is written to the write stream and when it is done, the 'finish' event is called. The output of the second stream is completely ignored.
Is there a way to write the output of the second read stream to the write stream?