@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.
- package/dist/cjs/async-debouncer.cjs +32 -17
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +51 -9
- package/dist/cjs/async-queuer.cjs +189 -191
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +151 -94
- package/dist/cjs/async-rate-limiter.cjs +23 -13
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +54 -5
- package/dist/cjs/async-throttler.cjs +38 -17
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +51 -8
- package/dist/cjs/batcher.cjs +138 -0
- package/dist/cjs/batcher.cjs.map +1 -0
- package/dist/cjs/batcher.d.cts +149 -0
- package/dist/cjs/debouncer.cjs +3 -4
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +1 -2
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +68 -41
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +123 -92
- package/dist/cjs/rate-limiter.cjs +3 -4
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +1 -2
- package/dist/cjs/throttler.cjs +3 -4
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +1 -2
- package/dist/cjs/types.d.cts +1 -0
- package/dist/esm/async-debouncer.d.ts +51 -9
- package/dist/esm/async-debouncer.js +32 -17
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +151 -94
- package/dist/esm/async-queuer.js +189 -191
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +54 -5
- package/dist/esm/async-rate-limiter.js +23 -13
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +51 -8
- package/dist/esm/async-throttler.js +38 -17
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +149 -0
- package/dist/esm/batcher.js +138 -0
- package/dist/esm/batcher.js.map +1 -0
- package/dist/esm/debouncer.d.ts +1 -2
- package/dist/esm/debouncer.js +3 -4
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +123 -92
- package/dist/esm/queuer.js +68 -41
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +1 -2
- package/dist/esm/rate-limiter.js +3 -4
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +1 -2
- package/dist/esm/throttler.js +3 -4
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +1 -0
- package/package.json +11 -1
- package/src/async-debouncer.ts +76 -20
- package/src/async-queuer.ts +309 -275
- package/src/async-rate-limiter.ts +73 -16
- package/src/async-throttler.ts +84 -20
- package/src/batcher.ts +253 -0
- package/src/debouncer.ts +3 -4
- package/src/index.ts +1 -0
- package/src/queuer.ts +142 -98
- package/src/rate-limiter.ts +3 -4
- package/src/throttler.ts +3 -4
- 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:
|
|
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:
|
|
34
|
+
getPriority?: (item: TValue) => number;
|
|
36
35
|
/**
|
|
37
36
|
* Initial items to populate the queuer with
|
|
38
37
|
*/
|
|
39
|
-
initialItems?: Array<
|
|
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
|
-
*
|
|
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
|
-
|
|
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:
|
|
64
|
+
onReject?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
|
|
62
65
|
/**
|
|
63
|
-
*
|
|
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
|
-
|
|
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
|
|
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
|
-
* -
|
|
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
|
|
98
|
+
* - Pause and resume processing
|
|
86
99
|
* - Task cancellation
|
|
87
|
-
* - Item expiration to
|
|
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
|
|
103
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
91
104
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
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
|
-
*
|
|
97
|
-
*
|
|
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
|
|
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
|
-
|
|
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():
|
|
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
|
|
162
|
+
* Processes items in the queue up to the concurrency limit. Internal use only.
|
|
140
163
|
*/
|
|
141
164
|
private tick;
|
|
142
165
|
/**
|
|
143
|
-
*
|
|
166
|
+
* Starts processing items in the queue. If already running, does nothing.
|
|
144
167
|
*/
|
|
145
|
-
|
|
168
|
+
start(): void;
|
|
146
169
|
/**
|
|
147
|
-
*
|
|
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
|
|
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
|
|
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(
|
|
192
|
+
addItem(item: TValue & {
|
|
166
193
|
priority?: number;
|
|
167
|
-
}, position?: QueuePosition, runOnItemsChange?: boolean):
|
|
168
|
-
/**
|
|
169
|
-
* Removes and returns
|
|
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
|
-
|
|
223
|
+
private checkExpiredItems;
|
|
172
224
|
/**
|
|
173
|
-
* Returns
|
|
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):
|
|
233
|
+
getPeek(position?: QueuePosition): TValue | undefined;
|
|
176
234
|
/**
|
|
177
|
-
* Returns true if the
|
|
235
|
+
* Returns true if the queue is empty (no pending items).
|
|
178
236
|
*/
|
|
179
237
|
getIsEmpty(): boolean;
|
|
180
238
|
/**
|
|
181
|
-
* Returns true if the
|
|
239
|
+
* Returns true if the queue is full (reached maxSize).
|
|
182
240
|
*/
|
|
183
241
|
getIsFull(): boolean;
|
|
184
242
|
/**
|
|
185
|
-
* Returns the
|
|
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
|
|
247
|
+
* Returns a copy of all items in the queue, including active and pending items.
|
|
190
248
|
*/
|
|
191
|
-
getAllItems(): Array<
|
|
249
|
+
getAllItems(): Array<TValue>;
|
|
192
250
|
/**
|
|
193
|
-
* Returns the active
|
|
251
|
+
* Returns the items currently being processed (active tasks).
|
|
194
252
|
*/
|
|
195
|
-
getActiveItems(): Array<
|
|
253
|
+
getActiveItems(): Array<TValue>;
|
|
196
254
|
/**
|
|
197
|
-
* Returns the pending
|
|
255
|
+
* Returns the items waiting to be processed (pending tasks).
|
|
198
256
|
*/
|
|
199
|
-
getPendingItems(): Array<
|
|
257
|
+
getPendingItems(): Array<TValue>;
|
|
200
258
|
/**
|
|
201
|
-
* Returns the number of items that have been
|
|
259
|
+
* Returns the number of items that have been successfully processed.
|
|
202
260
|
*/
|
|
203
|
-
|
|
261
|
+
getSuccessCount(): number;
|
|
204
262
|
/**
|
|
205
|
-
* Returns the number of items that have
|
|
263
|
+
* Returns the number of items that have failed processing.
|
|
206
264
|
*/
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Returns true if the queuer is running
|
|
210
|
-
*/
|
|
211
|
-
getIsRunning(): boolean;
|
|
265
|
+
getErrorCount(): number;
|
|
212
266
|
/**
|
|
213
|
-
* Returns
|
|
267
|
+
* Returns the number of items that have completed processing (success or error).
|
|
214
268
|
*/
|
|
215
|
-
|
|
269
|
+
getSettledCount(): number;
|
|
216
270
|
/**
|
|
217
|
-
*
|
|
271
|
+
* Returns the number of items that have been rejected from being added to the queue.
|
|
218
272
|
*/
|
|
219
|
-
|
|
273
|
+
getRejectionCount(): number;
|
|
220
274
|
/**
|
|
221
|
-
*
|
|
275
|
+
* Returns true if the queuer is currently running (processing items).
|
|
222
276
|
*/
|
|
223
|
-
|
|
277
|
+
getIsRunning(): boolean;
|
|
224
278
|
/**
|
|
225
|
-
*
|
|
279
|
+
* Returns true if the queuer is running but has no items to process and no active tasks.
|
|
226
280
|
*/
|
|
227
|
-
|
|
281
|
+
getIsIdle(): boolean;
|
|
228
282
|
/**
|
|
229
|
-
* Returns the number of items that have expired from the
|
|
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
|
|
235
|
-
* The queuer is automatically
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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>(
|
|
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) =>
|
|
309
|
+
}, position?: QueuePosition, runOnItemsChange?: boolean) => void;
|