Files: 1f9a3fcaa6f6fa4a3ad0a219f6df1a476588f6d5 / node_modules / pull-cat / index.js
835 bytesRaw
1 | var noop = function () {} |
2 | |
3 | function abortAll(ary, abort, cb) { |
4 | var n = ary.length |
5 | if(!n) return cb(abort) |
6 | ary.forEach(function (f) { |
7 | if(f) f(abort, next) |
8 | else next() |
9 | }) |
10 | |
11 | function next() { |
12 | if(--n) return |
13 | cb(abort) |
14 | } |
15 | if(!n) next() |
16 | } |
17 | |
18 | module.exports = function (streams) { |
19 | return function (abort, cb) { |
20 | ;(function next () { |
21 | if(abort) |
22 | abortAll(streams, abort, cb) |
23 | else if(!streams.length) |
24 | cb(true) |
25 | else if(!streams[0]) |
26 | streams.shift(), next() |
27 | else |
28 | streams[0](null, function (err, data) { |
29 | if(err) { |
30 | streams.shift() //drop the first, has already ended. |
31 | if(err === true) next() |
32 | else abortAll(streams, err, cb) |
33 | } |
34 | else |
35 | cb(null, data) |
36 | }) |
37 | })() |
38 | } |
39 | } |
40 | |
41 | |
42 |
Built with git-ssb-web