streams2: Only emit 'readable' when needed
This commit is contained in:
parent
62dd04027b
commit
286c54439a
@ -73,6 +73,7 @@ function ReadableState(options, stream) {
|
|||||||
// whenever we return null, then we set a flag to say
|
// whenever we return null, then we set a flag to say
|
||||||
// that we're awaiting a 'readable' event emission.
|
// that we're awaiting a 'readable' event emission.
|
||||||
this.needReadable = false;
|
this.needReadable = false;
|
||||||
|
this.emittedReadable = false;
|
||||||
|
|
||||||
this.decoder = null;
|
this.decoder = null;
|
||||||
if (options.encoding) {
|
if (options.encoding) {
|
||||||
@ -125,6 +126,9 @@ Readable.prototype.read = function(n) {
|
|||||||
var state = this._readableState;
|
var state = this._readableState;
|
||||||
var nOrig = n;
|
var nOrig = n;
|
||||||
|
|
||||||
|
if (typeof n !== 'number' || n > 0)
|
||||||
|
state.emittedReadable = false;
|
||||||
|
|
||||||
n = howMuchToRead(n, state);
|
n = howMuchToRead(n, state);
|
||||||
|
|
||||||
// if we've ended, and we're now clear, then finish it up.
|
// 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
|
// if we've ended and we have some data left, then emit
|
||||||
// 'readable' now to make sure it gets picked up.
|
// 'readable' now to make sure it gets picked up.
|
||||||
if (!sync) {
|
if (!sync) {
|
||||||
if (state.length > 0)
|
if (state.length > 0) {
|
||||||
|
state.needReadable = false;
|
||||||
|
if (!state.emittedReadable) {
|
||||||
|
state.emittedReadable = true;
|
||||||
this.emit('readable');
|
this.emit('readable');
|
||||||
else
|
}
|
||||||
|
} else
|
||||||
endReadable(this);
|
endReadable(this);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -254,9 +262,12 @@ function onread(er, chunk) {
|
|||||||
|
|
||||||
if (state.needReadable && !sync) {
|
if (state.needReadable && !sync) {
|
||||||
state.needReadable = false;
|
state.needReadable = false;
|
||||||
|
if (!state.emittedReadable) {
|
||||||
|
state.emittedReadable = true;
|
||||||
this.emit('readable');
|
this.emit('readable');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// abstract method. to be overridden in specific implementation classes.
|
// abstract method. to be overridden in specific implementation classes.
|
||||||
// call cb(er, data) where data is <= n in length.
|
// call cb(er, data) where data is <= n in length.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user