diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index b81f2a82a13..2f4d8a49633 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -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 @@ -378,7 +379,6 @@ function emitReadable(stream) { } function emitReadable_(stream) { - var state = stream._readableState; stream.emit('readable'); } @@ -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; }; diff --git a/test/simple/test-stream-readable-event.js b/test/simple/test-stream-readable-event.js new file mode 100644 index 00000000000..be17f0455f4 --- /dev/null +++ b/test/simple/test-stream-readable-event.js @@ -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'); + }); +})();