@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
package/dist/esm/queuer.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Options for configuring a Queuer instance
|
|
2
|
+
* Options for configuring a Queuer instance.
|
|
3
|
+
*
|
|
4
|
+
* These options control queue behavior, item expiration, callbacks, and more.
|
|
3
5
|
*/
|
|
4
6
|
export interface QueuerOptions<TValue> {
|
|
5
7
|
/**
|
|
@@ -42,7 +44,7 @@ export interface QueuerOptions<TValue> {
|
|
|
42
44
|
/**
|
|
43
45
|
* Callback fired whenever an item is removed from the queuer
|
|
44
46
|
*/
|
|
45
|
-
|
|
47
|
+
onExecute?: (item: TValue, queuer: Queuer<TValue>) => void;
|
|
46
48
|
/**
|
|
47
49
|
* Callback fired whenever the queuer's running state changes
|
|
48
50
|
*/
|
|
@@ -67,64 +69,75 @@ export interface QueuerOptions<TValue> {
|
|
|
67
69
|
wait?: number | ((queuer: Queuer<TValue>) => number);
|
|
68
70
|
}
|
|
69
71
|
/**
|
|
70
|
-
* Position type for addItem and getNextItem operations
|
|
72
|
+
* Position type for addItem and getNextItem operations.
|
|
73
|
+
*
|
|
74
|
+
* - 'front': Operate on the front of the queue (FIFO)
|
|
75
|
+
* - 'back': Operate on the back of the queue (LIFO)
|
|
71
76
|
*/
|
|
72
77
|
export type QueuePosition = 'front' | 'back';
|
|
73
78
|
/**
|
|
74
|
-
* A flexible queue
|
|
75
|
-
* with optional position overrides for stack-like or double-ended operations.
|
|
79
|
+
* A flexible queue that processes items with configurable wait times, expiration, and priority.
|
|
76
80
|
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
81
|
+
* Features:
|
|
82
|
+
* - Automatic or manual processing of items
|
|
83
|
+
* - FIFO (First In First Out), LIFO (Last In First Out), or double-ended queue behavior
|
|
84
|
+
* - Priority-based ordering when getPriority is provided
|
|
85
|
+
* - Item expiration and removal of stale items
|
|
86
|
+
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
80
87
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
88
|
+
* Running behavior:
|
|
89
|
+
* - `start()`: Begins automatically processing items in the queue (defaults to running)
|
|
90
|
+
* - `stop()`: Pauses processing but maintains queue state
|
|
91
|
+
* - `wait`: Configurable delay between processing items
|
|
92
|
+
* - `onItemsChange`/`onExecute`: Callbacks for monitoring queue state
|
|
83
93
|
*
|
|
84
|
-
*
|
|
85
|
-
* -
|
|
86
|
-
* - getNextItem()
|
|
94
|
+
* Manual processing is also supported when automatic processing is disabled:
|
|
95
|
+
* - `execute()`: Processes the next item using the provided function
|
|
96
|
+
* - `getNextItem()`: Removes and returns the next item without processing
|
|
87
97
|
*
|
|
88
|
-
*
|
|
89
|
-
* - addItem(item
|
|
90
|
-
* -
|
|
98
|
+
* Queue behavior defaults to FIFO:
|
|
99
|
+
* - `addItem(item)`: Adds to the back of the queue
|
|
100
|
+
* - Items processed from the front of the queue
|
|
91
101
|
*
|
|
92
|
-
*
|
|
93
|
-
* -
|
|
94
|
-
* - getNextItem(position): removes and returns from specified position
|
|
102
|
+
* Priority queue:
|
|
103
|
+
* - Provide a `getPriority` function; higher values are processed first
|
|
95
104
|
*
|
|
96
|
-
*
|
|
97
|
-
* -
|
|
98
|
-
* -
|
|
99
|
-
* - wait: configurable delay between processing items
|
|
100
|
-
* - onItemsChange/onGetNextItem: callbacks for monitoring queuer state
|
|
105
|
+
* Stack (LIFO):
|
|
106
|
+
* - `addItem(item, 'back')`: Adds to the back
|
|
107
|
+
* - `getNextItem('back')`: Removes from the back
|
|
101
108
|
*
|
|
102
|
-
*
|
|
103
|
-
* -
|
|
104
|
-
* -
|
|
105
|
-
* - onExpire: callback for when an item expires
|
|
109
|
+
* Double-ended queue:
|
|
110
|
+
* - `addItem(item, position)`: Adds to specified position ('front'/'back')
|
|
111
|
+
* - `getNextItem(position)`: Removes from specified position
|
|
106
112
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* queuer.addItem(1); // [1]
|
|
112
|
-
* queuer.addItem(2); // [1, 2]
|
|
113
|
-
* queuer.getNextItem(); // returns 1, queuer is [2]
|
|
113
|
+
* Item expiration:
|
|
114
|
+
* - `expirationDuration`: Maximum time items can stay in the queue
|
|
115
|
+
* - `getIsExpired`: Function to override default expiration
|
|
116
|
+
* - `onExpire`: Callback for expired items
|
|
114
117
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
+
* Example usage:
|
|
119
|
+
* ```ts
|
|
120
|
+
* // Auto-processing queue with wait time
|
|
121
|
+
* const autoQueue = new Queuer<number>((n) => console.log(n), {
|
|
118
122
|
* started: true, // Begin processing immediately
|
|
119
123
|
* wait: 1000, // Wait 1s between items
|
|
120
|
-
*
|
|
124
|
+
* onExecute: (item) => console.log(`Processed ${item}`)
|
|
121
125
|
* });
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
126
|
+
* autoQueue.addItem(1); // Will process after 1s
|
|
127
|
+
* autoQueue.addItem(2); // Will process 1s after first item
|
|
128
|
+
*
|
|
129
|
+
* // Manual processing queue
|
|
130
|
+
* const manualQueue = new Queuer<number>((n) => console.log(n), {
|
|
131
|
+
* started: false
|
|
132
|
+
* });
|
|
133
|
+
* manualQueue.addItem(1); // [1]
|
|
134
|
+
* manualQueue.addItem(2); // [1, 2]
|
|
135
|
+
* manualQueue.execute(); // logs 1, queue is [2]
|
|
136
|
+
* manualQueue.getNextItem(); // returns 2, queue is empty
|
|
125
137
|
* ```
|
|
126
138
|
*/
|
|
127
139
|
export declare class Queuer<TValue> {
|
|
140
|
+
private fn;
|
|
128
141
|
private _options;
|
|
129
142
|
private _items;
|
|
130
143
|
private _itemTimestamps;
|
|
@@ -134,136 +147,154 @@ export declare class Queuer<TValue> {
|
|
|
134
147
|
private _onItemsChanges;
|
|
135
148
|
private _running;
|
|
136
149
|
private _pendingTick;
|
|
137
|
-
constructor(initialOptions?: QueuerOptions<TValue>);
|
|
150
|
+
constructor(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>);
|
|
138
151
|
/**
|
|
139
|
-
* Updates the queuer options
|
|
140
|
-
* Returns the new options state
|
|
152
|
+
* Updates the queuer options. New options are merged with existing options.
|
|
141
153
|
*/
|
|
142
154
|
setOptions(newOptions: Partial<QueuerOptions<TValue>>): void;
|
|
143
155
|
/**
|
|
144
|
-
* Returns the current queuer options
|
|
156
|
+
* Returns the current queuer options, including defaults and any overrides.
|
|
145
157
|
*/
|
|
146
158
|
getOptions(): Required<QueuerOptions<TValue>>;
|
|
147
159
|
/**
|
|
148
|
-
* Returns the current wait time in milliseconds
|
|
160
|
+
* Returns the current wait time (in milliseconds) between processing items.
|
|
161
|
+
* If a function is provided, it is called with the queuer instance.
|
|
149
162
|
*/
|
|
150
163
|
getWait(): number;
|
|
151
164
|
/**
|
|
152
|
-
* Processes items in the
|
|
165
|
+
* Processes items in the queue up to the wait interval. Internal use only.
|
|
153
166
|
*/
|
|
154
167
|
private tick;
|
|
155
168
|
/**
|
|
156
|
-
* Checks for and removes
|
|
169
|
+
* Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
|
|
170
|
+
* Internal use only.
|
|
157
171
|
*/
|
|
158
172
|
private checkExpiredItems;
|
|
159
173
|
/**
|
|
160
|
-
* Stops the
|
|
174
|
+
* Stops processing items in the queue. Does not clear the queue.
|
|
161
175
|
*/
|
|
162
176
|
stop(): void;
|
|
163
177
|
/**
|
|
164
|
-
* Starts the
|
|
178
|
+
* Starts processing items in the queue. If already running, does nothing.
|
|
165
179
|
*/
|
|
166
180
|
start(): void;
|
|
167
181
|
/**
|
|
168
|
-
* Removes all items from the
|
|
182
|
+
* Removes all pending items from the queue. Does not affect items being processed.
|
|
169
183
|
*/
|
|
170
184
|
clear(): void;
|
|
171
185
|
/**
|
|
172
|
-
* Resets the queuer to its initial state
|
|
186
|
+
* Resets the queuer to its initial state. Optionally repopulates with initial items.
|
|
187
|
+
* Does not affect callbacks or options.
|
|
173
188
|
*/
|
|
174
189
|
reset(withInitialItems?: boolean): void;
|
|
175
190
|
/**
|
|
176
|
-
* Adds an item to the
|
|
177
|
-
*
|
|
191
|
+
* Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
|
|
192
|
+
* Items can be inserted based on priority or at the front/back depending on configuration.
|
|
193
|
+
*
|
|
194
|
+
* Returns true if the item was added, false if the queue is full.
|
|
195
|
+
*
|
|
196
|
+
* Example usage:
|
|
197
|
+
* ```ts
|
|
198
|
+
* queuer.addItem('task');
|
|
199
|
+
* queuer.addItem('task2', 'front');
|
|
200
|
+
* ```
|
|
178
201
|
*/
|
|
179
202
|
addItem(item: TValue, position?: QueuePosition, runOnUpdate?: boolean): boolean;
|
|
180
203
|
/**
|
|
181
|
-
* Removes and returns
|
|
204
|
+
* Removes and returns the next item from the queue without executing the function.
|
|
205
|
+
* Use for manual queue management. Normally, use execute() to process items.
|
|
182
206
|
*
|
|
183
|
-
*
|
|
207
|
+
* Example usage:
|
|
184
208
|
* ```ts
|
|
185
|
-
* //
|
|
186
|
-
* queuer.getNextItem()
|
|
187
|
-
* //
|
|
188
|
-
* queuer.getNextItem('back')
|
|
209
|
+
* // FIFO
|
|
210
|
+
* queuer.getNextItem();
|
|
211
|
+
* // LIFO
|
|
212
|
+
* queuer.getNextItem('back');
|
|
189
213
|
* ```
|
|
190
214
|
*/
|
|
191
215
|
getNextItem(position?: QueuePosition): TValue | undefined;
|
|
192
216
|
/**
|
|
193
|
-
*
|
|
217
|
+
* Removes and returns the next item from the queue and processes it using the provided function.
|
|
218
|
+
*
|
|
219
|
+
* Example usage:
|
|
220
|
+
* ```ts
|
|
221
|
+
* queuer.execute();
|
|
222
|
+
* // LIFO
|
|
223
|
+
* queuer.execute('back');
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
execute(position?: QueuePosition): TValue | undefined;
|
|
227
|
+
/**
|
|
228
|
+
* Returns the next item in the queue without removing it.
|
|
194
229
|
*
|
|
195
|
-
*
|
|
230
|
+
* Example usage:
|
|
196
231
|
* ```ts
|
|
197
|
-
* //
|
|
198
|
-
* queuer.getPeek()
|
|
199
|
-
* // Look at last item (like stack top)
|
|
200
|
-
* queuer.getPeek('back')
|
|
232
|
+
* queuer.getPeek(); // front
|
|
233
|
+
* queuer.getPeek('back'); // back
|
|
201
234
|
* ```
|
|
202
235
|
*/
|
|
203
236
|
getPeek(position?: QueuePosition): TValue | undefined;
|
|
204
237
|
/**
|
|
205
|
-
* Returns true if the
|
|
238
|
+
* Returns true if the queue is empty (no pending items).
|
|
206
239
|
*/
|
|
207
240
|
getIsEmpty(): boolean;
|
|
208
241
|
/**
|
|
209
|
-
* Returns true if the
|
|
242
|
+
* Returns true if the queue is full (reached maxSize).
|
|
210
243
|
*/
|
|
211
244
|
getIsFull(): boolean;
|
|
212
245
|
/**
|
|
213
|
-
* Returns the
|
|
246
|
+
* Returns the number of pending items in the queue.
|
|
214
247
|
*/
|
|
215
248
|
getSize(): number;
|
|
216
249
|
/**
|
|
217
|
-
* Returns a copy of all items in the
|
|
250
|
+
* Returns a copy of all items in the queue.
|
|
218
251
|
*/
|
|
219
252
|
getAllItems(): Array<TValue>;
|
|
220
253
|
/**
|
|
221
|
-
* Returns the number of items that have been removed from the
|
|
254
|
+
* Returns the number of items that have been processed and removed from the queue.
|
|
222
255
|
*/
|
|
223
256
|
getExecutionCount(): number;
|
|
224
257
|
/**
|
|
225
|
-
* Returns the number of items that have been rejected from the
|
|
258
|
+
* Returns the number of items that have been rejected from being added to the queue.
|
|
226
259
|
*/
|
|
227
260
|
getRejectionCount(): number;
|
|
228
261
|
/**
|
|
229
|
-
* Returns the number of items that have expired from the
|
|
262
|
+
* Returns the number of items that have expired and been removed from the queue.
|
|
230
263
|
*/
|
|
231
264
|
getExpirationCount(): number;
|
|
232
265
|
/**
|
|
233
|
-
* Returns true if the queuer is running
|
|
266
|
+
* Returns true if the queuer is currently running (processing items).
|
|
234
267
|
*/
|
|
235
268
|
getIsRunning(): boolean;
|
|
236
269
|
/**
|
|
237
|
-
* Returns true if the queuer is running but has no items to process
|
|
270
|
+
* Returns true if the queuer is running but has no items to process.
|
|
238
271
|
*/
|
|
239
272
|
getIsIdle(): boolean;
|
|
240
273
|
}
|
|
241
274
|
/**
|
|
242
|
-
* Creates a queue that processes items
|
|
275
|
+
* Creates a queue that processes items immediately upon addition.
|
|
243
276
|
* Items are processed sequentially in FIFO order by default.
|
|
244
277
|
*
|
|
245
278
|
* This is a simplified wrapper around the Queuer class that only exposes the
|
|
246
|
-
* `addItem` method.
|
|
247
|
-
* For more control over
|
|
248
|
-
* directly which provides methods like `start`, `stop`, `reset`, and more.
|
|
279
|
+
* `addItem` method. The queue is always running and will process items as they are added.
|
|
280
|
+
* For more control over queue processing, use the Queuer class directly.
|
|
249
281
|
*
|
|
250
|
-
*
|
|
282
|
+
* Example usage:
|
|
251
283
|
* ```ts
|
|
252
284
|
* // Basic sequential processing
|
|
253
|
-
* const processItems =
|
|
285
|
+
* const processItems = queue<number>((n) => console.log(n), {
|
|
254
286
|
* wait: 1000,
|
|
255
287
|
* onItemsChange: (queuer) => console.log(queuer.getAllItems())
|
|
256
|
-
* })
|
|
257
|
-
* processItems(1) // Logs: 1
|
|
258
|
-
* processItems(2) // Logs: 2 after 1 completes
|
|
288
|
+
* });
|
|
289
|
+
* processItems(1); // Logs: 1
|
|
290
|
+
* processItems(2); // Logs: 2 after 1 completes
|
|
259
291
|
*
|
|
260
|
-
* // Priority
|
|
261
|
-
* const processPriority =
|
|
262
|
-
* process: async (n) => console.log(n),
|
|
292
|
+
* // Priority queue
|
|
293
|
+
* const processPriority = queue<number>((n) => console.log(n), {
|
|
263
294
|
* getPriority: n => n // Higher numbers processed first
|
|
264
|
-
* })
|
|
265
|
-
* processPriority(1)
|
|
266
|
-
* processPriority(3) // Processed before 1
|
|
295
|
+
* });
|
|
296
|
+
* processPriority(1);
|
|
297
|
+
* processPriority(3); // Processed before 1
|
|
267
298
|
* ```
|
|
268
299
|
*/
|
|
269
|
-
export declare function queue<TValue>(options: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnUpdate?: boolean) => boolean;
|
|
300
|
+
export declare function queue<TValue>(fn: (item: TValue) => void, options: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnUpdate?: boolean) => boolean;
|
package/dist/esm/queuer.js
CHANGED
|
@@ -7,7 +7,7 @@ const defaultOptions = {
|
|
|
7
7
|
expirationDuration: Infinity,
|
|
8
8
|
initialItems: [],
|
|
9
9
|
maxSize: Infinity,
|
|
10
|
-
|
|
10
|
+
onExecute: () => {
|
|
11
11
|
},
|
|
12
12
|
onIsRunningChange: () => {
|
|
13
13
|
},
|
|
@@ -21,7 +21,8 @@ const defaultOptions = {
|
|
|
21
21
|
wait: 0
|
|
22
22
|
};
|
|
23
23
|
class Queuer {
|
|
24
|
-
constructor(initialOptions =
|
|
24
|
+
constructor(fn, initialOptions = {}) {
|
|
25
|
+
this.fn = fn;
|
|
25
26
|
this._items = [];
|
|
26
27
|
this._itemTimestamps = [];
|
|
27
28
|
this._executionCount = 0;
|
|
@@ -38,26 +39,26 @@ class Queuer {
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
/**
|
|
41
|
-
* Updates the queuer options
|
|
42
|
-
* Returns the new options state
|
|
42
|
+
* Updates the queuer options. New options are merged with existing options.
|
|
43
43
|
*/
|
|
44
44
|
setOptions(newOptions) {
|
|
45
45
|
this._options = { ...this._options, ...newOptions };
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
* Returns the current queuer options
|
|
48
|
+
* Returns the current queuer options, including defaults and any overrides.
|
|
49
49
|
*/
|
|
50
50
|
getOptions() {
|
|
51
51
|
return this._options;
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* Returns the current wait time in milliseconds
|
|
54
|
+
* Returns the current wait time (in milliseconds) between processing items.
|
|
55
|
+
* If a function is provided, it is called with the queuer instance.
|
|
55
56
|
*/
|
|
56
57
|
getWait() {
|
|
57
58
|
return parseFunctionOrValue(this._options.wait, this);
|
|
58
59
|
}
|
|
59
60
|
/**
|
|
60
|
-
* Processes items in the
|
|
61
|
+
* Processes items in the queue up to the wait interval. Internal use only.
|
|
61
62
|
*/
|
|
62
63
|
tick() {
|
|
63
64
|
if (!this._running) {
|
|
@@ -66,7 +67,7 @@ class Queuer {
|
|
|
66
67
|
}
|
|
67
68
|
this.checkExpiredItems();
|
|
68
69
|
while (!this.getIsEmpty()) {
|
|
69
|
-
const nextItem = this.
|
|
70
|
+
const nextItem = this.execute(this._options.getItemsFrom);
|
|
70
71
|
if (nextItem === void 0) {
|
|
71
72
|
break;
|
|
72
73
|
}
|
|
@@ -81,7 +82,8 @@ class Queuer {
|
|
|
81
82
|
this._pendingTick = false;
|
|
82
83
|
}
|
|
83
84
|
/**
|
|
84
|
-
* Checks for and removes
|
|
85
|
+
* Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
|
|
86
|
+
* Internal use only.
|
|
85
87
|
*/
|
|
86
88
|
checkExpiredItems() {
|
|
87
89
|
if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
|
|
@@ -113,7 +115,7 @@ class Queuer {
|
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
/**
|
|
116
|
-
* Stops the
|
|
118
|
+
* Stops processing items in the queue. Does not clear the queue.
|
|
117
119
|
*/
|
|
118
120
|
stop() {
|
|
119
121
|
this._running = false;
|
|
@@ -121,7 +123,7 @@ class Queuer {
|
|
|
121
123
|
this._options.onIsRunningChange(this);
|
|
122
124
|
}
|
|
123
125
|
/**
|
|
124
|
-
* Starts the
|
|
126
|
+
* Starts processing items in the queue. If already running, does nothing.
|
|
125
127
|
*/
|
|
126
128
|
start() {
|
|
127
129
|
this._running = true;
|
|
@@ -132,14 +134,15 @@ class Queuer {
|
|
|
132
134
|
this._options.onIsRunningChange(this);
|
|
133
135
|
}
|
|
134
136
|
/**
|
|
135
|
-
* Removes all items from the
|
|
137
|
+
* Removes all pending items from the queue. Does not affect items being processed.
|
|
136
138
|
*/
|
|
137
139
|
clear() {
|
|
138
140
|
this._items = [];
|
|
139
141
|
this._options.onItemsChange(this);
|
|
140
142
|
}
|
|
141
143
|
/**
|
|
142
|
-
* Resets the queuer to its initial state
|
|
144
|
+
* Resets the queuer to its initial state. Optionally repopulates with initial items.
|
|
145
|
+
* Does not affect callbacks or options.
|
|
143
146
|
*/
|
|
144
147
|
reset(withInitialItems) {
|
|
145
148
|
this.clear();
|
|
@@ -150,8 +153,16 @@ class Queuer {
|
|
|
150
153
|
this._running = this._options.started;
|
|
151
154
|
}
|
|
152
155
|
/**
|
|
153
|
-
* Adds an item to the
|
|
154
|
-
*
|
|
156
|
+
* Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
|
|
157
|
+
* Items can be inserted based on priority or at the front/back depending on configuration.
|
|
158
|
+
*
|
|
159
|
+
* Returns true if the item was added, false if the queue is full.
|
|
160
|
+
*
|
|
161
|
+
* Example usage:
|
|
162
|
+
* ```ts
|
|
163
|
+
* queuer.addItem('task');
|
|
164
|
+
* queuer.addItem('task2', 'front');
|
|
165
|
+
* ```
|
|
155
166
|
*/
|
|
156
167
|
addItem(item, position = this._options.addItemsTo, runOnUpdate = true) {
|
|
157
168
|
if (this.getIsFull()) {
|
|
@@ -162,7 +173,7 @@ class Queuer {
|
|
|
162
173
|
if (this._options.getPriority !== defaultOptions.getPriority) {
|
|
163
174
|
const priority = this._options.getPriority(item);
|
|
164
175
|
const insertIndex = this._items.findIndex(
|
|
165
|
-
(existing) => this._options.getPriority(existing)
|
|
176
|
+
(existing) => this._options.getPriority(existing) < priority
|
|
166
177
|
);
|
|
167
178
|
if (insertIndex === -1) {
|
|
168
179
|
this._items.push(item);
|
|
@@ -190,14 +201,15 @@ class Queuer {
|
|
|
190
201
|
return true;
|
|
191
202
|
}
|
|
192
203
|
/**
|
|
193
|
-
* Removes and returns
|
|
204
|
+
* Removes and returns the next item from the queue without executing the function.
|
|
205
|
+
* Use for manual queue management. Normally, use execute() to process items.
|
|
194
206
|
*
|
|
195
|
-
*
|
|
207
|
+
* Example usage:
|
|
196
208
|
* ```ts
|
|
197
|
-
* //
|
|
198
|
-
* queuer.getNextItem()
|
|
199
|
-
* //
|
|
200
|
-
* queuer.getNextItem('back')
|
|
209
|
+
* // FIFO
|
|
210
|
+
* queuer.getNextItem();
|
|
211
|
+
* // LIFO
|
|
212
|
+
* queuer.getNextItem('back');
|
|
201
213
|
* ```
|
|
202
214
|
*/
|
|
203
215
|
getNextItem(position = this._options.getItemsFrom) {
|
|
@@ -210,21 +222,36 @@ class Queuer {
|
|
|
210
222
|
this._itemTimestamps.pop();
|
|
211
223
|
}
|
|
212
224
|
if (item !== void 0) {
|
|
213
|
-
this._executionCount++;
|
|
214
225
|
this._options.onItemsChange(this);
|
|
215
|
-
this._options.onGetNextItem(item, this);
|
|
216
226
|
}
|
|
217
227
|
return item;
|
|
218
228
|
}
|
|
219
229
|
/**
|
|
220
|
-
*
|
|
230
|
+
* Removes and returns the next item from the queue and processes it using the provided function.
|
|
231
|
+
*
|
|
232
|
+
* Example usage:
|
|
233
|
+
* ```ts
|
|
234
|
+
* queuer.execute();
|
|
235
|
+
* // LIFO
|
|
236
|
+
* queuer.execute('back');
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
execute(position) {
|
|
240
|
+
const item = this.getNextItem(position);
|
|
241
|
+
if (item !== void 0) {
|
|
242
|
+
this.fn(item);
|
|
243
|
+
this._executionCount++;
|
|
244
|
+
this._options.onExecute(item, this);
|
|
245
|
+
}
|
|
246
|
+
return item;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Returns the next item in the queue without removing it.
|
|
221
250
|
*
|
|
222
|
-
*
|
|
251
|
+
* Example usage:
|
|
223
252
|
* ```ts
|
|
224
|
-
* //
|
|
225
|
-
* queuer.getPeek()
|
|
226
|
-
* // Look at last item (like stack top)
|
|
227
|
-
* queuer.getPeek('back')
|
|
253
|
+
* queuer.getPeek(); // front
|
|
254
|
+
* queuer.getPeek('back'); // back
|
|
228
255
|
* ```
|
|
229
256
|
*/
|
|
230
257
|
getPeek(position = this._options.getItemsFrom) {
|
|
@@ -234,62 +261,62 @@ class Queuer {
|
|
|
234
261
|
return this._items[this._items.length - 1];
|
|
235
262
|
}
|
|
236
263
|
/**
|
|
237
|
-
* Returns true if the
|
|
264
|
+
* Returns true if the queue is empty (no pending items).
|
|
238
265
|
*/
|
|
239
266
|
getIsEmpty() {
|
|
240
267
|
return this._items.length === 0;
|
|
241
268
|
}
|
|
242
269
|
/**
|
|
243
|
-
* Returns true if the
|
|
270
|
+
* Returns true if the queue is full (reached maxSize).
|
|
244
271
|
*/
|
|
245
272
|
getIsFull() {
|
|
246
273
|
return this._items.length >= this._options.maxSize;
|
|
247
274
|
}
|
|
248
275
|
/**
|
|
249
|
-
* Returns the
|
|
276
|
+
* Returns the number of pending items in the queue.
|
|
250
277
|
*/
|
|
251
278
|
getSize() {
|
|
252
279
|
return this._items.length;
|
|
253
280
|
}
|
|
254
281
|
/**
|
|
255
|
-
* Returns a copy of all items in the
|
|
282
|
+
* Returns a copy of all items in the queue.
|
|
256
283
|
*/
|
|
257
284
|
getAllItems() {
|
|
258
285
|
return [...this._items];
|
|
259
286
|
}
|
|
260
287
|
/**
|
|
261
|
-
* Returns the number of items that have been removed from the
|
|
288
|
+
* Returns the number of items that have been processed and removed from the queue.
|
|
262
289
|
*/
|
|
263
290
|
getExecutionCount() {
|
|
264
291
|
return this._executionCount;
|
|
265
292
|
}
|
|
266
293
|
/**
|
|
267
|
-
* Returns the number of items that have been rejected from the
|
|
294
|
+
* Returns the number of items that have been rejected from being added to the queue.
|
|
268
295
|
*/
|
|
269
296
|
getRejectionCount() {
|
|
270
297
|
return this._rejectionCount;
|
|
271
298
|
}
|
|
272
299
|
/**
|
|
273
|
-
* Returns the number of items that have expired from the
|
|
300
|
+
* Returns the number of items that have expired and been removed from the queue.
|
|
274
301
|
*/
|
|
275
302
|
getExpirationCount() {
|
|
276
303
|
return this._expirationCount;
|
|
277
304
|
}
|
|
278
305
|
/**
|
|
279
|
-
* Returns true if the queuer is running
|
|
306
|
+
* Returns true if the queuer is currently running (processing items).
|
|
280
307
|
*/
|
|
281
308
|
getIsRunning() {
|
|
282
309
|
return this._running;
|
|
283
310
|
}
|
|
284
311
|
/**
|
|
285
|
-
* Returns true if the queuer is running but has no items to process
|
|
312
|
+
* Returns true if the queuer is running but has no items to process.
|
|
286
313
|
*/
|
|
287
314
|
getIsIdle() {
|
|
288
315
|
return this._running && this.getIsEmpty();
|
|
289
316
|
}
|
|
290
317
|
}
|
|
291
|
-
function queue(options) {
|
|
292
|
-
const queuer = new Queuer(options);
|
|
318
|
+
function queue(fn, options) {
|
|
319
|
+
const queuer = new Queuer(fn, options);
|
|
293
320
|
return queuer.addItem.bind(queuer);
|
|
294
321
|
}
|
|
295
322
|
export {
|