-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
175 lines (146 loc) · 3.93 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pTimes from 'p-times';
const sum = iterable => {
let total = 0;
for (const value of iterable.values()) {
total += value;
}
return total;
};
export class PProgress extends Promise {
static all(promises, options) {
return pProgress(async progress => {
if (
options && typeof options.concurrency === 'number'
&& !(promises.every(promise => typeof promise === 'function'))
) {
throw new TypeError('When `options.concurrency` is set, the first argument must be an Array of Promise-returning functions');
}
const progressMap = new Map();
const reportProgress = () => {
progress(sum(progressMap) / promises.length);
};
const mapper = async index => {
const nextValue = promises[index];
const promise = typeof nextValue === 'function' ? nextValue() : nextValue;
progressMap.set(promise, 0);
if (promise instanceof PProgress) {
promise.onProgress(percentage => {
progressMap.set(promise, percentage);
reportProgress();
});
}
const value = await promise;
progressMap.set(promise, 1);
reportProgress();
return value;
};
return pTimes(promises.length, mapper, options);
});
}
static allSettled(promises, {concurrency} = {}) {
return pProgress(async progress => {
if (
typeof concurrency === 'number'
&& !(promises.every(promise => typeof promise === 'function'))
) {
throw new TypeError('When `options.concurrency` is set, the first argument must be an Array of Promise-returning functions');
}
const progressMap = new Map();
const reportProgress = () => {
progress(sum(progressMap) / promises.length);
};
const mapper = async index => {
const nextValue = promises[index];
const promise = typeof nextValue === 'function' ? nextValue() : nextValue;
progressMap.set(promise, 0);
if (promise instanceof PProgress) {
promise.onProgress(percentage => {
progressMap.set(promise, percentage);
reportProgress();
});
}
try {
return {
status: 'fulfilled',
value: await promise,
};
} catch (error) {
return {
status: 'rejected',
reason: error,
};
} finally {
progressMap.set(promise, 1);
reportProgress();
}
};
return pTimes(promises.length, mapper, {
concurrency,
});
});
}
constructor(executor) {
const setProgress = progress => {
if (progress > 1 || progress < 0) {
throw new TypeError('The progress percentage should be a number between 0 and 1');
}
(async () => {
// We wait for the next microtask tick so `super` is called before we use `this`
await Promise.resolve();
// Note: we don't really have guarantees over
// the order in which async operations are evaluated,
// so if we get an out-of-order progress, we'll just discard it.
if (progress <= this._progress) {
return;
}
this._progress = progress;
for (const listener of this._listeners) {
listener(progress);
}
})();
};
super((resolve, reject) => {
executor(
value => {
setProgress(1);
resolve(value);
},
reject,
progress => {
if (progress !== 1) {
setProgress(progress);
}
},
);
});
this._listeners = new Set();
this._setProgress = setProgress;
this._progress = 0;
}
get progress() {
return this._progress;
}
onProgress(callback) {
if (typeof callback !== 'function') {
throw new TypeError(`Expected a \`Function\`, got \`${typeof callback}\``);
}
this._listeners.add(callback);
return this;
}
then(onFulfilled, onRejected) { // eslint-disable-line unicorn/no-thenable
const child = super.then(onFulfilled, onRejected);
this._listeners.add(progress => {
child._setProgress(progress);
});
return child;
}
}
export default function pProgress(input) {
return new PProgress(async (resolve, reject, progress) => {
try {
resolve(await input(progress));
} catch (error) {
reject(error);
}
});
}