I am still playing around with streams V2 and I was wondering whether it is possible to get the following example working somehow:
var stream = require('stream'),
util = require('util');
//
// Reading stuff
//
var ReadingStuff = function() {
this._data = [1, 2, 3, 4, 5];
this._readStreamIndex = 0;
stream.Readable.call(this);
};
util.inherits(ReadingStuff, stream.Readable);
ReadingStuff.prototype._read = function() {
if(this._readStreamIndex === this._data.length) {
this._readStreamIndex = 0;
this.push(null);
return;
}
this.push(this._data[this._readStreamIndex].toString());
this._readStreamIndex += 1;
};
//
// 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();
var writingStuff2 = new WritingStuff();
readingStuff.pipe(writingStuff);
process.nextTick(function() {
// Just simulating another event loop
readingStuff.pipe(writingStuff2);
});
I havce a single read stream that I want to pipe to multiple write streams on separate iterations of the event loop (using process.nextTick to simulate that the read stream is used again later on). What I see is that the output is written to the first writable stream (+ the finish event is called). Afterwards on the finish event is executed for the second writable stream. Is it OK in this case to skip push(null) in the read stream? This means that there's no way to know inside the Writable stream that there's no more data to be wriitten.