Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created September 29, 2012 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/3804825 to your computer and use it in GitHub Desktop.
Save isaacs/3804825 to your computer and use it in GitHub Desktop.
module.exports = Concat;
var util = require('util');
var Readable = require('readable-stream');
var assert = require('assert');
// mostly for instanceof checks or whatever.
// but also for .pipe() etc
util.inherits(Concat, Readable)
function Concat(/* readable, readable... */) {
// no relevant options, since we override read() anyway.
Readable.call(this);
this._length = arguments.length;
this._sources = new Array(this._length);
for (var i = 0; i < this._length; i++) {
var source = this._sources[i] = arguments[i];
// only new readables are supported.
assert(source instanceof Readable);
source.on('end', this._next.bind(this));
source.on('error', this.emit.bind(this, 'error'));
}
this._index = 0;
}
Concat.prototype.read = function(n) {
return this._sources[this._index].read(n);
};
Concat.prototype._next = function() {
this._index++;
if (this._index === this._length{
return this.emit('end');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment