-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (32 loc) · 957 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var PassThrough = require('readable-stream/passthrough')
var inherits = require('util').inherits
inherits(Multi, PassThrough)
module.exports = Multi
function Multi(options) {
if (!(this instanceof Multi)) return new Multi(options)
PassThrough.call(this, options)
this._readableState.objectMode =
this._writableState.objectMode = true
this._sources = []
this.on('pipe', this._onpipe)
}
Multi.prototype.pull = function (stream) {
var onsourceend = this._onsourceend.bind(this, stream)
return stream
.on('error', onsourceend)
.on('end', onsourceend)
.on('close', onsourceend)
.pipe(this, {
end: false
})
}
Multi.prototype._onpipe = function (stream) {
this._sources.push(stream)
}
Multi.prototype._onsourceend = function (stream) {
var sources = this._sources
var index = sources.indexOf(stream)
if (!~index) return // already removed or something
sources.splice(index, 1)
if (!sources.length) this.end()
}