@ls-stack/utils 3.39.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/consoleFmt.cjs +1 -0
- package/dist/consoleFmt.d.cts +6 -0
- package/dist/consoleFmt.d.ts +6 -0
- package/dist/consoleFmt.js +1 -0
- 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 +6 -2
- 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 -91
- 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
package/dist/asyncQueue.d.cts
CHANGED
|
@@ -2,18 +2,14 @@ import * as evtmitter from 'evtmitter';
|
|
|
2
2
|
import { ResultValidErrors, Result } from 't-result';
|
|
3
3
|
import { DurationObj } from './time.cjs';
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for rate limiting task execution
|
|
7
|
-
*/
|
|
5
|
+
/** Configuration for rate limiting task execution */
|
|
8
6
|
type RateLimit = {
|
|
9
7
|
/** Maximum number of tasks to execute within the interval */
|
|
10
8
|
maxTasks: number;
|
|
11
9
|
/** Time interval in milliseconds or as a duration object */
|
|
12
10
|
interval: DurationObj | number;
|
|
13
11
|
};
|
|
14
|
-
/**
|
|
15
|
-
* Configuration options for AsyncQueue initialization
|
|
16
|
-
*/
|
|
12
|
+
/** Configuration options for AsyncQueue initialization */
|
|
17
13
|
type AsyncQueueOptions = {
|
|
18
14
|
/** Maximum number of tasks to run concurrently (default: 1) */
|
|
19
15
|
concurrency?: number;
|
|
@@ -30,9 +26,7 @@ type AsyncQueueOptions = {
|
|
|
30
26
|
/** Rate limit configuration to limit tasks per time interval */
|
|
31
27
|
rateLimit?: RateLimit;
|
|
32
28
|
};
|
|
33
|
-
/**
|
|
34
|
-
* Options for adding individual tasks to the queue
|
|
35
|
-
*/
|
|
29
|
+
/** Options for adding individual tasks to the queue */
|
|
36
30
|
type AddOptions<I, T, E extends ResultValidErrors> = {
|
|
37
31
|
/** AbortSignal to cancel this specific task */
|
|
38
32
|
signal?: AbortSignal;
|
|
@@ -45,9 +39,7 @@ type AddOptions<I, T, E extends ResultValidErrors> = {
|
|
|
45
39
|
/** Callback invoked when task fails */
|
|
46
40
|
onError?: (error: E | Error) => void;
|
|
47
41
|
};
|
|
48
|
-
/**
|
|
49
|
-
* Runtime context passed to task functions
|
|
50
|
-
*/
|
|
42
|
+
/** Runtime context passed to task functions */
|
|
51
43
|
type RunCtx<I> = {
|
|
52
44
|
/** Combined AbortSignal from task, queue, and timeout signals */
|
|
53
45
|
signal?: AbortSignal;
|
|
@@ -57,88 +49,93 @@ type RunCtx<I> = {
|
|
|
57
49
|
/**
|
|
58
50
|
* A powerful async task queue with advanced error handling and flow control
|
|
59
51
|
*
|
|
60
|
-
* @
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* @example Basic Usage
|
|
65
|
-
* ```typescript
|
|
66
|
-
* const queue = createAsyncQueue<string>({ concurrency: 2 });
|
|
52
|
+
* @example
|
|
53
|
+
* Basic Usage
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const queue = createAsyncQueue<string>({ concurrency: 2 });
|
|
67
56
|
*
|
|
68
|
-
*
|
|
57
|
+
* const processedItems: string[] = [];
|
|
69
58
|
*
|
|
70
|
-
*
|
|
59
|
+
* queue.resultifyAdd(async () => {
|
|
71
60
|
* await delay(100);
|
|
72
61
|
* return 'task completed';
|
|
73
|
-
*
|
|
62
|
+
* }).then(result => {
|
|
74
63
|
* if (result.ok) processedItems.push(result.value);
|
|
75
|
-
*
|
|
64
|
+
* });
|
|
76
65
|
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
66
|
+
* await queue.onIdle();
|
|
67
|
+
* console.log('Processed:', processedItems);
|
|
68
|
+
* ```
|
|
80
69
|
*
|
|
81
|
-
* @example
|
|
82
|
-
*
|
|
83
|
-
*
|
|
70
|
+
* @example
|
|
71
|
+
* Error Recovery
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const queue = createAsyncQueue<string>({
|
|
84
74
|
* stopOnError: true,
|
|
85
75
|
* rejectPendingOnError: false
|
|
86
|
-
*
|
|
76
|
+
* });
|
|
87
77
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
78
|
+
* // Add batch of tasks
|
|
79
|
+
* const items = ['item1', 'item2', 'bad-item', 'item3'];
|
|
80
|
+
* items.forEach(item => {
|
|
91
81
|
* queue.resultifyAdd(async () => {
|
|
92
|
-
*
|
|
93
|
-
*
|
|
82
|
+
* if (item === 'bad-item') throw new Error('Processing failed');
|
|
83
|
+
* return item.toUpperCase();
|
|
84
|
+
* });
|
|
94
85
|
* });
|
|
95
|
-
* });
|
|
96
86
|
*
|
|
97
|
-
*
|
|
87
|
+
* await queue.onIdle();
|
|
98
88
|
*
|
|
99
|
-
*
|
|
89
|
+
* if (queue.isStopped) {
|
|
100
90
|
* console.log(`Stopped at ${queue.failed} failures, ${queue.size} remaining`);
|
|
101
91
|
* // Reset and continue with remaining tasks
|
|
102
92
|
* queue.reset();
|
|
103
93
|
* await queue.onIdle();
|
|
104
|
-
*
|
|
105
|
-
*
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* Lazy Start
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const queue = createAsyncQueue<string>({ autoStart: false });
|
|
106
101
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
102
|
+
* // Prepare all tasks without starting
|
|
103
|
+
* queue.resultifyAdd(() => processTask1());
|
|
104
|
+
* queue.resultifyAdd(() => processTask2());
|
|
105
|
+
* queue.resultifyAdd(() => processTask3());
|
|
110
106
|
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* queue.
|
|
114
|
-
*
|
|
107
|
+
* // Start processing when ready
|
|
108
|
+
* queue.start();
|
|
109
|
+
* await queue.onIdle();
|
|
110
|
+
* ```
|
|
115
111
|
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
112
|
+
* @template T - The type of value returned by successful tasks
|
|
113
|
+
* @template E - The type of errors that tasks can produce (defaults to Error)
|
|
114
|
+
* @template I - The type of metadata associated with tasks (defaults to
|
|
115
|
+
* unknown)
|
|
120
116
|
*/
|
|
121
117
|
declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
122
118
|
#private;
|
|
123
119
|
/**
|
|
124
120
|
* Event emitter for tracking task lifecycle
|
|
125
121
|
*
|
|
126
|
-
* @example
|
|
127
|
-
*
|
|
128
|
-
*
|
|
122
|
+
* @example
|
|
123
|
+
* Listening to Events
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const queue = createAsyncQueue<string>();
|
|
129
126
|
*
|
|
130
|
-
*
|
|
127
|
+
* queue.events.on('start', (event) => {
|
|
131
128
|
* console.log('Task started:', event.payload.meta);
|
|
132
|
-
*
|
|
129
|
+
* });
|
|
133
130
|
*
|
|
134
|
-
*
|
|
131
|
+
* queue.events.on('complete', (event) => {
|
|
135
132
|
* console.log('Task completed:', event.payload.value);
|
|
136
|
-
*
|
|
133
|
+
* });
|
|
137
134
|
*
|
|
138
|
-
*
|
|
135
|
+
* queue.events.on('error', (event) => {
|
|
139
136
|
* console.error('Task failed:', event.payload.error);
|
|
140
|
-
*
|
|
141
|
-
*
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
142
139
|
*/
|
|
143
140
|
events: evtmitter.Emitter<{
|
|
144
141
|
/** Emitted when a task starts executing */
|
|
@@ -170,32 +167,33 @@ declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
|
170
167
|
/**
|
|
171
168
|
* Add a task that returns a Result to the queue
|
|
172
169
|
*
|
|
173
|
-
* Use this method when your task function already returns a Result type.
|
|
174
|
-
*
|
|
170
|
+
* Use this method when your task function already returns a Result type. For
|
|
171
|
+
* functions that throw errors or return plain values, use `resultifyAdd`
|
|
172
|
+
* instead.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* const queue = createAsyncQueue<string>();
|
|
177
|
+
*
|
|
178
|
+
* const result = await queue.add(async () => {
|
|
179
|
+
* try {
|
|
180
|
+
* const data = await fetchData();
|
|
181
|
+
* return Result.ok(data);
|
|
182
|
+
* } catch (error) {
|
|
183
|
+
* return Result.err(error);
|
|
184
|
+
* }
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* if (result.ok) {
|
|
188
|
+
* console.log('Success:', result.value);
|
|
189
|
+
* } else {
|
|
190
|
+
* console.log('Error:', result.error);
|
|
191
|
+
* }
|
|
192
|
+
* ```;
|
|
175
193
|
*
|
|
176
194
|
* @param fn - Task function that returns a Result
|
|
177
195
|
* @param options - Optional configuration for this task
|
|
178
196
|
* @returns Promise that resolves with the task result
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const queue = createAsyncQueue<string>();
|
|
183
|
-
*
|
|
184
|
-
* const result = await queue.add(async () => {
|
|
185
|
-
* try {
|
|
186
|
-
* const data = await fetchData();
|
|
187
|
-
* return Result.ok(data);
|
|
188
|
-
* } catch (error) {
|
|
189
|
-
* return Result.err(error);
|
|
190
|
-
* }
|
|
191
|
-
* });
|
|
192
|
-
*
|
|
193
|
-
* if (result.ok) {
|
|
194
|
-
* console.log('Success:', result.value);
|
|
195
|
-
* } else {
|
|
196
|
-
* console.log('Error:', result.error);
|
|
197
|
-
* }
|
|
198
|
-
* ```
|
|
199
197
|
*/
|
|
200
198
|
add(fn: (ctx: RunCtx<I>) => Promise<Result<T, E>> | Result<T, E>, options?: AddOptions<I, T, E>): Promise<Result<T, E | Error>>;
|
|
201
199
|
/**
|
|
@@ -204,63 +202,68 @@ declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
|
204
202
|
* This is the most commonly used method. It automatically wraps your function
|
|
205
203
|
* to handle errors and convert them to Result types.
|
|
206
204
|
*
|
|
207
|
-
* @
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @example Basic Usage
|
|
212
|
-
* ```typescript
|
|
213
|
-
* const queue = createAsyncQueue<string>();
|
|
205
|
+
* @example
|
|
206
|
+
* Basic Usage
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const queue = createAsyncQueue<string>();
|
|
214
209
|
*
|
|
215
|
-
*
|
|
210
|
+
* queue.resultifyAdd(async () => {
|
|
216
211
|
* const response = await fetch('/api/data');
|
|
217
212
|
* return response.json();
|
|
218
|
-
*
|
|
213
|
+
* }).then(result => {
|
|
219
214
|
* if (result.ok) {
|
|
220
|
-
*
|
|
215
|
+
* console.log('Data:', result.value);
|
|
221
216
|
* } else {
|
|
222
|
-
*
|
|
217
|
+
* console.error('Failed:', result.error);
|
|
223
218
|
* }
|
|
224
|
-
*
|
|
225
|
-
*
|
|
219
|
+
* });
|
|
220
|
+
* ```
|
|
226
221
|
*
|
|
227
|
-
* @example
|
|
228
|
-
*
|
|
229
|
-
*
|
|
222
|
+
* @example
|
|
223
|
+
* With Callbacks
|
|
224
|
+
* ```typescript
|
|
225
|
+
* queue.resultifyAdd(
|
|
230
226
|
* async () => processData(),
|
|
231
227
|
* {
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
228
|
+
* onComplete: (data) => console.log('Processed:', data),
|
|
229
|
+
* onError: (error) => console.error('Failed:', error),
|
|
230
|
+
* timeout: 5000
|
|
235
231
|
* }
|
|
236
|
-
*
|
|
237
|
-
*
|
|
232
|
+
* );
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @param fn - Task function that returns a value or throws
|
|
236
|
+
* @param options - Optional configuration for this task
|
|
237
|
+
* @returns Promise that resolves with the task result wrapped in Result
|
|
238
238
|
*/
|
|
239
239
|
resultifyAdd(fn: (ctx: RunCtx<I>) => Promise<T> | T, options?: AddOptions<I, T, E>): Promise<Result<T, E | Error>>;
|
|
240
240
|
/**
|
|
241
|
-
* Wait for the queue to become idle (no pending tasks, no queued tasks, and
|
|
241
|
+
* Wait for the queue to become idle (no pending tasks, no queued tasks, and
|
|
242
|
+
* no rate-limit timers)
|
|
242
243
|
*
|
|
243
244
|
* This method resolves when:
|
|
245
|
+
*
|
|
244
246
|
* - All tasks have completed (success or failure)
|
|
245
247
|
* - The queue is stopped due to error (stopOnError), even with remaining tasks
|
|
246
|
-
* - There are no queued tasks, no running tasks, and no pending rate-limit
|
|
247
|
-
*
|
|
248
|
-
* @returns Promise that resolves when the queue is idle
|
|
248
|
+
* - There are no queued tasks, no running tasks, and no pending rate-limit
|
|
249
|
+
* timers
|
|
249
250
|
*
|
|
250
251
|
* @example
|
|
251
|
-
*
|
|
252
|
-
*
|
|
252
|
+
* ```typescript
|
|
253
|
+
* const queue = createAsyncQueue<string>();
|
|
253
254
|
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
255
|
+
* // Add multiple tasks
|
|
256
|
+
* for (let i = 0; i < 10; i++) {
|
|
256
257
|
* queue.resultifyAdd(async () => `task ${i}`);
|
|
257
|
-
*
|
|
258
|
+
* }
|
|
258
259
|
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
260
|
+
* // Wait for all tasks to complete
|
|
261
|
+
* await queue.onIdle();
|
|
262
|
+
*
|
|
263
|
+
* console.log(`Completed: ${queue.completed}, Failed: ${queue.failed}`);
|
|
264
|
+
* ```
|
|
261
265
|
*
|
|
262
|
-
*
|
|
263
|
-
* ```
|
|
266
|
+
* @returns Promise that resolves when the queue is idle
|
|
264
267
|
*/
|
|
265
268
|
onIdle(): Promise<void>;
|
|
266
269
|
/**
|
|
@@ -276,24 +279,24 @@ declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
|
276
279
|
/**
|
|
277
280
|
* Clear all queued tasks (does not affect currently running tasks)
|
|
278
281
|
*
|
|
279
|
-
* This removes all tasks waiting in the queue but allows currently
|
|
280
|
-
*
|
|
282
|
+
* This removes all tasks waiting in the queue but allows currently executing
|
|
283
|
+
* tasks to complete normally.
|
|
281
284
|
*
|
|
282
285
|
* @example
|
|
283
|
-
*
|
|
284
|
-
*
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const queue = createAsyncQueue({ concurrency: 1 });
|
|
285
288
|
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
289
|
+
* // Add multiple tasks
|
|
290
|
+
* queue.resultifyAdd(async () => longRunningTask()); // Will start immediately
|
|
291
|
+
* queue.resultifyAdd(async () => task2()); // Queued
|
|
292
|
+
* queue.resultifyAdd(async () => task3()); // Queued
|
|
290
293
|
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
294
|
+
* // Clear remaining queued tasks
|
|
295
|
+
* queue.clear();
|
|
293
296
|
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
+
* // Only the first task will complete
|
|
298
|
+
* await queue.onIdle();
|
|
299
|
+
* ```;
|
|
297
300
|
*/
|
|
298
301
|
clear(): void;
|
|
299
302
|
/** Number of tasks that have completed successfully */
|
|
@@ -308,66 +311,64 @@ declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
|
308
311
|
* Manually start processing tasks (only needed if autoStart: false)
|
|
309
312
|
*
|
|
310
313
|
* @example
|
|
311
|
-
*
|
|
312
|
-
*
|
|
314
|
+
* ```typescript
|
|
315
|
+
* const queue = createAsyncQueue({ autoStart: false });
|
|
313
316
|
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
+
* // Add tasks without starting processing
|
|
318
|
+
* queue.resultifyAdd(async () => 'task1');
|
|
319
|
+
* queue.resultifyAdd(async () => 'task2');
|
|
317
320
|
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
321
|
+
* // Start processing when ready
|
|
322
|
+
* queue.start();
|
|
323
|
+
* await queue.onIdle();
|
|
324
|
+
* ```;
|
|
322
325
|
*/
|
|
323
326
|
start(): void;
|
|
324
327
|
/**
|
|
325
328
|
* Pause processing new tasks (currently running tasks continue)
|
|
326
329
|
*
|
|
327
330
|
* @example
|
|
328
|
-
*
|
|
329
|
-
*
|
|
331
|
+
* ```typescript
|
|
332
|
+
* const queue = createAsyncQueue();
|
|
330
333
|
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
+
* // Start some tasks
|
|
335
|
+
* queue.resultifyAdd(async () => longRunningTask1());
|
|
336
|
+
* queue.resultifyAdd(async () => longRunningTask2());
|
|
334
337
|
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
338
|
+
* // Pause before more tasks are picked up
|
|
339
|
+
* queue.pause();
|
|
337
340
|
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
+
* // Later, resume processing
|
|
342
|
+
* queue.resume();
|
|
343
|
+
* ```;
|
|
341
344
|
*/
|
|
342
345
|
pause(): void;
|
|
343
|
-
/**
|
|
344
|
-
* Resume processing tasks after pause
|
|
345
|
-
*/
|
|
346
|
+
/** Resume processing tasks after pause */
|
|
346
347
|
resume(): void;
|
|
347
348
|
/**
|
|
348
349
|
* Reset the queue after being stopped, allowing new tasks to be processed
|
|
349
350
|
*
|
|
350
|
-
* This clears the stopped state and error reason, and resumes processing
|
|
351
|
-
*
|
|
351
|
+
* This clears the stopped state and error reason, and resumes processing any
|
|
352
|
+
* remaining queued tasks if autoStart was enabled.
|
|
352
353
|
*
|
|
353
354
|
* @example
|
|
354
|
-
*
|
|
355
|
-
*
|
|
355
|
+
* ```typescript
|
|
356
|
+
* const queue = createAsyncQueue({ stopOnError: true });
|
|
356
357
|
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
358
|
+
* // Add tasks that will cause the queue to stop
|
|
359
|
+
* queue.resultifyAdd(async () => { throw new Error('fail'); });
|
|
360
|
+
* queue.resultifyAdd(async () => 'remaining task');
|
|
360
361
|
*
|
|
361
|
-
*
|
|
362
|
+
* await queue.onIdle();
|
|
362
363
|
*
|
|
363
|
-
*
|
|
364
|
+
* if (queue.isStopped) {
|
|
364
365
|
* console.log(`Queue stopped, ${queue.size} tasks remaining`);
|
|
365
366
|
*
|
|
366
367
|
* // Reset and process remaining tasks
|
|
367
368
|
* queue.reset();
|
|
368
369
|
* await queue.onIdle();
|
|
369
|
-
*
|
|
370
|
-
*
|
|
370
|
+
* }
|
|
371
|
+
* ```
|
|
371
372
|
*/
|
|
372
373
|
reset(): void;
|
|
373
374
|
/** Whether the queue is stopped due to an error */
|
|
@@ -379,9 +380,7 @@ declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = unknown> {
|
|
|
379
380
|
/** The error that caused the queue to stop (if any) */
|
|
380
381
|
get stoppedReason(): Error | undefined;
|
|
381
382
|
}
|
|
382
|
-
/**
|
|
383
|
-
* AddOptions variant that requires metadata to be provided
|
|
384
|
-
*/
|
|
383
|
+
/** AddOptions variant that requires metadata to be provided */
|
|
385
384
|
type AddOptionsWithId<I, T, E extends ResultValidErrors> = Omit<AddOptions<I, T, E>, 'meta'> & {
|
|
386
385
|
meta: I;
|
|
387
386
|
};
|
|
@@ -391,29 +390,29 @@ type AddOptionsWithId<I, T, E extends ResultValidErrors> = Omit<AddOptions<I, T,
|
|
|
391
390
|
* This class enforces that every task must include metadata, which is useful
|
|
392
391
|
* when you need to track or identify tasks consistently.
|
|
393
392
|
*
|
|
394
|
-
* @template T - The type of value returned by successful tasks
|
|
395
|
-
* @template I - The type of metadata (required for all tasks)
|
|
396
|
-
* @template E - The type of errors that tasks can produce
|
|
397
|
-
*
|
|
398
393
|
* @example
|
|
399
|
-
*
|
|
400
|
-
*
|
|
394
|
+
* ```typescript
|
|
395
|
+
* interface TaskMeta {
|
|
401
396
|
* id: string;
|
|
402
397
|
* priority: number;
|
|
403
|
-
*
|
|
398
|
+
* }
|
|
404
399
|
*
|
|
405
|
-
*
|
|
400
|
+
* const queue = createAsyncQueueWithMeta<string, TaskMeta>({ concurrency: 2 });
|
|
406
401
|
*
|
|
407
|
-
*
|
|
402
|
+
* queue.resultifyAdd(
|
|
408
403
|
* async () => processImportantTask(),
|
|
409
404
|
* { meta: { id: 'task-1', priority: 1 } }
|
|
410
|
-
*
|
|
405
|
+
* );
|
|
411
406
|
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
407
|
+
* // Listen to events with metadata
|
|
408
|
+
* queue.events.on('complete', (event) => {
|
|
414
409
|
* console.log(`Task ${event.payload.meta.id} completed`);
|
|
415
|
-
*
|
|
416
|
-
*
|
|
410
|
+
* });
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @template T - The type of value returned by successful tasks
|
|
414
|
+
* @template I - The type of metadata (required for all tasks)
|
|
415
|
+
* @template E - The type of errors that tasks can produce
|
|
417
416
|
*/
|
|
418
417
|
declare class AsyncQueueWithMeta<T, I, E extends ResultValidErrors = Error> extends AsyncQueue<T, E, I> {
|
|
419
418
|
constructor(options?: AsyncQueueOptions);
|
|
@@ -423,63 +422,66 @@ declare class AsyncQueueWithMeta<T, I, E extends ResultValidErrors = Error> exte
|
|
|
423
422
|
/**
|
|
424
423
|
* Create a new AsyncQueue instance
|
|
425
424
|
*
|
|
426
|
-
* @
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
* @example Basic Queue
|
|
432
|
-
* ```typescript
|
|
433
|
-
* const queue = createAsyncQueue<string>({ concurrency: 3 });
|
|
434
|
-
* ```
|
|
425
|
+
* @example
|
|
426
|
+
* Basic Queue
|
|
427
|
+
* ```typescript
|
|
428
|
+
* const queue = createAsyncQueue<string>({ concurrency: 3 });
|
|
429
|
+
* ```
|
|
435
430
|
*
|
|
436
|
-
* @example
|
|
437
|
-
*
|
|
438
|
-
*
|
|
431
|
+
* @example
|
|
432
|
+
* Error Handling Queue
|
|
433
|
+
* ```typescript
|
|
434
|
+
* const queue = createAsyncQueue<string>({
|
|
439
435
|
* concurrency: 2,
|
|
440
436
|
* stopOnError: true,
|
|
441
437
|
* rejectPendingOnError: true
|
|
442
|
-
*
|
|
443
|
-
*
|
|
438
|
+
* });
|
|
439
|
+
* ```
|
|
444
440
|
*
|
|
445
|
-
* @example
|
|
446
|
-
*
|
|
447
|
-
*
|
|
441
|
+
* @example
|
|
442
|
+
* Lazy Start Queue
|
|
443
|
+
* ```typescript
|
|
444
|
+
* const queue = createAsyncQueue<string>({
|
|
448
445
|
* autoStart: false,
|
|
449
446
|
* concurrency: 1
|
|
450
|
-
*
|
|
451
|
-
*
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*
|
|
450
|
+
* @template T - The type of value returned by successful tasks
|
|
451
|
+
* @template E - The type of errors that tasks can produce (defaults to Error)
|
|
452
|
+
* @param options - Configuration options for the queue
|
|
453
|
+
* @returns A new AsyncQueue instance
|
|
452
454
|
*/
|
|
453
455
|
declare function createAsyncQueue<T, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueue<T, E>;
|
|
454
456
|
|
|
455
457
|
/**
|
|
456
458
|
* Create a new AsyncQueueWithMeta instance that requires metadata for all tasks
|
|
457
459
|
*
|
|
458
|
-
* @template T - The type of value returned by successful tasks
|
|
459
|
-
* @template I - The type of metadata (required for all tasks)
|
|
460
|
-
* @template E - The type of errors that tasks can produce (defaults to Error)
|
|
461
|
-
* @param options - Configuration options for the queue
|
|
462
|
-
* @returns A new AsyncQueueWithMeta instance
|
|
463
|
-
*
|
|
464
460
|
* @example
|
|
465
|
-
*
|
|
466
|
-
*
|
|
461
|
+
* ```typescript
|
|
462
|
+
* interface TaskInfo {
|
|
467
463
|
* taskId: string;
|
|
468
464
|
* userId: string;
|
|
469
|
-
*
|
|
465
|
+
* }
|
|
470
466
|
*
|
|
471
|
-
*
|
|
467
|
+
* const queue = createAsyncQueueWithMeta<ProcessResult, TaskInfo>({
|
|
472
468
|
* concurrency: 5
|
|
473
|
-
*
|
|
469
|
+
* });
|
|
474
470
|
*
|
|
475
|
-
*
|
|
471
|
+
* queue.resultifyAdd(
|
|
476
472
|
* async (ctx) => {
|
|
477
|
-
*
|
|
478
|
-
*
|
|
473
|
+
* console.log(`Processing task ${ctx.meta.taskId} for user ${ctx.meta.userId}`);
|
|
474
|
+
* return await processUserTask(ctx.meta.userId);
|
|
479
475
|
* },
|
|
480
476
|
* { meta: { taskId: '123', userId: 'user456' } }
|
|
481
|
-
*
|
|
482
|
-
*
|
|
477
|
+
* );
|
|
478
|
+
* ```
|
|
479
|
+
*
|
|
480
|
+
* @template T - The type of value returned by successful tasks
|
|
481
|
+
* @template I - The type of metadata (required for all tasks)
|
|
482
|
+
* @template E - The type of errors that tasks can produce (defaults to Error)
|
|
483
|
+
* @param options - Configuration options for the queue
|
|
484
|
+
* @returns A new AsyncQueueWithMeta instance
|
|
483
485
|
*/
|
|
484
486
|
declare function createAsyncQueueWithMeta<T, I, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueueWithMeta<T, I, E>;
|
|
485
487
|
|