@ls-stack/utils 3.21.0 → 3.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/concurrentCalls/-internal-.md +28 -26
- package/docs/concurrentCalls/README.md +6 -18
- package/docs/retryOnError/README.md +1 -5
- package/lib/concurrentCalls.cjs +18 -34
- package/lib/concurrentCalls.d.cts +5 -5
- package/lib/concurrentCalls.d.ts +5 -5
- package/lib/concurrentCalls.js +22 -36
- package/lib/retryOnError.cjs +7 -1
- package/lib/retryOnError.d.cts +1 -2
- package/lib/retryOnError.d.ts +1 -2
- package/lib/retryOnError.js +7 -1
- package/package.json +1 -1
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
### ConcurrentCalls\<R, E\>
|
|
12
12
|
|
|
13
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
13
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:31](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L31)
|
|
14
14
|
|
|
15
15
|
#### Type Parameters
|
|
16
16
|
|
|
@@ -42,7 +42,7 @@ new ConcurrentCalls<R, E>(): ConcurrentCalls<R, E>;
|
|
|
42
42
|
add(...calls): this;
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
45
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:35](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L35)
|
|
46
46
|
|
|
47
47
|
###### Parameters
|
|
48
48
|
|
|
@@ -60,13 +60,13 @@ Defined in: [packages/utils/src/concurrentCalls.ts:36](https://github.com/lucaso
|
|
|
60
60
|
resultifyAdd(...calls): this;
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
63
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:41](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L41)
|
|
64
64
|
|
|
65
65
|
###### Parameters
|
|
66
66
|
|
|
67
67
|
###### calls
|
|
68
68
|
|
|
69
|
-
...(
|
|
69
|
+
...(() => `R` \| () => `Promise`\<`R`\>)[]
|
|
70
70
|
|
|
71
71
|
###### Returns
|
|
72
72
|
|
|
@@ -78,7 +78,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:42](https://github.com/lucaso
|
|
|
78
78
|
runAll(__namedParameters): Promise<Result<R[], E>>;
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
81
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:58](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L58)
|
|
82
82
|
|
|
83
83
|
###### Parameters
|
|
84
84
|
|
|
@@ -102,7 +102,7 @@ runAllSettled(__namedParameters): Promise<{
|
|
|
102
102
|
}>;
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
105
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:90](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L90)
|
|
106
106
|
|
|
107
107
|
###### Parameters
|
|
108
108
|
|
|
@@ -124,7 +124,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:112](https://github.com/lucas
|
|
|
124
124
|
|
|
125
125
|
### ConcurrentCallsWithMetadata\<M, R, E\>
|
|
126
126
|
|
|
127
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
127
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:157](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L157)
|
|
128
128
|
|
|
129
129
|
#### Type Parameters
|
|
130
130
|
|
|
@@ -160,7 +160,7 @@ new ConcurrentCallsWithMetadata<M, R, E>(): ConcurrentCallsWithMetadata<M, R, E>
|
|
|
160
160
|
add(...calls): this;
|
|
161
161
|
```
|
|
162
162
|
|
|
163
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
163
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:165](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L165)
|
|
164
164
|
|
|
165
165
|
###### Parameters
|
|
166
166
|
|
|
@@ -178,7 +178,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:192](https://github.com/lucas
|
|
|
178
178
|
resultifyAdd(...items): this;
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
181
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:179](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L179)
|
|
182
182
|
|
|
183
183
|
###### Parameters
|
|
184
184
|
|
|
@@ -196,7 +196,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:206](https://github.com/lucas
|
|
|
196
196
|
runAll(__namedParameters): Promise<Result<SucceededCall<R, M>[], FailedCall<M, E>>>;
|
|
197
197
|
```
|
|
198
198
|
|
|
199
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
199
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:199](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L199)
|
|
200
200
|
|
|
201
201
|
###### Parameters
|
|
202
202
|
|
|
@@ -222,7 +222,7 @@ runAllSettled(__namedParameters): Promise<{
|
|
|
222
222
|
}>;
|
|
223
223
|
```
|
|
224
224
|
|
|
225
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
225
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:246](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L246)
|
|
226
226
|
|
|
227
227
|
###### Parameters
|
|
228
228
|
|
|
@@ -244,15 +244,13 @@ Defined in: [packages/utils/src/concurrentCalls.ts:274](https://github.com/lucas
|
|
|
244
244
|
|
|
245
245
|
## Type Aliases
|
|
246
246
|
|
|
247
|
-
### Action\<R, E\>
|
|
247
|
+
### Action()\<R, E\>
|
|
248
248
|
|
|
249
249
|
```ts
|
|
250
|
-
type Action<R, E> =
|
|
251
|
-
| () => Promise<Result<R, E>>
|
|
252
|
-
| Promise<Result<R, E>>;
|
|
250
|
+
type Action<R, E> = () => Promise<Result<R, E>>;
|
|
253
251
|
```
|
|
254
252
|
|
|
255
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
253
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:24](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L24)
|
|
256
254
|
|
|
257
255
|
#### Type Parameters
|
|
258
256
|
|
|
@@ -264,6 +262,10 @@ Defined in: [packages/utils/src/concurrentCalls.ts:23](https://github.com/lucaso
|
|
|
264
262
|
|
|
265
263
|
`E` *extends* `Error`
|
|
266
264
|
|
|
265
|
+
#### Returns
|
|
266
|
+
|
|
267
|
+
`Promise`\<`Result`\<`R`, `E`\>\>
|
|
268
|
+
|
|
267
269
|
***
|
|
268
270
|
|
|
269
271
|
### FailedCall\<M, E\>
|
|
@@ -272,7 +274,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:23](https://github.com/lucaso
|
|
|
272
274
|
type FailedCall<M, E> = object;
|
|
273
275
|
```
|
|
274
276
|
|
|
275
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
277
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:19](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L19)
|
|
276
278
|
|
|
277
279
|
#### Type Parameters
|
|
278
280
|
|
|
@@ -292,7 +294,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:18](https://github.com/lucaso
|
|
|
292
294
|
error: E;
|
|
293
295
|
```
|
|
294
296
|
|
|
295
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
297
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:21](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L21)
|
|
296
298
|
|
|
297
299
|
##### metadata
|
|
298
300
|
|
|
@@ -300,7 +302,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:20](https://github.com/lucaso
|
|
|
300
302
|
metadata: M;
|
|
301
303
|
```
|
|
302
304
|
|
|
303
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
305
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:20](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L20)
|
|
304
306
|
|
|
305
307
|
***
|
|
306
308
|
|
|
@@ -310,7 +312,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:19](https://github.com/lucaso
|
|
|
310
312
|
type RunProps = object;
|
|
311
313
|
```
|
|
312
314
|
|
|
313
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
315
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:10](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L10)
|
|
314
316
|
|
|
315
317
|
#### Properties
|
|
316
318
|
|
|
@@ -320,7 +322,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:9](https://github.com/lucasol
|
|
|
320
322
|
optional delayStart: (index) => number;
|
|
321
323
|
```
|
|
322
324
|
|
|
323
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
325
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:11](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L11)
|
|
324
326
|
|
|
325
327
|
###### Parameters
|
|
326
328
|
|
|
@@ -351,7 +353,7 @@ type SettledResultWithMetadata<R, M, E> =
|
|
|
351
353
|
};
|
|
352
354
|
```
|
|
353
355
|
|
|
354
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
356
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:27](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L27)
|
|
355
357
|
|
|
356
358
|
#### Type Parameters
|
|
357
359
|
|
|
@@ -375,7 +377,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:28](https://github.com/lucaso
|
|
|
375
377
|
type SucceededCall<R, M> = object;
|
|
376
378
|
```
|
|
377
379
|
|
|
378
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
380
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:15](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L15)
|
|
379
381
|
|
|
380
382
|
#### Type Parameters
|
|
381
383
|
|
|
@@ -395,7 +397,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:14](https://github.com/lucaso
|
|
|
395
397
|
metadata: M;
|
|
396
398
|
```
|
|
397
399
|
|
|
398
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
400
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:17](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L17)
|
|
399
401
|
|
|
400
402
|
##### value
|
|
401
403
|
|
|
@@ -403,7 +405,7 @@ Defined in: [packages/utils/src/concurrentCalls.ts:16](https://github.com/lucaso
|
|
|
403
405
|
value: R;
|
|
404
406
|
```
|
|
405
407
|
|
|
406
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
408
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:16](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L16)
|
|
407
409
|
|
|
408
410
|
***
|
|
409
411
|
|
|
@@ -413,4 +415,4 @@ Defined in: [packages/utils/src/concurrentCalls.ts:15](https://github.com/lucaso
|
|
|
413
415
|
type ValidMetadata = string | number | boolean | Record<string, unknown>;
|
|
414
416
|
```
|
|
415
417
|
|
|
416
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
418
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:8](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L8)
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
### concurrentCalls()
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
function concurrentCalls<R
|
|
18
|
+
function concurrentCalls<R>(): ConcurrentCalls<R, Error>;
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
21
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:153](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L153)
|
|
22
22
|
|
|
23
23
|
Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
|
|
24
24
|
|
|
@@ -30,25 +30,19 @@ Executes multiple asynchronous calls concurrently and collects the results in a
|
|
|
30
30
|
|
|
31
31
|
The type of the result value.
|
|
32
32
|
|
|
33
|
-
##### E
|
|
34
|
-
|
|
35
|
-
`E` *extends* `Error` = `Error`
|
|
36
|
-
|
|
37
|
-
The type of the error.
|
|
38
|
-
|
|
39
33
|
#### Returns
|
|
40
34
|
|
|
41
|
-
[`ConcurrentCalls`](-internal-.md#concurrentcalls)\<`R`, `
|
|
35
|
+
[`ConcurrentCalls`](-internal-.md#concurrentcalls)\<`R`, `Error`\>
|
|
42
36
|
|
|
43
37
|
***
|
|
44
38
|
|
|
45
39
|
### concurrentCallsWithMetadata()
|
|
46
40
|
|
|
47
41
|
```ts
|
|
48
|
-
function concurrentCallsWithMetadata<M, R
|
|
42
|
+
function concurrentCallsWithMetadata<M, R>(): ConcurrentCallsWithMetadata<M, R, Error>;
|
|
49
43
|
```
|
|
50
44
|
|
|
51
|
-
Defined in: [packages/utils/src/concurrentCalls.ts:
|
|
45
|
+
Defined in: [packages/utils/src/concurrentCalls.ts:345](https://github.com/lucasols/utils/blob/main/packages/utils/src/concurrentCalls.ts#L345)
|
|
52
46
|
|
|
53
47
|
Executes multiple asynchronous calls concurrently and collects the results in a easier to use format.
|
|
54
48
|
|
|
@@ -66,12 +60,6 @@ The type of the call metadata.
|
|
|
66
60
|
|
|
67
61
|
The type of the result value.
|
|
68
62
|
|
|
69
|
-
##### E
|
|
70
|
-
|
|
71
|
-
`E` *extends* `Error` = `Error`
|
|
72
|
-
|
|
73
|
-
The type of the error from individual Result objects.
|
|
74
|
-
|
|
75
63
|
#### Returns
|
|
76
64
|
|
|
77
|
-
[`ConcurrentCallsWithMetadata`](-internal-.md#concurrentcallswithmetadata)\<`M`, `R`, `
|
|
65
|
+
[`ConcurrentCallsWithMetadata`](-internal-.md#concurrentcallswithmetadata)\<`M`, `R`, `Error`\>
|
|
@@ -23,7 +23,7 @@ function retryOnError<T>(
|
|
|
23
23
|
originalMaxRetries): Promise<T>;
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
Defined in: [packages/utils/src/retryOnError.ts:
|
|
26
|
+
Defined in: [packages/utils/src/retryOnError.ts:36](https://github.com/lucasols/utils/blob/main/packages/utils/src/retryOnError.ts#L36)
|
|
27
27
|
|
|
28
28
|
Retries a function on error with configurable retry logic.
|
|
29
29
|
|
|
@@ -57,14 +57,10 @@ Configuration options
|
|
|
57
57
|
|
|
58
58
|
`number` = `0`
|
|
59
59
|
|
|
60
|
-
Internal parameter tracking current retry count
|
|
61
|
-
|
|
62
60
|
##### originalMaxRetries
|
|
63
61
|
|
|
64
62
|
`number` = `maxRetries`
|
|
65
63
|
|
|
66
|
-
Internal parameter tracking original max retries for logging
|
|
67
|
-
|
|
68
64
|
#### Returns
|
|
69
65
|
|
|
70
66
|
`Promise`\<`T`\>
|
package/lib/concurrentCalls.cjs
CHANGED
|
@@ -44,7 +44,6 @@ function invariant(condition, error = "Invariant violation") {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
var isFunction2 = isFunction;
|
|
47
|
-
var isPromise2 = isPromise;
|
|
48
47
|
|
|
49
48
|
// src/arrayUtils.ts
|
|
50
49
|
function truncateArray(array, maxLength, appendIfTruncated) {
|
|
@@ -80,23 +79,17 @@ var ConcurrentCalls = class {
|
|
|
80
79
|
}
|
|
81
80
|
resultifyAdd(...calls) {
|
|
82
81
|
const processedCalls = calls.map((call) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
} catch (err) {
|
|
93
|
-
return import_t_result.Result.err((0, import_t_result.unknownToError)(err));
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
82
|
+
return async () => {
|
|
83
|
+
try {
|
|
84
|
+
const valueOrPromise = call();
|
|
85
|
+
const value = isPromise(valueOrPromise) ? await valueOrPromise : valueOrPromise;
|
|
86
|
+
return import_t_result.Result.ok(value);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
return import_t_result.Result.err((0, import_t_result.unknownToError)(err));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
97
91
|
});
|
|
98
|
-
|
|
99
|
-
return this.add(...correctlyTypedProcessedCalls);
|
|
92
|
+
return this.add(...processedCalls);
|
|
100
93
|
}
|
|
101
94
|
async runAll({ delayStart } = {}) {
|
|
102
95
|
invariant(!this.#alreadyRun, "Already run");
|
|
@@ -105,15 +98,10 @@ var ConcurrentCalls = class {
|
|
|
105
98
|
const asyncResults = await Promise.all(
|
|
106
99
|
this.#pendingCalls.map(async (fn, i) => {
|
|
107
100
|
try {
|
|
108
|
-
const isP = isPromise2(fn);
|
|
109
101
|
if (delayStart) {
|
|
110
|
-
invariant(
|
|
111
|
-
!isP,
|
|
112
|
-
"Delay start is not supported direct promise calls"
|
|
113
|
-
);
|
|
114
102
|
await sleep(delayStart(i));
|
|
115
103
|
}
|
|
116
|
-
const result = await
|
|
104
|
+
const result = await fn();
|
|
117
105
|
if (!result.ok) throw result.error;
|
|
118
106
|
return result.value;
|
|
119
107
|
} catch (exception) {
|
|
@@ -135,15 +123,10 @@ var ConcurrentCalls = class {
|
|
|
135
123
|
const settledResults = await Promise.allSettled(
|
|
136
124
|
this.#pendingCalls.map(async (callOrPromise, i) => {
|
|
137
125
|
try {
|
|
138
|
-
const isP = isPromise2(callOrPromise);
|
|
139
126
|
if (delayStart) {
|
|
140
|
-
invariant(
|
|
141
|
-
!isP,
|
|
142
|
-
"Delay start is not supported for direct promise calls"
|
|
143
|
-
);
|
|
144
127
|
await sleep(delayStart(i));
|
|
145
128
|
}
|
|
146
|
-
const result = await
|
|
129
|
+
const result = await callOrPromise();
|
|
147
130
|
if (!result.ok) {
|
|
148
131
|
throw result.error;
|
|
149
132
|
}
|
|
@@ -190,9 +173,10 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
190
173
|
}
|
|
191
174
|
resultifyAdd(...items) {
|
|
192
175
|
const processedItems = items.map(({ fn, metadata }) => {
|
|
193
|
-
const cb =
|
|
194
|
-
const
|
|
195
|
-
|
|
176
|
+
const cb = () => (0, import_t_result.resultify)(async () => {
|
|
177
|
+
const valueOrPromise = fn();
|
|
178
|
+
const value = isPromise(valueOrPromise) ? await valueOrPromise : valueOrPromise;
|
|
179
|
+
return value;
|
|
196
180
|
});
|
|
197
181
|
return {
|
|
198
182
|
fn: cb,
|
|
@@ -214,7 +198,7 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
214
198
|
if (delayStart) {
|
|
215
199
|
await sleep(delayStart(i));
|
|
216
200
|
}
|
|
217
|
-
const result = await
|
|
201
|
+
const result = await call.fn();
|
|
218
202
|
if (!result.ok) {
|
|
219
203
|
throw { metadata: call.metadata, error: result.error };
|
|
220
204
|
}
|
|
@@ -261,7 +245,7 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
261
245
|
if (delayStart) {
|
|
262
246
|
await sleep(delayStart(i));
|
|
263
247
|
}
|
|
264
|
-
const result = await
|
|
248
|
+
const result = await call.fn();
|
|
265
249
|
if (!result.ok) {
|
|
266
250
|
throw result.error;
|
|
267
251
|
}
|
|
@@ -12,7 +12,7 @@ type FailedCall<M, E extends Error = Error> = {
|
|
|
12
12
|
metadata: M;
|
|
13
13
|
error: E;
|
|
14
14
|
};
|
|
15
|
-
type Action<R, E extends Error> = (
|
|
15
|
+
type Action<R, E extends Error> = () => Promise<Result<R, E>>;
|
|
16
16
|
type SettledResultWithMetadata<R, M, E extends Error = Error> = {
|
|
17
17
|
ok: true;
|
|
18
18
|
value: R;
|
|
@@ -26,7 +26,7 @@ type SettledResultWithMetadata<R, M, E extends Error = Error> = {
|
|
|
26
26
|
declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
|
|
27
27
|
#private;
|
|
28
28
|
add(...calls: Action<R, E>[]): this;
|
|
29
|
-
resultifyAdd(...calls: (
|
|
29
|
+
resultifyAdd(...calls: ((() => R) | (() => Promise<R>))[]): this;
|
|
30
30
|
runAll({ delayStart }?: RunProps): Promise<Result<R[], E>>;
|
|
31
31
|
runAllSettled({ delayStart }?: RunProps): Promise<{
|
|
32
32
|
allFailed: boolean;
|
|
@@ -42,7 +42,7 @@ declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
|
|
|
42
42
|
* @template R - The type of the result value.
|
|
43
43
|
* @template E - The type of the error.
|
|
44
44
|
*/
|
|
45
|
-
declare function concurrentCalls<R = unknown
|
|
45
|
+
declare function concurrentCalls<R = unknown>(): ConcurrentCalls<R, Error>;
|
|
46
46
|
declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown, E extends Error = Error> {
|
|
47
47
|
#private;
|
|
48
48
|
add(...calls: {
|
|
@@ -50,7 +50,7 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
|
|
|
50
50
|
metadata: M;
|
|
51
51
|
}[]): this;
|
|
52
52
|
resultifyAdd(...items: {
|
|
53
|
-
fn: (() => R) | (() => Promise<R>)
|
|
53
|
+
fn: (() => R) | (() => Promise<R>);
|
|
54
54
|
metadata: M;
|
|
55
55
|
}[]): this;
|
|
56
56
|
runAll({ delayStart }?: RunProps): Promise<Result<SucceededCall<R, M>[], FailedCall<M, E>>>;
|
|
@@ -72,6 +72,6 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
|
|
|
72
72
|
* @template R - The type of the result value.
|
|
73
73
|
* @template E - The type of the error from individual Result objects.
|
|
74
74
|
*/
|
|
75
|
-
declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown
|
|
75
|
+
declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown>(): ConcurrentCallsWithMetadata<M, R, Error>;
|
|
76
76
|
|
|
77
77
|
export { concurrentCalls, concurrentCallsWithMetadata };
|
package/lib/concurrentCalls.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ type FailedCall<M, E extends Error = Error> = {
|
|
|
12
12
|
metadata: M;
|
|
13
13
|
error: E;
|
|
14
14
|
};
|
|
15
|
-
type Action<R, E extends Error> = (
|
|
15
|
+
type Action<R, E extends Error> = () => Promise<Result<R, E>>;
|
|
16
16
|
type SettledResultWithMetadata<R, M, E extends Error = Error> = {
|
|
17
17
|
ok: true;
|
|
18
18
|
value: R;
|
|
@@ -26,7 +26,7 @@ type SettledResultWithMetadata<R, M, E extends Error = Error> = {
|
|
|
26
26
|
declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
|
|
27
27
|
#private;
|
|
28
28
|
add(...calls: Action<R, E>[]): this;
|
|
29
|
-
resultifyAdd(...calls: (
|
|
29
|
+
resultifyAdd(...calls: ((() => R) | (() => Promise<R>))[]): this;
|
|
30
30
|
runAll({ delayStart }?: RunProps): Promise<Result<R[], E>>;
|
|
31
31
|
runAllSettled({ delayStart }?: RunProps): Promise<{
|
|
32
32
|
allFailed: boolean;
|
|
@@ -42,7 +42,7 @@ declare class ConcurrentCalls<R = unknown, E extends Error = Error> {
|
|
|
42
42
|
* @template R - The type of the result value.
|
|
43
43
|
* @template E - The type of the error.
|
|
44
44
|
*/
|
|
45
|
-
declare function concurrentCalls<R = unknown
|
|
45
|
+
declare function concurrentCalls<R = unknown>(): ConcurrentCalls<R, Error>;
|
|
46
46
|
declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown, E extends Error = Error> {
|
|
47
47
|
#private;
|
|
48
48
|
add(...calls: {
|
|
@@ -50,7 +50,7 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
|
|
|
50
50
|
metadata: M;
|
|
51
51
|
}[]): this;
|
|
52
52
|
resultifyAdd(...items: {
|
|
53
|
-
fn: (() => R) | (() => Promise<R>)
|
|
53
|
+
fn: (() => R) | (() => Promise<R>);
|
|
54
54
|
metadata: M;
|
|
55
55
|
}[]): this;
|
|
56
56
|
runAll({ delayStart }?: RunProps): Promise<Result<SucceededCall<R, M>[], FailedCall<M, E>>>;
|
|
@@ -72,6 +72,6 @@ declare class ConcurrentCallsWithMetadata<M extends ValidMetadata, R = unknown,
|
|
|
72
72
|
* @template R - The type of the result value.
|
|
73
73
|
* @template E - The type of the error from individual Result objects.
|
|
74
74
|
*/
|
|
75
|
-
declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown
|
|
75
|
+
declare function concurrentCallsWithMetadata<M extends ValidMetadata, R = unknown>(): ConcurrentCallsWithMetadata<M, R, Error>;
|
|
76
76
|
|
|
77
77
|
export { concurrentCalls, concurrentCallsWithMetadata };
|
package/lib/concurrentCalls.js
CHANGED
|
@@ -8,10 +8,11 @@ import {
|
|
|
8
8
|
truncateArray
|
|
9
9
|
} from "./chunk-SRVMMYSW.js";
|
|
10
10
|
import {
|
|
11
|
-
invariant
|
|
12
|
-
isPromise
|
|
11
|
+
invariant
|
|
13
12
|
} from "./chunk-C2SVCIWE.js";
|
|
14
|
-
import
|
|
13
|
+
import {
|
|
14
|
+
isPromise
|
|
15
|
+
} from "./chunk-JF2MDHOJ.js";
|
|
15
16
|
|
|
16
17
|
// src/concurrentCalls.ts
|
|
17
18
|
import { Result, resultify, unknownToError } from "t-result";
|
|
@@ -24,23 +25,17 @@ var ConcurrentCalls = class {
|
|
|
24
25
|
}
|
|
25
26
|
resultifyAdd(...calls) {
|
|
26
27
|
const processedCalls = calls.map((call) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
} catch (err) {
|
|
37
|
-
return Result.err(unknownToError(err));
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
28
|
+
return async () => {
|
|
29
|
+
try {
|
|
30
|
+
const valueOrPromise = call();
|
|
31
|
+
const value = isPromise(valueOrPromise) ? await valueOrPromise : valueOrPromise;
|
|
32
|
+
return Result.ok(value);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
return Result.err(unknownToError(err));
|
|
35
|
+
}
|
|
36
|
+
};
|
|
41
37
|
});
|
|
42
|
-
|
|
43
|
-
return this.add(...correctlyTypedProcessedCalls);
|
|
38
|
+
return this.add(...processedCalls);
|
|
44
39
|
}
|
|
45
40
|
async runAll({ delayStart } = {}) {
|
|
46
41
|
invariant(!this.#alreadyRun, "Already run");
|
|
@@ -49,15 +44,10 @@ var ConcurrentCalls = class {
|
|
|
49
44
|
const asyncResults = await Promise.all(
|
|
50
45
|
this.#pendingCalls.map(async (fn, i) => {
|
|
51
46
|
try {
|
|
52
|
-
const isP = isPromise(fn);
|
|
53
47
|
if (delayStart) {
|
|
54
|
-
invariant(
|
|
55
|
-
!isP,
|
|
56
|
-
"Delay start is not supported direct promise calls"
|
|
57
|
-
);
|
|
58
48
|
await sleep(delayStart(i));
|
|
59
49
|
}
|
|
60
|
-
const result = await
|
|
50
|
+
const result = await fn();
|
|
61
51
|
if (!result.ok) throw result.error;
|
|
62
52
|
return result.value;
|
|
63
53
|
} catch (exception) {
|
|
@@ -79,15 +69,10 @@ var ConcurrentCalls = class {
|
|
|
79
69
|
const settledResults = await Promise.allSettled(
|
|
80
70
|
this.#pendingCalls.map(async (callOrPromise, i) => {
|
|
81
71
|
try {
|
|
82
|
-
const isP = isPromise(callOrPromise);
|
|
83
72
|
if (delayStart) {
|
|
84
|
-
invariant(
|
|
85
|
-
!isP,
|
|
86
|
-
"Delay start is not supported for direct promise calls"
|
|
87
|
-
);
|
|
88
73
|
await sleep(delayStart(i));
|
|
89
74
|
}
|
|
90
|
-
const result = await
|
|
75
|
+
const result = await callOrPromise();
|
|
91
76
|
if (!result.ok) {
|
|
92
77
|
throw result.error;
|
|
93
78
|
}
|
|
@@ -134,9 +119,10 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
134
119
|
}
|
|
135
120
|
resultifyAdd(...items) {
|
|
136
121
|
const processedItems = items.map(({ fn, metadata }) => {
|
|
137
|
-
const cb =
|
|
138
|
-
const
|
|
139
|
-
|
|
122
|
+
const cb = () => resultify(async () => {
|
|
123
|
+
const valueOrPromise = fn();
|
|
124
|
+
const value = isPromise(valueOrPromise) ? await valueOrPromise : valueOrPromise;
|
|
125
|
+
return value;
|
|
140
126
|
});
|
|
141
127
|
return {
|
|
142
128
|
fn: cb,
|
|
@@ -158,7 +144,7 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
158
144
|
if (delayStart) {
|
|
159
145
|
await sleep(delayStart(i));
|
|
160
146
|
}
|
|
161
|
-
const result = await
|
|
147
|
+
const result = await call.fn();
|
|
162
148
|
if (!result.ok) {
|
|
163
149
|
throw { metadata: call.metadata, error: result.error };
|
|
164
150
|
}
|
|
@@ -205,7 +191,7 @@ var ConcurrentCallsWithMetadata = class {
|
|
|
205
191
|
if (delayStart) {
|
|
206
192
|
await sleep(delayStart(i));
|
|
207
193
|
}
|
|
208
|
-
const result = await
|
|
194
|
+
const result = await call.fn();
|
|
209
195
|
if (!result.ok) {
|
|
210
196
|
throw result.error;
|
|
211
197
|
}
|
package/lib/retryOnError.cjs
CHANGED
|
@@ -54,7 +54,13 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
|
|
|
54
54
|
typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
|
|
55
55
|
);
|
|
56
56
|
}
|
|
57
|
-
return retryOnError(
|
|
57
|
+
return retryOnError(
|
|
58
|
+
fn,
|
|
59
|
+
maxRetries - 1,
|
|
60
|
+
options,
|
|
61
|
+
retry + 1,
|
|
62
|
+
originalMaxRetries
|
|
63
|
+
);
|
|
58
64
|
} else {
|
|
59
65
|
throw error;
|
|
60
66
|
}
|
package/lib/retryOnError.d.cts
CHANGED
|
@@ -18,8 +18,6 @@ type RetryOptions = {
|
|
|
18
18
|
* @param fn - Function to retry that receives context with retry count
|
|
19
19
|
* @param maxRetries - Maximum number of retries
|
|
20
20
|
* @param options - Configuration options
|
|
21
|
-
* @param retry - Internal parameter tracking current retry count
|
|
22
|
-
* @param originalMaxRetries - Internal parameter tracking original max retries for logging
|
|
23
21
|
* @returns Promise resolving to the function result or rejecting with the final error
|
|
24
22
|
*
|
|
25
23
|
* @example
|
|
@@ -33,6 +31,7 @@ type RetryOptions = {
|
|
|
33
31
|
* );
|
|
34
32
|
*/
|
|
35
33
|
declare function retryOnError<T>(fn: (ctx: {
|
|
34
|
+
/** Current retry count, (0 for first attempt) */
|
|
36
35
|
retry: number;
|
|
37
36
|
}) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
|
|
38
37
|
|
package/lib/retryOnError.d.ts
CHANGED
|
@@ -18,8 +18,6 @@ type RetryOptions = {
|
|
|
18
18
|
* @param fn - Function to retry that receives context with retry count
|
|
19
19
|
* @param maxRetries - Maximum number of retries
|
|
20
20
|
* @param options - Configuration options
|
|
21
|
-
* @param retry - Internal parameter tracking current retry count
|
|
22
|
-
* @param originalMaxRetries - Internal parameter tracking original max retries for logging
|
|
23
21
|
* @returns Promise resolving to the function result or rejecting with the final error
|
|
24
22
|
*
|
|
25
23
|
* @example
|
|
@@ -33,6 +31,7 @@ type RetryOptions = {
|
|
|
33
31
|
* );
|
|
34
32
|
*/
|
|
35
33
|
declare function retryOnError<T>(fn: (ctx: {
|
|
34
|
+
/** Current retry count, (0 for first attempt) */
|
|
36
35
|
retry: number;
|
|
37
36
|
}) => Promise<T>, maxRetries: number, options?: RetryOptions, retry?: number, originalMaxRetries?: number): Promise<T>;
|
|
38
37
|
|
package/lib/retryOnError.js
CHANGED
|
@@ -27,7 +27,13 @@ async function retryOnError(fn, maxRetries, options = {}, retry = 0, originalMax
|
|
|
27
27
|
typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
|
|
28
28
|
);
|
|
29
29
|
}
|
|
30
|
-
return retryOnError(
|
|
30
|
+
return retryOnError(
|
|
31
|
+
fn,
|
|
32
|
+
maxRetries - 1,
|
|
33
|
+
options,
|
|
34
|
+
retry + 1,
|
|
35
|
+
originalMaxRetries
|
|
36
|
+
);
|
|
31
37
|
} else {
|
|
32
38
|
throw error;
|
|
33
39
|
}
|