diff options
author | Matteo Collina <hello@matteocollina.com> | 2018-02-26 09:24:30 +0100 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-04-06 13:50:15 +0200 |
commit | cf5f9867ff3e700dfd72519e7bdeb701e254317f (patch) | |
tree | 83472945c722622dae2d64cc97bad86e5c4547f9 /doc | |
parent | 1e07acd476309e7ddc4981160b89731b61a31179 (diff) | |
download | android-node-v8-cf5f9867ff3e700dfd72519e7bdeb701e254317f.tar.gz android-node-v8-cf5f9867ff3e700dfd72519e7bdeb701e254317f.tar.bz2 android-node-v8-cf5f9867ff3e700dfd72519e7bdeb701e254317f.zip |
stream: 'readable' have precedence over flowing
In Streams3 the 'readable' event/.read() method had a lower precedence
than the `'data'` event that made them impossible to use them together.
This make `.resume()` a no-op if there is a listener for the
`'readable'` event, making the stream non-flowing if there is a
`'data'` listener.
Fixes: https://github.com/nodejs/node/issues/18058
PR-URL: https://github.com/nodejs/node/pull/18994
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/stream.md | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md index 9f233cd325..86286906b7 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -762,6 +762,9 @@ changes: description: > 'readable' is always emitted in the next tick after .push() is called + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/18994 + description: Using 'readable' requires calling .read(). --> The `'readable'` event is emitted when there is data available to be read from @@ -770,10 +773,16 @@ cause some amount of data to be read into an internal buffer. ```javascript const readable = getReadableStreamSomehow(); -readable.on('readable', () => { +readable.on('readable', function() { // there is some data to read now + let data; + + while (data = this.read()) { + console.log(data); + } }); ``` + The `'readable'` event will also be emitted once the end of the stream data has been reached but before the `'end'` event is emitted. @@ -806,6 +815,10 @@ In general, the `readable.pipe()` and `'data'` event mechanisms are easier to understand than the `'readable'` event. However, handling `'readable'` might result in increased throughput. +If both `'readable'` and [`'data'`][] are used at the same time, `'readable'` +takes precedence in controlling the flow, i.e. `'data'` will be emitted +only when [`stream.read()`][stream-read] is called. + ##### readable.destroy([error]) <!-- YAML added: v8.0.0 @@ -997,6 +1010,10 @@ the status of the `highWaterMark`. ##### readable.resume() <!-- YAML added: v0.9.4 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/18994 + description: Resume has no effect if there is a 'readable' event listening --> * Returns: {this} @@ -1016,6 +1033,9 @@ getReadableStreamSomehow() }); ``` +The `readable.resume()` method has no effect if there is a `'readable'` +event listener. + ##### readable.setEncoding(encoding) <!-- YAML added: v0.9.4 |