-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.js
78 lines (62 loc) · 1.79 KB
/
control.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
68
69
70
71
72
73
74
75
76
77
78
var pull = require("pull-stream");
var balance = require("pull-balance");
var serial = module.exports.serial = pull.Through(function (read) {
var reading, reads = [];
return function (end, cb) {
reads.push([end, cb]);
if (reading) return;
;(function drain () {
if (reads.length && !reading)
(function (end, cb) {
reading = true;
read(end, function (end, data) {
reading = false;
cb(end, data);
drain();
})
})(reads[0][0], reads.shift()[1]);
})();
}
})
var parallel = module.exports.parallel = pull.Through(function (read, highWaterMark) {
var ended, streams = [];
function _read(end, cb) {
if (ended) return cb(ended);
read(end, function (end, data) {
if (end) {
streams = [];
ended = true;
}
cb(end, data);
})
}
for (var i = 0; i < highWaterMark; ++i)
streams[i] = hwm(1);
return balance(streams)(_read);
})
var hwm = pull.Through(function (read, highWaterMark) {
var buffer = [], waiting = [], ended, ending, reading = false
highWaterMark = highWaterMark || 10
function readAhead () {
while(waiting.length && (buffer.length || ended))
waiting.shift()(ended, ended ? null : buffer.shift())
if (!buffer.length && ending) ended = true;
}
function next () {
if(ended || ending || reading || buffer.length >= highWaterMark)
return readAhead();
reading = true
return read(ended, function (end, data) {
reading = false
ending = ending || end
if(data != null) buffer.push(data)
readAhead(); next();
})
}
process.nextTick(next)
return function (end, cb) {
ended = ended || end
waiting.push(cb)
next(); readAhead()
}
})