mirror of https://github.com/nodejs/node.git
stream.pipe should remove listeners on dest close
parent
2320497992
commit
c321e9893d
|
@ -10,13 +10,17 @@ exports.Stream = Stream;
|
|||
Stream.prototype.pipe = function (dest, options) {
|
||||
var source = this;
|
||||
|
||||
source.on("data", function (chunk) {
|
||||
function ondata (chunk) {
|
||||
if (false === dest.write(chunk)) source.pause();
|
||||
});
|
||||
}
|
||||
|
||||
dest.on("drain", function () {
|
||||
source.on("data", ondata);
|
||||
|
||||
function ondrain () {
|
||||
if (source.readable) source.resume();
|
||||
});
|
||||
}
|
||||
|
||||
dest.on("drain", ondrain);
|
||||
|
||||
/*
|
||||
* If the 'end' option is not supplied, dest.end() will be called when
|
||||
|
@ -24,11 +28,20 @@ Stream.prototype.pipe = function (dest, options) {
|
|||
*/
|
||||
|
||||
if (!options || options.end !== false) {
|
||||
source.on("end", function () {
|
||||
function onend () {
|
||||
dest.end();
|
||||
});
|
||||
}
|
||||
|
||||
source.on("end", onend);
|
||||
}
|
||||
|
||||
dest.on('close', function () {
|
||||
dest.removeListener('data', ondata);
|
||||
dest.removeListener('drain', ondrain);
|
||||
dest.removeListener('end', onend);
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
* Questionable:
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue