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

better documentation for learners #2

Open
dominictarr opened this issue Mar 23, 2016 · 9 comments
Open

better documentation for learners #2

dominictarr opened this issue Mar 23, 2016 · 9 comments

Comments

@dominictarr
Copy link
Member

we need lots more examples to make pull streams easy to learn.

  1. simpest source
  2. simplest sink
  3. simplest through (mention duplex but leave it for later)
  4. errors and aborting. (take(n), what happens if you don't do this: fs / streams: file descriptor leak on connection abort nodejs/node#1834
  5. some useful streams: pull-cat, pull-paramap, pull-defer, pull-many, pull-merge, pull-through, pull-pushable, pull-traverse, pull-stringify, pull-level, pull-glob (maybe split these up by difficulty? with examples where they are useful?)
  6. interaction with node streams? stream-to-pull-stream
  7. muxrpc (maybe put this sooner?)
  8. secret-handshake?

@yoshuawuyts @ahdinosaur @DamonOehlman @pfraze any other ideas?

@yoshuawuyts
Copy link

I'd put networking and fs in here too, perhaps also JSON parsing - covering a good rang of practical uses makes it more likely people will use it.

@ahdinosaur
Copy link
Member

ooh yes, this is awesome! really keen on more accessible documentation for pull streams. 🐱

other ideas:

  • composable pull streams
    • pull(through, through, through) returns the equivalent of a through stream
    • pull(source, through, through) returns the equivalent of a source stream
    • pull(through, through, sink) returns the equivalent of a sink stream
  • lazy pull streams
  • functional programming with through streams (pull.map, pull.filter, pull.reduce, and such)
  • common message passing architectures
  • duplex streams for common transports (http, ws, cli)
  • how to use pull streams with other "frameworks" to build full stack apps: react (e.g. patchwork), redux, (pull-redux?), ...

also, some examples of other nice "stream" documentation in the wild:

/cc @pietgeursen

@dominictarr
Copy link
Member Author

I think the most important thing here is to find realistic examples that represent problems that are interesting and or people actually have.

Oh, another category is real time data, and streaming of that. maybe an example is an infinite scroller that uses back pressure to stop reading while until you scroll down more.

@dominictarr
Copy link
Member Author

@ahdinosaur compose https://github.com/dominictarr/pull-stream-examples/blob/master/compose.js
can you think of some more examples where composition is useful?

@eugene-eeo
Copy link

eugene-eeo commented Jul 30, 2016

@dominictarr maybe I misunderstood this because I'm still trying to learn this concept, but running the last example in pull.js, gives 100 results instead of 101. Maybe the code should be changed to:

function take(n) {
    return function(read) {
        return function(abort, cb) {
            if (!n--) return read(true, cb);
            read(null, cb);
        };
    };
}

@dominictarr
Copy link
Member Author

@eugene-eeo good point. fixed

@yoshuawuyts
Copy link

Another cool pattern I found myself needing the other day was:

Eventual values

Return a stream from a function synchronously, emit data from it
asynchronously.

const notify = require('pull-notify')
const pull = require('pull-stream')
const xhr = require('xhr')

pull(request('foobar.com'), pull.log())

function request (url) {
  const xhr$ = notify()

  xhr(url, (err, res, body) {
    if (err) return xhr$.abort(err)
    xhr$(body)
    xhr$.end()
  })

  return xhr$.listen()
}

Probably not doing it 100% right here - while this could be expanded to be a through stream, the package used allows for multiplexing; wonder if I'm doing it right. Think it'd be a neat little pattern to throw at users

@ahdinosaur
Copy link
Member

hey @yoshuawuyts, i wrote pull-async for exactly that use case (returning a single value or error), for returning more than one value see pull-defer.

pull-async example:

function request (url) {
  return pullAsync(cb => {
    xhr(url, (err, res, body) => {
      if (err) return cb(err)
      cb(body)
    })
  })
}

pull-defer example:

function request (url) {
  var deferred = pullDefer.source()
  xhr(url, (err, res, body) => {
    if (err) deferred.abort(err)
    else deferred.resolve(pull.values([body]))
  })
  return deferred
}

@dominictarr
Copy link
Member Author

@yoshuawuyts that way ruins back pressure (not that it matters with XHR, though)
in general, you want to use pull-defer for this. hmm, although, this is making me realize that pull-defer is not obvious enough (if I have to tell you two about it)

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

No branches or pull requests

4 participants