@tanstack/pacer 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-batcher.cjs +163 -0
- package/dist/cjs/async-batcher.cjs.map +1 -0
- package/dist/cjs/async-batcher.d.cts +273 -0
- package/dist/cjs/async-debouncer.cjs +149 -162
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +76 -57
- package/dist/cjs/async-queuer.cjs +282 -343
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +121 -100
- package/dist/cjs/async-rate-limiter.cjs +128 -185
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +72 -61
- package/dist/cjs/async-throttler.cjs +168 -178
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +97 -69
- package/dist/cjs/batcher.cjs +110 -119
- package/dist/cjs/batcher.cjs.map +1 -1
- package/dist/cjs/batcher.d.cts +76 -51
- package/dist/cjs/debouncer.cjs +97 -85
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +54 -26
- package/dist/cjs/index.cjs +3 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/queuer.cjs +247 -294
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +102 -81
- package/dist/cjs/rate-limiter.cjs +97 -130
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +50 -37
- package/dist/cjs/throttler.cjs +107 -123
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +59 -35
- package/dist/cjs/utils.cjs +0 -13
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +0 -1
- package/dist/esm/async-batcher.d.ts +273 -0
- package/dist/esm/async-batcher.js +163 -0
- package/dist/esm/async-batcher.js.map +1 -0
- package/dist/esm/async-debouncer.d.ts +76 -57
- package/dist/esm/async-debouncer.js +149 -162
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +121 -100
- package/dist/esm/async-queuer.js +282 -343
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +72 -61
- package/dist/esm/async-rate-limiter.js +128 -185
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +97 -69
- package/dist/esm/async-throttler.js +168 -178
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +76 -51
- package/dist/esm/batcher.js +110 -119
- package/dist/esm/batcher.js.map +1 -1
- package/dist/esm/debouncer.d.ts +54 -26
- package/dist/esm/debouncer.js +97 -85
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -7
- package/dist/esm/queuer.d.ts +102 -81
- package/dist/esm/queuer.js +247 -294
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +50 -37
- package/dist/esm/rate-limiter.js +97 -130
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +59 -35
- package/dist/esm/throttler.js +107 -123
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/utils.d.ts +0 -1
- package/dist/esm/utils.js +0 -13
- package/dist/esm/utils.js.map +1 -1
- package/package.json +14 -11
- package/src/async-batcher.ts +475 -0
- package/src/async-debouncer.ts +201 -121
- package/src/async-queuer.ts +337 -216
- package/src/async-rate-limiter.ts +176 -136
- package/src/async-throttler.ts +233 -139
- package/src/batcher.ts +158 -92
- package/src/debouncer.ts +135 -52
- package/src/index.ts +1 -1
- package/src/queuer.ts +349 -226
- package/src/rate-limiter.ts +125 -80
- package/src/throttler.ts +152 -78
- package/src/utils.ts +0 -15
- package/dist/cjs/compare.cjs +0 -72
- package/dist/cjs/compare.cjs.map +0 -1
- package/dist/cjs/compare.d.cts +0 -12
- package/dist/esm/compare.d.ts +0 -12
- package/dist/esm/compare.js +0 -72
- package/dist/esm/compare.js.map +0 -1
- 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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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>>)
|
|
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)
|
|
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)
|
|
252
|
+
execute: (position?: QueuePosition) => Promise<any>;
|
|
219
253
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
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
|
-
|
|
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)
|
|
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()
|
|
271
|
+
peekAllItems: () => Array<TValue>;
|
|
250
272
|
/**
|
|
251
273
|
* Returns the items currently being processed (active tasks).
|
|
252
274
|
*/
|
|
253
|
-
peekActiveItems()
|
|
275
|
+
peekActiveItems: () => Array<TValue>;
|
|
254
276
|
/**
|
|
255
277
|
* Returns the items waiting to be processed (pending tasks).
|
|
256
278
|
*/
|
|
257
|
-
peekPendingItems()
|
|
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
|
-
*
|
|
281
|
+
* Starts processing items in the queue. If already running, does nothing.
|
|
272
282
|
*/
|
|
273
|
-
|
|
283
|
+
start: () => void;
|
|
274
284
|
/**
|
|
275
|
-
*
|
|
285
|
+
* Stops processing items in the queue. Does not clear the queue.
|
|
276
286
|
*/
|
|
277
|
-
|
|
287
|
+
stop: () => void;
|
|
278
288
|
/**
|
|
279
|
-
*
|
|
289
|
+
* Removes all pending items from the queue. Does not affect active tasks.
|
|
280
290
|
*/
|
|
281
|
-
|
|
291
|
+
clear: () => void;
|
|
282
292
|
/**
|
|
283
|
-
*
|
|
293
|
+
* Resets the queuer state to its default values
|
|
284
294
|
*/
|
|
285
|
-
|
|
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;
|