Skip to content

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

Closed as not planned
@yunnysunny

Description

@yunnysunny

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions