Skip to content

Instantly share code, notes, and snippets.

@ry
Created October 24, 2011 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry/1309814 to your computer and use it in GitHub Desktop.
Save ry/1309814 to your computer and use it in GitHub Desktop.
From 813ef44acd83b35580c1a14214c712ab7a56ef35 Mon Sep 17 00:00:00 2001
From: Ryan Dahl <ry@tinyclouds.org>
Date: Mon, 24 Oct 2011 11:55:02 -0700
Subject: [PATCH] Remove resume and pause events
---
doc/api/streams.markdown | 3 ---
lib/stream.js | 37 ++++++-------------------------------
lib/util.js | 16 ----------------
3 files changed, 6 insertions(+), 50 deletions(-)
diff --git a/doc/api/streams.markdown b/doc/api/streams.markdown
index 441c608..6637b39 100644
--- a/doc/api/streams.markdown
+++ b/doc/api/streams.markdown
@@ -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
diff --git a/lib/stream.js b/lib/stream.js
index 2869e82..90d4d5c 100644
--- a/lib/stream.js
+++ b/lib/stream.js
@@ -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);
@@ -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);
@@ -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);
diff --git a/lib/util.js b/lib/util.js
index a13a70d..bb6b840 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -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();
});
--
1.7.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment