Browse Source

streams2: Only emit 'readable' when needed

v0.9.4-release
isaacs 12 years ago
parent
commit
286c54439a
  1. 15
      lib/_stream_readable.js

15
lib/_stream_readable.js

@ -73,6 +73,7 @@ function ReadableState(options, stream) {
// whenever we return null, then we set a flag to say
// that we're awaiting a 'readable' event emission.
this.needReadable = false;
this.emittedReadable = false;
this.decoder = null;
if (options.encoding) {
@ -125,6 +126,9 @@ Readable.prototype.read = function(n) {
var state = this._readableState;
var nOrig = n;
if (typeof n !== 'number' || n > 0)
state.emittedReadable = false;
n = howMuchToRead(n, state);
// if we've ended, and we're now clear, then finish it up.
@ -224,9 +228,13 @@ function onread(er, chunk) {
// if we've ended and we have some data left, then emit
// 'readable' now to make sure it gets picked up.
if (!sync) {
if (state.length > 0)
if (state.length > 0) {
state.needReadable = false;
if (!state.emittedReadable) {
state.emittedReadable = true;
this.emit('readable');
else
}
} else
endReadable(this);
}
return;
@ -254,8 +262,11 @@ function onread(er, chunk) {
if (state.needReadable && !sync) {
state.needReadable = false;
if (!state.emittedReadable) {
state.emittedReadable = true;
this.emit('readable');
}
}
}
// abstract method. to be overridden in specific implementation classes.

Loading…
Cancel
Save