git ssb

2+

Dominic / pull-stream



Tree: 0cb1978d6503fa3d1e02279db3a4a0bd01bff81d

Files: 0cb1978d6503fa3d1e02279db3a4a0bd01bff81d / sinks.js

1091 bytesRaw
1var 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 }
13
14 op && op(data)
15
16 if(!sync) next()
17 })
18 sync = false
19 if(!returned) return
20 } while (loop);
21 })()
22}
23
24var reduce = exports.reduce =
25function (read, reduce, acc, cb) {
26 drain(read, function (data) {
27 acc = reduce(acc, data)
28 }, function (err) {
29 cb(err, acc)
30 })
31}
32
33var collect = exports.collect = exports.writeArray =
34function (read, cb) {
35 return reduce(read, function (arr, item) {
36 arr.push(item)
37 return arr
38 }, [], cb)
39}
40
41//if the source callsback sync, then loop
42//rather than recurse
43
44var onEnd = exports.onEnd = function (read, done) {
45 return drain(read, null, done)
46}
47
48var log = exports.log = function (read, done) {
49 return drain(read, console.log.bind(console), done)
50}
51
52

Built with git-ssb-web