Browse Source

doc: clarify Readable._read and Readable.push

Minor clarifications around Readable._read and Readable.push
to make their implementation/usage easier to understand.

https://github.com/joyent/node/issues/14124#issuecomment-115392674

Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/joyent/node/pull/25591
v0.12-staging
fresheneesz 10 years ago
committed by James M Snell
parent
commit
4952e2b4d2
  1. 45
      doc/api/stream.markdown

45
doc/api/stream.markdown

@ -944,24 +944,22 @@ initialized.
* `size` {Number} Number of bytes to read asynchronously * `size` {Number} Number of bytes to read asynchronously
Note: **Implement this function, but do NOT call it directly.** Note: **Implement this method, but do NOT call it directly.**
This function should NOT be called directly. It should be implemented This method is prefixed with an underscore because it is internal to the
by child classes, and only called by the internal Readable class class that defines it and should only be called by the internal Readable
methods. class methods. All Readable stream implementations must provide a _read
method to fetch data from the underlying resource.
All Readable stream implementations must provide a `_read` method to When _read is called, if data is available from the resource, `_read` should
fetch data from the underlying resource. start pushing that data into the read queue by calling `this.push(dataChunk)`.
`_read` should continue reading from the resource and pushing data until push
returns false, at which point it should stop reading from the resource. Only
when _read is called again after it has stopped should it start reading
more data from the resource and pushing that data onto the queue.
This method is prefixed with an underscore because it is internal to Note: once the `_read()` method is called, it will not be called again until
the class that defines it, and should not be called directly by user the `push` method is called.
programs. However, you **are** expected to override this method in
your own extension classes.
When data is available, put it into the read queue by calling
`readable.push(chunk)`. If `push` returns false, then you should stop
reading. When `_read` is called again, you should start pushing more
data.
The `size` argument is advisory. Implementations where a "read" is a The `size` argument is advisory. Implementations where a "read" is a
single call that returns data can use this to know how much data to single call that returns data can use this to know how much data to
@ -977,19 +975,16 @@ becomes available. There is no need, for example to "wait" until
Buffer encoding, such as `'utf8'` or `'ascii'` Buffer encoding, such as `'utf8'` or `'ascii'`
* return {Boolean} Whether or not more pushes should be performed * return {Boolean} Whether or not more pushes should be performed
Note: **This function should be called by Readable implementors, NOT Note: **This method should be called by Readable implementors, NOT
by consumers of Readable streams.** by consumers of Readable streams.**
The `_read()` function will not be called again until at least one If a value other than null is passed, The `push()` method adds a chunk of data
`push(chunk)` call is made. into the queue for subsequent stream processors to consume. If `null` is
passed, it signals the end of the stream (EOF), after which no more data
The `Readable` class works by putting data into a read queue to be can be written.
pulled out later by calling the `read()` method when the `'readable'`
event fires.
The `push()` method will explicitly insert some data into the read The data added with `push` can be pulled out by calling the `read()` method
queue. If it is called with `null` then it will signal the end of the when the `'readable'`event fires.
data (EOF).
This API is designed to be as flexible as possible. For example, This API is designed to be as flexible as possible. For example,
you may be wrapping a lower-level source which has some sort of you may be wrapping a lower-level source which has some sort of

Loading…
Cancel
Save