@tanstack/pacer 0.21.1 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +21 -6
  2. package/dist/async-batcher.d.ts +11 -14
  3. package/dist/async-batcher.js +186 -132
  4. package/dist/async-debouncer.d.ts +10 -13
  5. package/dist/async-debouncer.js +187 -146
  6. package/dist/async-queuer.d.ts +12 -14
  7. package/dist/async-queuer.js +384 -259
  8. package/dist/async-rate-limiter.d.ts +9 -12
  9. package/dist/async-rate-limiter.js +198 -147
  10. package/dist/async-retryer.d.ts +10 -10
  11. package/dist/async-retryer.js +209 -188
  12. package/dist/async-throttler.d.ts +10 -13
  13. package/dist/async-throttler.js +215 -161
  14. package/dist/batcher.d.ts +5 -8
  15. package/dist/batcher.js +107 -87
  16. package/dist/debouncer.d.ts +6 -9
  17. package/dist/debouncer.js +99 -86
  18. package/dist/event-client.d.ts +8 -11
  19. package/dist/event-client.js +1 -2
  20. package/dist/index.js +1 -1
  21. package/dist/queuer.d.ts +8 -10
  22. package/dist/queuer.js +277 -207
  23. package/dist/rate-limiter.d.ts +6 -9
  24. package/dist/rate-limiter.js +127 -109
  25. package/dist/throttler.d.ts +6 -9
  26. package/dist/throttler.js +127 -90
  27. package/dist/types.d.ts +4 -6
  28. package/dist/utils.d.ts +3 -6
  29. package/dist/utils.js +1 -2
  30. package/package.json +23 -70
  31. package/dist/async-batcher.cjs +0 -337
  32. package/dist/async-batcher.cjs.map +0 -1
  33. package/dist/async-batcher.d.cts +0 -344
  34. package/dist/async-batcher.js.map +0 -1
  35. package/dist/async-debouncer.cjs +0 -330
  36. package/dist/async-debouncer.cjs.map +0 -1
  37. package/dist/async-debouncer.d.cts +0 -300
  38. package/dist/async-debouncer.js.map +0 -1
  39. package/dist/async-queuer.cjs +0 -484
  40. package/dist/async-queuer.cjs.map +0 -1
  41. package/dist/async-queuer.d.cts +0 -440
  42. package/dist/async-queuer.js.map +0 -1
  43. package/dist/async-rate-limiter.cjs +0 -373
  44. package/dist/async-rate-limiter.cjs.map +0 -1
  45. package/dist/async-rate-limiter.d.cts +0 -357
  46. package/dist/async-rate-limiter.js.map +0 -1
  47. package/dist/async-retryer.cjs +0 -374
  48. package/dist/async-retryer.cjs.map +0 -1
  49. package/dist/async-retryer.d.cts +0 -319
  50. package/dist/async-retryer.js.map +0 -1
  51. package/dist/async-throttler.cjs +0 -347
  52. package/dist/async-throttler.cjs.map +0 -1
  53. package/dist/async-throttler.d.cts +0 -320
  54. package/dist/async-throttler.js.map +0 -1
  55. package/dist/batcher.cjs +0 -200
  56. package/dist/batcher.cjs.map +0 -1
  57. package/dist/batcher.d.cts +0 -180
  58. package/dist/batcher.js.map +0 -1
  59. package/dist/debouncer.cjs +0 -203
  60. package/dist/debouncer.cjs.map +0 -1
  61. package/dist/debouncer.d.cts +0 -167
  62. package/dist/debouncer.js.map +0 -1
  63. package/dist/event-client.cjs +0 -64
  64. package/dist/event-client.cjs.map +0 -1
  65. package/dist/event-client.d.cts +0 -66
  66. package/dist/event-client.js.map +0 -1
  67. package/dist/index.cjs +0 -52
  68. package/dist/index.d.cts +0 -15
  69. package/dist/queuer.cjs +0 -401
  70. package/dist/queuer.cjs.map +0 -1
  71. package/dist/queuer.d.cts +0 -345
  72. package/dist/queuer.js.map +0 -1
  73. package/dist/rate-limiter.cjs +0 -263
  74. package/dist/rate-limiter.cjs.map +0 -1
  75. package/dist/rate-limiter.d.cts +0 -215
  76. package/dist/rate-limiter.js.map +0 -1
  77. package/dist/throttler.cjs +0 -215
  78. package/dist/throttler.cjs.map +0 -1
  79. package/dist/throttler.d.cts +0 -207
  80. package/dist/throttler.js.map +0 -1
  81. package/dist/types.cjs +0 -0
  82. package/dist/types.d.cts +0 -13
  83. package/dist/utils.cjs +0 -14
  84. package/dist/utils.cjs.map +0 -1
  85. package/dist/utils.d.cts +0 -8
  86. package/dist/utils.js.map +0 -1
  87. package/src/async-batcher.ts +0 -594
  88. package/src/async-debouncer.ts +0 -565
  89. package/src/async-queuer.ts +0 -925
  90. package/src/async-rate-limiter.ts +0 -647
  91. package/src/async-retryer.ts +0 -684
  92. package/src/async-throttler.ts +0 -633
  93. package/src/batcher.ts +0 -329
  94. package/src/debouncer.ts +0 -334
  95. package/src/event-client.ts +0 -129
  96. package/src/index.ts +0 -24
  97. package/src/queuer.ts +0 -740
  98. package/src/rate-limiter.ts +0 -429
  99. package/src/throttler.ts +0 -380
  100. package/src/types.ts +0 -12
  101. package/src/utils.ts +0 -12
@@ -1,925 +0,0 @@
1
- import { Store } from '@tanstack/store'
2
- import { AsyncRetryer } from './async-retryer'
3
- import { parseFunctionOrValue } from './utils'
4
- import { emitChange, pacerEventClient } from './event-client'
5
- import type { AsyncRetryerOptions } from './async-retryer'
6
- import type { OptionalKeys } from './types'
7
- import type { QueuePosition } from './queuer'
8
-
9
- export interface AsyncQueuerState<TValue> {
10
- /**
11
- * Items currently being processed by the queuer
12
- */
13
- activeItems: Array<TValue>
14
- /**
15
- * Number of times addItem has been called (for reduction calculations)
16
- */
17
- addItemCount: number
18
- /**
19
- * Number of task executions that have resulted in errors
20
- */
21
- errorCount: number
22
- /**
23
- * Number of times execute has been called
24
- */
25
- executeCount: number
26
- /**
27
- * Number of items that have been removed from the queue due to expiration
28
- */
29
- expirationCount: number
30
- /**
31
- * Whether the queuer has no items to process (items array is empty)
32
- */
33
- isEmpty: boolean
34
- /**
35
- * Whether the queuer is currently executing
36
- */
37
- isExecuting: boolean
38
- /**
39
- * Whether the queuer has reached its maximum capacity
40
- */
41
- isFull: boolean
42
- /**
43
- * Whether the queuer is not currently processing any items
44
- */
45
- isIdle: boolean
46
- /**
47
- * Whether the queuer is active and will process items automatically
48
- */
49
- isRunning: boolean
50
- /**
51
- * Array of items currently waiting to be processed
52
- */
53
- items: Array<TValue>
54
- /**
55
- * Timestamps when items were added to the queue for expiration tracking
56
- */
57
- itemTimestamps: Array<number>
58
- /**
59
- * The result from the most recent task execution
60
- */
61
- lastResult: any
62
- /**
63
- * Whether the queuer has a pending timeout for processing the next item
64
- */
65
- pendingTick: boolean
66
- /**
67
- * Number of items that have been rejected from being added to the queue
68
- */
69
- rejectionCount: number
70
- /**
71
- * Number of task executions that have completed (either successfully or with errors)
72
- */
73
- settledCount: number
74
- /**
75
- * Number of items currently in the queue
76
- */
77
- size: number
78
- /**
79
- * Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
80
- */
81
- status: 'idle' | 'running' | 'stopped'
82
- /**
83
- * Number of task executions that have completed successfully
84
- */
85
- successCount: number
86
- }
87
-
88
- function getDefaultAsyncQueuerState<TValue>(): AsyncQueuerState<TValue> {
89
- return {
90
- activeItems: [],
91
- addItemCount: 0,
92
- errorCount: 0,
93
- executeCount: 0,
94
- expirationCount: 0,
95
- isEmpty: true,
96
- isExecuting: false,
97
- isFull: false,
98
- isIdle: true,
99
- isRunning: true,
100
- itemTimestamps: [],
101
- items: [],
102
- lastResult: null,
103
- pendingTick: false,
104
- rejectionCount: 0,
105
- settledCount: 0,
106
- size: 0,
107
- status: 'idle',
108
- successCount: 0,
109
- }
110
- }
111
-
112
- export interface AsyncQueuerOptions<TValue> {
113
- /**
114
- * Options for configuring the underlying async retryer
115
- */
116
- asyncRetryerOptions?: AsyncRetryerOptions<(item: TValue) => Promise<any>>
117
- /**
118
- * Default position to add items to the queuer
119
- * @default 'back'
120
- */
121
- addItemsTo?: QueuePosition
122
- /**
123
- * Maximum number of concurrent tasks to process.
124
- * Can be a number or a function that returns a number.
125
- * @default 1
126
- */
127
- concurrency?: number | ((queuer: AsyncQueuer<TValue>) => number)
128
- /**
129
- * Maximum time in milliseconds that an item can stay in the queue
130
- * If not provided, items will never expire
131
- */
132
- expirationDuration?: number
133
- /**
134
- * Function to determine if an item has expired
135
- * If provided, this overrides the expirationDuration behavior
136
- */
137
- getIsExpired?: (item: TValue, addedAt: number) => boolean
138
- /**
139
- * Default position to get items from during processing
140
- * @default 'front'
141
- */
142
- getItemsFrom?: QueuePosition
143
- /**
144
- * Function to determine priority of items in the queuer
145
- * Higher priority items will be processed first
146
- * If not provided, will use static priority values attached to tasks
147
- */
148
- getPriority?: (item: TValue) => number
149
- /**
150
- * Initial items to populate the queuer with
151
- */
152
- initialItems?: Array<TValue>
153
- /**
154
- * Initial state for the async queuer
155
- */
156
- initialState?: Partial<AsyncQueuerState<TValue>>
157
- /**
158
- * Optional key to identify this async queuer instance.
159
- * If provided, the async queuer will be identified by this key in the devtools and PacerProvider if applicable.
160
- */
161
- key?: string
162
- /**
163
- * Maximum number of items allowed in the queuer
164
- */
165
- maxSize?: number
166
- /**
167
- * Optional error handler for when a task throws.
168
- * If provided, the handler will be called with the error and queuer instance.
169
- * This can be used alongside throwOnError - the handler will be called before any error is thrown.
170
- */
171
- onError?: (error: Error, item: TValue, queuer: AsyncQueuer<TValue>) => void
172
- /**
173
- * Callback fired whenever an item expires in the queuer
174
- */
175
- onExpire?: (item: TValue, queuer: AsyncQueuer<TValue>) => void
176
- /**
177
- * Callback fired whenever an item is added or removed from the queuer
178
- */
179
- onItemsChange?: (queuer: AsyncQueuer<TValue>) => void
180
- /**
181
- * Callback fired whenever an item is rejected from being added to the queuer
182
- */
183
- onReject?: (item: TValue, queuer: AsyncQueuer<TValue>) => void
184
- /**
185
- * Optional callback to call when a task is settled
186
- */
187
- onSettled?: (item: TValue, queuer: AsyncQueuer<TValue>) => void
188
- /**
189
- * Optional callback to call when a task succeeds
190
- */
191
- onSuccess?: (result: any, item: TValue, queuer: AsyncQueuer<TValue>) => void
192
- /**
193
- * Whether the queuer should start processing tasks immediately or not.
194
- */
195
- started?: boolean
196
- /**
197
- * Whether to throw errors when they occur.
198
- * Defaults to true if no onError handler is provided, false if an onError handler is provided.
199
- * Can be explicitly set to override these defaults.
200
- */
201
- throwOnError?: boolean
202
- /**
203
- * Time in milliseconds to wait between processing items.
204
- * Can be a number or a function that returns a number.
205
- * @default 0
206
- */
207
- wait?: number | ((queuer: AsyncQueuer<TValue>) => number)
208
- }
209
-
210
- /**
211
- * Utility function for sharing common `AsyncQueuerOptions` options between different `AsyncQueuer` instances.
212
- */
213
- export function asyncQueuerOptions<
214
- TValue = any,
215
- TOptions extends Partial<AsyncQueuerOptions<TValue>> = Partial<
216
- AsyncQueuerOptions<TValue>
217
- >,
218
- >(options: TOptions): TOptions {
219
- return options
220
- }
221
-
222
- type AsyncQueuerOptionsWithOptionalCallbacks = OptionalKeys<
223
- Required<AsyncQueuerOptions<any>>,
224
- | 'initialState'
225
- | 'throwOnError'
226
- | 'onSuccess'
227
- | 'onSettled'
228
- | 'onReject'
229
- | 'onItemsChange'
230
- | 'onExpire'
231
- | 'onError'
232
- | 'key'
233
- >
234
-
235
- const defaultOptions: AsyncQueuerOptionsWithOptionalCallbacks = {
236
- addItemsTo: 'back',
237
- asyncRetryerOptions: {
238
- maxAttempts: 1,
239
- },
240
- concurrency: 1,
241
- expirationDuration: Infinity,
242
- getIsExpired: () => false,
243
- getItemsFrom: 'front',
244
- getPriority: (item: any) => item?.priority ?? 0,
245
- initialItems: [],
246
- maxSize: Infinity,
247
- started: true,
248
- wait: 0,
249
- }
250
-
251
- /**
252
- * A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
253
- *
254
- * Async vs Sync Versions:
255
- * The async version provides advanced features over the sync Queuer:
256
- * - Returns promises that can be awaited for task results
257
- * - Built-in retry support via AsyncRetryer integration for each queued task
258
- * - Abort support to cancel in-flight task executions
259
- * - Comprehensive error handling with onError callbacks and throwOnError control
260
- * - Detailed execution tracking (success/error/settle counts)
261
- * - Concurrent execution support (process multiple items simultaneously)
262
- *
263
- * The sync Queuer is lighter weight and simpler when you don't need async features,
264
- * return values, or execution control.
265
- *
266
- * What is Queuing?
267
- * Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
268
- * Tasks are processed up to the configured concurrency limit. When a task completes,
269
- * the next pending task is processed if the concurrency limit allows.
270
- *
271
- * Key Features:
272
- * - Priority queue support via the getPriority option
273
- * - Configurable concurrency limit
274
- * - Callbacks for task success, error, completion, and queue state changes
275
- * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
276
- * - Pause and resume processing
277
- * - Item expiration to remove stale items from the queue
278
- *
279
- * Error Handling:
280
- * - If an `onError` handler is provided, it will be called with the error and queuer instance
281
- * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
282
- * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
283
- * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
284
- * - The error state can be checked using the AsyncQueuer instance
285
- *
286
- * State Management:
287
- * - Uses TanStack Store for reactive state management
288
- * - Use `initialState` to provide initial state values when creating the async queuer
289
- * - Use `onSuccess` callback to react to successful task execution and implement custom logic
290
- * - Use `onError` callback to react to task execution errors and implement custom error handling
291
- * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
292
- * - Use `onItemsChange` callback to react to items being added or removed from the queue
293
- * - Use `onExpire` callback to react to items expiring and implement custom logic
294
- * - Use `onReject` callback to react to items being rejected when the queue is full
295
- * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
296
- * - State can be accessed via `asyncQueuer.store.state` when using the class directly
297
- * - When using framework adapters (React/Solid), state is accessed from `asyncQueuer.state`
298
- *
299
- * Example usage:
300
- * ```ts
301
- * const asyncQueuer = new AsyncQueuer<string>(async (item) => {
302
- * // process item
303
- * return item.toUpperCase();
304
- * }, {
305
- * concurrency: 2,
306
- * onSuccess: (result) => {
307
- * console.log(result);
308
- * }
309
- * });
310
- *
311
- * asyncQueuer.addItem('hello');
312
- * asyncQueuer.start();
313
- * ```
314
- */
315
- export class AsyncQueuer<TValue> {
316
- readonly store: Store<Readonly<AsyncQueuerState<TValue>>> = new Store<
317
- AsyncQueuerState<TValue>
318
- >(getDefaultAsyncQueuerState<TValue>())
319
- key: string | undefined
320
- options: AsyncQueuerOptions<TValue>
321
- asyncRetryers = new Map<
322
- number,
323
- AsyncRetryer<(item: TValue) => Promise<any>>
324
- >()
325
- #timeoutIds: Set<ReturnType<typeof setTimeout>> = new Set()
326
-
327
- constructor(
328
- public fn: (item: TValue) => Promise<any>,
329
- initialOptions: AsyncQueuerOptions<TValue> = {},
330
- ) {
331
- this.key = initialOptions.key
332
- this.options = {
333
- ...defaultOptions,
334
- ...initialOptions,
335
- throwOnError: initialOptions.throwOnError ?? !initialOptions.onError,
336
- }
337
- const isInitiallyRunning =
338
- this.options.initialState?.isRunning ?? this.options.started ?? true
339
- this.#setState({
340
- ...this.options.initialState,
341
- isRunning: isInitiallyRunning,
342
- })
343
-
344
- if (this.options.initialState?.items) {
345
- if (this.store.state.isRunning) {
346
- this.#tick()
347
- }
348
- } else {
349
- for (let i = 0; i < (this.options.initialItems?.length ?? 0); i++) {
350
- const item = this.options.initialItems![i]!
351
- const isLast = i === (this.options.initialItems?.length ?? 0) - 1
352
- this.addItem(item, this.options.addItemsTo ?? 'back', isLast)
353
- }
354
- }
355
-
356
- if (this.key) {
357
- pacerEventClient.on('d-AsyncQueuer', (e) => {
358
- if (e.payload.key !== this.key) return
359
- this.#setState(
360
- e.payload.store.state as Partial<AsyncQueuerState<TValue>>,
361
- )
362
- this.setOptions(
363
- e.payload.options as Partial<AsyncQueuerOptions<TValue>>,
364
- )
365
- })
366
- }
367
- }
368
-
369
- /**
370
- * Updates the queuer options. New options are merged with existing options.
371
- */
372
- setOptions = (newOptions: Partial<AsyncQueuerOptions<TValue>>): void => {
373
- this.options = { ...this.options, ...newOptions }
374
- }
375
-
376
- #setState = (newState: Partial<AsyncQueuerState<TValue>>): void => {
377
- this.store.setState((state) => {
378
- const combinedState = {
379
- ...state,
380
- ...newState,
381
- }
382
-
383
- const { activeItems, items, isRunning } = combinedState
384
-
385
- const size = items.length
386
- const isFull = size >= (this.options.maxSize ?? Infinity)
387
- const isEmpty = size === 0
388
- const isIdle = isRunning && isEmpty && activeItems.length === 0
389
-
390
- const status = isIdle ? 'idle' : isRunning ? 'running' : 'stopped'
391
-
392
- return {
393
- ...combinedState,
394
- isEmpty,
395
- isFull,
396
- isIdle,
397
- size,
398
- status,
399
- }
400
- })
401
- emitChange('AsyncQueuer', this)
402
- }
403
-
404
- /**
405
- * Returns the current wait time (in milliseconds) between processing items.
406
- * If a function is provided, it is called with the queuer instance.
407
- */
408
- #getWait = (): number => {
409
- return parseFunctionOrValue(this.options.wait ?? 0, this)
410
- }
411
-
412
- /**
413
- * Returns the current concurrency limit for processing items.
414
- * If a function is provided, it is called with the queuer instance.
415
- */
416
- #getConcurrency = (): number => {
417
- return parseFunctionOrValue(this.options.concurrency ?? 1, this)
418
- }
419
-
420
- /**
421
- * Processes items in the queue up to the concurrency limit. Internal use only.
422
- */
423
- #tick = () => {
424
- if (!this.store.state.isRunning) {
425
- this.#setState({ pendingTick: false })
426
- return
427
- }
428
- this.#setState({ pendingTick: true })
429
-
430
- // Check for expired items
431
- this.#checkExpiredItems()
432
-
433
- // Process items concurrently up to the concurrency limit
434
- const activeItems = this.store.state.activeItems
435
- while (
436
- activeItems.length < this.#getConcurrency() &&
437
- this.store.state.items.length > 0
438
- ) {
439
- const nextItem = this.peekNextItem()
440
- if (!nextItem) {
441
- break
442
- }
443
- activeItems.push(nextItem)
444
- this.#setState({
445
- activeItems,
446
- })
447
- ;(async () => {
448
- await this.execute()
449
-
450
- const wait = this.#getWait()
451
- if (wait > 0) {
452
- const timeoutId = setTimeout(() => this.#tick(), wait)
453
- this.#timeoutIds.add(timeoutId)
454
- return
455
- }
456
-
457
- this.#tick()
458
- })()
459
- }
460
-
461
- this.#setState({ pendingTick: false })
462
- }
463
-
464
- /**
465
- * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
466
- * Items can be inserted based on priority or at the front/back depending on configuration.
467
- *
468
- * @example
469
- * ```ts
470
- * queuer.addItem({ value: 'task', priority: 10 });
471
- * queuer.addItem('task2', 'front');
472
- * ```
473
- */
474
- addItem = (
475
- item: TValue,
476
- position: QueuePosition = this.options.addItemsTo ?? 'back',
477
- runOnItemsChange: boolean = true,
478
- ): boolean => {
479
- this.#setState({
480
- addItemCount: this.store.state.addItemCount + 1,
481
- })
482
-
483
- if (this.store.state.items.length >= (this.options.maxSize ?? Infinity)) {
484
- this.#setState({
485
- rejectionCount: this.store.state.rejectionCount + 1,
486
- })
487
- this.options.onReject?.(item, this)
488
- return false
489
- }
490
-
491
- // Get priority either from the function or from getPriority option
492
- const priority =
493
- this.options.getPriority !== defaultOptions.getPriority
494
- ? this.options.getPriority!(item)
495
- : (item as any).priority
496
-
497
- const items = this.store.state.items
498
- const itemTimestamps = this.store.state.itemTimestamps
499
-
500
- if (priority !== undefined) {
501
- // Insert based on priority - higher priority items go to front
502
- const insertIndex = items.findIndex((existing) => {
503
- const existingPriority =
504
- this.options.getPriority !== defaultOptions.getPriority
505
- ? this.options.getPriority!(existing)
506
- : (existing as any).priority
507
- return existingPriority < priority
508
- })
509
-
510
- if (insertIndex === -1) {
511
- items.push(item)
512
- itemTimestamps.push(Date.now())
513
- } else {
514
- items.splice(insertIndex, 0, item)
515
- itemTimestamps.splice(insertIndex, 0, Date.now())
516
- }
517
- } else {
518
- if (position === 'front') {
519
- // Default FIFO/LIFO behavior
520
- items.unshift(item)
521
- itemTimestamps.unshift(Date.now())
522
- } else {
523
- // LIFO
524
- items.push(item)
525
- itemTimestamps.push(Date.now())
526
- }
527
- }
528
-
529
- this.#setState({
530
- items,
531
- itemTimestamps,
532
- })
533
-
534
- if (runOnItemsChange) {
535
- this.options.onItemsChange?.(this)
536
- }
537
-
538
- if (this.store.state.isRunning && !this.store.state.pendingTick) {
539
- this.#tick()
540
- }
541
-
542
- return true
543
- }
544
-
545
- /**
546
- * Removes and returns the next item from the queue without executing the task function.
547
- * Use for manual queue management. Normally, use execute() to process items.
548
- *
549
- * @example
550
- * ```ts
551
- * // FIFO
552
- * queuer.getNextItem();
553
- * // LIFO
554
- * queuer.getNextItem('back');
555
- * ```
556
- */
557
- getNextItem = (
558
- position: QueuePosition = this.options.getItemsFrom ?? 'front',
559
- ): TValue | undefined => {
560
- const { items, itemTimestamps } = this.store.state
561
- let item: TValue | undefined
562
-
563
- // When priority function is provided or position is 'front', always get from front (highest priority)
564
- // Priority takes precedence over FIFO/LIFO behavior
565
- if (
566
- this.options.getPriority !== defaultOptions.getPriority ||
567
- position === 'front'
568
- ) {
569
- item = items[0]
570
- if (item !== undefined) {
571
- this.#setState({
572
- items: items.slice(1),
573
- itemTimestamps: itemTimestamps.slice(1),
574
- })
575
- }
576
- } else {
577
- item = items[items.length - 1]
578
- if (item !== undefined) {
579
- this.#setState({
580
- items: items.slice(0, -1),
581
- itemTimestamps: itemTimestamps.slice(0, -1),
582
- })
583
- }
584
- }
585
-
586
- if (item !== undefined) {
587
- this.options.onItemsChange?.(this)
588
- }
589
-
590
- return item
591
- }
592
-
593
- #getAllItems = (): Array<TValue> => {
594
- const items = this.peekAllItems()
595
- this.clear()
596
- return items
597
- }
598
-
599
- /**
600
- * Removes and returns the next item from the queue and executes the task function with it.
601
- *
602
- * @example
603
- * ```ts
604
- * queuer.execute();
605
- * // LIFO
606
- * queuer.execute('back');
607
- * ```
608
- */
609
- execute = async (position?: QueuePosition): Promise<any> => {
610
- const item = this.getNextItem(position)
611
-
612
- if (item !== undefined) {
613
- const currentExecuteCount = this.store.state.executeCount + 1
614
- this.#setState({
615
- executeCount: currentExecuteCount,
616
- isExecuting: true,
617
- })
618
- try {
619
- const currentAsyncRetryer = new AsyncRetryer(this.fn, {
620
- ...this.options.asyncRetryerOptions,
621
- key: `${this.key}-retryer-${currentExecuteCount}`,
622
- })
623
- this.asyncRetryers.set(currentExecuteCount, currentAsyncRetryer)
624
- const lastResult = await currentAsyncRetryer.execute(item) // EXECUTE!
625
- this.#setState({
626
- successCount: this.store.state.successCount + 1,
627
- lastResult,
628
- })
629
- this.options.onSuccess?.(lastResult, item, this)
630
- } catch (error) {
631
- this.#setState({
632
- errorCount: this.store.state.errorCount + 1,
633
- })
634
- this.options.onError?.(error as Error, item, this)
635
- if (this.options.throwOnError) {
636
- throw error
637
- }
638
- } finally {
639
- this.asyncRetryers.delete(currentExecuteCount) // dispose retryer
640
- this.#setState({
641
- activeItems: this.store.state.activeItems.filter(
642
- (activeItem) => activeItem !== item,
643
- ),
644
- isExecuting: false,
645
- settledCount: this.store.state.settledCount + 1,
646
- })
647
- this.options.onSettled?.(item, this)
648
- }
649
- }
650
- return item
651
- }
652
-
653
- /**
654
- * Processes a specified number of items to execute immediately with no wait time
655
- * If no numberOfItems is provided, all items will be processed
656
- */
657
- flush = async (
658
- numberOfItems: number = this.store.state.items.length,
659
- position?: QueuePosition,
660
- ): Promise<void> => {
661
- this.#clearTimeouts() // clear any pending timeouts
662
- await Promise.all(
663
- Array.from({ length: numberOfItems }, () => this.execute(position)),
664
- )
665
- }
666
-
667
- /**
668
- * Processes all items in the queue as a batch using the provided function as an argument
669
- * The queue is cleared after processing
670
- */
671
- flushAsBatch = async (
672
- batchFunction: (items: Array<TValue>) => Promise<any>,
673
- ): Promise<void> => {
674
- this.#clearTimeouts() // clear any pending timeouts
675
- const items = this.#getAllItems()
676
- await batchFunction(items)
677
- }
678
-
679
- /**
680
- * Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
681
- * Internal use only.
682
- */
683
- #checkExpiredItems = (): void => {
684
- if (
685
- (this.options.expirationDuration ?? Infinity) === Infinity &&
686
- this.options.getIsExpired === defaultOptions.getIsExpired
687
- ) {
688
- return
689
- }
690
-
691
- const now = Date.now()
692
- const expiredIndices: Array<number> = []
693
-
694
- // Find indices of expired items
695
- for (let i = 0; i < this.store.state.items.length; i++) {
696
- const timestamp = this.store.state.itemTimestamps[i]
697
- if (timestamp === undefined) continue
698
-
699
- const item = this.store.state.items[i]
700
- if (item === undefined) continue
701
-
702
- const isExpired =
703
- this.options.getIsExpired !== defaultOptions.getIsExpired
704
- ? this.options.getIsExpired!(item, timestamp)
705
- : now - timestamp > (this.options.expirationDuration ?? Infinity)
706
-
707
- if (isExpired) {
708
- expiredIndices.push(i)
709
- }
710
- }
711
-
712
- // Remove expired items from back to front to maintain indices
713
- for (let i = expiredIndices.length - 1; i >= 0; i--) {
714
- const index = expiredIndices[i]
715
- if (index === undefined) continue
716
-
717
- const expiredItem = this.store.state.items[index]
718
- if (expiredItem === undefined) continue
719
-
720
- const newItems = [...this.store.state.items]
721
- const newTimestamps = [...this.store.state.itemTimestamps]
722
- newItems.splice(index, 1)
723
- newTimestamps.splice(index, 1)
724
- this.#setState({
725
- items: newItems,
726
- itemTimestamps: newTimestamps,
727
- expirationCount: this.store.state.expirationCount + 1,
728
- })
729
- this.options.onExpire?.(expiredItem, this)
730
- }
731
-
732
- if (expiredIndices.length > 0) {
733
- this.options.onItemsChange?.(this)
734
- }
735
- }
736
-
737
- /**
738
- * Returns the next item in the queue without removing it.
739
- *
740
- * @example
741
- * ```ts
742
- * queuer.peekNextItem(); // front
743
- * queuer.peekNextItem('back'); // back
744
- * ```
745
- */
746
- peekNextItem = (position: QueuePosition = 'front'): TValue | undefined => {
747
- if (position === 'front') {
748
- return this.store.state.items[0]
749
- }
750
- return this.store.state.items[this.store.state.items.length - 1]
751
- }
752
-
753
- /**
754
- * Returns a copy of all items in the queue, including active and pending items.
755
- */
756
- peekAllItems = (): Array<TValue> => {
757
- return [...this.peekActiveItems(), ...this.peekPendingItems()]
758
- }
759
-
760
- /**
761
- * Returns the items currently being processed (active tasks).
762
- */
763
- peekActiveItems = (): Array<TValue> => {
764
- return [...this.store.state.activeItems]
765
- }
766
-
767
- /**
768
- * Returns the items waiting to be processed (pending tasks).
769
- */
770
- peekPendingItems = (): Array<TValue> => {
771
- return [...this.store.state.items]
772
- }
773
-
774
- /**
775
- * Starts processing items in the queue. If already running, does nothing.
776
- */
777
- start = (): void => {
778
- this.#setState({ isRunning: true })
779
- if (!this.store.state.pendingTick && this.store.state.items.length > 0) {
780
- this.#tick()
781
- }
782
- }
783
-
784
- /**
785
- * Stops processing items in the queue. Does not clear the queue.
786
- */
787
- stop = (): void => {
788
- this.#clearTimeouts()
789
- this.#setState({ isRunning: false, pendingTick: false })
790
- }
791
-
792
- #clearTimeouts = (): void => {
793
- this.#timeoutIds.forEach((timeoutId) => clearTimeout(timeoutId))
794
- this.#timeoutIds.clear()
795
- }
796
-
797
- /**
798
- * Removes all pending items from the queue.
799
- * Does NOT affect active tasks.
800
- */
801
- clear = (): void => {
802
- this.#setState({ items: [], itemTimestamps: [] })
803
- this.options.onItemsChange?.(this)
804
- }
805
-
806
- /**
807
- * Returns the AbortSignal for a specific execution.
808
- * If no executeCount is provided, returns the signal for the most recent execution.
809
- * Returns null if no execution is found or not currently executing.
810
- *
811
- * @param executeCount - Optional specific execution to get signal for
812
- * @example
813
- * ```typescript
814
- * const queuer = new AsyncQueuer(
815
- * async (item: string) => {
816
- * const signal = queuer.getAbortSignal()
817
- * if (signal) {
818
- * const response = await fetch(`/api/process/${item}`, { signal })
819
- * return response.json()
820
- * }
821
- * },
822
- * { concurrency: 2 }
823
- * )
824
- * ```
825
- */
826
- getAbortSignal = (executeCount?: number): AbortSignal | null => {
827
- const count = executeCount ?? this.store.state.executeCount
828
- const retryer = this.asyncRetryers.get(count)
829
- return retryer?.getAbortSignal() ?? null
830
- }
831
-
832
- /**
833
- * Aborts all ongoing executions with the internal abort controllers.
834
- * Does NOT clear out the items.
835
- */
836
- abort = (): void => {
837
- this.asyncRetryers.forEach((retryer) => retryer.abort())
838
- this.asyncRetryers.clear()
839
- this.#setState({
840
- isExecuting: false,
841
- })
842
- }
843
-
844
- /**
845
- * Resets the queuer state to its default values
846
- */
847
- reset = (): void => {
848
- this.#setState(getDefaultAsyncQueuerState<TValue>())
849
- this.options.onItemsChange?.(this)
850
- this.asyncRetryers.forEach((retryer) => retryer.reset())
851
- }
852
- }
853
-
854
- /**
855
- * Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
856
- * The queuer is started automatically and ready to process items.
857
- *
858
- * Async vs Sync Versions:
859
- * The async version provides advanced features over the sync queue function:
860
- * - Returns promises that can be awaited for task results
861
- * - Built-in retry support via AsyncRetryer integration for each queued task
862
- * - Abort support to cancel in-flight task executions
863
- * - Comprehensive error handling with onError callbacks and throwOnError control
864
- * - Detailed execution tracking (success/error/settle counts)
865
- * - Concurrent execution support (process multiple items simultaneously)
866
- *
867
- * The sync queue function is lighter weight and simpler when you don't need async features,
868
- * return values, or execution control.
869
- *
870
- * What is Queuing?
871
- * Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
872
- * Tasks are processed up to the configured concurrency limit. When a task completes,
873
- * the next pending task is processed if the concurrency limit allows.
874
- *
875
- * Configuration Options:
876
- * - `concurrency`: Maximum number of concurrent tasks (default: 1)
877
- * - `wait`: Time to wait between processing items (default: 0)
878
- * - `maxSize`: Maximum number of items allowed in the queue (default: Infinity)
879
- * - `getPriority`: Function to determine item priority
880
- * - `addItemsTo`: Default position to add items ('back' or 'front', default: 'back')
881
- * - `getItemsFrom`: Default position to get items ('front' or 'back', default: 'front')
882
- * - `expirationDuration`: Maximum time items can stay in queue
883
- * - `started`: Whether to start processing immediately (default: true)
884
- * - `asyncRetryerOptions`: Configure retry behavior for task executions
885
- *
886
- * Error Handling:
887
- * - If an `onError` handler is provided, it will be called with the error and queuer instance
888
- * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
889
- * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
890
- * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
891
- * - The error state can be checked using the underlying AsyncQueuer instance
892
- *
893
- * State Management:
894
- * - Uses TanStack Store for reactive state management
895
- * - Use `initialState` to provide initial state values when creating the async queuer
896
- * - Use `onSuccess` callback to react to successful task execution and implement custom logic
897
- * - Use `onError` callback to react to task execution errors and implement custom error handling
898
- * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
899
- * - Use `onItemsChange` callback to react to items being added or removed from the queue
900
- * - Use `onExpire` callback to react to items expiring and implement custom logic
901
- * - Use `onReject` callback to react to items being rejected when the queue is full
902
- * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
903
- * - State can be accessed via the underlying AsyncQueuer instance's `store.state` property
904
- * - When using framework adapters (React/Solid), state is accessed from the hook's state property
905
- *
906
- * @example
907
- * ```ts
908
- * const enqueue = asyncQueue<string>(async (item) => {
909
- * return item.toUpperCase();
910
- * }, {
911
- * concurrency: 2,
912
- * wait: 100,
913
- * onSuccess: (result) => console.log('Processed:', result)
914
- * });
915
- *
916
- * enqueue('hello');
917
- * ```
918
- */
919
- export function asyncQueue<TValue>(
920
- fn: (value: TValue) => Promise<any>,
921
- initialOptions: AsyncQueuerOptions<TValue>,
922
- ) {
923
- const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)
924
- return asyncQueuer.addItem
925
- }