Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
stream: Handle late 'readable' event listeners
Browse files Browse the repository at this point in the history
In cases where a stream may have data added to the read queue before the
user adds a 'readable' event, there is never any indication that it's
time to start reading.

True, there's already data there, which the user would get if they
checked However, as we use 'readable' event listening as the signal to
start the flow of data with a read(0) call internally, we ought to
trigger the same effect (ie, emitting a 'readable' event) even if the
'readable' listener is added after the first emission.

To avoid confusing weirdness, only the *first* 'readable' event listener
is granted this privileged status.  After we've started the flow (or,
alerted the consumer that the flow has started) we don't need to start
it again.  At that point, it's the consumer's responsibility to consume
the stream.

Closes #5141
  • Loading branch information
isaacs committed Mar 28, 2013
1 parent 5ae26f3 commit eafa902
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function ReadableState(options, stream) {
// that we're awaiting a 'readable' event emission.
this.needReadable = false;
this.emittedReadable = false;
this.readableListening = false;


// object stream flag. Used to make read(n) ignore n and to
Expand Down Expand Up @@ -378,7 +379,6 @@ function emitReadable(stream) {
}

function emitReadable_(stream) {
var state = stream._readableState;
stream.emit('readable');
}

Expand Down Expand Up @@ -655,8 +655,19 @@ Readable.prototype.on = function(ev, fn) {
if (ev === 'data' && !this._readableState.flowing)
emitDataEvents(this);

if (ev === 'readable' && !this._readableState.reading)
this.read(0);
if (ev === 'readable' && this.readable) {
var state = this._readableState;
if (!state.readableListening) {
state.readableListening = true;
state.emittedReadable = false;
state.needReadable = true;
if (!state.reading) {
this.read(0);
} else if (state.length) {
emitReadable(this, state);
}
}
}

return res;
};
Expand Down
82 changes: 82 additions & 0 deletions test/simple/test-stream-readable-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');

var Readable = require('stream').Readable;

(function first() {
// First test, not reading when the readable is added.
// make sure that read(0) triggers a readable event.
var r = new Readable({
highWaterMark: 3
});

r._read = function(n) {
r.push(new Buffer(new Array(n + 1).join('x')));
};

// This triggers a 'readable' event, which is lost.
r.push(new Buffer('blerg'));

var caughtReadable = false;
setTimeout(function() {
r.on('readable', function() {
caughtReadable = true;
});
});

process.on('exit', function() {
assert(caughtReadable);
console.log('ok 1');
});
})();

(function second() {
// second test, make sure that readable is re-emitted if there's
// already a length, while it IS reading.

var r = new Readable({
highWaterMark: 3
});

r._read = function(n) {
setTimeout(function() {
r.push(new Buffer(new Array(n + 1).join('x')));
});
};

// This triggers a 'readable' event, which is lost.
r.push(new Buffer('blerg'));

var caughtReadable = false;
process.nextTick(function() {
r.on('readable', function() {
caughtReadable = true;
});
});

process.on('exit', function() {
assert(caughtReadable);
console.log('ok 2');
});
})();

0 comments on commit eafa902

Please sign in to comment.