@tanstack/pacer 0.8.0 → 0.9.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 (91) hide show
  1. package/dist/cjs/async-batcher.cjs +163 -0
  2. package/dist/cjs/async-batcher.cjs.map +1 -0
  3. package/dist/cjs/async-batcher.d.cts +273 -0
  4. package/dist/cjs/async-debouncer.cjs +149 -162
  5. package/dist/cjs/async-debouncer.cjs.map +1 -1
  6. package/dist/cjs/async-debouncer.d.cts +76 -57
  7. package/dist/cjs/async-queuer.cjs +282 -343
  8. package/dist/cjs/async-queuer.cjs.map +1 -1
  9. package/dist/cjs/async-queuer.d.cts +121 -100
  10. package/dist/cjs/async-rate-limiter.cjs +128 -185
  11. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  12. package/dist/cjs/async-rate-limiter.d.cts +72 -61
  13. package/dist/cjs/async-throttler.cjs +168 -178
  14. package/dist/cjs/async-throttler.cjs.map +1 -1
  15. package/dist/cjs/async-throttler.d.cts +97 -69
  16. package/dist/cjs/batcher.cjs +110 -119
  17. package/dist/cjs/batcher.cjs.map +1 -1
  18. package/dist/cjs/batcher.d.cts +76 -51
  19. package/dist/cjs/debouncer.cjs +97 -85
  20. package/dist/cjs/debouncer.cjs.map +1 -1
  21. package/dist/cjs/debouncer.d.cts +54 -26
  22. package/dist/cjs/index.cjs +3 -6
  23. package/dist/cjs/index.cjs.map +1 -1
  24. package/dist/cjs/index.d.cts +1 -1
  25. package/dist/cjs/queuer.cjs +246 -294
  26. package/dist/cjs/queuer.cjs.map +1 -1
  27. package/dist/cjs/queuer.d.cts +102 -81
  28. package/dist/cjs/rate-limiter.cjs +97 -130
  29. package/dist/cjs/rate-limiter.cjs.map +1 -1
  30. package/dist/cjs/rate-limiter.d.cts +50 -37
  31. package/dist/cjs/throttler.cjs +107 -123
  32. package/dist/cjs/throttler.cjs.map +1 -1
  33. package/dist/cjs/throttler.d.cts +59 -35
  34. package/dist/cjs/utils.cjs +0 -13
  35. package/dist/cjs/utils.cjs.map +1 -1
  36. package/dist/cjs/utils.d.cts +0 -1
  37. package/dist/esm/async-batcher.d.ts +273 -0
  38. package/dist/esm/async-batcher.js +163 -0
  39. package/dist/esm/async-batcher.js.map +1 -0
  40. package/dist/esm/async-debouncer.d.ts +76 -57
  41. package/dist/esm/async-debouncer.js +149 -162
  42. package/dist/esm/async-debouncer.js.map +1 -1
  43. package/dist/esm/async-queuer.d.ts +121 -100
  44. package/dist/esm/async-queuer.js +282 -343
  45. package/dist/esm/async-queuer.js.map +1 -1
  46. package/dist/esm/async-rate-limiter.d.ts +72 -61
  47. package/dist/esm/async-rate-limiter.js +128 -185
  48. package/dist/esm/async-rate-limiter.js.map +1 -1
  49. package/dist/esm/async-throttler.d.ts +97 -69
  50. package/dist/esm/async-throttler.js +168 -178
  51. package/dist/esm/async-throttler.js.map +1 -1
  52. package/dist/esm/batcher.d.ts +76 -51
  53. package/dist/esm/batcher.js +110 -119
  54. package/dist/esm/batcher.js.map +1 -1
  55. package/dist/esm/debouncer.d.ts +54 -26
  56. package/dist/esm/debouncer.js +97 -85
  57. package/dist/esm/debouncer.js.map +1 -1
  58. package/dist/esm/index.d.ts +1 -1
  59. package/dist/esm/index.js +4 -7
  60. package/dist/esm/queuer.d.ts +102 -81
  61. package/dist/esm/queuer.js +246 -294
  62. package/dist/esm/queuer.js.map +1 -1
  63. package/dist/esm/rate-limiter.d.ts +50 -37
  64. package/dist/esm/rate-limiter.js +97 -130
  65. package/dist/esm/rate-limiter.js.map +1 -1
  66. package/dist/esm/throttler.d.ts +59 -35
  67. package/dist/esm/throttler.js +107 -123
  68. package/dist/esm/throttler.js.map +1 -1
  69. package/dist/esm/utils.d.ts +0 -1
  70. package/dist/esm/utils.js +0 -13
  71. package/dist/esm/utils.js.map +1 -1
  72. package/package.json +14 -11
  73. package/src/async-batcher.ts +475 -0
  74. package/src/async-debouncer.ts +201 -121
  75. package/src/async-queuer.ts +337 -216
  76. package/src/async-rate-limiter.ts +176 -136
  77. package/src/async-throttler.ts +233 -139
  78. package/src/batcher.ts +158 -92
  79. package/src/debouncer.ts +135 -52
  80. package/src/index.ts +1 -1
  81. package/src/queuer.ts +348 -226
  82. package/src/rate-limiter.ts +125 -80
  83. package/src/throttler.ts +152 -78
  84. package/src/utils.ts +0 -15
  85. package/dist/cjs/compare.cjs +0 -72
  86. package/dist/cjs/compare.cjs.map +0 -1
  87. package/dist/cjs/compare.d.cts +0 -12
  88. package/dist/esm/compare.d.ts +0 -12
  89. package/dist/esm/compare.js +0 -72
  90. package/dist/esm/compare.js.map +0 -1
  91. package/src/compare.ts +0 -105
@@ -1,4 +1,71 @@
1
+ import { Store } from '@tanstack/store';
1
2
  import { QueuePosition } from './queuer.js';
3
+ export interface AsyncQueuerState<TValue> {
4
+ /**
5
+ * Items currently being processed by the queuer
6
+ */
7
+ activeItems: Array<TValue>;
8
+ /**
9
+ * Number of task executions that have resulted in errors
10
+ */
11
+ errorCount: number;
12
+ /**
13
+ * Number of items that have been removed from the queue due to expiration
14
+ */
15
+ expirationCount: number;
16
+ /**
17
+ * Whether the queuer has no items to process (items array is empty)
18
+ */
19
+ isEmpty: boolean;
20
+ /**
21
+ * Whether the queuer has reached its maximum capacity
22
+ */
23
+ isFull: boolean;
24
+ /**
25
+ * Whether the queuer is not currently processing any items
26
+ */
27
+ isIdle: boolean;
28
+ /**
29
+ * Whether the queuer is active and will process items automatically
30
+ */
31
+ isRunning: boolean;
32
+ /**
33
+ * Timestamps when items were added to the queue for expiration tracking
34
+ */
35
+ itemTimestamps: Array<number>;
36
+ /**
37
+ * Array of items currently waiting to be processed
38
+ */
39
+ items: Array<TValue>;
40
+ /**
41
+ * The result from the most recent task execution
42
+ */
43
+ lastResult: any;
44
+ /**
45
+ * Whether the queuer has a pending timeout for processing the next item
46
+ */
47
+ pendingTick: boolean;
48
+ /**
49
+ * Number of items that have been rejected from being added to the queue
50
+ */
51
+ rejectionCount: number;
52
+ /**
53
+ * Number of task executions that have completed (either successfully or with errors)
54
+ */
55
+ settledCount: number;
56
+ /**
57
+ * Number of items currently in the queue
58
+ */
59
+ size: number;
60
+ /**
61
+ * Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
62
+ */
63
+ status: 'idle' | 'running' | 'stopped';
64
+ /**
65
+ * Number of task executions that have completed successfully
66
+ */
67
+ successCount: number;
68
+ }
2
69
  export interface AsyncQueuerOptions<TValue> {
3
70
  /**
4
71
  * Default position to add items to the queuer
@@ -36,6 +103,10 @@ export interface AsyncQueuerOptions<TValue> {
36
103
  * Initial items to populate the queuer with
37
104
  */
38
105
  initialItems?: Array<TValue>;
106
+ /**
107
+ * Initial state for the async queuer
108
+ */
109
+ initialState?: Partial<AsyncQueuerState<TValue>>;
39
110
  /**
40
111
  * Maximum number of items allowed in the queuer
41
112
  */
@@ -50,10 +121,6 @@ export interface AsyncQueuerOptions<TValue> {
50
121
  * Callback fired whenever an item expires in the queuer
51
122
  */
52
123
  onExpire?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
53
- /**
54
- * Callback fired whenever the queuer's running state changes
55
- */
56
- onIsRunningChange?: (queuer: AsyncQueuer<TValue>) => void;
57
124
  /**
58
125
  * Callback fired whenever an item is added or removed from the queuer
59
126
  */
@@ -109,6 +176,19 @@ export interface AsyncQueuerOptions<TValue> {
109
176
  * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
110
177
  * - The error state can be checked using the AsyncQueuer instance
111
178
  *
179
+ * State Management:
180
+ * - Uses TanStack Store for reactive state management
181
+ * - Use `initialState` to provide initial state values when creating the async queuer
182
+ * - Use `onSuccess` callback to react to successful task execution and implement custom logic
183
+ * - Use `onError` callback to react to task execution errors and implement custom error handling
184
+ * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
185
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
186
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
187
+ * - Use `onReject` callback to react to items being rejected when the queue is full
188
+ * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
189
+ * - State can be accessed via `asyncQueuer.store.state` when using the class directly
190
+ * - When using framework adapters (React/Solid), state is accessed from `asyncQueuer.state`
191
+ *
112
192
  * Example usage:
113
193
  * ```ts
114
194
  * const asyncQueuer = new AsyncQueuer<string>(async (item) => {
@@ -126,59 +206,15 @@ export interface AsyncQueuerOptions<TValue> {
126
206
  * ```
127
207
  */
128
208
  export declare class AsyncQueuer<TValue> {
209
+ #private;
129
210
  private fn;
130
- private _options;
131
- private _activeItems;
132
- private _successCount;
133
- private _errorCount;
134
- private _settledCount;
135
- private _rejectionCount;
136
- private _expirationCount;
137
- private _items;
138
- private _itemTimestamps;
139
- private _pendingTick;
140
- private _running;
141
- private _lastResult;
142
- constructor(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>);
211
+ readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
212
+ options: AsyncQueuerOptions<TValue>;
213
+ constructor(fn: (item: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>);
143
214
  /**
144
215
  * Updates the queuer options. New options are merged with existing options.
145
216
  */
146
- setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): void;
147
- /**
148
- * Returns the current queuer options, including defaults and any overrides.
149
- */
150
- getOptions(): AsyncQueuerOptions<TValue>;
151
- /**
152
- * Returns the current wait time (in milliseconds) between processing items.
153
- * If a function is provided, it is called with the queuer instance.
154
- */
155
- getWait(): number;
156
- /**
157
- * Returns the current concurrency limit for processing items.
158
- * If a function is provided, it is called with the queuer instance.
159
- */
160
- getConcurrency(): number;
161
- /**
162
- * Processes items in the queue up to the concurrency limit. Internal use only.
163
- */
164
- private tick;
165
- /**
166
- * Starts processing items in the queue. If already running, does nothing.
167
- */
168
- start(): void;
169
- /**
170
- * Stops processing items in the queue. Does not clear the queue.
171
- */
172
- stop(): void;
173
- /**
174
- * Removes all pending items from the queue. Does not affect active tasks.
175
- */
176
- clear(): void;
177
- /**
178
- * Resets the queuer to its initial state. Optionally repopulates with initial items.
179
- * Does not affect callbacks or options.
180
- */
181
- reset(withInitialItems?: boolean): void;
217
+ setOptions: (newOptions: Partial<AsyncQueuerOptions<TValue>>) => void;
182
218
  /**
183
219
  * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
184
220
  * Items can be inserted based on priority or at the front/back depending on configuration.
@@ -189,9 +225,7 @@ export declare class AsyncQueuer<TValue> {
189
225
  * queuer.addItem('task2', 'front');
190
226
  * ```
191
227
  */
192
- addItem(item: TValue & {
193
- priority?: number;
194
- }, position?: QueuePosition, runOnItemsChange?: boolean): void;
228
+ addItem: (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
195
229
  /**
196
230
  * Removes and returns the next item from the queue without executing the task function.
197
231
  * Use for manual queue management. Normally, use execute() to process items.
@@ -204,7 +238,7 @@ export declare class AsyncQueuer<TValue> {
204
238
  * queuer.getNextItem('back');
205
239
  * ```
206
240
  */
207
- getNextItem(position?: QueuePosition): TValue | undefined;
241
+ getNextItem: (position?: QueuePosition) => TValue | undefined;
208
242
  /**
209
243
  * Removes and returns the next item from the queue and executes the task function with it.
210
244
  *
@@ -215,12 +249,12 @@ export declare class AsyncQueuer<TValue> {
215
249
  * queuer.execute('back');
216
250
  * ```
217
251
  */
218
- execute(position?: QueuePosition): Promise<any>;
252
+ execute: (position?: QueuePosition) => Promise<any>;
219
253
  /**
220
- * Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
221
- * Internal use only.
254
+ * Processes a specified number of items to execute immediately with no wait time
255
+ * If no numberOfItems is provided, all items will be processed
222
256
  */
223
- private checkExpiredItems;
257
+ flush: (numberOfItems?: number, position?: QueuePosition) => void;
224
258
  /**
225
259
  * Returns the next item in the queue without removing it.
226
260
  *
@@ -230,59 +264,35 @@ export declare class AsyncQueuer<TValue> {
230
264
  * queuer.peekNextItem('back'); // back
231
265
  * ```
232
266
  */
233
- peekNextItem(position?: QueuePosition): TValue | undefined;
234
- /**
235
- * Returns true if the queue is empty (no pending items).
236
- */
237
- getIsEmpty(): boolean;
238
- /**
239
- * Returns true if the queue is full (reached maxSize).
240
- */
241
- getIsFull(): boolean;
242
- /**
243
- * Returns the number of pending items in the queue.
244
- */
245
- getSize(): number;
267
+ peekNextItem: (position?: QueuePosition) => TValue | undefined;
246
268
  /**
247
269
  * Returns a copy of all items in the queue, including active and pending items.
248
270
  */
249
- peekAllItems(): Array<TValue>;
271
+ peekAllItems: () => Array<TValue>;
250
272
  /**
251
273
  * Returns the items currently being processed (active tasks).
252
274
  */
253
- peekActiveItems(): Array<TValue>;
275
+ peekActiveItems: () => Array<TValue>;
254
276
  /**
255
277
  * Returns the items waiting to be processed (pending tasks).
256
278
  */
257
- peekPendingItems(): Array<TValue>;
258
- /**
259
- * Returns the number of items that have been successfully processed.
260
- */
261
- getSuccessCount(): number;
262
- /**
263
- * Returns the number of items that have failed processing.
264
- */
265
- getErrorCount(): number;
266
- /**
267
- * Returns the number of items that have completed processing (success or error).
268
- */
269
- getSettledCount(): number;
279
+ peekPendingItems: () => Array<TValue>;
270
280
  /**
271
- * Returns the number of items that have been rejected from being added to the queue.
281
+ * Starts processing items in the queue. If already running, does nothing.
272
282
  */
273
- getRejectionCount(): number;
283
+ start: () => void;
274
284
  /**
275
- * Returns true if the queuer is currently running (processing items).
285
+ * Stops processing items in the queue. Does not clear the queue.
276
286
  */
277
- getIsRunning(): boolean;
287
+ stop: () => void;
278
288
  /**
279
- * Returns true if the queuer is running but has no items to process and no active tasks.
289
+ * Removes all pending items from the queue. Does not affect active tasks.
280
290
  */
281
- getIsIdle(): boolean;
291
+ clear: () => void;
282
292
  /**
283
- * Returns the number of items that have expired and been removed from the queue.
293
+ * Resets the queuer state to its default values
284
294
  */
285
- getExpirationCount(): number;
295
+ reset: () => void;
286
296
  }
287
297
  /**
288
298
  * Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
@@ -295,6 +305,19 @@ export declare class AsyncQueuer<TValue> {
295
305
  * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
296
306
  * - The error state can be checked using the underlying AsyncQueuer instance
297
307
  *
308
+ * State Management:
309
+ * - Uses TanStack Store for reactive state management
310
+ * - Use `initialState` to provide initial state values when creating the async queuer
311
+ * - Use `onSuccess` callback to react to successful task execution and implement custom logic
312
+ * - Use `onError` callback to react to task execution errors and implement custom error handling
313
+ * - Use `onSettled` callback to react to task execution completion (success or error) and implement custom logic
314
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
315
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
316
+ * - Use `onReject` callback to react to items being rejected when the queue is full
317
+ * - The state includes error count, expiration count, rejection count, running status, and success/settle counts
318
+ * - State can be accessed via the underlying AsyncQueuer instance's `store.state` property
319
+ * - When using framework adapters (React/Solid), state is accessed from the hook's state property
320
+ *
298
321
  * Example usage:
299
322
  * ```ts
300
323
  * const enqueue = asyncQueue<string>(async (item) => {
@@ -304,6 +327,4 @@ export declare class AsyncQueuer<TValue> {
304
327
  * enqueue('hello');
305
328
  * ```
306
329
  */
307
- export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue & {
308
- priority?: number;
309
- }, position?: QueuePosition, runOnItemsChange?: boolean) => void;
330
+ export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;