git ssb

2+

Dominic / pull-stream



Tree: b97db9e7ed0815998c76014c59a9bfb764eae06d

Files: b97db9e7ed0815998c76014c59a9bfb764eae06d / throughs / async-map.js

1023 bytesRaw
1'use strict'
2
3function id (e) { return e }
4var prop = require('../util/prop')
5
6module.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