Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created April 17, 2013 20:15
Show Gist options
  • Save bodokaiser/5407385 to your computer and use it in GitHub Desktop.
Save bodokaiser/5407385 to your computer and use it in GitHub Desktop.
How could I let multiple readable stream read on a source readable stream?
var stream = require('stream');
var sstream = new stream.Readable();
var rstream1 = new stream.Readable();
var rstream2 = new stream.Readable();
sstream._read = function() {};
rstream1._read = function() {
var chunk = sstream.read();
if (chunk === null)
return this.push('');
sstream.unshift(chunk.slice(2));
this.push(chunk.slice(0, 2));
this.push(null);
};
rstream2._read = function() {
var chunk = sstream.read();
if (chunk === null)
return this.push('');
sstream.unshift(chunk.slice(2));
return this.push(chunk.slice(0, 2));
this.push(null);
};
sstream.on('readable', function() {
rstream1.read(0);
});
sstream.on('readable', function() {
rstream2.read(0);
});
rstream1.on('readable', function() {
console.log('rstream1', rstream1.read());
});
rstream2.on('readable', function() {
console.log('rstream1', rstream2.read());
});
sstream.push(new Buffer([0x01, 0x02]));
sstream.push(new Buffer([0x03, 0x04]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment