Commit 0cb1978d6503fa3d1e02279db3a4a0bd01bff81d
use non-recursive version of sink
Dominic Tarr committed on 4/13/2013, 4:51:32 PMParent: 47bdf22607fad13d9ecd2486e099f7bcc2c2207e
Files changed
sinks.js | changed |
sinks.js | ||
---|---|---|
@@ -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 | + } | |
1 | 13 | |
14 | + op && op(data) | |
15 | + | |
16 | + if(!sync) next() | |
17 | + }) | |
18 | + sync = false | |
19 | + if(!returned) return | |
20 | + } while (loop); | |
21 | + })() | |
22 | +} | |
23 | + | |
2 | 24 | var reduce = exports.reduce = |
3 | 25 | 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) { | |
6 | 27 | acc = reduce(acc, data) |
7 | - read(null, next) | |
28 | + }, function (err) { | |
29 | + cb(err, acc) | |
8 | 30 | }) |
9 | 31 | } |
10 | 32 | |
11 | 33 | var collect = exports.collect = exports.writeArray = |
@@ -15,24 +37,15 @@ | ||
15 | 37 | return arr |
16 | 38 | }, [], cb) |
17 | 39 | } |
18 | 40 | |
41 | +//if the source callsback sync, then loop | |
42 | +//rather than recurse | |
19 | 43 | |
20 | 44 | 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) | |
25 | 46 | } |
26 | 47 | |
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 | - | |
35 | 48 | var log = exports.log = function (read, done) { |
36 | 49 | return drain(read, console.log.bind(console), done) |
37 | 50 | } |
38 | 51 |
Built with git-ssb-web