Skip to content

Instantly share code, notes, and snippets.

@coyotte508
Created November 30, 2017 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyotte508/fe33b1b8c284a45a76b01b5f3d1d19b4 to your computer and use it in GitHub Desktop.
Save coyotte508/fe33b1b8c284a45a76b01b5f3d1d19b4 to your computer and use it in GitHub Desktop.
var Transform = require('stream').Transform;
class StreamConcat extends Transform {
constructor(streams, options) {
super(options);
this.streams = streams;
this.canAddStream = true;
this.currentStream = null;
this.streamIndex = 0;
this.nextStream();
}
nextStream () {
this.currentStream = null;
if (Array.isArray(this.streams) && this.streamIndex < this.streams.length) {
this.currentStream = this.streams[this.streamIndex++];
} else if (typeof this.streams === 'function') {
this.canAddStream = false;
this.currentStream = self.streams();
}
if (this.currentStream === null) {
this.canAddStream = false;
this.push(null);
} else {
this.currentStream.pipe(this, {end: false});
this.currentStream.on('end', () => this.nextStream());
}
}
addStream (newStream) {
if (this.canAddStream)
this.streams.push(newStream);
else
this.emit('error', new Error('Can\'t add stream.'));
}
_transform (chunk, encoding, callback) {
callback(null, chunk);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment