blob: 3cdbb41c35b0536c1c8603ec3dcfdf316fb7694b (
plain)
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
|
'use strict';
class Concurrent {
constructor(runnables, bail) {
if (!Array.isArray(runnables)) {
throw new TypeError('Expected an array of runnables');
}
this.runnables = runnables;
this.bail = bail || false;
}
run() {
let allPassed = true;
let pending;
let rejectPending;
let resolvePending;
const allPromises = [];
const handlePromise = promise => {
if (!pending) {
pending = new Promise((resolve, reject) => {
rejectPending = reject;
resolvePending = resolve;
});
}
allPromises.push(promise.then(passed => {
if (!passed) {
allPassed = false;
if (this.bail) {
// Stop if the test failed and bail mode is on.
resolvePending();
}
}
}, rejectPending));
};
for (const runnable of this.runnables) {
const passedOrPromise = runnable.run();
if (!passedOrPromise) {
if (this.bail) {
// Stop if the test failed and bail mode is on.
return false;
}
allPassed = false;
} else if (passedOrPromise !== true) {
handlePromise(passedOrPromise);
}
}
if (pending) {
Promise.all(allPromises).then(resolvePending);
return pending.then(() => allPassed);
}
return allPassed;
}
}
module.exports = Concurrent;
|