-
Notifications
You must be signed in to change notification settings - Fork 7.3k
readable.unshift(chunk) not resetting state.reading #14604
Comments
Sorry. Don't fully understand the concern. May also be advantageous to pull in @chrisdickinson |
@trevnorris @chrisdickinson ... if I understand the original post correctly, the question is about why |
Ah. Um, no idea. I think @isaacs was the one to implement that code. |
@isaacs ... any chance you could help shed some light on this one? |
@jasnell @trevnorris Thanks for response. Here is a more simple example to show my confusion: var rs = require('stream').Readable({ objectMode: true })
var buffer = ['a', 'b']
var unshift = false
rs._read = function () {
if (!unshift) {
this.unshift('c') // this leaves rs._readableState.reading `true`
unshift = true
this.push('') // this makes rs._readableState.reading `false`
}
else {
if (buffer.length) {
this.push(buffer.pop())
}
else {
this.push(null)
}
}
}
console.log('data:"' + rs.read() + '"')
console.log(rs._readableState.reading)
console.log('data:"' + rs.read() + '"')
console.log('data:"' + rs.read() + '"')
console.log('data:"' + rs.read() + '"') output:
However, if we do not output:
I believe that |
Perhaps the example above does not use The following case is more reasonable: var rs = require('stream').Readable({ objectMode: true })
var buffer = ['a', 'b']
var unshift = false
rs._read = function () {
if (buffer.length) {
this.push(buffer.pop())
}
else {
this.push(null)
}
}
rs.on('data', function (chunk) {
console.log(chunk)
if (!unshift) {
this.unshift('c')
unshift = true
}
}) output:
|
So does that resolve the issue for you @zoubin ? |
@jasnell Almost. Thanks. If we use |
Per nodejs#14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided.
Will have a pull request shortly that addresses this issue in documentation |
Per #14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: #25635
Per nodejs#14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs#25635
Per #14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: #25591
Per nodejs/node-v0.x-archive#14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs/node-v0.x-archive#25591
Per nodejs#14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs#25591
I see it is done on purpose in
readableAddChunk
, but it confuses me. Especially when I find that the example given in the documentation fails because it does nothing to reset the reading flag (likepush('')
) afterthis.unshift(b)
.Is it necessary to behave this way?
The text was updated successfully, but these errors were encountered: