-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
68 lines (61 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var looper = require('looper')
module.exports = function (map, width, inOrder) {
inOrder = inOrder === undefined ? true : inOrder
var reading = false, abort
return function (read) {
var i = 0, j = 0, last = 0
var seen = [], started = false, ended = false, _cb, error
function drain () {
if(_cb) {
var cb = _cb
if(error) {
_cb = null
return cb(error)
}
if(Object.hasOwnProperty.call(seen, j)) {
_cb = null
var data = seen[j]; delete seen[j]; j++
cb(null, data)
if(width) start()
} else if(j >= last && ended) {
_cb = null
cb(ended)
}
}
}
var start = looper(function () {
started = true
if(ended) return drain()
if(reading || width && (i - width >= j)) return
reading = true
read(abort, function (end, data) {
reading = false
if(end) {
last = i; ended = end
drain()
} else {
var k = i++
map(data, function (err, data) {
if (inOrder) seen[k] = data
else seen.push(data)
if(err) error = err
drain()
})
if(!ended)
start()
}
})
})
return function (_abort, cb) {
if(_abort)
read(ended = abort = _abort, function (err) {
if(cb) return cb(err)
})
else {
_cb = cb
if(!started) start()
drain()
}
}
}
}