Created
November 30, 2017 20:08
-
-
Save coyotte508/fe33b1b8c284a45a76b01b5f3d1d19b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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