Closed davedoesdev closed 8 years ago
/cc @nodejs/streams
hey @davedoesdev!
What you describe is the currently intended behavior. Setting on('readable')
trigger as a side effect read(0)
. In fact, 'end'
is emitted straight away, because the read was triggered.
This is what is triggered: https://github.com/nodejs/node/blob/v6.7.0/lib/_stream_readable.js#L710-L713
What you want is to "undo" that scheduled read(0)
operation. I'm not 100% convinced that we should support this. What's the use case for doing so?
There probably is a different bug about 'readableListening'
not being reset to false. But fixing it would not prevent the behavior you are describing here.
I have two separate bits of code (separate modules). One which reads a header from a stream and another which then reads the rest of the stream. The second bit of code when passed the stream (after the first has finished processing) then registers its own readable
and end
handlers. It needs to know when the stream has ended.
It's fine, I can get the second bit of code to check if the stream is already ended (using _readableState
I guess).
That is probably the best approach, yes.
Thanks for the help @mcollina
This program prints "END":
whereas this program does not:
The only difference is that a
readable
listener is added and then removed before the stream is ended.My question is whether the behaviour should be the same, given that there is no
readable
listener in both cases when the stream is ended.I can see in the code why: At https://github.com/nodejs/node/blob/v6.7.0/lib/_stream_readable.js#L696
readableListening
is set totrue
but it's never reset tofalse
if allreadable
listeners are removed.Before making any changes however, I'd like to ask whether this is intended behaviour?