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
|
||||
// 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)
|
||||
this.emit('readable');
|
||||
else
|
||||
if (state.length > 0) {
|
||||
state.needReadable = false;
|
||||
if (!state.emittedReadable) {
|
||||
state.emittedReadable = true;
|
||||
this.emit('readable');
|
||||
}
|
||||
} else
|
||||
endReadable(this);
|
||||
}
|
||||
return;
|
||||
@ -254,7 +262,10 @@ function onread(er, chunk) {
|
||||
|
||||
if (state.needReadable && !sync) {
|
||||
state.needReadable = false;
|
||||
this.emit('readable');
|
||||
if (!state.emittedReadable) {
|
||||
state.emittedReadable = true;
|
||||
this.emit('readable');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user