-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.js
129 lines (117 loc) · 3.01 KB
/
run.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
module.exports = run;
function run(generator, callback) {
var iterator;
if (typeof generator === "function") {
// Pass in resume for no-wrap function calls
iterator = generator(resume);
}
else if (typeof generator === "object") {
// Oterwise, assume they gave us the iterator directly.
iterator = generator;
}
else {
throw new TypeError("Expected generator or iterator and got " + typeof generator);
}
var data = null, yielded = false;
var next = callback ? nextSafe : nextPlain;
next();
check();
function nextSafe(err, item) {
var n;
try {
n = (err ? iterator.throw(err) : iterator.next(item));
if (!n.done) {
if (n.value) start(n.value);
yielded = true;
return;
}
}
catch (err) {
return callback(err);
}
return callback(null, n.value);
}
function nextPlain(err, item) {
var cont = (err ? iterator.throw(err) : iterator.next(item)).value;
if (cont) start(cont);
yielded = true;
}
function start(cont) {
// Pass in resume to continuables if one was yielded.
if (typeof cont === "function") return cont(resume());
// If an array of continuables is yielded, run in parallel
if (Array.isArray(cont)) {
for (var i = 0, l = cont.length; i < l; ++i) {
if (typeof cont[i] !== "function") return;
}
return parallel(cont, resume());
}
// Also run hash of continuables in parallel, but name results.
if (typeof cont === "object" && Object.getPrototypeOf(cont) === Object.prototype) {
var keys = Object.keys(cont);
for (var i = 0, l = keys.length; i < l; ++i) {
if (typeof cont[keys[i]] !== "function") return;
}
return parallelNamed(keys, cont, resume());
}
}
function resume() {
var done = false;
return function () {
if (done) return;
done = true;
data = arguments;
check();
};
}
function check() {
while (data && yielded) {
var err = data[0];
var item = data[1];
data = null;
yielded = false;
next(err, item);
yielded = true;
}
}
}
function parallel(array, callback) {
var length = array.length;
var left = length;
var results = new Array(length);
var done = false;
return array.forEach(function (cont, i) {
cont(function (err, result) {
if (done) return;
if (err) {
done = true;
return callback(err);
}
results[i] = result;
if (--left) return;
done = true;
return callback(null, results);
});
});
}
function parallelNamed(keys, obj, callback) {
var length = keys.length;
var left = length;
var results = {};
var done = false;
return keys.forEach(function (key) {
var cont = obj[key];
results[key] = null;
cont(function (err, result) {
if (done) return;
if (err) {
done = true;
return callback(err);
}
results[key] = result;
if (--left) return;
done = true;
return callback(null, results);
});
});
}