Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it can read data in data event even if I have added readable event before #55704

Open
yunnysunny opened this issue Nov 3, 2024 · 0 comments
Open
Labels
doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem.

Comments

@yunnysunny
Copy link
Contributor

Affected URL(s)

https://nodejs.org/docs/latest/api/stream.html#two-reading-modes

Description of the problem

As the docs said:

Adding a 'readable' event handler automatically makes the stream stop flowing, and the data has to be consumed via readable.read(). If the 'readable' event handler is removed, then the stream will start flowing again if there is a 'data' event handler.

I added both the readable and data event listener, then I found both of them emit.

const { Readable } = require('stream');

class MyReadable extends Readable {
    _read () {
        console.log('_read has been called');
    }
}

const reader = new MyReadable();
const initSize = 6;
for (let i = 0; i < initSize; i++) {
    reader.push(Buffer.from([i & 0xff]));
}
reader.on('readable', function () {
    console.log('get data', reader.read());
});

reader.on('data', (chunk) => {
    console.log('stream mod2', reader.readableFlowing);
    console.log('data event', chunk);
});

console.log('stream mode', reader.readableFlowing);

The output of code is:

stream mode false
_read has been called
stream mode2 false
data event <Buffer 00 01 02 03 04 05>
get data <Buffer 00 01 02 03 04 05>

It seemed that the buffer can been read in flowing mode and paused mode at same time.

@yunnysunny yunnysunny added the doc Issues and PRs related to the documentations. label Nov 3, 2024
@KhafraDev KhafraDev added stream Issues and PRs related to the stream subsystem. and removed web streams labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants