You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
652 B
35 lines
652 B
4 years ago
|
var WriteStream = require("write-stream")
|
||
|
|
||
|
module.exports = EndStream
|
||
|
|
||
|
function EndStream(write, end) {
|
||
|
var counter = 0
|
||
|
, ended = false
|
||
|
|
||
|
end = end || noop
|
||
|
|
||
|
var stream = WriteStream(function (chunk) {
|
||
|
counter++
|
||
|
write(chunk, function (err) {
|
||
|
if (err) {
|
||
|
return stream.emit("error", err)
|
||
|
}
|
||
|
|
||
|
counter--
|
||
|
|
||
|
if (counter === 0 && ended) {
|
||
|
stream.emit("finish")
|
||
|
}
|
||
|
})
|
||
|
}, function () {
|
||
|
ended = true
|
||
|
if (counter === 0) {
|
||
|
this.emit("finish")
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return stream
|
||
|
}
|
||
|
|
||
|
function noop() {}
|