-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.d.ts
248 lines (204 loc) · 6.53 KB
/
index.d.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// https://github.com/microsoft/TypeScript/blob/582e404a1041ce95d22939b73f0b4d95be77c6ec/lib/lib.es2020.promise.d.ts#L21-L31
export type PromiseSettledResult<ResolveValueType> = {
status: 'fulfilled';
value: ResolveValueType;
} | {
status: 'rejected';
reason: unknown;
};
export type Options = {
/**
The number of concurrently pending promises. Minimum: `1`.
To run the promises in series, set it to `1`.
When this option is set, the first argument must be an array of promise-returning functions.
@default Infinity
*/
readonly concurrency: number;
};
export type PromiseFactory<ValueType> = () => PromiseLike<ValueType>;
export type ProgressNotifier = (progress: number) => void;
export class PProgress<ValueType> extends Promise<ValueType> { // eslint-disable-line @typescript-eslint/naming-convention
/**
Convenience method to run multiple promises and get a total progress of all of them. It counts normal promises with progress `0` when pending and progress `1` when resolved. For `PProgress` type promises, it listens to their `onProgress()` method for more fine grained progress reporting. You can mix and match normal promises and `PProgress` promises.
@param promises - Promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).
@example
```
import pProgress, {PProgress} from 'p-progress';
import delay from 'delay';
const progressPromise = () => pProgress(async progress => {
progress(0.14);
await delay(52);
progress(0.37);
await delay(104);
progress(0.41);
await delay(26);
progress(0.93);
await delay(55);
});
const allProgressPromise = PProgress.all([
delay(103),
progressPromise(),
delay(55),
delay(209)
]);
allProgressPromise.onProgress(console.log);
//=> 0.0925
//=> 0.3425
//=> 0.5925
//=> 0.6025
//=> 0.7325
//=> 0.9825
//=> 1
await allProgressPromise;
```
*/
static all<Promises extends Array<PromiseFactory<unknown> | PromiseLike<unknown>>>(
promises: readonly [...Promises],
options?: Options
): PProgress<{
[Promise_ in keyof Promises]: (
Promises[Promise_] extends PromiseLike<unknown>
? Awaited<Promises[Promise_]>
: (
Promises[Promise_] extends PromiseFactory<unknown>
? Awaited<ReturnType<Promises[Promise_]>>
: Promises[Promise_]
)
)
}>;
static all<ReturnValue>(
promises: Iterable<PromiseFactory<ReturnValue> | PromiseLike<ReturnValue>>,
options?: Options
): PProgress<Iterable<ReturnValue>>;
/**
Like [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) but also exposes the total progress of all of the promises like `PProgress.all`.
@param promises - Promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).
@example
```
import pProgress, {PProgress} from 'p-progress';
import delay from 'delay';
const progressPromise = () => pProgress(async progress => {
progress(0.14);
await delay(52);
progress(0.37);
await delay(104);
progress(0.41);
await delay(26);
progress(0.93);
await delay(55);
return 1;
});
const progressPromise2 = () => pProgress(async progress => {
progress(0.14);
await delay(52);
progress(0.37);
await delay(104);
progress(0.41);
await delay(26);
progress(0.93);
await delay(55);
throw new Error('Catch me if you can!');
});
const allProgressPromise = PProgress.allSettled([
progressPromise(),
progressPromise2()
]);
allProgressPromise.onProgress(console.log);
//=> 0.0925
//=> 0.3425
//=> 0.5925
//=> 0.6025
//=> 0.7325
//=> 0.9825
//=> 1
console.log(await allProgressPromise);
//=> [{status: 'fulfilled', value: 1}, {status: 'rejected', reason: Error: Catch me if you can!}]
```
*/
static allSettled<Promises extends Array<PromiseFactory<unknown> | PromiseLike<unknown>>>(
promises: readonly [...Promises],
options?: Options
): PProgress<{
[Promise_ in keyof Promises]: PromiseSettledResult<Promises[Promise_] extends PromiseLike<unknown>
? Awaited<Promises[Promise_]>
: (
Promises[Promise_] extends PromiseFactory<unknown>
? Awaited<ReturnType<Promises[Promise_]>>
: Promises[Promise_]
)>
}>;
static allSettled<ReturnValue>(
promises: Iterable<PromiseFactory<ReturnValue> | PromiseLike<ReturnValue>>,
options?: Options
): PProgress<Iterable<PromiseSettledResult<ReturnValue>>>;
/**
The current progress percentage of the promise as a number between 0 and 1.
*/
readonly progress: number;
/**
Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).
@param executor - Same as the `Promise` constructor but with an appended `progress` parameter in `executor`.
@example
```
import {PProgress} from 'p-progress';
const progressPromise = new PProgress((resolve, reject, progress) => {
const job = new Job();
job.on('data', data => {
progress(data.length / job.totalSize);
});
job.on('finish', resolve);
job.on('error', reject);
});
progressPromise.onProgress(progress => {
console.log(`${progress * 100}%`);
//=> 9%
//=> 23%
//=> 59%
//=> 75%
//=> 100%
});
await progressPromise;
```
*/
constructor(
/**
@param progress - Call this with progress updates. It expects a number between 0 and 1.
Multiple calls with the same number will result in only one `onProgress()` event.
Calling with a number lower than previously will be ignored.
Progress percentage `1` is reported for you when the promise resolves. If you set it yourself, it will simply be ignored.
*/
executor: (
resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
reject: (reason?: unknown) => void,
progress: ProgressNotifier
) => void
);
/**
Accepts a function that gets `instance.progress` as an argument and is called for every progress event.
*/
onProgress(callback: ProgressNotifier): void;
}
/**
Convenience method to make your promise-returning or async function report progress.
The function you specify will be passed the `progress()` function as a parameter.
@example
```
import pProgress from 'p-progress';
const runJob = async name => pProgress(async progress => {
const job = new Job(name);
job.on('data', data => {
progress(data.length / job.totalSize);
});
await job.run();
});
const progressPromise = runJob('Gather rainbows');
progressPromise.onProgress(console.log);
//=> 0.09
//=> 0.23
//=> 0.59
//=> 0.75
//=> 1
await progressPromise;
```
*/
export default function pProgress<ReturnValue>(input: (progress: ProgressNotifier) => PromiseLike<ReturnValue> | ReturnValue): PProgress<ReturnValue>;