Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

streams: Replace _read callback with push() #4878

Closed
wants to merge 4 commits into from

Conversation

isaacs
Copy link

@isaacs isaacs commented Feb 28, 2013

Also, this splits up several readable methods so that they're more inline-friendly, implements Readable.unshift(chunk) (the mirror of Readable.push(chunk)) and adds a parser example to the docs.

A primary motivation of this is to make the onread function more
inline-friendly, but also to make it more easy to explore not having
onread at all, in favor of always using push() to signal the end of
reading.
@isaacs
Copy link
Author

isaacs commented Mar 1, 2013

Oops, some tests are failing, don't land this yet :)

This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.

It is still possible, of course, to implement a `CallbackReadable` on
top of this.  Simply provide a method like this as the callback:

    function readCallback(er, chunk) {
      if (er)
        stream.emit('error', er);
      else
        stream.push(chunk);
    }

However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
The first example uses Readable, and shows the use of
readable.unshift().  The second uses the Transform class, showing that
it's much simpler in this case.
@isaacs
Copy link
Author

isaacs commented Mar 1, 2013

Fixed tests. Was a dumb debuggery typo. The system works!

@TooTallNate
Copy link

LGTM. This definitely simplifies the API which is one of the major things I was desiring 🍻

@isaacs
Copy link
Author

isaacs commented Mar 1, 2013

Landed on master. Thanks!

@isaacs isaacs closed this Mar 1, 2013
@isaacs isaacs deleted the stream-readable-split-up-onread branch March 1, 2013 01:39
@kanongil
Copy link

kanongil commented Mar 1, 2013

This looks nice, and matches another idea of mine, though I didn't think about making _read completely sync which solves some issues. 👍

I guess now the conceptual API is that any number of push's will eventually be followed by a _read, and that no further _read's will be called until after at least one push (of any size).

Also, you missed a callback in the example for push(). 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants