Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Remove resume and pause events
Browse files Browse the repository at this point in the history
Too complex; unnecessary.
  • Loading branch information
ry committed Oct 24, 2011
1 parent 4c5751b commit 239b3d6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 50 deletions.
3 changes: 0 additions & 3 deletions doc/api/streams.markdown
Expand Up @@ -93,9 +93,6 @@ This keeps `process.stdout` open so that "Goodbye" can be written at the end.
process.stdout.write("Goodbye\n");
});

NOTE: If the source stream does not support `pause()` and `resume()`, this function
adds simple definitions which simply emit `'pause'` and `'resume'` events on
the source stream.

## Writable Stream

Expand Down
37 changes: 6 additions & 31 deletions lib/stream.js
Expand Up @@ -33,14 +33,18 @@ Stream.prototype.pipe = function(dest, options) {

function ondata(chunk) {
if (dest.writable) {
if (false === dest.write(chunk)) source.pause();
if (false === dest.write(chunk) && source.pause) {
source.pause();
}
}
}

source.on('data', ondata);

function ondrain() {
if (source.readable) source.resume();
if (source.readable && source.resume) {
source.resume();
}
}

dest.on('drain', ondrain);
Expand Down Expand Up @@ -103,32 +107,6 @@ Stream.prototype.pipe = function(dest, options) {
source.on('error', onerror);
dest.on('error', onerror);

// guarantee that source streams can be paused and resumed, even
// if the only effect is to proxy the event back up the pipe chain.
if (!source.pause) {
source.pause = function() {
source.emit('pause');
};
}

if (!source.resume) {
source.resume = function() {
source.emit('resume');
};
}

function onpause() {
source.pause();
}

dest.on('pause', onpause);

function onresume() {
if (source.readable) source.resume();
}

dest.on('resume', onresume);

// remove all the event listeners that were added.
function cleanup() {
source.removeListener('data', ondata);
Expand All @@ -137,9 +115,6 @@ Stream.prototype.pipe = function(dest, options) {
source.removeListener('end', onend);
source.removeListener('close', onclose);

dest.removeListener('pause', onpause);
dest.removeListener('resume', onresume);

source.removeListener('error', onerror);
dest.removeListener('error', onerror);

Expand Down
16 changes: 0 additions & 16 deletions lib/util.js
Expand Up @@ -466,30 +466,14 @@ exports.pump = function(readStream, writeStream, callback) {
}
}

if (!readStream.pause) {
readStream.pause = function() {readStream.emit('pause');};
}

if (!readStream.resume) {
readStream.resume = function() {readStream.emit('resume');};
}

readStream.addListener('data', function(chunk) {
if (writeStream.write(chunk) === false) readStream.pause();
});

writeStream.addListener('pause', function() {
readStream.pause();
});

writeStream.addListener('drain', function() {
readStream.resume();
});

writeStream.addListener('resume', function() {
readStream.resume();
});

readStream.addListener('end', function() {
writeStream.end();
});
Expand Down

0 comments on commit 239b3d6

Please sign in to comment.