@tanstack/pacer 0.5.0 → 0.7.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 (74) hide show
  1. package/dist/cjs/async-debouncer.cjs +32 -17
  2. package/dist/cjs/async-debouncer.cjs.map +1 -1
  3. package/dist/cjs/async-debouncer.d.cts +51 -9
  4. package/dist/cjs/async-queuer.cjs +189 -191
  5. package/dist/cjs/async-queuer.cjs.map +1 -1
  6. package/dist/cjs/async-queuer.d.cts +151 -94
  7. package/dist/cjs/async-rate-limiter.cjs +23 -13
  8. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  9. package/dist/cjs/async-rate-limiter.d.cts +54 -5
  10. package/dist/cjs/async-throttler.cjs +38 -17
  11. package/dist/cjs/async-throttler.cjs.map +1 -1
  12. package/dist/cjs/async-throttler.d.cts +51 -8
  13. package/dist/cjs/batcher.cjs +138 -0
  14. package/dist/cjs/batcher.cjs.map +1 -0
  15. package/dist/cjs/batcher.d.cts +149 -0
  16. package/dist/cjs/debouncer.cjs +3 -4
  17. package/dist/cjs/debouncer.cjs.map +1 -1
  18. package/dist/cjs/debouncer.d.cts +1 -2
  19. package/dist/cjs/index.cjs +3 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/cjs/index.d.cts +1 -0
  22. package/dist/cjs/queuer.cjs +68 -41
  23. package/dist/cjs/queuer.cjs.map +1 -1
  24. package/dist/cjs/queuer.d.cts +123 -92
  25. package/dist/cjs/rate-limiter.cjs +3 -4
  26. package/dist/cjs/rate-limiter.cjs.map +1 -1
  27. package/dist/cjs/rate-limiter.d.cts +1 -2
  28. package/dist/cjs/throttler.cjs +3 -4
  29. package/dist/cjs/throttler.cjs.map +1 -1
  30. package/dist/cjs/throttler.d.cts +1 -2
  31. package/dist/cjs/types.d.cts +1 -0
  32. package/dist/esm/async-debouncer.d.ts +51 -9
  33. package/dist/esm/async-debouncer.js +32 -17
  34. package/dist/esm/async-debouncer.js.map +1 -1
  35. package/dist/esm/async-queuer.d.ts +151 -94
  36. package/dist/esm/async-queuer.js +189 -191
  37. package/dist/esm/async-queuer.js.map +1 -1
  38. package/dist/esm/async-rate-limiter.d.ts +54 -5
  39. package/dist/esm/async-rate-limiter.js +23 -13
  40. package/dist/esm/async-rate-limiter.js.map +1 -1
  41. package/dist/esm/async-throttler.d.ts +51 -8
  42. package/dist/esm/async-throttler.js +38 -17
  43. package/dist/esm/async-throttler.js.map +1 -1
  44. package/dist/esm/batcher.d.ts +149 -0
  45. package/dist/esm/batcher.js +138 -0
  46. package/dist/esm/batcher.js.map +1 -0
  47. package/dist/esm/debouncer.d.ts +1 -2
  48. package/dist/esm/debouncer.js +3 -4
  49. package/dist/esm/debouncer.js.map +1 -1
  50. package/dist/esm/index.d.ts +1 -0
  51. package/dist/esm/index.js +3 -0
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/queuer.d.ts +123 -92
  54. package/dist/esm/queuer.js +68 -41
  55. package/dist/esm/queuer.js.map +1 -1
  56. package/dist/esm/rate-limiter.d.ts +1 -2
  57. package/dist/esm/rate-limiter.js +3 -4
  58. package/dist/esm/rate-limiter.js.map +1 -1
  59. package/dist/esm/throttler.d.ts +1 -2
  60. package/dist/esm/throttler.js +3 -4
  61. package/dist/esm/throttler.js.map +1 -1
  62. package/dist/esm/types.d.ts +1 -0
  63. package/package.json +11 -1
  64. package/src/async-debouncer.ts +76 -20
  65. package/src/async-queuer.ts +309 -275
  66. package/src/async-rate-limiter.ts +73 -16
  67. package/src/async-throttler.ts +84 -20
  68. package/src/batcher.ts +253 -0
  69. package/src/debouncer.ts +3 -4
  70. package/src/index.ts +1 -0
  71. package/src/queuer.ts +142 -98
  72. package/src/rate-limiter.ts +3 -4
  73. package/src/throttler.ts +3 -4
  74. package/src/types.ts +3 -0
@@ -1,4 +1,3 @@
1
- import { AnyAsyncFunction } from './types.js';
2
1
  import { QueuePosition } from './queuer.js';
3
2
  export interface AsyncQueuerOptions<TValue> {
4
3
  /**
@@ -21,7 +20,7 @@ export interface AsyncQueuerOptions<TValue> {
21
20
  * Function to determine if an item has expired
22
21
  * If provided, this overrides the expirationDuration behavior
23
22
  */
24
- getIsExpired?: (item: () => Promise<TValue>, addedAt: number) => boolean;
23
+ getIsExpired?: (item: TValue, addedAt: number) => boolean;
25
24
  /**
26
25
  * Default position to get items from during processing
27
26
  * @default 'front'
@@ -32,21 +31,25 @@ export interface AsyncQueuerOptions<TValue> {
32
31
  * Higher priority items will be processed first
33
32
  * If not provided, will use static priority values attached to tasks
34
33
  */
35
- getPriority?: (item: () => Promise<TValue>) => number;
34
+ getPriority?: (item: TValue) => number;
36
35
  /**
37
36
  * Initial items to populate the queuer with
38
37
  */
39
- initialItems?: Array<(() => Promise<TValue>) & {
40
- priority?: number;
41
- }>;
38
+ initialItems?: Array<TValue>;
42
39
  /**
43
40
  * Maximum number of items allowed in the queuer
44
41
  */
45
42
  maxSize?: number;
46
43
  /**
47
- * Callback fired whenever an item is removed from the queuer
44
+ * Optional error handler for when a task throws.
45
+ * If provided, the handler will be called with the error and queuer instance.
46
+ * This can be used alongside throwOnError - the handler will be called before any error is thrown.
47
+ */
48
+ onError?: (error: unknown, queuer: AsyncQueuer<TValue>) => void;
49
+ /**
50
+ * Callback fired whenever an item expires in the queuer
48
51
  */
49
- onGetNextItem?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
52
+ onExpire?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
50
53
  /**
51
54
  * Callback fired whenever the queuer's running state changes
52
55
  */
@@ -58,15 +61,25 @@ export interface AsyncQueuerOptions<TValue> {
58
61
  /**
59
62
  * Callback fired whenever an item is rejected from being added to the queuer
60
63
  */
61
- onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
64
+ onReject?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
62
65
  /**
63
- * Callback fired whenever an item expires in the queuer
66
+ * Optional callback to call when a task is settled
67
+ */
68
+ onSettled?: (queuer: AsyncQueuer<TValue>) => void;
69
+ /**
70
+ * Optional callback to call when a task succeeds
64
71
  */
65
- onExpire?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
72
+ onSuccess?: (result: TValue, queuer: AsyncQueuer<TValue>) => void;
66
73
  /**
67
74
  * Whether the queuer should start processing tasks immediately or not.
68
75
  */
69
76
  started?: boolean;
77
+ /**
78
+ * Whether to throw errors when they occur.
79
+ * Defaults to true if no onError handler is provided, false if an onError handler is provided.
80
+ * Can be explicitly set to override these defaults.
81
+ */
82
+ throwOnError?: boolean;
70
83
  /**
71
84
  * Time in milliseconds to wait between processing items.
72
85
  * Can be a number or a function that returns a number.
@@ -75,178 +88,222 @@ export interface AsyncQueuerOptions<TValue> {
75
88
  wait?: number | ((queuer: AsyncQueuer<TValue>) => number);
76
89
  }
77
90
  /**
78
- * A flexible asynchronous queue that processes tasks with configurable concurrency control.
91
+ * A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
79
92
  *
80
93
  * Features:
81
- * - Priority queue support via getPriority option
94
+ * - Priority queue support via the getPriority option
82
95
  * - Configurable concurrency limit
83
- * - Task success/error/completion callbacks
96
+ * - Callbacks for task success, error, completion, and queue state changes
84
97
  * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
85
- * - Pause/resume task processing
98
+ * - Pause and resume processing
86
99
  * - Task cancellation
87
- * - Item expiration to clear stale items from the queue
100
+ * - Item expiration to remove stale items from the queue
88
101
  *
89
102
  * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
90
- * the next pending task is processed if below the concurrency limit.
103
+ * the next pending task is processed if the concurrency limit allows.
91
104
  *
92
- * @example
93
- * ```ts
94
- * const asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });
105
+ * Error Handling:
106
+ * - If an `onError` handler is provided, it will be called with the error and queuer instance
107
+ * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
108
+ * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
109
+ * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
110
+ * - The error state can be checked using the AsyncQueuer instance
95
111
  *
96
- * asyncQueuer.addItem(async () => {
97
- * return 'Hello';
112
+ * Example usage:
113
+ * ```ts
114
+ * const asyncQueuer = new AsyncQueuer<string>(async (item) => {
115
+ * // process item
116
+ * return item.toUpperCase();
117
+ * }, {
118
+ * concurrency: 2,
119
+ * onSuccess: (result) => {
120
+ * console.log(result);
121
+ * }
98
122
  * });
99
123
  *
124
+ * asyncQueuer.addItem('hello');
100
125
  * asyncQueuer.start();
101
- *
102
- * asyncQueuer.onSuccess((result) => {
103
- * console.log(result); // 'Hello'
104
- * });
105
126
  * ```
106
127
  */
107
128
  export declare class AsyncQueuer<TValue> {
129
+ private fn;
108
130
  private _options;
109
131
  private _activeItems;
110
- private _executionCount;
132
+ private _successCount;
133
+ private _errorCount;
134
+ private _settledCount;
111
135
  private _rejectionCount;
112
136
  private _expirationCount;
113
137
  private _items;
114
138
  private _itemTimestamps;
115
- private _onErrorCallbacks;
116
- private _onSettledCallbacks;
117
- private _onSuccessCallbacks;
118
139
  private _pendingTick;
119
140
  private _running;
120
- constructor(initialOptions?: AsyncQueuerOptions<TValue>);
141
+ private _lastResult;
142
+ constructor(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>);
121
143
  /**
122
- * Updates the queuer options
123
- * Returns the new options state
144
+ * Updates the queuer options. New options are merged with existing options.
124
145
  */
125
146
  setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): void;
126
147
  /**
127
- * Returns the current queuer options
148
+ * Returns the current queuer options, including defaults and any overrides.
128
149
  */
129
- getOptions(): Required<AsyncQueuerOptions<TValue>>;
150
+ getOptions(): AsyncQueuerOptions<TValue>;
130
151
  /**
131
- * Returns the current wait time between processing items
152
+ * Returns the current wait time (in milliseconds) between processing items.
153
+ * If a function is provided, it is called with the queuer instance.
132
154
  */
133
155
  getWait(): number;
134
156
  /**
135
- * Returns the current concurrency limit
157
+ * Returns the current concurrency limit for processing items.
158
+ * If a function is provided, it is called with the queuer instance.
136
159
  */
137
160
  getConcurrency(): number;
138
161
  /**
139
- * Processes items in the queuer
162
+ * Processes items in the queue up to the concurrency limit. Internal use only.
140
163
  */
141
164
  private tick;
142
165
  /**
143
- * Checks for and removes expired items from the queuer
166
+ * Starts processing items in the queue. If already running, does nothing.
144
167
  */
145
- private checkExpiredItems;
168
+ start(): void;
146
169
  /**
147
- * Starts the queuer and processes items
148
- */
149
- start(): Promise<void>;
150
- /**
151
- * Stops the queuer from processing items
170
+ * Stops processing items in the queue. Does not clear the queue.
152
171
  */
153
172
  stop(): void;
154
173
  /**
155
- * Removes all items from the queuer
174
+ * Removes all pending items from the queue. Does not affect active tasks.
156
175
  */
157
176
  clear(): void;
158
177
  /**
159
- * Resets the queuer to its initial state
178
+ * Resets the queuer to its initial state. Optionally repopulates with initial items.
179
+ * Does not affect callbacks or options.
160
180
  */
161
181
  reset(withInitialItems?: boolean): void;
162
182
  /**
163
- * Adds a task to the queuer
183
+ * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
184
+ * Items can be inserted based on priority or at the front/back depending on configuration.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * queuer.addItem({ value: 'task', priority: 10 });
189
+ * queuer.addItem('task2', 'front');
190
+ * ```
164
191
  */
165
- addItem(fn: AnyAsyncFunction & {
192
+ addItem(item: TValue & {
166
193
  priority?: number;
167
- }, position?: QueuePosition, runOnItemsChange?: boolean): Promise<TValue>;
168
- /**
169
- * Removes and returns an item from the queuer
194
+ }, position?: QueuePosition, runOnItemsChange?: boolean): void;
195
+ /**
196
+ * Removes and returns the next item from the queue without executing the task function.
197
+ * Use for manual queue management. Normally, use execute() to process items.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * // FIFO
202
+ * queuer.getNextItem();
203
+ * // LIFO
204
+ * queuer.getNextItem('back');
205
+ * ```
206
+ */
207
+ getNextItem(position?: QueuePosition): TValue | undefined;
208
+ /**
209
+ * Removes and returns the next item from the queue and executes the task function with it.
210
+ *
211
+ * @example
212
+ * ```ts
213
+ * queuer.execute();
214
+ * // LIFO
215
+ * queuer.execute('back');
216
+ * ```
217
+ */
218
+ execute(position?: QueuePosition): Promise<any>;
219
+ /**
220
+ * Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
221
+ * Internal use only.
170
222
  */
171
- getNextItem(position?: QueuePosition): (() => Promise<TValue>) | undefined;
223
+ private checkExpiredItems;
172
224
  /**
173
- * Returns an item without removing it
225
+ * Returns the next item in the queue without removing it.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * queuer.getPeek(); // front
230
+ * queuer.getPeek('back'); // back
231
+ * ```
174
232
  */
175
- getPeek(position?: QueuePosition): (() => Promise<TValue>) | undefined;
233
+ getPeek(position?: QueuePosition): TValue | undefined;
176
234
  /**
177
- * Returns true if the queuer is empty
235
+ * Returns true if the queue is empty (no pending items).
178
236
  */
179
237
  getIsEmpty(): boolean;
180
238
  /**
181
- * Returns true if the queuer is full
239
+ * Returns true if the queue is full (reached maxSize).
182
240
  */
183
241
  getIsFull(): boolean;
184
242
  /**
185
- * Returns the current size of the queuer
243
+ * Returns the number of pending items in the queue.
186
244
  */
187
245
  getSize(): number;
188
246
  /**
189
- * Returns a copy of all items in the queuer
247
+ * Returns a copy of all items in the queue, including active and pending items.
190
248
  */
191
- getAllItems(): Array<() => Promise<TValue>>;
249
+ getAllItems(): Array<TValue>;
192
250
  /**
193
- * Returns the active items
251
+ * Returns the items currently being processed (active tasks).
194
252
  */
195
- getActiveItems(): Array<() => Promise<TValue>>;
253
+ getActiveItems(): Array<TValue>;
196
254
  /**
197
- * Returns the pending items
255
+ * Returns the items waiting to be processed (pending tasks).
198
256
  */
199
- getPendingItems(): Array<() => Promise<TValue>>;
257
+ getPendingItems(): Array<TValue>;
200
258
  /**
201
- * Returns the number of items that have been removed from the queuer
259
+ * Returns the number of items that have been successfully processed.
202
260
  */
203
- getExecutionCount(): number;
261
+ getSuccessCount(): number;
204
262
  /**
205
- * Returns the number of items that have been rejected from the queuer
263
+ * Returns the number of items that have failed processing.
206
264
  */
207
- getRejectionCount(): number;
208
- /**
209
- * Returns true if the queuer is running
210
- */
211
- getIsRunning(): boolean;
265
+ getErrorCount(): number;
212
266
  /**
213
- * Returns true if the queuer is running but has no items to process
267
+ * Returns the number of items that have completed processing (success or error).
214
268
  */
215
- getIsIdle(): boolean;
269
+ getSettledCount(): number;
216
270
  /**
217
- * Adds a callback to be called when a task succeeds
271
+ * Returns the number of items that have been rejected from being added to the queue.
218
272
  */
219
- onSuccess(cb: (result: TValue) => void): () => void;
273
+ getRejectionCount(): number;
220
274
  /**
221
- * Adds a callback to be called when a task errors
275
+ * Returns true if the queuer is currently running (processing items).
222
276
  */
223
- onError(cb: (error: Error) => void): () => void;
277
+ getIsRunning(): boolean;
224
278
  /**
225
- * Adds a callback to be called when a task is settled
279
+ * Returns true if the queuer is running but has no items to process and no active tasks.
226
280
  */
227
- onSettled(cb: (result: TValue | Error) => void): () => void;
281
+ getIsIdle(): boolean;
228
282
  /**
229
- * Returns the number of items that have expired from the queuer
283
+ * Returns the number of items that have expired and been removed from the queue.
230
284
  */
231
285
  getExpirationCount(): number;
232
286
  }
233
287
  /**
234
- * Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.
235
- * The queuer is automatically started and ready to process items.
288
+ * Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
289
+ * The queuer is started automatically and ready to process items.
236
290
  *
237
- * @example
291
+ * Error Handling:
292
+ * - If an `onError` handler is provided, it will be called with the error and queuer instance
293
+ * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
294
+ * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
295
+ * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
296
+ * - The error state can be checked using the underlying AsyncQueuer instance
297
+ *
298
+ * Example usage:
238
299
  * ```ts
239
- * const enqueue = asyncQueue<string>();
300
+ * const enqueue = asyncQueue<string>(async (item) => {
301
+ * return item.toUpperCase();
302
+ * }, {...options});
240
303
  *
241
- * // Add items to be processed
242
- * enqueue(async () => {
243
- * return 'Hello';
244
- * });
304
+ * enqueue('hello');
245
305
  * ```
246
- *
247
- * @param options - Configuration options for the AsyncQueuer
248
- * @returns A bound addItem function that can be used to add tasks to the queuer
249
306
  */
250
- export declare function asyncQueue<TValue>(options: AsyncQueuerOptions<TValue>): (fn: AnyAsyncFunction & {
307
+ export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue & {
251
308
  priority?: number;
252
- }, position?: QueuePosition, runOnItemsChange?: boolean) => Promise<TValue>;
309
+ }, position?: QueuePosition, runOnItemsChange?: boolean) => void;