From 689e5c9d3db67ccfb81a1caefb04176c41a17744 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 27 Aug 2013 18:59:58 -0700 Subject: [PATCH] stream: return this from pause()/resume() --- doc/api/stream.markdown | 5 +++++ lib/_debugger.js | 6 ++++-- lib/_stream_readable.js | 2 ++ lib/readline.js | 2 ++ test/simple/test-stream2-basic.js | 8 ++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/doc/api/stream.markdown b/doc/api/stream.markdown index 881b33808af2ef..1ee834f867c7e1 100644 --- a/doc/api/stream.markdown +++ b/doc/api/stream.markdown @@ -244,6 +244,7 @@ emission of a [`'data'` event][]. #### readable.setEncoding(encoding) * `encoding` {String} The encoding to use. +* Return: `this` Call this function to cause the stream to return strings of the specified encoding instead of Buffer objects. For example, if you do @@ -268,6 +269,8 @@ readable.on('data', function(chunk) { #### readable.resume() +* Return: `this` + This method will cause the readable stream to resume emitting `data` events. @@ -286,6 +289,8 @@ readable.on('end', function(chunk) { #### readable.pause() +* Return: `this` + This method will cause a stream in flowing mode to stop emitting `data` events, switching out of flowing mode. Any data that becomes available will remain in the internal buffer. diff --git a/lib/_debugger.js b/lib/_debugger.js index aca52e5f300f0e..e67d010260912f 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -855,13 +855,14 @@ function Interface(stdin, stdout, args) { Interface.prototype.pause = function() { - if (this.killed || this.paused++ > 0) return false; + if (this.killed || this.paused++ > 0) return this; this.repl.rli.pause(); this.stdin.pause(); + return this; }; Interface.prototype.resume = function(silent) { - if (this.killed || this.paused === 0 || --this.paused !== 0) return false; + if (this.killed || this.paused === 0 || --this.paused !== 0) return this; this.repl.rli.resume(); if (silent !== true) { this.repl.displayPrompt(); @@ -872,6 +873,7 @@ Interface.prototype.resume = function(silent) { this.waiting(); this.waiting = null; } + return this; }; diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 450b50658570d3..e1636c85a27c24 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -705,6 +705,7 @@ Readable.prototype.resume = function() { } resume(this, state); } + return this; }; function resume(stream, state) { @@ -731,6 +732,7 @@ Readable.prototype.pause = function() { this._readableState.flowing = false; this.emit('pause'); } + return this; }; function flow(stream) { diff --git a/lib/readline.js b/lib/readline.js index 8b5ad99efca023..e45fdeac794463 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -268,6 +268,7 @@ Interface.prototype.pause = function() { this.input.pause(); this.paused = true; this.emit('pause'); + return this; }; @@ -276,6 +277,7 @@ Interface.prototype.resume = function() { this.input.resume(); this.paused = false; this.emit('resume'); + return this; }; diff --git a/test/simple/test-stream2-basic.js b/test/simple/test-stream2-basic.js index 3814bf07b469b2..f210c9f0309cbe 100644 --- a/test/simple/test-stream2-basic.js +++ b/test/simple/test-stream2-basic.js @@ -473,3 +473,11 @@ test('adding readable triggers data flow', function(t) { t.end(); }); }); + +test('chainable', function(t) { + var r = new R(); + r._read = function() {}; + var r2 = r.setEncoding('utf8').pause().resume().pause(); + t.equal(r, r2); + t.end(); +});