My simple stream API: stream.read(len, cb) stream.write(data, cb)
`read` returns its result through `cb`, just like fs.read `len` is an optional parameter for read. If you omit it, read just returns the next chunk. `read` returns null if you are positioned at the end of stream. `write` calls the cb when the stream is ready for more write, just like fs.write passing data = null to `write` ends the stream. I also added a stream.unread(data). Handy for parsers that need to look ahead. I've been using this API for more than 2 years and I'm quite happy with it. It handles backpressure just fine (just relax and let the event loop do its thing). blog article: https://bjouhier.wordpress.com/2012/07/04/node-js-stream-api-events-or-callbacks/ implementation: https://github.com/Sage/streamlinejs/blob/master/lib/streams/server/streams._js Bruno On Tuesday, July 2, 2013 10:40:33 AM UTC+2, Gil Pedersen wrote: > > This looks interesting, but not applicable to my case since I don't want > to require harmony (yet). > > On 01/07/2013, at 21.13, tjholowaychuk <tjholo...@gmail.com <javascript:>> > wrote: > > This is what I resorted to: > https://github.com/visionmedia/co/blob/master/examples/streams.js#L42, > there might be a nicer way to handle that though > > On Monday, 1 July 2013 06:23:44 UTC-7, Gil Pedersen wrote: >> >> Hi, >> >> I have a case where I want to consume a Readable, by: >> >> 1. Read n bytes. >> 2. Process buffer using a function with an async callback. >> 3. When callback completes, goto 1. >> >> I fail to see any simple solution to what I feel must be a somewhat >> common use case. The standard "on('readable', function() { this.read(); ... >> })" doesn't apply, as I have no way of deferring future 'readable' emits >> once I have returned from the handler. >> >> Can it really be that the most sensible solution is to fall back to the >> old API, using pause() and resume()? or have I missed something? >> >> Regards, >> Gil > > > -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.