@ls-stack/utils 3.40.0 → 3.41.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/dist/arrayUtils.d.cts +70 -57
- package/dist/arrayUtils.d.ts +70 -57
- package/dist/assertions.d.cts +128 -117
- package/dist/assertions.d.ts +128 -117
- package/dist/asyncQueue.cjs +117 -112
- package/dist/asyncQueue.d.cts +217 -215
- package/dist/asyncQueue.d.ts +217 -215
- package/dist/asyncQueue.js +117 -112
- package/dist/awaitDebounce.d.cts +26 -23
- package/dist/awaitDebounce.d.ts +26 -23
- package/dist/cache.cjs +2 -1
- package/dist/cache.d.cts +15 -9
- package/dist/cache.d.ts +15 -9
- package/dist/cache.js +2 -1
- package/dist/{chunk-GHAQOUA6.js → chunk-23KPGKDT.js} +22 -1
- package/dist/{chunk-ADM37GSC.js → chunk-7L4KCZJJ.js} +12 -10
- package/dist/{chunk-KW55OTUG.js → chunk-B3KFV2MH.js} +2 -2
- package/dist/chunk-DTE2QMWE.js +48 -0
- package/dist/concurrentCalls.d.cts +8 -4
- package/dist/concurrentCalls.d.ts +8 -4
- package/dist/concurrentCalls.js +1 -1
- package/dist/debounce.d.cts +12 -20
- package/dist/debounce.d.ts +12 -20
- package/dist/dedent.d.cts +31 -31
- package/dist/dedent.d.ts +31 -31
- package/dist/deepEqual.d.cts +10 -9
- package/dist/deepEqual.d.ts +10 -9
- package/dist/filterObjectOrArrayKeys.d.cts +44 -19
- package/dist/filterObjectOrArrayKeys.d.ts +44 -19
- package/dist/getAutoIncrementId.d.cts +25 -23
- package/dist/getAutoIncrementId.d.ts +25 -23
- package/dist/getCompositeKey.d.cts +2 -1
- package/dist/getCompositeKey.d.ts +2 -1
- package/dist/getValueStableKey.d.cts +4 -3
- package/dist/getValueStableKey.d.ts +4 -3
- package/dist/mutationUtils.d.cts +2 -2
- package/dist/mutationUtils.d.ts +2 -2
- package/dist/objUtils.cjs +51 -2
- package/dist/objUtils.d.cts +8 -3
- package/dist/objUtils.d.ts +8 -3
- package/dist/objUtils.js +11 -3
- package/dist/parallelAsyncCalls.cjs +2 -1
- package/dist/parallelAsyncCalls.d.cts +5 -4
- package/dist/parallelAsyncCalls.d.ts +5 -4
- package/dist/parallelAsyncCalls.js +2 -1
- package/dist/retryOnError.d.cts +23 -20
- package/dist/retryOnError.d.ts +23 -20
- package/dist/runShellCmd.d.cts +40 -40
- package/dist/runShellCmd.d.ts +40 -40
- package/dist/safeJson.d.cts +6 -2
- package/dist/safeJson.d.ts +6 -2
- package/dist/saferTyping.d.cts +21 -14
- package/dist/saferTyping.d.ts +21 -14
- package/dist/shallowEqual.d.cts +1 -1
- package/dist/shallowEqual.d.ts +1 -1
- package/dist/stringUtils.cjs +2 -2
- package/dist/stringUtils.d.cts +6 -7
- package/dist/stringUtils.d.ts +6 -7
- package/dist/stringUtils.js +1 -1
- package/dist/testUtils.cjs +11 -9
- package/dist/testUtils.d.cts +32 -14
- package/dist/testUtils.d.ts +32 -14
- package/dist/testUtils.js +4 -3
- package/dist/throttle.d.cts +57 -48
- package/dist/throttle.d.ts +57 -48
- package/dist/timers.d.cts +68 -63
- package/dist/timers.d.ts +68 -63
- package/dist/tsResult.d.cts +7 -6
- package/dist/tsResult.d.ts +7 -6
- package/dist/typeGuards.d.cts +65 -64
- package/dist/typeGuards.d.ts +65 -64
- package/dist/typeUtils.d.cts +18 -4
- package/dist/typeUtils.d.ts +18 -4
- package/dist/typedStrings.d.cts +68 -57
- package/dist/typedStrings.d.ts +68 -57
- package/dist/typingFnUtils.d.cts +12 -6
- package/dist/typingFnUtils.d.ts +12 -6
- package/dist/typingFnUtils.js +12 -35
- package/dist/typingTestUtils.cjs +7 -1
- package/dist/typingTestUtils.d.cts +52 -11
- package/dist/typingTestUtils.d.ts +52 -11
- package/dist/typingTestUtils.js +7 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.cjs +11 -9
- package/dist/yamlStringify.js +2 -2
- package/package.json +5 -1
- package/docs/README.md +0 -72
- package/docs/_media/modules.md +0 -58
- package/docs/arrayUtils/-internal-.md +0 -179
- package/docs/arrayUtils/README.md +0 -550
- package/docs/assertions/-internal-.md +0 -63
- package/docs/assertions/README.md +0 -565
- package/docs/asyncQueue/-internal-.md +0 -297
- package/docs/asyncQueue/README.md +0 -1485
- package/docs/awaitDebounce.md +0 -66
- package/docs/cache/-internal-.md +0 -168
- package/docs/cache/README.md +0 -360
- package/docs/castValues.md +0 -47
- package/docs/concurrentCalls/-internal-.md +0 -490
- package/docs/concurrentCalls/README.md +0 -299
- package/docs/consoleFmt.md +0 -115
- package/docs/conversions.md +0 -27
- package/docs/createThrottleController/-internal-.md +0 -73
- package/docs/createThrottleController/README.md +0 -31
- package/docs/debounce.md +0 -188
- package/docs/dedent/-internal-.md +0 -17
- package/docs/dedent/README.md +0 -204
- package/docs/deepEqual.md +0 -94
- package/docs/enhancedMap.md +0 -358
- package/docs/exhaustiveMatch/-internal-.md +0 -39
- package/docs/exhaustiveMatch/README.md +0 -146
- package/docs/filterObjectOrArrayKeys.md +0 -109
- package/docs/getAutoIncrementId.md +0 -93
- package/docs/getCompositeKey.md +0 -39
- package/docs/getValueStableKey.md +0 -57
- package/docs/hash.md +0 -31
- package/docs/interpolate/-internal-.md +0 -61
- package/docs/interpolate/README.md +0 -62
- package/docs/keepPrevIfUnchanged.md +0 -43
- package/docs/levenshtein.md +0 -93
- package/docs/main.md +0 -21
- package/docs/mathUtils.md +0 -137
- package/docs/modules.md +0 -58
- package/docs/mutationUtils.md +0 -44
- package/docs/objUtils.md +0 -237
- package/docs/parallelAsyncCalls/-internal-.md +0 -347
- package/docs/parallelAsyncCalls/README.md +0 -45
- package/docs/promiseUtils/-internal-.md +0 -69
- package/docs/promiseUtils/README.md +0 -31
- package/docs/retryOnError/-internal-.md +0 -111
- package/docs/retryOnError/README.md +0 -168
- package/docs/runShellCmd/-internal-.md +0 -111
- package/docs/runShellCmd/README.md +0 -201
- package/docs/safeJson.md +0 -51
- package/docs/saferTyping.md +0 -228
- package/docs/serializeXML.md +0 -100
- package/docs/shallowEqual.md +0 -33
- package/docs/sleep.md +0 -27
- package/docs/stringUtils/-internal-.md +0 -17
- package/docs/stringUtils/README.md +0 -270
- package/docs/testUtils.md +0 -382
- package/docs/throttle/-internal-.md +0 -47
- package/docs/throttle/README.md +0 -178
- package/docs/time.md +0 -274
- package/docs/timers.md +0 -256
- package/docs/tsResult/-internal-.md +0 -327
- package/docs/tsResult/README.md +0 -702
- package/docs/typeGuards.md +0 -399
- package/docs/typeUtils/-internal-.md +0 -99
- package/docs/typeUtils/README.md +0 -195
- package/docs/typeUtils.typesTest.md +0 -7
- package/docs/typedStrings.md +0 -458
- package/docs/typingFnUtils/-internal-.md +0 -43
- package/docs/typingFnUtils/README.md +0 -317
- package/docs/typingTestUtils.md +0 -172
- package/docs/typingUtils.md +0 -135
- package/docs/yamlStringify.md +0 -83
|
@@ -1,1485 +0,0 @@
|
|
|
1
|
-
[**@ls-stack/utils**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@ls-stack/utils](../modules.md) / asyncQueue
|
|
6
|
-
|
|
7
|
-
# asyncQueue
|
|
8
|
-
|
|
9
|
-
## Modules
|
|
10
|
-
|
|
11
|
-
- [\<internal\>](-internal-.md)
|
|
12
|
-
|
|
13
|
-
## Classes
|
|
14
|
-
|
|
15
|
-
### AsyncQueue\<T, E, I\>
|
|
16
|
-
|
|
17
|
-
Defined in: [packages/utils/src/asyncQueue.ts:202](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L202)
|
|
18
|
-
|
|
19
|
-
A powerful async task queue with advanced error handling and flow control
|
|
20
|
-
|
|
21
|
-
#### Examples
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
const queue = createAsyncQueue<string>({ concurrency: 2 });
|
|
25
|
-
|
|
26
|
-
const processedItems: string[] = [];
|
|
27
|
-
|
|
28
|
-
queue.resultifyAdd(async () => {
|
|
29
|
-
await delay(100);
|
|
30
|
-
return 'task completed';
|
|
31
|
-
}).then(result => {
|
|
32
|
-
if (result.ok) processedItems.push(result.value);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
await queue.onIdle();
|
|
36
|
-
console.log('Processed:', processedItems);
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
```typescript
|
|
40
|
-
const queue = createAsyncQueue<string>({
|
|
41
|
-
stopOnError: true,
|
|
42
|
-
rejectPendingOnError: false
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Add batch of tasks
|
|
46
|
-
const items = ['item1', 'item2', 'bad-item', 'item3'];
|
|
47
|
-
items.forEach(item => {
|
|
48
|
-
queue.resultifyAdd(async () => {
|
|
49
|
-
if (item === 'bad-item') throw new Error('Processing failed');
|
|
50
|
-
return item.toUpperCase();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
await queue.onIdle();
|
|
55
|
-
|
|
56
|
-
if (queue.isStopped) {
|
|
57
|
-
console.log(`Stopped at ${queue.failed} failures, ${queue.size} remaining`);
|
|
58
|
-
// Reset and continue with remaining tasks
|
|
59
|
-
queue.reset();
|
|
60
|
-
await queue.onIdle();
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
const queue = createAsyncQueue<string>({ autoStart: false });
|
|
66
|
-
|
|
67
|
-
// Prepare all tasks without starting
|
|
68
|
-
queue.resultifyAdd(() => processTask1());
|
|
69
|
-
queue.resultifyAdd(() => processTask2());
|
|
70
|
-
queue.resultifyAdd(() => processTask3());
|
|
71
|
-
|
|
72
|
-
// Start processing when ready
|
|
73
|
-
queue.start();
|
|
74
|
-
await queue.onIdle();
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
#### Extended by
|
|
78
|
-
|
|
79
|
-
- [`AsyncQueueWithMeta`](#asyncqueuewithmeta)
|
|
80
|
-
|
|
81
|
-
#### Type Parameters
|
|
82
|
-
|
|
83
|
-
##### T
|
|
84
|
-
|
|
85
|
-
`T`
|
|
86
|
-
|
|
87
|
-
The type of value returned by successful tasks
|
|
88
|
-
|
|
89
|
-
##### E
|
|
90
|
-
|
|
91
|
-
`E` *extends* `ResultValidErrors` = `Error`
|
|
92
|
-
|
|
93
|
-
The type of errors that tasks can produce (defaults to Error)
|
|
94
|
-
|
|
95
|
-
##### I
|
|
96
|
-
|
|
97
|
-
`I` = `unknown`
|
|
98
|
-
|
|
99
|
-
The type of metadata associated with tasks (defaults to unknown)
|
|
100
|
-
|
|
101
|
-
#### Constructors
|
|
102
|
-
|
|
103
|
-
##### Constructor
|
|
104
|
-
|
|
105
|
-
```ts
|
|
106
|
-
new AsyncQueue<T, E, I>(__namedParameters): AsyncQueue<T, E, I>;
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
Defined in: [packages/utils/src/asyncQueue.ts:257](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L257)
|
|
110
|
-
|
|
111
|
-
###### Parameters
|
|
112
|
-
|
|
113
|
-
###### \_\_namedParameters
|
|
114
|
-
|
|
115
|
-
[`AsyncQueueOptions`](-internal-.md#asyncqueueoptions) = `{}`
|
|
116
|
-
|
|
117
|
-
###### Returns
|
|
118
|
-
|
|
119
|
-
[`AsyncQueue`](#asyncqueue)\<`T`, `E`, `I`\>
|
|
120
|
-
|
|
121
|
-
#### Properties
|
|
122
|
-
|
|
123
|
-
##### completions
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
completions: object[] = [];
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
Defined in: [packages/utils/src/asyncQueue.ts:255](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L255)
|
|
130
|
-
|
|
131
|
-
Array of all task completions with metadata for debugging and analysis
|
|
132
|
-
|
|
133
|
-
###### meta
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
meta: I;
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
###### value
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
value: T;
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
##### events
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
events: Emitter<{
|
|
149
|
-
complete: {
|
|
150
|
-
meta: I;
|
|
151
|
-
value: T;
|
|
152
|
-
};
|
|
153
|
-
error: {
|
|
154
|
-
error: Error | E;
|
|
155
|
-
meta: I;
|
|
156
|
-
};
|
|
157
|
-
start: {
|
|
158
|
-
meta: I;
|
|
159
|
-
};
|
|
160
|
-
}>;
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
Defined in: [packages/utils/src/asyncQueue.ts:231](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L231)
|
|
164
|
-
|
|
165
|
-
Event emitter for tracking task lifecycle
|
|
166
|
-
|
|
167
|
-
###### Example
|
|
168
|
-
|
|
169
|
-
```typescript
|
|
170
|
-
const queue = createAsyncQueue<string>();
|
|
171
|
-
|
|
172
|
-
queue.events.on('start', (event) => {
|
|
173
|
-
console.log('Task started:', event.payload.meta);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
queue.events.on('complete', (event) => {
|
|
177
|
-
console.log('Task completed:', event.payload.value);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
queue.events.on('error', (event) => {
|
|
181
|
-
console.error('Task failed:', event.payload.error);
|
|
182
|
-
});
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
##### failures
|
|
186
|
-
|
|
187
|
-
```ts
|
|
188
|
-
failures: object[] = [];
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Defined in: [packages/utils/src/asyncQueue.ts:252](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L252)
|
|
192
|
-
|
|
193
|
-
Array of all task failures with metadata for debugging and analysis
|
|
194
|
-
|
|
195
|
-
###### error
|
|
196
|
-
|
|
197
|
-
```ts
|
|
198
|
-
error: Error | E;
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
###### meta
|
|
202
|
-
|
|
203
|
-
```ts
|
|
204
|
-
meta: I;
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
#### Accessors
|
|
208
|
-
|
|
209
|
-
##### completed
|
|
210
|
-
|
|
211
|
-
###### Get Signature
|
|
212
|
-
|
|
213
|
-
```ts
|
|
214
|
-
get completed(): number;
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
Defined in: [packages/utils/src/asyncQueue.ts:785](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L785)
|
|
218
|
-
|
|
219
|
-
Number of tasks that have completed successfully
|
|
220
|
-
|
|
221
|
-
###### Returns
|
|
222
|
-
|
|
223
|
-
`number`
|
|
224
|
-
|
|
225
|
-
##### failed
|
|
226
|
-
|
|
227
|
-
###### Get Signature
|
|
228
|
-
|
|
229
|
-
```ts
|
|
230
|
-
get failed(): number;
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
Defined in: [packages/utils/src/asyncQueue.ts:790](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L790)
|
|
234
|
-
|
|
235
|
-
Number of tasks that have failed
|
|
236
|
-
|
|
237
|
-
###### Returns
|
|
238
|
-
|
|
239
|
-
`number`
|
|
240
|
-
|
|
241
|
-
##### isPaused
|
|
242
|
-
|
|
243
|
-
###### Get Signature
|
|
244
|
-
|
|
245
|
-
```ts
|
|
246
|
-
get isPaused(): boolean;
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
Defined in: [packages/utils/src/asyncQueue.ts:900](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L900)
|
|
250
|
-
|
|
251
|
-
Whether the queue is currently paused
|
|
252
|
-
|
|
253
|
-
###### Returns
|
|
254
|
-
|
|
255
|
-
`boolean`
|
|
256
|
-
|
|
257
|
-
##### isStarted
|
|
258
|
-
|
|
259
|
-
###### Get Signature
|
|
260
|
-
|
|
261
|
-
```ts
|
|
262
|
-
get isStarted(): boolean;
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
Defined in: [packages/utils/src/asyncQueue.ts:905](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L905)
|
|
266
|
-
|
|
267
|
-
Whether the queue has been started (relevant for autoStart: false)
|
|
268
|
-
|
|
269
|
-
###### Returns
|
|
270
|
-
|
|
271
|
-
`boolean`
|
|
272
|
-
|
|
273
|
-
##### isStopped
|
|
274
|
-
|
|
275
|
-
###### Get Signature
|
|
276
|
-
|
|
277
|
-
```ts
|
|
278
|
-
get isStopped(): boolean;
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
Defined in: [packages/utils/src/asyncQueue.ts:895](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L895)
|
|
282
|
-
|
|
283
|
-
Whether the queue is stopped due to an error
|
|
284
|
-
|
|
285
|
-
###### Returns
|
|
286
|
-
|
|
287
|
-
`boolean`
|
|
288
|
-
|
|
289
|
-
##### pending
|
|
290
|
-
|
|
291
|
-
###### Get Signature
|
|
292
|
-
|
|
293
|
-
```ts
|
|
294
|
-
get pending(): number;
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
Defined in: [packages/utils/src/asyncQueue.ts:795](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L795)
|
|
298
|
-
|
|
299
|
-
Number of tasks currently being processed
|
|
300
|
-
|
|
301
|
-
###### Returns
|
|
302
|
-
|
|
303
|
-
`number`
|
|
304
|
-
|
|
305
|
-
##### size
|
|
306
|
-
|
|
307
|
-
###### Get Signature
|
|
308
|
-
|
|
309
|
-
```ts
|
|
310
|
-
get size(): number;
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
Defined in: [packages/utils/src/asyncQueue.ts:800](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L800)
|
|
314
|
-
|
|
315
|
-
Number of tasks waiting in the queue to be processed
|
|
316
|
-
|
|
317
|
-
###### Returns
|
|
318
|
-
|
|
319
|
-
`number`
|
|
320
|
-
|
|
321
|
-
##### stoppedReason
|
|
322
|
-
|
|
323
|
-
###### Get Signature
|
|
324
|
-
|
|
325
|
-
```ts
|
|
326
|
-
get stoppedReason(): undefined | Error;
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
Defined in: [packages/utils/src/asyncQueue.ts:910](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L910)
|
|
330
|
-
|
|
331
|
-
The error that caused the queue to stop (if any)
|
|
332
|
-
|
|
333
|
-
###### Returns
|
|
334
|
-
|
|
335
|
-
`undefined` \| `Error`
|
|
336
|
-
|
|
337
|
-
#### Methods
|
|
338
|
-
|
|
339
|
-
##### add()
|
|
340
|
-
|
|
341
|
-
```ts
|
|
342
|
-
add(fn, options?): Promise<Result<T, Error | E>>;
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
Defined in: [packages/utils/src/asyncQueue.ts:401](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L401)
|
|
346
|
-
|
|
347
|
-
Add a task that returns a Result to the queue
|
|
348
|
-
|
|
349
|
-
Use this method when your task function already returns a Result type.
|
|
350
|
-
For functions that throw errors or return plain values, use `resultifyAdd` instead.
|
|
351
|
-
|
|
352
|
-
###### Parameters
|
|
353
|
-
|
|
354
|
-
###### fn
|
|
355
|
-
|
|
356
|
-
(`ctx`) => `Result`\<`T`, `E`\> \| `Promise`\<`Result`\<`T`, `E`\>\>
|
|
357
|
-
|
|
358
|
-
Task function that returns a Result
|
|
359
|
-
|
|
360
|
-
###### options?
|
|
361
|
-
|
|
362
|
-
[`AddOptions`](-internal-.md#addoptions)\<`I`, `T`, `E`\>
|
|
363
|
-
|
|
364
|
-
Optional configuration for this task
|
|
365
|
-
|
|
366
|
-
###### Returns
|
|
367
|
-
|
|
368
|
-
`Promise`\<`Result`\<`T`, `Error` \| `E`\>\>
|
|
369
|
-
|
|
370
|
-
Promise that resolves with the task result
|
|
371
|
-
|
|
372
|
-
###### Example
|
|
373
|
-
|
|
374
|
-
```typescript
|
|
375
|
-
const queue = createAsyncQueue<string>();
|
|
376
|
-
|
|
377
|
-
const result = await queue.add(async () => {
|
|
378
|
-
try {
|
|
379
|
-
const data = await fetchData();
|
|
380
|
-
return Result.ok(data);
|
|
381
|
-
} catch (error) {
|
|
382
|
-
return Result.err(error);
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
if (result.ok) {
|
|
387
|
-
console.log('Success:', result.value);
|
|
388
|
-
} else {
|
|
389
|
-
console.log('Error:', result.error);
|
|
390
|
-
}
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
##### clear()
|
|
394
|
-
|
|
395
|
-
```ts
|
|
396
|
-
clear(): void;
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
Defined in: [packages/utils/src/asyncQueue.ts:767](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L767)
|
|
400
|
-
|
|
401
|
-
Clear all queued tasks (does not affect currently running tasks)
|
|
402
|
-
|
|
403
|
-
This removes all tasks waiting in the queue but allows currently
|
|
404
|
-
executing tasks to complete normally.
|
|
405
|
-
|
|
406
|
-
###### Returns
|
|
407
|
-
|
|
408
|
-
`void`
|
|
409
|
-
|
|
410
|
-
###### Example
|
|
411
|
-
|
|
412
|
-
```typescript
|
|
413
|
-
const queue = createAsyncQueue({ concurrency: 1 });
|
|
414
|
-
|
|
415
|
-
// Add multiple tasks
|
|
416
|
-
queue.resultifyAdd(async () => longRunningTask()); // Will start immediately
|
|
417
|
-
queue.resultifyAdd(async () => task2()); // Queued
|
|
418
|
-
queue.resultifyAdd(async () => task3()); // Queued
|
|
419
|
-
|
|
420
|
-
// Clear remaining queued tasks
|
|
421
|
-
queue.clear();
|
|
422
|
-
|
|
423
|
-
// Only the first task will complete
|
|
424
|
-
await queue.onIdle();
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
##### onIdle()
|
|
428
|
-
|
|
429
|
-
```ts
|
|
430
|
-
onIdle(): Promise<void>;
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
Defined in: [packages/utils/src/asyncQueue.ts:711](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L711)
|
|
434
|
-
|
|
435
|
-
Wait for the queue to become idle (no pending tasks, no queued tasks, and no rate-limit timers)
|
|
436
|
-
|
|
437
|
-
This method resolves when:
|
|
438
|
-
- All tasks have completed (success or failure)
|
|
439
|
-
- The queue is stopped due to error (stopOnError), even with remaining tasks
|
|
440
|
-
- There are no queued tasks, no running tasks, and no pending rate-limit timers
|
|
441
|
-
|
|
442
|
-
###### Returns
|
|
443
|
-
|
|
444
|
-
`Promise`\<`void`\>
|
|
445
|
-
|
|
446
|
-
Promise that resolves when the queue is idle
|
|
447
|
-
|
|
448
|
-
###### Example
|
|
449
|
-
|
|
450
|
-
```typescript
|
|
451
|
-
const queue = createAsyncQueue<string>();
|
|
452
|
-
|
|
453
|
-
// Add multiple tasks
|
|
454
|
-
for (let i = 0; i < 10; i++) {
|
|
455
|
-
queue.resultifyAdd(async () => `task ${i}`);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// Wait for all tasks to complete
|
|
459
|
-
await queue.onIdle();
|
|
460
|
-
|
|
461
|
-
console.log(`Completed: ${queue.completed}, Failed: ${queue.failed}`);
|
|
462
|
-
```
|
|
463
|
-
|
|
464
|
-
##### onSizeLessThan()
|
|
465
|
-
|
|
466
|
-
```ts
|
|
467
|
-
onSizeLessThan(limit): Promise<void>;
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
Defined in: [packages/utils/src/asyncQueue.ts:736](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L736)
|
|
471
|
-
|
|
472
|
-
Wait until the queued task count is below a limit
|
|
473
|
-
|
|
474
|
-
Resolves immediately if `size < limit` at the moment of calling. This only
|
|
475
|
-
considers queued (not yet started) tasks; running tasks are tracked by
|
|
476
|
-
`pending`.
|
|
477
|
-
|
|
478
|
-
###### Parameters
|
|
479
|
-
|
|
480
|
-
###### limit
|
|
481
|
-
|
|
482
|
-
`number`
|
|
483
|
-
|
|
484
|
-
Threshold that `size` must be below to resolve
|
|
485
|
-
|
|
486
|
-
###### Returns
|
|
487
|
-
|
|
488
|
-
`Promise`\<`void`\>
|
|
489
|
-
|
|
490
|
-
##### pause()
|
|
491
|
-
|
|
492
|
-
```ts
|
|
493
|
-
pause(): void;
|
|
494
|
-
```
|
|
495
|
-
|
|
496
|
-
Defined in: [packages/utils/src/asyncQueue.ts:846](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L846)
|
|
497
|
-
|
|
498
|
-
Pause processing new tasks (currently running tasks continue)
|
|
499
|
-
|
|
500
|
-
###### Returns
|
|
501
|
-
|
|
502
|
-
`void`
|
|
503
|
-
|
|
504
|
-
###### Example
|
|
505
|
-
|
|
506
|
-
```typescript
|
|
507
|
-
const queue = createAsyncQueue();
|
|
508
|
-
|
|
509
|
-
// Start some tasks
|
|
510
|
-
queue.resultifyAdd(async () => longRunningTask1());
|
|
511
|
-
queue.resultifyAdd(async () => longRunningTask2());
|
|
512
|
-
|
|
513
|
-
// Pause before more tasks are picked up
|
|
514
|
-
queue.pause();
|
|
515
|
-
|
|
516
|
-
// Later, resume processing
|
|
517
|
-
queue.resume();
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
##### reset()
|
|
521
|
-
|
|
522
|
-
```ts
|
|
523
|
-
reset(): void;
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
Defined in: [packages/utils/src/asyncQueue.ts:885](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L885)
|
|
527
|
-
|
|
528
|
-
Reset the queue after being stopped, allowing new tasks to be processed
|
|
529
|
-
|
|
530
|
-
This clears the stopped state and error reason, and resumes processing
|
|
531
|
-
any remaining queued tasks if autoStart was enabled.
|
|
532
|
-
|
|
533
|
-
###### Returns
|
|
534
|
-
|
|
535
|
-
`void`
|
|
536
|
-
|
|
537
|
-
###### Example
|
|
538
|
-
|
|
539
|
-
```typescript
|
|
540
|
-
const queue = createAsyncQueue({ stopOnError: true });
|
|
541
|
-
|
|
542
|
-
// Add tasks that will cause the queue to stop
|
|
543
|
-
queue.resultifyAdd(async () => { throw new Error('fail'); });
|
|
544
|
-
queue.resultifyAdd(async () => 'remaining task');
|
|
545
|
-
|
|
546
|
-
await queue.onIdle();
|
|
547
|
-
|
|
548
|
-
if (queue.isStopped) {
|
|
549
|
-
console.log(`Queue stopped, ${queue.size} tasks remaining`);
|
|
550
|
-
|
|
551
|
-
// Reset and process remaining tasks
|
|
552
|
-
queue.reset();
|
|
553
|
-
await queue.onIdle();
|
|
554
|
-
}
|
|
555
|
-
```
|
|
556
|
-
|
|
557
|
-
##### resultifyAdd()
|
|
558
|
-
|
|
559
|
-
```ts
|
|
560
|
-
resultifyAdd(fn, options?): Promise<Result<T, Error | E>>;
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
Defined in: [packages/utils/src/asyncQueue.ts:489](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L489)
|
|
564
|
-
|
|
565
|
-
Add a task that returns a plain value or throws errors to the queue
|
|
566
|
-
|
|
567
|
-
This is the most commonly used method. It automatically wraps your function
|
|
568
|
-
to handle errors and convert them to Result types.
|
|
569
|
-
|
|
570
|
-
###### Parameters
|
|
571
|
-
|
|
572
|
-
###### fn
|
|
573
|
-
|
|
574
|
-
(`ctx`) => `T` \| `Promise`\<`T`\>
|
|
575
|
-
|
|
576
|
-
Task function that returns a value or throws
|
|
577
|
-
|
|
578
|
-
###### options?
|
|
579
|
-
|
|
580
|
-
[`AddOptions`](-internal-.md#addoptions)\<`I`, `T`, `E`\>
|
|
581
|
-
|
|
582
|
-
Optional configuration for this task
|
|
583
|
-
|
|
584
|
-
###### Returns
|
|
585
|
-
|
|
586
|
-
`Promise`\<`Result`\<`T`, `Error` \| `E`\>\>
|
|
587
|
-
|
|
588
|
-
Promise that resolves with the task result wrapped in Result
|
|
589
|
-
|
|
590
|
-
###### Examples
|
|
591
|
-
|
|
592
|
-
```typescript
|
|
593
|
-
const queue = createAsyncQueue<string>();
|
|
594
|
-
|
|
595
|
-
queue.resultifyAdd(async () => {
|
|
596
|
-
const response = await fetch('/api/data');
|
|
597
|
-
return response.json();
|
|
598
|
-
}).then(result => {
|
|
599
|
-
if (result.ok) {
|
|
600
|
-
console.log('Data:', result.value);
|
|
601
|
-
} else {
|
|
602
|
-
console.error('Failed:', result.error);
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
```
|
|
606
|
-
|
|
607
|
-
```typescript
|
|
608
|
-
queue.resultifyAdd(
|
|
609
|
-
async () => processData(),
|
|
610
|
-
{
|
|
611
|
-
onComplete: (data) => console.log('Processed:', data),
|
|
612
|
-
onError: (error) => console.error('Failed:', error),
|
|
613
|
-
timeout: 5000
|
|
614
|
-
}
|
|
615
|
-
);
|
|
616
|
-
```
|
|
617
|
-
|
|
618
|
-
##### resume()
|
|
619
|
-
|
|
620
|
-
```ts
|
|
621
|
-
resume(): void;
|
|
622
|
-
```
|
|
623
|
-
|
|
624
|
-
Defined in: [packages/utils/src/asyncQueue.ts:853](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L853)
|
|
625
|
-
|
|
626
|
-
Resume processing tasks after pause
|
|
627
|
-
|
|
628
|
-
###### Returns
|
|
629
|
-
|
|
630
|
-
`void`
|
|
631
|
-
|
|
632
|
-
##### start()
|
|
633
|
-
|
|
634
|
-
```ts
|
|
635
|
-
start(): void;
|
|
636
|
-
```
|
|
637
|
-
|
|
638
|
-
Defined in: [packages/utils/src/asyncQueue.ts:820](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L820)
|
|
639
|
-
|
|
640
|
-
Manually start processing tasks (only needed if autoStart: false)
|
|
641
|
-
|
|
642
|
-
###### Returns
|
|
643
|
-
|
|
644
|
-
`void`
|
|
645
|
-
|
|
646
|
-
###### Example
|
|
647
|
-
|
|
648
|
-
```typescript
|
|
649
|
-
const queue = createAsyncQueue({ autoStart: false });
|
|
650
|
-
|
|
651
|
-
// Add tasks without starting processing
|
|
652
|
-
queue.resultifyAdd(async () => 'task1');
|
|
653
|
-
queue.resultifyAdd(async () => 'task2');
|
|
654
|
-
|
|
655
|
-
// Start processing when ready
|
|
656
|
-
queue.start();
|
|
657
|
-
await queue.onIdle();
|
|
658
|
-
```
|
|
659
|
-
|
|
660
|
-
***
|
|
661
|
-
|
|
662
|
-
### AsyncQueueWithMeta\<T, I, E\>
|
|
663
|
-
|
|
664
|
-
Defined in: [packages/utils/src/asyncQueue.ts:953](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L953)
|
|
665
|
-
|
|
666
|
-
AsyncQueue variant that requires metadata for all tasks
|
|
667
|
-
|
|
668
|
-
This class enforces that every task must include metadata, which is useful
|
|
669
|
-
when you need to track or identify tasks consistently.
|
|
670
|
-
|
|
671
|
-
#### Example
|
|
672
|
-
|
|
673
|
-
```typescript
|
|
674
|
-
interface TaskMeta {
|
|
675
|
-
id: string;
|
|
676
|
-
priority: number;
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
const queue = createAsyncQueueWithMeta<string, TaskMeta>({ concurrency: 2 });
|
|
680
|
-
|
|
681
|
-
queue.resultifyAdd(
|
|
682
|
-
async () => processImportantTask(),
|
|
683
|
-
{ meta: { id: 'task-1', priority: 1 } }
|
|
684
|
-
);
|
|
685
|
-
|
|
686
|
-
// Listen to events with metadata
|
|
687
|
-
queue.events.on('complete', (event) => {
|
|
688
|
-
console.log(`Task ${event.payload.meta.id} completed`);
|
|
689
|
-
});
|
|
690
|
-
```
|
|
691
|
-
|
|
692
|
-
#### Extends
|
|
693
|
-
|
|
694
|
-
- [`AsyncQueue`](#asyncqueue)\<`T`, `E`, `I`\>
|
|
695
|
-
|
|
696
|
-
#### Type Parameters
|
|
697
|
-
|
|
698
|
-
##### T
|
|
699
|
-
|
|
700
|
-
`T`
|
|
701
|
-
|
|
702
|
-
The type of value returned by successful tasks
|
|
703
|
-
|
|
704
|
-
##### I
|
|
705
|
-
|
|
706
|
-
`I`
|
|
707
|
-
|
|
708
|
-
The type of metadata (required for all tasks)
|
|
709
|
-
|
|
710
|
-
##### E
|
|
711
|
-
|
|
712
|
-
`E` *extends* `ResultValidErrors` = `Error`
|
|
713
|
-
|
|
714
|
-
The type of errors that tasks can produce
|
|
715
|
-
|
|
716
|
-
#### Constructors
|
|
717
|
-
|
|
718
|
-
##### Constructor
|
|
719
|
-
|
|
720
|
-
```ts
|
|
721
|
-
new AsyncQueueWithMeta<T, I, E>(options?): AsyncQueueWithMeta<T, I, E>;
|
|
722
|
-
```
|
|
723
|
-
|
|
724
|
-
Defined in: [packages/utils/src/asyncQueue.ts:958](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L958)
|
|
725
|
-
|
|
726
|
-
###### Parameters
|
|
727
|
-
|
|
728
|
-
###### options?
|
|
729
|
-
|
|
730
|
-
[`AsyncQueueOptions`](-internal-.md#asyncqueueoptions)
|
|
731
|
-
|
|
732
|
-
###### Returns
|
|
733
|
-
|
|
734
|
-
[`AsyncQueueWithMeta`](#asyncqueuewithmeta)\<`T`, `I`, `E`\>
|
|
735
|
-
|
|
736
|
-
###### Overrides
|
|
737
|
-
|
|
738
|
-
[`AsyncQueue`](#asyncqueue).[`constructor`](#constructor)
|
|
739
|
-
|
|
740
|
-
#### Properties
|
|
741
|
-
|
|
742
|
-
##### completions
|
|
743
|
-
|
|
744
|
-
```ts
|
|
745
|
-
completions: object[] = [];
|
|
746
|
-
```
|
|
747
|
-
|
|
748
|
-
Defined in: [packages/utils/src/asyncQueue.ts:255](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L255)
|
|
749
|
-
|
|
750
|
-
Array of all task completions with metadata for debugging and analysis
|
|
751
|
-
|
|
752
|
-
###### meta
|
|
753
|
-
|
|
754
|
-
```ts
|
|
755
|
-
meta: I;
|
|
756
|
-
```
|
|
757
|
-
|
|
758
|
-
###### value
|
|
759
|
-
|
|
760
|
-
```ts
|
|
761
|
-
value: T;
|
|
762
|
-
```
|
|
763
|
-
|
|
764
|
-
###### Inherited from
|
|
765
|
-
|
|
766
|
-
[`AsyncQueue`](#asyncqueue).[`completions`](#completions)
|
|
767
|
-
|
|
768
|
-
##### events
|
|
769
|
-
|
|
770
|
-
```ts
|
|
771
|
-
events: Emitter<{
|
|
772
|
-
complete: {
|
|
773
|
-
meta: I;
|
|
774
|
-
value: T;
|
|
775
|
-
};
|
|
776
|
-
error: {
|
|
777
|
-
error: Error | E;
|
|
778
|
-
meta: I;
|
|
779
|
-
};
|
|
780
|
-
start: {
|
|
781
|
-
meta: I;
|
|
782
|
-
};
|
|
783
|
-
}>;
|
|
784
|
-
```
|
|
785
|
-
|
|
786
|
-
Defined in: [packages/utils/src/asyncQueue.ts:231](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L231)
|
|
787
|
-
|
|
788
|
-
Event emitter for tracking task lifecycle
|
|
789
|
-
|
|
790
|
-
###### Example
|
|
791
|
-
|
|
792
|
-
```typescript
|
|
793
|
-
const queue = createAsyncQueue<string>();
|
|
794
|
-
|
|
795
|
-
queue.events.on('start', (event) => {
|
|
796
|
-
console.log('Task started:', event.payload.meta);
|
|
797
|
-
});
|
|
798
|
-
|
|
799
|
-
queue.events.on('complete', (event) => {
|
|
800
|
-
console.log('Task completed:', event.payload.value);
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
queue.events.on('error', (event) => {
|
|
804
|
-
console.error('Task failed:', event.payload.error);
|
|
805
|
-
});
|
|
806
|
-
```
|
|
807
|
-
|
|
808
|
-
###### Inherited from
|
|
809
|
-
|
|
810
|
-
[`AsyncQueue`](#asyncqueue).[`events`](#events)
|
|
811
|
-
|
|
812
|
-
##### failures
|
|
813
|
-
|
|
814
|
-
```ts
|
|
815
|
-
failures: object[] = [];
|
|
816
|
-
```
|
|
817
|
-
|
|
818
|
-
Defined in: [packages/utils/src/asyncQueue.ts:252](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L252)
|
|
819
|
-
|
|
820
|
-
Array of all task failures with metadata for debugging and analysis
|
|
821
|
-
|
|
822
|
-
###### error
|
|
823
|
-
|
|
824
|
-
```ts
|
|
825
|
-
error: Error | E;
|
|
826
|
-
```
|
|
827
|
-
|
|
828
|
-
###### meta
|
|
829
|
-
|
|
830
|
-
```ts
|
|
831
|
-
meta: I;
|
|
832
|
-
```
|
|
833
|
-
|
|
834
|
-
###### Inherited from
|
|
835
|
-
|
|
836
|
-
[`AsyncQueue`](#asyncqueue).[`failures`](#failures)
|
|
837
|
-
|
|
838
|
-
#### Accessors
|
|
839
|
-
|
|
840
|
-
##### completed
|
|
841
|
-
|
|
842
|
-
###### Get Signature
|
|
843
|
-
|
|
844
|
-
```ts
|
|
845
|
-
get completed(): number;
|
|
846
|
-
```
|
|
847
|
-
|
|
848
|
-
Defined in: [packages/utils/src/asyncQueue.ts:785](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L785)
|
|
849
|
-
|
|
850
|
-
Number of tasks that have completed successfully
|
|
851
|
-
|
|
852
|
-
###### Returns
|
|
853
|
-
|
|
854
|
-
`number`
|
|
855
|
-
|
|
856
|
-
###### Inherited from
|
|
857
|
-
|
|
858
|
-
[`AsyncQueue`](#asyncqueue).[`completed`](#completed)
|
|
859
|
-
|
|
860
|
-
##### failed
|
|
861
|
-
|
|
862
|
-
###### Get Signature
|
|
863
|
-
|
|
864
|
-
```ts
|
|
865
|
-
get failed(): number;
|
|
866
|
-
```
|
|
867
|
-
|
|
868
|
-
Defined in: [packages/utils/src/asyncQueue.ts:790](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L790)
|
|
869
|
-
|
|
870
|
-
Number of tasks that have failed
|
|
871
|
-
|
|
872
|
-
###### Returns
|
|
873
|
-
|
|
874
|
-
`number`
|
|
875
|
-
|
|
876
|
-
###### Inherited from
|
|
877
|
-
|
|
878
|
-
[`AsyncQueue`](#asyncqueue).[`failed`](#failed)
|
|
879
|
-
|
|
880
|
-
##### isPaused
|
|
881
|
-
|
|
882
|
-
###### Get Signature
|
|
883
|
-
|
|
884
|
-
```ts
|
|
885
|
-
get isPaused(): boolean;
|
|
886
|
-
```
|
|
887
|
-
|
|
888
|
-
Defined in: [packages/utils/src/asyncQueue.ts:900](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L900)
|
|
889
|
-
|
|
890
|
-
Whether the queue is currently paused
|
|
891
|
-
|
|
892
|
-
###### Returns
|
|
893
|
-
|
|
894
|
-
`boolean`
|
|
895
|
-
|
|
896
|
-
###### Inherited from
|
|
897
|
-
|
|
898
|
-
[`AsyncQueue`](#asyncqueue).[`isPaused`](#ispaused)
|
|
899
|
-
|
|
900
|
-
##### isStarted
|
|
901
|
-
|
|
902
|
-
###### Get Signature
|
|
903
|
-
|
|
904
|
-
```ts
|
|
905
|
-
get isStarted(): boolean;
|
|
906
|
-
```
|
|
907
|
-
|
|
908
|
-
Defined in: [packages/utils/src/asyncQueue.ts:905](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L905)
|
|
909
|
-
|
|
910
|
-
Whether the queue has been started (relevant for autoStart: false)
|
|
911
|
-
|
|
912
|
-
###### Returns
|
|
913
|
-
|
|
914
|
-
`boolean`
|
|
915
|
-
|
|
916
|
-
###### Inherited from
|
|
917
|
-
|
|
918
|
-
[`AsyncQueue`](#asyncqueue).[`isStarted`](#isstarted)
|
|
919
|
-
|
|
920
|
-
##### isStopped
|
|
921
|
-
|
|
922
|
-
###### Get Signature
|
|
923
|
-
|
|
924
|
-
```ts
|
|
925
|
-
get isStopped(): boolean;
|
|
926
|
-
```
|
|
927
|
-
|
|
928
|
-
Defined in: [packages/utils/src/asyncQueue.ts:895](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L895)
|
|
929
|
-
|
|
930
|
-
Whether the queue is stopped due to an error
|
|
931
|
-
|
|
932
|
-
###### Returns
|
|
933
|
-
|
|
934
|
-
`boolean`
|
|
935
|
-
|
|
936
|
-
###### Inherited from
|
|
937
|
-
|
|
938
|
-
[`AsyncQueue`](#asyncqueue).[`isStopped`](#isstopped)
|
|
939
|
-
|
|
940
|
-
##### pending
|
|
941
|
-
|
|
942
|
-
###### Get Signature
|
|
943
|
-
|
|
944
|
-
```ts
|
|
945
|
-
get pending(): number;
|
|
946
|
-
```
|
|
947
|
-
|
|
948
|
-
Defined in: [packages/utils/src/asyncQueue.ts:795](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L795)
|
|
949
|
-
|
|
950
|
-
Number of tasks currently being processed
|
|
951
|
-
|
|
952
|
-
###### Returns
|
|
953
|
-
|
|
954
|
-
`number`
|
|
955
|
-
|
|
956
|
-
###### Inherited from
|
|
957
|
-
|
|
958
|
-
[`AsyncQueue`](#asyncqueue).[`pending`](#pending)
|
|
959
|
-
|
|
960
|
-
##### size
|
|
961
|
-
|
|
962
|
-
###### Get Signature
|
|
963
|
-
|
|
964
|
-
```ts
|
|
965
|
-
get size(): number;
|
|
966
|
-
```
|
|
967
|
-
|
|
968
|
-
Defined in: [packages/utils/src/asyncQueue.ts:800](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L800)
|
|
969
|
-
|
|
970
|
-
Number of tasks waiting in the queue to be processed
|
|
971
|
-
|
|
972
|
-
###### Returns
|
|
973
|
-
|
|
974
|
-
`number`
|
|
975
|
-
|
|
976
|
-
###### Inherited from
|
|
977
|
-
|
|
978
|
-
[`AsyncQueue`](#asyncqueue).[`size`](#size)
|
|
979
|
-
|
|
980
|
-
##### stoppedReason
|
|
981
|
-
|
|
982
|
-
###### Get Signature
|
|
983
|
-
|
|
984
|
-
```ts
|
|
985
|
-
get stoppedReason(): undefined | Error;
|
|
986
|
-
```
|
|
987
|
-
|
|
988
|
-
Defined in: [packages/utils/src/asyncQueue.ts:910](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L910)
|
|
989
|
-
|
|
990
|
-
The error that caused the queue to stop (if any)
|
|
991
|
-
|
|
992
|
-
###### Returns
|
|
993
|
-
|
|
994
|
-
`undefined` \| `Error`
|
|
995
|
-
|
|
996
|
-
###### Inherited from
|
|
997
|
-
|
|
998
|
-
[`AsyncQueue`](#asyncqueue).[`stoppedReason`](#stoppedreason)
|
|
999
|
-
|
|
1000
|
-
#### Methods
|
|
1001
|
-
|
|
1002
|
-
##### add()
|
|
1003
|
-
|
|
1004
|
-
```ts
|
|
1005
|
-
add(fn, options): Promise<Result<T, Error | E>>;
|
|
1006
|
-
```
|
|
1007
|
-
|
|
1008
|
-
Defined in: [packages/utils/src/asyncQueue.ts:962](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L962)
|
|
1009
|
-
|
|
1010
|
-
Add a task that returns a Result to the queue
|
|
1011
|
-
|
|
1012
|
-
Use this method when your task function already returns a Result type.
|
|
1013
|
-
For functions that throw errors or return plain values, use `resultifyAdd` instead.
|
|
1014
|
-
|
|
1015
|
-
###### Parameters
|
|
1016
|
-
|
|
1017
|
-
###### fn
|
|
1018
|
-
|
|
1019
|
-
(`ctx`) => `Result`\<`T`, `E`\> \| `Promise`\<`Result`\<`T`, `E`\>\>
|
|
1020
|
-
|
|
1021
|
-
Task function that returns a Result
|
|
1022
|
-
|
|
1023
|
-
###### options
|
|
1024
|
-
|
|
1025
|
-
[`AddOptionsWithId`](-internal-.md#addoptionswithid)\<`I`, `T`, `E`\>
|
|
1026
|
-
|
|
1027
|
-
Optional configuration for this task
|
|
1028
|
-
|
|
1029
|
-
###### Returns
|
|
1030
|
-
|
|
1031
|
-
`Promise`\<`Result`\<`T`, `Error` \| `E`\>\>
|
|
1032
|
-
|
|
1033
|
-
Promise that resolves with the task result
|
|
1034
|
-
|
|
1035
|
-
###### Example
|
|
1036
|
-
|
|
1037
|
-
```typescript
|
|
1038
|
-
const queue = createAsyncQueue<string>();
|
|
1039
|
-
|
|
1040
|
-
const result = await queue.add(async () => {
|
|
1041
|
-
try {
|
|
1042
|
-
const data = await fetchData();
|
|
1043
|
-
return Result.ok(data);
|
|
1044
|
-
} catch (error) {
|
|
1045
|
-
return Result.err(error);
|
|
1046
|
-
}
|
|
1047
|
-
});
|
|
1048
|
-
|
|
1049
|
-
if (result.ok) {
|
|
1050
|
-
console.log('Success:', result.value);
|
|
1051
|
-
} else {
|
|
1052
|
-
console.log('Error:', result.error);
|
|
1053
|
-
}
|
|
1054
|
-
```
|
|
1055
|
-
|
|
1056
|
-
###### Overrides
|
|
1057
|
-
|
|
1058
|
-
[`AsyncQueue`](#asyncqueue).[`add`](#add)
|
|
1059
|
-
|
|
1060
|
-
##### clear()
|
|
1061
|
-
|
|
1062
|
-
```ts
|
|
1063
|
-
clear(): void;
|
|
1064
|
-
```
|
|
1065
|
-
|
|
1066
|
-
Defined in: [packages/utils/src/asyncQueue.ts:767](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L767)
|
|
1067
|
-
|
|
1068
|
-
Clear all queued tasks (does not affect currently running tasks)
|
|
1069
|
-
|
|
1070
|
-
This removes all tasks waiting in the queue but allows currently
|
|
1071
|
-
executing tasks to complete normally.
|
|
1072
|
-
|
|
1073
|
-
###### Returns
|
|
1074
|
-
|
|
1075
|
-
`void`
|
|
1076
|
-
|
|
1077
|
-
###### Example
|
|
1078
|
-
|
|
1079
|
-
```typescript
|
|
1080
|
-
const queue = createAsyncQueue({ concurrency: 1 });
|
|
1081
|
-
|
|
1082
|
-
// Add multiple tasks
|
|
1083
|
-
queue.resultifyAdd(async () => longRunningTask()); // Will start immediately
|
|
1084
|
-
queue.resultifyAdd(async () => task2()); // Queued
|
|
1085
|
-
queue.resultifyAdd(async () => task3()); // Queued
|
|
1086
|
-
|
|
1087
|
-
// Clear remaining queued tasks
|
|
1088
|
-
queue.clear();
|
|
1089
|
-
|
|
1090
|
-
// Only the first task will complete
|
|
1091
|
-
await queue.onIdle();
|
|
1092
|
-
```
|
|
1093
|
-
|
|
1094
|
-
###### Inherited from
|
|
1095
|
-
|
|
1096
|
-
[`AsyncQueue`](#asyncqueue).[`clear`](#clear)
|
|
1097
|
-
|
|
1098
|
-
##### onIdle()
|
|
1099
|
-
|
|
1100
|
-
```ts
|
|
1101
|
-
onIdle(): Promise<void>;
|
|
1102
|
-
```
|
|
1103
|
-
|
|
1104
|
-
Defined in: [packages/utils/src/asyncQueue.ts:711](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L711)
|
|
1105
|
-
|
|
1106
|
-
Wait for the queue to become idle (no pending tasks, no queued tasks, and no rate-limit timers)
|
|
1107
|
-
|
|
1108
|
-
This method resolves when:
|
|
1109
|
-
- All tasks have completed (success or failure)
|
|
1110
|
-
- The queue is stopped due to error (stopOnError), even with remaining tasks
|
|
1111
|
-
- There are no queued tasks, no running tasks, and no pending rate-limit timers
|
|
1112
|
-
|
|
1113
|
-
###### Returns
|
|
1114
|
-
|
|
1115
|
-
`Promise`\<`void`\>
|
|
1116
|
-
|
|
1117
|
-
Promise that resolves when the queue is idle
|
|
1118
|
-
|
|
1119
|
-
###### Example
|
|
1120
|
-
|
|
1121
|
-
```typescript
|
|
1122
|
-
const queue = createAsyncQueue<string>();
|
|
1123
|
-
|
|
1124
|
-
// Add multiple tasks
|
|
1125
|
-
for (let i = 0; i < 10; i++) {
|
|
1126
|
-
queue.resultifyAdd(async () => `task ${i}`);
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
// Wait for all tasks to complete
|
|
1130
|
-
await queue.onIdle();
|
|
1131
|
-
|
|
1132
|
-
console.log(`Completed: ${queue.completed}, Failed: ${queue.failed}`);
|
|
1133
|
-
```
|
|
1134
|
-
|
|
1135
|
-
###### Inherited from
|
|
1136
|
-
|
|
1137
|
-
[`AsyncQueue`](#asyncqueue).[`onIdle`](#onidle)
|
|
1138
|
-
|
|
1139
|
-
##### onSizeLessThan()
|
|
1140
|
-
|
|
1141
|
-
```ts
|
|
1142
|
-
onSizeLessThan(limit): Promise<void>;
|
|
1143
|
-
```
|
|
1144
|
-
|
|
1145
|
-
Defined in: [packages/utils/src/asyncQueue.ts:736](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L736)
|
|
1146
|
-
|
|
1147
|
-
Wait until the queued task count is below a limit
|
|
1148
|
-
|
|
1149
|
-
Resolves immediately if `size < limit` at the moment of calling. This only
|
|
1150
|
-
considers queued (not yet started) tasks; running tasks are tracked by
|
|
1151
|
-
`pending`.
|
|
1152
|
-
|
|
1153
|
-
###### Parameters
|
|
1154
|
-
|
|
1155
|
-
###### limit
|
|
1156
|
-
|
|
1157
|
-
`number`
|
|
1158
|
-
|
|
1159
|
-
Threshold that `size` must be below to resolve
|
|
1160
|
-
|
|
1161
|
-
###### Returns
|
|
1162
|
-
|
|
1163
|
-
`Promise`\<`void`\>
|
|
1164
|
-
|
|
1165
|
-
###### Inherited from
|
|
1166
|
-
|
|
1167
|
-
[`AsyncQueue`](#asyncqueue).[`onSizeLessThan`](#onsizelessthan)
|
|
1168
|
-
|
|
1169
|
-
##### pause()
|
|
1170
|
-
|
|
1171
|
-
```ts
|
|
1172
|
-
pause(): void;
|
|
1173
|
-
```
|
|
1174
|
-
|
|
1175
|
-
Defined in: [packages/utils/src/asyncQueue.ts:846](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L846)
|
|
1176
|
-
|
|
1177
|
-
Pause processing new tasks (currently running tasks continue)
|
|
1178
|
-
|
|
1179
|
-
###### Returns
|
|
1180
|
-
|
|
1181
|
-
`void`
|
|
1182
|
-
|
|
1183
|
-
###### Example
|
|
1184
|
-
|
|
1185
|
-
```typescript
|
|
1186
|
-
const queue = createAsyncQueue();
|
|
1187
|
-
|
|
1188
|
-
// Start some tasks
|
|
1189
|
-
queue.resultifyAdd(async () => longRunningTask1());
|
|
1190
|
-
queue.resultifyAdd(async () => longRunningTask2());
|
|
1191
|
-
|
|
1192
|
-
// Pause before more tasks are picked up
|
|
1193
|
-
queue.pause();
|
|
1194
|
-
|
|
1195
|
-
// Later, resume processing
|
|
1196
|
-
queue.resume();
|
|
1197
|
-
```
|
|
1198
|
-
|
|
1199
|
-
###### Inherited from
|
|
1200
|
-
|
|
1201
|
-
[`AsyncQueue`](#asyncqueue).[`pause`](#pause)
|
|
1202
|
-
|
|
1203
|
-
##### reset()
|
|
1204
|
-
|
|
1205
|
-
```ts
|
|
1206
|
-
reset(): void;
|
|
1207
|
-
```
|
|
1208
|
-
|
|
1209
|
-
Defined in: [packages/utils/src/asyncQueue.ts:885](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L885)
|
|
1210
|
-
|
|
1211
|
-
Reset the queue after being stopped, allowing new tasks to be processed
|
|
1212
|
-
|
|
1213
|
-
This clears the stopped state and error reason, and resumes processing
|
|
1214
|
-
any remaining queued tasks if autoStart was enabled.
|
|
1215
|
-
|
|
1216
|
-
###### Returns
|
|
1217
|
-
|
|
1218
|
-
`void`
|
|
1219
|
-
|
|
1220
|
-
###### Example
|
|
1221
|
-
|
|
1222
|
-
```typescript
|
|
1223
|
-
const queue = createAsyncQueue({ stopOnError: true });
|
|
1224
|
-
|
|
1225
|
-
// Add tasks that will cause the queue to stop
|
|
1226
|
-
queue.resultifyAdd(async () => { throw new Error('fail'); });
|
|
1227
|
-
queue.resultifyAdd(async () => 'remaining task');
|
|
1228
|
-
|
|
1229
|
-
await queue.onIdle();
|
|
1230
|
-
|
|
1231
|
-
if (queue.isStopped) {
|
|
1232
|
-
console.log(`Queue stopped, ${queue.size} tasks remaining`);
|
|
1233
|
-
|
|
1234
|
-
// Reset and process remaining tasks
|
|
1235
|
-
queue.reset();
|
|
1236
|
-
await queue.onIdle();
|
|
1237
|
-
}
|
|
1238
|
-
```
|
|
1239
|
-
|
|
1240
|
-
###### Inherited from
|
|
1241
|
-
|
|
1242
|
-
[`AsyncQueue`](#asyncqueue).[`reset`](#reset)
|
|
1243
|
-
|
|
1244
|
-
##### resultifyAdd()
|
|
1245
|
-
|
|
1246
|
-
```ts
|
|
1247
|
-
resultifyAdd(fn, options): Promise<Result<T, Error | E>>;
|
|
1248
|
-
```
|
|
1249
|
-
|
|
1250
|
-
Defined in: [packages/utils/src/asyncQueue.ts:969](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L969)
|
|
1251
|
-
|
|
1252
|
-
Add a task that returns a plain value or throws errors to the queue
|
|
1253
|
-
|
|
1254
|
-
This is the most commonly used method. It automatically wraps your function
|
|
1255
|
-
to handle errors and convert them to Result types.
|
|
1256
|
-
|
|
1257
|
-
###### Parameters
|
|
1258
|
-
|
|
1259
|
-
###### fn
|
|
1260
|
-
|
|
1261
|
-
(`ctx`) => `T` \| `Promise`\<`T`\>
|
|
1262
|
-
|
|
1263
|
-
Task function that returns a value or throws
|
|
1264
|
-
|
|
1265
|
-
###### options
|
|
1266
|
-
|
|
1267
|
-
[`AddOptionsWithId`](-internal-.md#addoptionswithid)\<`I`, `T`, `E`\>
|
|
1268
|
-
|
|
1269
|
-
Optional configuration for this task
|
|
1270
|
-
|
|
1271
|
-
###### Returns
|
|
1272
|
-
|
|
1273
|
-
`Promise`\<`Result`\<`T`, `Error` \| `E`\>\>
|
|
1274
|
-
|
|
1275
|
-
Promise that resolves with the task result wrapped in Result
|
|
1276
|
-
|
|
1277
|
-
###### Examples
|
|
1278
|
-
|
|
1279
|
-
```typescript
|
|
1280
|
-
const queue = createAsyncQueue<string>();
|
|
1281
|
-
|
|
1282
|
-
queue.resultifyAdd(async () => {
|
|
1283
|
-
const response = await fetch('/api/data');
|
|
1284
|
-
return response.json();
|
|
1285
|
-
}).then(result => {
|
|
1286
|
-
if (result.ok) {
|
|
1287
|
-
console.log('Data:', result.value);
|
|
1288
|
-
} else {
|
|
1289
|
-
console.error('Failed:', result.error);
|
|
1290
|
-
}
|
|
1291
|
-
});
|
|
1292
|
-
```
|
|
1293
|
-
|
|
1294
|
-
```typescript
|
|
1295
|
-
queue.resultifyAdd(
|
|
1296
|
-
async () => processData(),
|
|
1297
|
-
{
|
|
1298
|
-
onComplete: (data) => console.log('Processed:', data),
|
|
1299
|
-
onError: (error) => console.error('Failed:', error),
|
|
1300
|
-
timeout: 5000
|
|
1301
|
-
}
|
|
1302
|
-
);
|
|
1303
|
-
```
|
|
1304
|
-
|
|
1305
|
-
###### Overrides
|
|
1306
|
-
|
|
1307
|
-
[`AsyncQueue`](#asyncqueue).[`resultifyAdd`](#resultifyadd)
|
|
1308
|
-
|
|
1309
|
-
##### resume()
|
|
1310
|
-
|
|
1311
|
-
```ts
|
|
1312
|
-
resume(): void;
|
|
1313
|
-
```
|
|
1314
|
-
|
|
1315
|
-
Defined in: [packages/utils/src/asyncQueue.ts:853](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L853)
|
|
1316
|
-
|
|
1317
|
-
Resume processing tasks after pause
|
|
1318
|
-
|
|
1319
|
-
###### Returns
|
|
1320
|
-
|
|
1321
|
-
`void`
|
|
1322
|
-
|
|
1323
|
-
###### Inherited from
|
|
1324
|
-
|
|
1325
|
-
[`AsyncQueue`](#asyncqueue).[`resume`](#resume)
|
|
1326
|
-
|
|
1327
|
-
##### start()
|
|
1328
|
-
|
|
1329
|
-
```ts
|
|
1330
|
-
start(): void;
|
|
1331
|
-
```
|
|
1332
|
-
|
|
1333
|
-
Defined in: [packages/utils/src/asyncQueue.ts:820](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L820)
|
|
1334
|
-
|
|
1335
|
-
Manually start processing tasks (only needed if autoStart: false)
|
|
1336
|
-
|
|
1337
|
-
###### Returns
|
|
1338
|
-
|
|
1339
|
-
`void`
|
|
1340
|
-
|
|
1341
|
-
###### Example
|
|
1342
|
-
|
|
1343
|
-
```typescript
|
|
1344
|
-
const queue = createAsyncQueue({ autoStart: false });
|
|
1345
|
-
|
|
1346
|
-
// Add tasks without starting processing
|
|
1347
|
-
queue.resultifyAdd(async () => 'task1');
|
|
1348
|
-
queue.resultifyAdd(async () => 'task2');
|
|
1349
|
-
|
|
1350
|
-
// Start processing when ready
|
|
1351
|
-
queue.start();
|
|
1352
|
-
await queue.onIdle();
|
|
1353
|
-
```
|
|
1354
|
-
|
|
1355
|
-
###### Inherited from
|
|
1356
|
-
|
|
1357
|
-
[`AsyncQueue`](#asyncqueue).[`start`](#start)
|
|
1358
|
-
|
|
1359
|
-
## Functions
|
|
1360
|
-
|
|
1361
|
-
### createAsyncQueue()
|
|
1362
|
-
|
|
1363
|
-
```ts
|
|
1364
|
-
function createAsyncQueue<T, E>(options?): AsyncQueue<T, E>;
|
|
1365
|
-
```
|
|
1366
|
-
|
|
1367
|
-
Defined in: [packages/utils/src/asyncQueue.ts:1007](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L1007)
|
|
1368
|
-
|
|
1369
|
-
Create a new AsyncQueue instance
|
|
1370
|
-
|
|
1371
|
-
#### Type Parameters
|
|
1372
|
-
|
|
1373
|
-
##### T
|
|
1374
|
-
|
|
1375
|
-
`T`
|
|
1376
|
-
|
|
1377
|
-
The type of value returned by successful tasks
|
|
1378
|
-
|
|
1379
|
-
##### E
|
|
1380
|
-
|
|
1381
|
-
`E` *extends* `ResultValidErrors` = `Error`
|
|
1382
|
-
|
|
1383
|
-
The type of errors that tasks can produce (defaults to Error)
|
|
1384
|
-
|
|
1385
|
-
#### Parameters
|
|
1386
|
-
|
|
1387
|
-
##### options?
|
|
1388
|
-
|
|
1389
|
-
[`AsyncQueueOptions`](-internal-.md#asyncqueueoptions)
|
|
1390
|
-
|
|
1391
|
-
Configuration options for the queue
|
|
1392
|
-
|
|
1393
|
-
#### Returns
|
|
1394
|
-
|
|
1395
|
-
[`AsyncQueue`](#asyncqueue)\<`T`, `E`\>
|
|
1396
|
-
|
|
1397
|
-
A new AsyncQueue instance
|
|
1398
|
-
|
|
1399
|
-
#### Examples
|
|
1400
|
-
|
|
1401
|
-
```typescript
|
|
1402
|
-
const queue = createAsyncQueue<string>({ concurrency: 3 });
|
|
1403
|
-
```
|
|
1404
|
-
|
|
1405
|
-
```typescript
|
|
1406
|
-
const queue = createAsyncQueue<string>({
|
|
1407
|
-
concurrency: 2,
|
|
1408
|
-
stopOnError: true,
|
|
1409
|
-
rejectPendingOnError: true
|
|
1410
|
-
});
|
|
1411
|
-
```
|
|
1412
|
-
|
|
1413
|
-
```typescript
|
|
1414
|
-
const queue = createAsyncQueue<string>({
|
|
1415
|
-
autoStart: false,
|
|
1416
|
-
concurrency: 1
|
|
1417
|
-
});
|
|
1418
|
-
```
|
|
1419
|
-
|
|
1420
|
-
***
|
|
1421
|
-
|
|
1422
|
-
### createAsyncQueueWithMeta()
|
|
1423
|
-
|
|
1424
|
-
```ts
|
|
1425
|
-
function createAsyncQueueWithMeta<T, I, E>(options?): AsyncQueueWithMeta<T, I, E>;
|
|
1426
|
-
```
|
|
1427
|
-
|
|
1428
|
-
Defined in: [packages/utils/src/asyncQueue.ts:1044](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L1044)
|
|
1429
|
-
|
|
1430
|
-
Create a new AsyncQueueWithMeta instance that requires metadata for all tasks
|
|
1431
|
-
|
|
1432
|
-
#### Type Parameters
|
|
1433
|
-
|
|
1434
|
-
##### T
|
|
1435
|
-
|
|
1436
|
-
`T`
|
|
1437
|
-
|
|
1438
|
-
The type of value returned by successful tasks
|
|
1439
|
-
|
|
1440
|
-
##### I
|
|
1441
|
-
|
|
1442
|
-
`I`
|
|
1443
|
-
|
|
1444
|
-
The type of metadata (required for all tasks)
|
|
1445
|
-
|
|
1446
|
-
##### E
|
|
1447
|
-
|
|
1448
|
-
`E` *extends* `ResultValidErrors` = `Error`
|
|
1449
|
-
|
|
1450
|
-
The type of errors that tasks can produce (defaults to Error)
|
|
1451
|
-
|
|
1452
|
-
#### Parameters
|
|
1453
|
-
|
|
1454
|
-
##### options?
|
|
1455
|
-
|
|
1456
|
-
[`AsyncQueueOptions`](-internal-.md#asyncqueueoptions)
|
|
1457
|
-
|
|
1458
|
-
Configuration options for the queue
|
|
1459
|
-
|
|
1460
|
-
#### Returns
|
|
1461
|
-
|
|
1462
|
-
[`AsyncQueueWithMeta`](#asyncqueuewithmeta)\<`T`, `I`, `E`\>
|
|
1463
|
-
|
|
1464
|
-
A new AsyncQueueWithMeta instance
|
|
1465
|
-
|
|
1466
|
-
#### Example
|
|
1467
|
-
|
|
1468
|
-
```typescript
|
|
1469
|
-
interface TaskInfo {
|
|
1470
|
-
taskId: string;
|
|
1471
|
-
userId: string;
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
const queue = createAsyncQueueWithMeta<ProcessResult, TaskInfo>({
|
|
1475
|
-
concurrency: 5
|
|
1476
|
-
});
|
|
1477
|
-
|
|
1478
|
-
queue.resultifyAdd(
|
|
1479
|
-
async (ctx) => {
|
|
1480
|
-
console.log(`Processing task ${ctx.meta.taskId} for user ${ctx.meta.userId}`);
|
|
1481
|
-
return await processUserTask(ctx.meta.userId);
|
|
1482
|
-
},
|
|
1483
|
-
{ meta: { taskId: '123', userId: 'user456' } }
|
|
1484
|
-
);
|
|
1485
|
-
```
|