-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (74 loc) · 1.96 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const AggregateError = require('aggregate-error');
class Limiter {
constructor({ concurrency, stopOnError = true }) {
this.concurrency = concurrency;
this.stopOnError = stopOnError; // todo: implement
this.inProgress = 0;
this.waiters = [];
this.errors = [];
}
add(promise) {
this.inProgress++;
promise
.then(() => {
this.inProgress--;
this.resolveWaiters();
})
.catch(err => {
this.inProgress--;
this.errors.push(err);
this.resolveWaiters();
});
}
resolveWaiters() {
if (this.hasErrors) {
if (this.inProgress > 0) {
// wait until all inProgress jobs are finished before resolving with errors, to get consistent behavior
return;
}
this.waiters.forEach(({ reject }) => {
reject(new AggregateError(this.errors));
}, this); // todo: do we need to pas context ? or arrow func is enough ?
this.waiters = [];
} else {
this.waiters.forEach(({ resolve, lessThan }, i) => {
if (this.inProgress < lessThan) {
resolve();
this.waiters[i] = null;
}
}, this);
this.waiters = this.waiters.filter(Boolean);
}
}
get hasErrors() {
return this.errors.length;
}
waitInProgressLessThan(lessThan) {
if (this.hasErrors) {
// if we already have errors, we want to wait until all tasks are finished
lessThan = 1;
}
if (this.inProgress < lessThan) {
return;
}
return new Promise((resolve, reject) => {
const waiter = { resolve, reject, lessThan };
this.waiters.push(waiter);
});
}
async ready() {
return this.waitInProgressLessThan(this.concurrency);
}
async finished() {
return this.waitInProgressLessThan(1);
}
static async iterate(iterable, concurrency, iteratee) {
const limiter = new Limiter({ concurrency });
for await (const item of iterable) { // should we try/catch iterable error and add to this.errors here ? probably yes, but be careful
await limiter.ready();
limiter.add(iteratee(item));
}
await limiter.finished();
}
}
module.exports = Limiter;