@tanstack/pacer 0.16.3 → 0.16.4

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.
Files changed (169) hide show
  1. package/README.md +165 -0
  2. package/dist/async-batcher.cjs +355 -0
  3. package/dist/async-batcher.cjs.map +1 -0
  4. package/dist/async-batcher.d.cts +344 -0
  5. package/dist/async-batcher.d.ts +344 -0
  6. package/dist/async-batcher.js +353 -0
  7. package/dist/async-batcher.js.map +1 -0
  8. package/dist/async-debouncer.cjs +340 -0
  9. package/dist/async-debouncer.cjs.map +1 -0
  10. package/dist/async-debouncer.d.cts +300 -0
  11. package/dist/async-debouncer.d.ts +300 -0
  12. package/dist/async-debouncer.js +338 -0
  13. package/dist/async-debouncer.js.map +1 -0
  14. package/dist/async-queuer.cjs +496 -0
  15. package/dist/async-queuer.cjs.map +1 -0
  16. package/dist/async-queuer.d.cts +440 -0
  17. package/dist/async-queuer.d.ts +440 -0
  18. package/dist/async-queuer.js +494 -0
  19. package/dist/async-queuer.js.map +1 -0
  20. package/dist/async-rate-limiter.cjs +381 -0
  21. package/dist/async-rate-limiter.cjs.map +1 -0
  22. package/dist/{cjs/async-rate-limiter.d.cts → async-rate-limiter.d.cts} +190 -185
  23. package/dist/{esm/async-rate-limiter.d.ts → async-rate-limiter.d.ts} +190 -185
  24. package/dist/async-rate-limiter.js +379 -0
  25. package/dist/async-rate-limiter.js.map +1 -0
  26. package/dist/async-retryer.cjs +378 -0
  27. package/dist/async-retryer.cjs.map +1 -0
  28. package/dist/async-retryer.d.cts +312 -0
  29. package/dist/async-retryer.d.ts +312 -0
  30. package/dist/async-retryer.js +376 -0
  31. package/dist/async-retryer.js.map +1 -0
  32. package/dist/async-throttler.cjs +362 -0
  33. package/dist/async-throttler.cjs.map +1 -0
  34. package/dist/async-throttler.d.cts +320 -0
  35. package/dist/async-throttler.d.ts +320 -0
  36. package/dist/async-throttler.js +360 -0
  37. package/dist/async-throttler.js.map +1 -0
  38. package/dist/batcher.cjs +194 -0
  39. package/dist/batcher.cjs.map +1 -0
  40. package/dist/batcher.d.cts +180 -0
  41. package/dist/batcher.d.ts +180 -0
  42. package/dist/batcher.js +193 -0
  43. package/dist/batcher.js.map +1 -0
  44. package/dist/debouncer.cjs +197 -0
  45. package/dist/debouncer.cjs.map +1 -0
  46. package/dist/debouncer.d.cts +167 -0
  47. package/dist/debouncer.d.ts +167 -0
  48. package/dist/debouncer.js +195 -0
  49. package/dist/debouncer.js.map +1 -0
  50. package/dist/event-client.cjs +20 -0
  51. package/dist/event-client.cjs.map +1 -0
  52. package/dist/event-client.d.cts +49 -0
  53. package/dist/event-client.d.ts +49 -0
  54. package/dist/event-client.js +19 -0
  55. package/dist/event-client.js.map +1 -0
  56. package/dist/index.cjs +49 -0
  57. package/dist/index.d.cts +15 -0
  58. package/dist/index.d.ts +15 -0
  59. package/dist/{esm/index.js → index.js} +5 -41
  60. package/dist/queuer.cjs +393 -0
  61. package/dist/queuer.cjs.map +1 -0
  62. package/dist/queuer.d.cts +345 -0
  63. package/dist/queuer.d.ts +345 -0
  64. package/dist/queuer.js +391 -0
  65. package/dist/queuer.js.map +1 -0
  66. package/dist/rate-limiter.cjs +251 -0
  67. package/dist/rate-limiter.cjs.map +1 -0
  68. package/dist/{cjs/rate-limiter.d.cts → rate-limiter.d.cts} +113 -108
  69. package/dist/{esm/rate-limiter.d.ts → rate-limiter.d.ts} +113 -108
  70. package/dist/rate-limiter.js +249 -0
  71. package/dist/rate-limiter.js.map +1 -0
  72. package/dist/throttler.cjs +209 -0
  73. package/dist/throttler.cjs.map +1 -0
  74. package/dist/throttler.d.cts +207 -0
  75. package/dist/throttler.d.ts +207 -0
  76. package/dist/throttler.js +207 -0
  77. package/dist/throttler.js.map +1 -0
  78. package/dist/types.cjs +0 -0
  79. package/dist/types.d.cts +13 -0
  80. package/dist/types.d.ts +13 -0
  81. package/dist/types.js +1 -0
  82. package/dist/utils.cjs +13 -0
  83. package/dist/utils.cjs.map +1 -0
  84. package/dist/utils.d.cts +8 -0
  85. package/dist/utils.d.ts +8 -0
  86. package/dist/utils.js +11 -0
  87. package/dist/utils.js.map +1 -0
  88. package/package.json +38 -122
  89. package/dist/cjs/async-batcher.cjs +0 -222
  90. package/dist/cjs/async-batcher.cjs.map +0 -1
  91. package/dist/cjs/async-batcher.d.cts +0 -340
  92. package/dist/cjs/async-debouncer.cjs +0 -233
  93. package/dist/cjs/async-debouncer.cjs.map +0 -1
  94. package/dist/cjs/async-debouncer.d.cts +0 -295
  95. package/dist/cjs/async-queuer.cjs +0 -399
  96. package/dist/cjs/async-queuer.cjs.map +0 -1
  97. package/dist/cjs/async-queuer.d.cts +0 -435
  98. package/dist/cjs/async-rate-limiter.cjs +0 -254
  99. package/dist/cjs/async-rate-limiter.cjs.map +0 -1
  100. package/dist/cjs/async-retryer.cjs +0 -287
  101. package/dist/cjs/async-retryer.cjs.map +0 -1
  102. package/dist/cjs/async-retryer.d.cts +0 -308
  103. package/dist/cjs/async-throttler.cjs +0 -263
  104. package/dist/cjs/async-throttler.cjs.map +0 -1
  105. package/dist/cjs/async-throttler.d.cts +0 -315
  106. package/dist/cjs/batcher.cjs +0 -132
  107. package/dist/cjs/batcher.cjs.map +0 -1
  108. package/dist/cjs/batcher.d.cts +0 -176
  109. package/dist/cjs/debouncer.cjs +0 -139
  110. package/dist/cjs/debouncer.cjs.map +0 -1
  111. package/dist/cjs/debouncer.d.cts +0 -162
  112. package/dist/cjs/event-client.cjs +0 -20
  113. package/dist/cjs/event-client.cjs.map +0 -1
  114. package/dist/cjs/event-client.d.cts +0 -45
  115. package/dist/cjs/index.cjs +0 -51
  116. package/dist/cjs/index.cjs.map +0 -1
  117. package/dist/cjs/index.d.cts +0 -15
  118. package/dist/cjs/queuer.cjs +0 -308
  119. package/dist/cjs/queuer.cjs.map +0 -1
  120. package/dist/cjs/queuer.d.cts +0 -340
  121. package/dist/cjs/rate-limiter.cjs +0 -181
  122. package/dist/cjs/rate-limiter.cjs.map +0 -1
  123. package/dist/cjs/throttler.cjs +0 -153
  124. package/dist/cjs/throttler.cjs.map +0 -1
  125. package/dist/cjs/throttler.d.cts +0 -202
  126. package/dist/cjs/types.d.cts +0 -9
  127. package/dist/cjs/utils.cjs +0 -11
  128. package/dist/cjs/utils.cjs.map +0 -1
  129. package/dist/cjs/utils.d.cts +0 -3
  130. package/dist/esm/async-batcher.d.ts +0 -340
  131. package/dist/esm/async-batcher.js +0 -222
  132. package/dist/esm/async-batcher.js.map +0 -1
  133. package/dist/esm/async-debouncer.d.ts +0 -295
  134. package/dist/esm/async-debouncer.js +0 -233
  135. package/dist/esm/async-debouncer.js.map +0 -1
  136. package/dist/esm/async-queuer.d.ts +0 -435
  137. package/dist/esm/async-queuer.js +0 -399
  138. package/dist/esm/async-queuer.js.map +0 -1
  139. package/dist/esm/async-rate-limiter.js +0 -254
  140. package/dist/esm/async-rate-limiter.js.map +0 -1
  141. package/dist/esm/async-retryer.d.ts +0 -308
  142. package/dist/esm/async-retryer.js +0 -287
  143. package/dist/esm/async-retryer.js.map +0 -1
  144. package/dist/esm/async-throttler.d.ts +0 -315
  145. package/dist/esm/async-throttler.js +0 -263
  146. package/dist/esm/async-throttler.js.map +0 -1
  147. package/dist/esm/batcher.d.ts +0 -176
  148. package/dist/esm/batcher.js +0 -132
  149. package/dist/esm/batcher.js.map +0 -1
  150. package/dist/esm/debouncer.d.ts +0 -162
  151. package/dist/esm/debouncer.js +0 -139
  152. package/dist/esm/debouncer.js.map +0 -1
  153. package/dist/esm/event-client.d.ts +0 -45
  154. package/dist/esm/event-client.js +0 -20
  155. package/dist/esm/event-client.js.map +0 -1
  156. package/dist/esm/index.d.ts +0 -15
  157. package/dist/esm/index.js.map +0 -1
  158. package/dist/esm/queuer.d.ts +0 -340
  159. package/dist/esm/queuer.js +0 -308
  160. package/dist/esm/queuer.js.map +0 -1
  161. package/dist/esm/rate-limiter.js +0 -181
  162. package/dist/esm/rate-limiter.js.map +0 -1
  163. package/dist/esm/throttler.d.ts +0 -202
  164. package/dist/esm/throttler.js +0 -153
  165. package/dist/esm/throttler.js.map +0 -1
  166. package/dist/esm/types.d.ts +0 -9
  167. package/dist/esm/utils.d.ts +0 -3
  168. package/dist/esm/utils.js +0 -11
  169. package/dist/esm/utils.js.map +0 -1
@@ -1,435 +0,0 @@
1
- import { Store } from '@tanstack/store';
2
- import { AsyncRetryer, AsyncRetryerOptions } from './async-retryer.js';
3
- import { QueuePosition } from './queuer.js';
4
- export interface AsyncQueuerState<TValue> {
5
- /**
6
- * Items currently being processed by the queuer
7
- */
8
- activeItems: Array<TValue>;
9
- /**
10
- * Number of times addItem has been called (for reduction calculations)
11
- */
12
- addItemCount: number;
13
- /**
14
- * Number of task executions that have resulted in errors
15
- */
16
- errorCount: number;
17
- /**
18
- * Number of times execute has been called
19
- */
20
- executeCount: number;
21
- /**
22
- * Number of items that have been removed from the queue due to expiration
23
- */
24
- expirationCount: number;
25
- /**
26
- * Whether the queuer has no items to process (items array is empty)
27
- */
28
- isEmpty: boolean;
29
- /**
30
- * Whether the queuer is currently executing
31
- */
32
- isExecuting: boolean;
33
- /**
34
- * Whether the queuer has reached its maximum capacity
35
- */
36
- isFull: boolean;
37
- /**
38
- * Whether the queuer is not currently processing any items
39
- */
40
- isIdle: boolean;
41
- /**
42
- * Whether the queuer is active and will process items automatically
43
- */
44
- isRunning: boolean;
45
- /**
46
- * Array of items currently waiting to be processed
47
- */
48
- items: Array<TValue>;
49
- /**
50
- * Timestamps when items were added to the queue for expiration tracking
51
- */
52
- itemTimestamps: Array<number>;
53
- /**
54
- * The result from the most recent task execution
55
- */
56
- lastResult: any;
57
- /**
58
- * Whether the queuer has a pending timeout for processing the next item
59
- */
60
- pendingTick: boolean;
61
- /**
62
- * Number of items that have been rejected from being added to the queue
63
- */
64
- rejectionCount: number;
65
- /**
66
- * Number of task executions that have completed (either successfully or with errors)
67
- */
68
- settledCount: number;
69
- /**
70
- * Number of items currently in the queue
71
- */
72
- size: number;
73
- /**
74
- * Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
75
- */
76
- status: 'idle' | 'running' | 'stopped';
77
- /**
78
- * Number of task executions that have completed successfully
79
- */
80
- successCount: number;
81
- }
82
- export interface AsyncQueuerOptions<TValue> {
83
- /**
84
- * Options for configuring the underlying async retryer
85
- */
86
- asyncRetryerOptions?: AsyncRetryerOptions<(item: TValue) => Promise<any>>;
87
- /**
88
- * Default position to add items to the queuer
89
- * @default 'back'
90
- */
91
- addItemsTo?: QueuePosition;
92
- /**
93
- * Maximum number of concurrent tasks to process.
94
- * Can be a number or a function that returns a number.
95
- * @default 1
96
- */
97
- concurrency?: number | ((queuer: AsyncQueuer<TValue>) => number);
98
- /**
99
- * Maximum time in milliseconds that an item can stay in the queue
100
- * If not provided, items will never expire
101
- */
102
- expirationDuration?: number;
103
- /**
104
- * Function to determine if an item has expired
105
- * If provided, this overrides the expirationDuration behavior
106
- */
107
- getIsExpired?: (item: TValue, addedAt: number) => boolean;
108
- /**
109
- * Default position to get items from during processing
110
- * @default 'front'
111
- */
112
- getItemsFrom?: QueuePosition;
113
- /**
114
- * Function to determine priority of items in the queuer
115
- * Higher priority items will be processed first
116
- * If not provided, will use static priority values attached to tasks
117
- */
118
- getPriority?: (item: TValue) => number;
119
- /**
120
- * Initial items to populate the queuer with
121
- */
122
- initialItems?: Array<TValue>;
123
- /**
124
- * Initial state for the async queuer
125
- */
126
- initialState?: Partial<AsyncQueuerState<TValue>>;
127
- /**
128
- * Optional key to identify this async queuer instance.
129
- * If provided, the async queuer will be identified by this key in the devtools and PacerProvider if applicable.
130
- */
131
- key?: string;
132
- /**
133
- * Maximum number of items allowed in the queuer
134
- */
135
- maxSize?: number;
136
- /**
137
- * Optional error handler for when a task throws.
138
- * If provided, the handler will be called with the error and queuer instance.
139
- * This can be used alongside throwOnError - the handler will be called before any error is thrown.
140
- */
141
- onError?: (error: Error, item: TValue, queuer: AsyncQueuer<TValue>) => void;
142
- /**
143
- * Callback fired whenever an item expires in the queuer
144
- */
145
- onExpire?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
146
- /**
147
- * Callback fired whenever an item is added or removed from the queuer
148
- */
149
- onItemsChange?: (queuer: AsyncQueuer<TValue>) => void;
150
- /**
151
- * Callback fired whenever an item is rejected from being added to the queuer
152
- */
153
- onReject?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
154
- /**
155
- * Optional callback to call when a task is settled
156
- */
157
- onSettled?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
158
- /**
159
- * Optional callback to call when a task succeeds
160
- */
161
- onSuccess?: (result: any, item: TValue, queuer: AsyncQueuer<TValue>) => void;
162
- /**
163
- * Whether the queuer should start processing tasks immediately or not.
164
- */
165
- started?: boolean;
166
- /**
167
- * Whether to throw errors when they occur.
168
- * Defaults to true if no onError handler is provided, false if an onError handler is provided.
169
- * Can be explicitly set to override these defaults.
170
- */
171
- throwOnError?: boolean;
172
- /**
173
- * Time in milliseconds to wait between processing items.
174
- * Can be a number or a function that returns a number.
175
- * @default 0
176
- */
177
- wait?: number | ((queuer: AsyncQueuer<TValue>) => number);
178
- }
179
- /**
180
- * Utility function for sharing common `AsyncQueuerOptions` options between different `AsyncQueuer` instances.
181
- */
182
- export declare function asyncQueuerOptions<TValue = any, TOptions extends Partial<AsyncQueuerOptions<TValue>> = Partial<AsyncQueuerOptions<TValue>>>(options: TOptions): TOptions;
183
- /**
184
- * A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
185
- *
186
- * Async vs Sync Versions:
187
- * The async version provides advanced features over the sync Queuer:
188
- * - Returns promises that can be awaited for task results
189
- * - Built-in retry support via AsyncRetryer integration for each queued task
190
- * - Abort support to cancel in-flight task executions
191
- * - Comprehensive error handling with onError callbacks and throwOnError control
192
- * - Detailed execution tracking (success/error/settle counts)
193
- * - Concurrent execution support (process multiple items simultaneously)
194
- *
195
- * The sync Queuer is lighter weight and simpler when you don't need async features,
196
- * return values, or execution control.
197
- *
198
- * What is Queuing?
199
- * Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
200
- * Tasks are processed up to the configured concurrency limit. When a task completes,
201
- * the next pending task is processed if the concurrency limit allows.
202
- *
203
- * Key Features:
204
- * - Priority queue support via the getPriority option
205
- * - Configurable concurrency limit
206
- * - Callbacks for task success, error, completion, and queue state changes
207
- * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
208
- * - Pause and resume processing
209
- * - Item expiration to remove stale items from the queue
210
- *
211
- * Error Handling:
212
- * - If an `onError` handler is provided, it will be called with the error and queuer instance
213
- * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
214
- * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
215
- * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
216
- * - The error state can be checked using the AsyncQueuer instance
217
- *
218
- * State Management:
219
- * - Uses TanStack Store for reactive state management
220
- * - Use `initialState` to provide initial state values when creating the async queuer
221
- * - Use `onSuccess` callback to react to successful task execution and implement custom logic
222
- * - Use `onError` callback to react to task execution errors and implement custom error handling
223
- * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
224
- * - Use `onItemsChange` callback to react to items being added or removed from the queue
225
- * - Use `onExpire` callback to react to items expiring and implement custom logic
226
- * - Use `onReject` callback to react to items being rejected when the queue is full
227
- * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
228
- * - State can be accessed via `asyncQueuer.store.state` when using the class directly
229
- * - When using framework adapters (React/Solid), state is accessed from `asyncQueuer.state`
230
- *
231
- * Example usage:
232
- * ```ts
233
- * const asyncQueuer = new AsyncQueuer<string>(async (item) => {
234
- * // process item
235
- * return item.toUpperCase();
236
- * }, {
237
- * concurrency: 2,
238
- * onSuccess: (result) => {
239
- * console.log(result);
240
- * }
241
- * });
242
- *
243
- * asyncQueuer.addItem('hello');
244
- * asyncQueuer.start();
245
- * ```
246
- */
247
- export declare class AsyncQueuer<TValue> {
248
- #private;
249
- fn: (item: TValue) => Promise<any>;
250
- readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
251
- key: string | undefined;
252
- options: AsyncQueuerOptions<TValue>;
253
- asyncRetryers: Map<number, AsyncRetryer<(item: TValue) => Promise<any>>>;
254
- constructor(fn: (item: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>);
255
- /**
256
- * Updates the queuer options. New options are merged with existing options.
257
- */
258
- setOptions: (newOptions: Partial<AsyncQueuerOptions<TValue>>) => void;
259
- /**
260
- * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
261
- * Items can be inserted based on priority or at the front/back depending on configuration.
262
- *
263
- * @example
264
- * ```ts
265
- * queuer.addItem({ value: 'task', priority: 10 });
266
- * queuer.addItem('task2', 'front');
267
- * ```
268
- */
269
- addItem: (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
270
- /**
271
- * Removes and returns the next item from the queue without executing the task function.
272
- * Use for manual queue management. Normally, use execute() to process items.
273
- *
274
- * @example
275
- * ```ts
276
- * // FIFO
277
- * queuer.getNextItem();
278
- * // LIFO
279
- * queuer.getNextItem('back');
280
- * ```
281
- */
282
- getNextItem: (position?: QueuePosition) => TValue | undefined;
283
- /**
284
- * Removes and returns the next item from the queue and executes the task function with it.
285
- *
286
- * @example
287
- * ```ts
288
- * queuer.execute();
289
- * // LIFO
290
- * queuer.execute('back');
291
- * ```
292
- */
293
- execute: (position?: QueuePosition) => Promise<any>;
294
- /**
295
- * Processes a specified number of items to execute immediately with no wait time
296
- * If no numberOfItems is provided, all items will be processed
297
- */
298
- flush: (numberOfItems?: number, position?: QueuePosition) => Promise<void>;
299
- /**
300
- * Processes all items in the queue as a batch using the provided function as an argument
301
- * The queue is cleared after processing
302
- */
303
- flushAsBatch: (batchFunction: (items: Array<TValue>) => Promise<any>) => Promise<void>;
304
- /**
305
- * Returns the next item in the queue without removing it.
306
- *
307
- * @example
308
- * ```ts
309
- * queuer.peekNextItem(); // front
310
- * queuer.peekNextItem('back'); // back
311
- * ```
312
- */
313
- peekNextItem: (position?: QueuePosition) => TValue | undefined;
314
- /**
315
- * Returns a copy of all items in the queue, including active and pending items.
316
- */
317
- peekAllItems: () => Array<TValue>;
318
- /**
319
- * Returns the items currently being processed (active tasks).
320
- */
321
- peekActiveItems: () => Array<TValue>;
322
- /**
323
- * Returns the items waiting to be processed (pending tasks).
324
- */
325
- peekPendingItems: () => Array<TValue>;
326
- /**
327
- * Starts processing items in the queue. If already running, does nothing.
328
- */
329
- start: () => void;
330
- /**
331
- * Stops processing items in the queue. Does not clear the queue.
332
- */
333
- stop: () => void;
334
- /**
335
- * Removes all pending items from the queue.
336
- * Does NOT affect active tasks.
337
- */
338
- clear: () => void;
339
- /**
340
- * Returns the AbortSignal for a specific execution.
341
- * If no executeCount is provided, returns the signal for the most recent execution.
342
- * Returns null if no execution is found or not currently executing.
343
- *
344
- * @param executeCount - Optional specific execution to get signal for
345
- * @example
346
- * ```typescript
347
- * const queuer = new AsyncQueuer(
348
- * async (item: string) => {
349
- * const signal = queuer.getAbortSignal()
350
- * if (signal) {
351
- * const response = await fetch(`/api/process/${item}`, { signal })
352
- * return response.json()
353
- * }
354
- * },
355
- * { concurrency: 2 }
356
- * )
357
- * ```
358
- */
359
- getAbortSignal(executeCount?: number): AbortSignal | null;
360
- /**
361
- * Aborts all ongoing executions with the internal abort controllers.
362
- * Does NOT clear out the items.
363
- */
364
- abort: () => void;
365
- /**
366
- * Resets the queuer state to its default values
367
- */
368
- reset: () => void;
369
- }
370
- /**
371
- * Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
372
- * The queuer is started automatically and ready to process items.
373
- *
374
- * Async vs Sync Versions:
375
- * The async version provides advanced features over the sync queue function:
376
- * - Returns promises that can be awaited for task results
377
- * - Built-in retry support via AsyncRetryer integration for each queued task
378
- * - Abort support to cancel in-flight task executions
379
- * - Comprehensive error handling with onError callbacks and throwOnError control
380
- * - Detailed execution tracking (success/error/settle counts)
381
- * - Concurrent execution support (process multiple items simultaneously)
382
- *
383
- * The sync queue function is lighter weight and simpler when you don't need async features,
384
- * return values, or execution control.
385
- *
386
- * What is Queuing?
387
- * Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
388
- * Tasks are processed up to the configured concurrency limit. When a task completes,
389
- * the next pending task is processed if the concurrency limit allows.
390
- *
391
- * Configuration Options:
392
- * - `concurrency`: Maximum number of concurrent tasks (default: 1)
393
- * - `wait`: Time to wait between processing items (default: 0)
394
- * - `maxSize`: Maximum number of items allowed in the queue (default: Infinity)
395
- * - `getPriority`: Function to determine item priority
396
- * - `addItemsTo`: Default position to add items ('back' or 'front', default: 'back')
397
- * - `getItemsFrom`: Default position to get items ('front' or 'back', default: 'front')
398
- * - `expirationDuration`: Maximum time items can stay in queue
399
- * - `started`: Whether to start processing immediately (default: true)
400
- * - `asyncRetryerOptions`: Configure retry behavior for task executions
401
- *
402
- * Error Handling:
403
- * - If an `onError` handler is provided, it will be called with the error and queuer instance
404
- * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
405
- * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
406
- * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
407
- * - The error state can be checked using the underlying AsyncQueuer instance
408
- *
409
- * State Management:
410
- * - Uses TanStack Store for reactive state management
411
- * - Use `initialState` to provide initial state values when creating the async queuer
412
- * - Use `onSuccess` callback to react to successful task execution and implement custom logic
413
- * - Use `onError` callback to react to task execution errors and implement custom error handling
414
- * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
415
- * - Use `onItemsChange` callback to react to items being added or removed from the queue
416
- * - Use `onExpire` callback to react to items expiring and implement custom logic
417
- * - Use `onReject` callback to react to items being rejected when the queue is full
418
- * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
419
- * - State can be accessed via the underlying AsyncQueuer instance's `store.state` property
420
- * - When using framework adapters (React/Solid), state is accessed from the hook's state property
421
- *
422
- * @example
423
- * ```ts
424
- * const enqueue = asyncQueue<string>(async (item) => {
425
- * return item.toUpperCase();
426
- * }, {
427
- * concurrency: 2,
428
- * wait: 100,
429
- * onSuccess: (result) => console.log('Processed:', result)
430
- * });
431
- *
432
- * enqueue('hello');
433
- * ```
434
- */
435
- export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;