stream.pipe should remove listeners on dest close

v0.7.4-release
Ryan Dahl 2010-11-20 23:08:45 -08:00
parent 2320497992
commit c321e9893d
1 changed files with 19 additions and 6 deletions

View File

@ -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:
*/