git ssb

2+

Dominic / pull-stream



Commit 0cb1978d6503fa3d1e02279db3a4a0bd01bff81d

use non-recursive version of sink

Dominic Tarr committed on 4/13/2013, 4:51:32 PM
Parent: 47bdf22607fad13d9ecd2486e099f7bcc2c2207e

Files changed

sinks.jschanged
sinks.jsView
@@ -1,11 +1,33 @@
1+var drain = exports.drain = function (read, op, done) {
2+ ;(function next() {
3+ var sync = true, returned = false, loop = true
4+ do {
5+ returned = false; sync = true
6+ read(null, function (err, data) {
7+ returned = true
8+
9+ if(err) {
10+ done && done(err === true ? null : err)
11+ return loop = false
12+ }
113
14+ op && op(data)
15+
16+ if(!sync) next()
17+ })
18+ sync = false
19+ if(!returned) return
20+ } while (loop);
21+ })()
22+}
23+
224 var reduce = exports.reduce =
325 function (read, reduce, acc, cb) {
4- read(null, function next (end, data) {
5- if(end) return cb(end === true ? null : end, acc)
26+ drain(read, function (data) {
627 acc = reduce(acc, data)
7- read(null, next)
28+ }, function (err) {
29+ cb(err, acc)
830 })
931 }
1032
1133 var collect = exports.collect = exports.writeArray =
@@ -15,24 +37,15 @@
1537 return arr
1638 }, [], cb)
1739 }
1840
41+//if the source callsback sync, then loop
42+//rather than recurse
1943
2044 var onEnd = exports.onEnd = function (read, done) {
21- return read(null, function next (err, data) {
22- if(err) return done(err === true ? null : err)
23- read(null, next)
24- })
45+ return drain(read, null, done)
2546 }
2647
27-var drain = exports.drain = function (read, op, done) {
28- return read(null, function next (err, data) {
29- if(err) return done && done(err === true ? null : err)
30- op && op(data)
31- read(null, next)
32- })
33-}
34-
3548 var log = exports.log = function (read, done) {
3649 return drain(read, console.log.bind(console), done)
3750 }
3851

Built with git-ssb-web