-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
43 lines (36 loc) · 965 Bytes
/
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
var pull = require('pull-core')
module.exports = pull.Source(function (onClose) {
var buffer = [], cbs = [], waiting = [], ended
function drain() {
var l
while(waiting.length && ((l = buffer.length) || ended)) {
var data = buffer.shift()
var cb = cbs.shift()
waiting.shift()(l ? null : ended, data)
cb && cb(ended === true ? null : ended)
}
}
function read (end, cb) {
ended = ended || end
waiting.push(cb)
drain()
if(ended)
onClose && onClose(ended === true ? null : ended)
}
read.push = function (data, cb) {
if(ended)
return cb && cb(ended === true ? null : ended)
buffer.push(data); cbs.push(cb)
drain()
}
read.end = function (end, cb) {
if('function' === typeof end)
cb = end, end = true
ended = ended || end || true;
if(cb) cbs.push(cb)
drain()
if(ended)
onClose && onClose(ended === true ? null : ended)
}
return read
})