@ls-stack/utils 3.38.0 → 3.39.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.
@@ -10,6 +10,1352 @@
10
10
 
11
11
  - [\<internal\>](-internal-.md)
12
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
+
13
1359
  ## Functions
14
1360
 
15
1361
  ### createAsyncQueue()
@@ -18,7 +1364,9 @@
18
1364
  function createAsyncQueue<T, E>(options?): AsyncQueue<T, E>;
19
1365
  ```
20
1366
 
21
- Defined in: [packages/utils/src/asyncQueue.ts:328](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L328)
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
22
1370
 
23
1371
  #### Type Parameters
24
1372
 
@@ -26,19 +1374,48 @@ Defined in: [packages/utils/src/asyncQueue.ts:328](https://github.com/lucasols/u
26
1374
 
27
1375
  `T`
28
1376
 
1377
+ The type of value returned by successful tasks
1378
+
29
1379
  ##### E
30
1380
 
31
1381
  `E` *extends* `ResultValidErrors` = `Error`
32
1382
 
1383
+ The type of errors that tasks can produce (defaults to Error)
1384
+
33
1385
  #### Parameters
34
1386
 
35
1387
  ##### options?
36
1388
 
37
1389
  [`AsyncQueueOptions`](-internal-.md#asyncqueueoptions)
38
1390
 
1391
+ Configuration options for the queue
1392
+
39
1393
  #### Returns
40
1394
 
41
- [`AsyncQueue`](-internal-.md#asyncqueue)\<`T`, `E`\>
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
+ ```
42
1419
 
43
1420
  ***
44
1421
 
@@ -48,7 +1425,9 @@ Defined in: [packages/utils/src/asyncQueue.ts:328](https://github.com/lucasols/u
48
1425
  function createAsyncQueueWithMeta<T, I, E>(options?): AsyncQueueWithMeta<T, I, E>;
49
1426
  ```
50
1427
 
51
- Defined in: [packages/utils/src/asyncQueue.ts:334](https://github.com/lucasols/utils/blob/main/packages/utils/src/asyncQueue.ts#L334)
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
52
1431
 
53
1432
  #### Type Parameters
54
1433
 
@@ -56,20 +1435,51 @@ Defined in: [packages/utils/src/asyncQueue.ts:334](https://github.com/lucasols/u
56
1435
 
57
1436
  `T`
58
1437
 
1438
+ The type of value returned by successful tasks
1439
+
59
1440
  ##### I
60
1441
 
61
1442
  `I`
62
1443
 
1444
+ The type of metadata (required for all tasks)
1445
+
63
1446
  ##### E
64
1447
 
65
1448
  `E` *extends* `ResultValidErrors` = `Error`
66
1449
 
1450
+ The type of errors that tasks can produce (defaults to Error)
1451
+
67
1452
  #### Parameters
68
1453
 
69
1454
  ##### options?
70
1455
 
71
1456
  [`AsyncQueueOptions`](-internal-.md#asyncqueueoptions)
72
1457
 
1458
+ Configuration options for the queue
1459
+
73
1460
  #### Returns
74
1461
 
75
- [`AsyncQueueWithMeta`](-internal-.md#asyncqueuewithmeta)\<`T`, `I`, `E`\>
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
+ ```