Files: 3584caaf3ca81314dcd4b0437ecbf1b538ab973d / throughs / async-map.js
1023 bytesRaw
1 | |
2 | |
3 | function id (e) { return e } |
4 | var prop = require('../util/prop') |
5 | |
6 | module.exports = function asyncMap (map) { |
7 | if(!map) return id |
8 | map = prop(map) |
9 | var busy = false, abortCb, aborted |
10 | return function (read) { |
11 | return function next (abort, cb) { |
12 | if(aborted) return cb(aborted) |
13 | if(abort) { |
14 | aborted = abort |
15 | if(!busy) read(abort, cb) |
16 | else read(abort, function () { |
17 | //if we are still busy, wait for the mapper to complete. |
18 | if(busy) abortCb = cb |
19 | else cb(abort) |
20 | }) |
21 | } |
22 | else |
23 | read(null, function (end, data) { |
24 | if(end) cb(end) |
25 | else if(aborted) cb(aborted) |
26 | else { |
27 | busy = true |
28 | map(data, function (err, data) { |
29 | busy = false |
30 | if(aborted) { |
31 | cb(aborted) |
32 | abortCb(aborted) |
33 | } |
34 | else if(err) next (err, cb) |
35 | else cb(null, data) |
36 | }) |
37 | } |
38 | }) |
39 | } |
40 | } |
41 | } |
42 | |
43 | |
44 |
Built with git-ssb-web