This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: Handle late 'readable' event listeners
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
Showing
2 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
})(); |