stream: return this from pause()/resume()

This commit is contained in:
isaacs 2013-08-27 18:59:58 -07:00
parent f91b047891
commit 689e5c9d3d
5 changed files with 21 additions and 2 deletions

View File

@ -244,6 +244,7 @@ emission of a [`'data'` event][].
#### readable.setEncoding(encoding) #### readable.setEncoding(encoding)
* `encoding` {String} The encoding to use. * `encoding` {String} The encoding to use.
* Return: `this`
Call this function to cause the stream to return strings of the Call this function to cause the stream to return strings of the
specified encoding instead of Buffer objects. For example, if you do specified encoding instead of Buffer objects. For example, if you do
@ -268,6 +269,8 @@ readable.on('data', function(chunk) {
#### readable.resume() #### readable.resume()
* Return: `this`
This method will cause the readable stream to resume emitting `data` This method will cause the readable stream to resume emitting `data`
events. events.
@ -286,6 +289,8 @@ readable.on('end', function(chunk) {
#### readable.pause() #### readable.pause()
* Return: `this`
This method will cause a stream in flowing mode to stop emitting This method will cause a stream in flowing mode to stop emitting
`data` events, switching out of flowing mode. Any data that becomes `data` events, switching out of flowing mode. Any data that becomes
available will remain in the internal buffer. available will remain in the internal buffer.

View File

@ -855,13 +855,14 @@ function Interface(stdin, stdout, args) {
Interface.prototype.pause = function() { 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.repl.rli.pause();
this.stdin.pause(); this.stdin.pause();
return this;
}; };
Interface.prototype.resume = function(silent) { 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(); this.repl.rli.resume();
if (silent !== true) { if (silent !== true) {
this.repl.displayPrompt(); this.repl.displayPrompt();
@ -872,6 +873,7 @@ Interface.prototype.resume = function(silent) {
this.waiting(); this.waiting();
this.waiting = null; this.waiting = null;
} }
return this;
}; };

View File

@ -705,6 +705,7 @@ Readable.prototype.resume = function() {
} }
resume(this, state); resume(this, state);
} }
return this;
}; };
function resume(stream, state) { function resume(stream, state) {
@ -731,6 +732,7 @@ Readable.prototype.pause = function() {
this._readableState.flowing = false; this._readableState.flowing = false;
this.emit('pause'); this.emit('pause');
} }
return this;
}; };
function flow(stream) { function flow(stream) {

View File

@ -268,6 +268,7 @@ Interface.prototype.pause = function() {
this.input.pause(); this.input.pause();
this.paused = true; this.paused = true;
this.emit('pause'); this.emit('pause');
return this;
}; };
@ -276,6 +277,7 @@ Interface.prototype.resume = function() {
this.input.resume(); this.input.resume();
this.paused = false; this.paused = false;
this.emit('resume'); this.emit('resume');
return this;
}; };

View File

@ -473,3 +473,11 @@ test('adding readable triggers data flow', function(t) {
t.end(); 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();
});