@tanstack/pacer 0.4.0 → 0.6.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 +39 -15
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +63 -11
- package/dist/cjs/async-queuer.cjs +102 -119
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +94 -52
- package/dist/cjs/async-rate-limiter.cjs +48 -16
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +74 -9
- package/dist/cjs/async-throttler.cjs +42 -17
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +62 -10
- package/dist/cjs/debouncer.cjs +16 -3
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +14 -4
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/queuer.cjs +13 -5
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +9 -3
- package/dist/cjs/rate-limiter.cjs +26 -7
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +20 -6
- package/dist/cjs/throttler.cjs +19 -5
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +15 -4
- package/dist/cjs/types.d.cts +1 -0
- package/dist/cjs/utils.cjs +18 -7
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +3 -0
- package/dist/esm/async-debouncer.d.ts +63 -11
- package/dist/esm/async-debouncer.js +39 -15
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +94 -52
- package/dist/esm/async-queuer.js +102 -119
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +74 -9
- package/dist/esm/async-rate-limiter.js +48 -16
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +62 -10
- package/dist/esm/async-throttler.js +42 -17
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +14 -4
- package/dist/esm/debouncer.js +16 -3
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/queuer.d.ts +9 -3
- package/dist/esm/queuer.js +13 -5
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +20 -6
- package/dist/esm/rate-limiter.js +26 -7
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +15 -4
- package/dist/esm/throttler.js +19 -5
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/utils.d.ts +3 -0
- package/dist/esm/utils.js +19 -8
- package/dist/esm/utils.js.map +1 -1
- package/package.json +9 -3
- package/src/async-debouncer.ts +89 -22
- package/src/async-queuer.ts +205 -175
- package/src/async-rate-limiter.ts +111 -25
- package/src/async-throttler.ts +92 -24
- package/src/debouncer.ts +24 -7
- package/src/queuer.ts +19 -7
- package/src/rate-limiter.ts +37 -13
- package/src/throttler.ts +28 -9
- package/src/types.ts +3 -0
- package/src/utils.ts +19 -5
package/dist/esm/async-queuer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseFunctionOrValue } from "./utils.js";
|
|
1
2
|
const defaultOptions = {
|
|
2
3
|
addItemsTo: "back",
|
|
3
4
|
concurrency: 1,
|
|
@@ -7,32 +8,25 @@ const defaultOptions = {
|
|
|
7
8
|
getPriority: (item) => (item == null ? void 0 : item.priority) ?? 0,
|
|
8
9
|
initialItems: [],
|
|
9
10
|
maxSize: Infinity,
|
|
10
|
-
onGetNextItem: () => {
|
|
11
|
-
},
|
|
12
|
-
onIsRunningChange: () => {
|
|
13
|
-
},
|
|
14
|
-
onItemsChange: () => {
|
|
15
|
-
},
|
|
16
|
-
onReject: () => {
|
|
17
|
-
},
|
|
18
|
-
onExpire: () => {
|
|
19
|
-
},
|
|
20
11
|
started: true,
|
|
21
12
|
wait: 0
|
|
22
13
|
};
|
|
23
14
|
class AsyncQueuer {
|
|
24
15
|
constructor(initialOptions = defaultOptions) {
|
|
25
16
|
this._activeItems = /* @__PURE__ */ new Set();
|
|
26
|
-
this.
|
|
17
|
+
this._successCount = 0;
|
|
18
|
+
this._errorCount = 0;
|
|
19
|
+
this._settledCount = 0;
|
|
27
20
|
this._rejectionCount = 0;
|
|
28
21
|
this._expirationCount = 0;
|
|
29
22
|
this._items = [];
|
|
30
23
|
this._itemTimestamps = [];
|
|
31
|
-
this._onErrorCallbacks = [];
|
|
32
|
-
this._onSettledCallbacks = [];
|
|
33
|
-
this._onSuccessCallbacks = [];
|
|
34
24
|
this._pendingTick = false;
|
|
35
|
-
this._options = {
|
|
25
|
+
this._options = {
|
|
26
|
+
...defaultOptions,
|
|
27
|
+
...initialOptions,
|
|
28
|
+
throwOnError: initialOptions.throwOnError ?? !initialOptions.onError
|
|
29
|
+
};
|
|
36
30
|
this._running = this._options.started;
|
|
37
31
|
for (let i = 0; i < this._options.initialItems.length; i++) {
|
|
38
32
|
const item = this._options.initialItems[i];
|
|
@@ -53,43 +47,59 @@ class AsyncQueuer {
|
|
|
53
47
|
getOptions() {
|
|
54
48
|
return this._options;
|
|
55
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the current wait time between processing items
|
|
52
|
+
*/
|
|
53
|
+
getWait() {
|
|
54
|
+
return parseFunctionOrValue(this._options.wait, this);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the current concurrency limit
|
|
58
|
+
*/
|
|
59
|
+
getConcurrency() {
|
|
60
|
+
return parseFunctionOrValue(this._options.concurrency, this);
|
|
61
|
+
}
|
|
56
62
|
/**
|
|
57
63
|
* Processes items in the queuer
|
|
58
64
|
*/
|
|
59
65
|
tick() {
|
|
66
|
+
var _a, _b;
|
|
60
67
|
if (!this._running) {
|
|
61
68
|
this._pendingTick = false;
|
|
62
69
|
return;
|
|
63
70
|
}
|
|
64
71
|
this.checkExpiredItems();
|
|
65
|
-
while (this._activeItems.size < this.
|
|
72
|
+
while (this._activeItems.size < this.getConcurrency() && !this.getIsEmpty()) {
|
|
66
73
|
const nextFn = this.getNextItem();
|
|
67
74
|
if (!nextFn) {
|
|
68
75
|
break;
|
|
69
76
|
}
|
|
70
77
|
this._activeItems.add(nextFn);
|
|
71
|
-
this._options.onItemsChange(this);
|
|
78
|
+
(_b = (_a = this._options).onItemsChange) == null ? void 0 : _b.call(_a, this);
|
|
72
79
|
(async () => {
|
|
73
|
-
|
|
80
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h;
|
|
74
81
|
let res;
|
|
75
|
-
let error;
|
|
76
82
|
try {
|
|
77
83
|
res = await nextFn();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
this._successCount++;
|
|
85
|
+
(_b2 = (_a2 = this._options).onSuccess) == null ? void 0 : _b2.call(_a2, res, this);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
this._errorCount++;
|
|
88
|
+
(_d = (_c = this._options).onError) == null ? void 0 : _d.call(_c, error, this);
|
|
89
|
+
if (this._options.throwOnError) {
|
|
90
|
+
throw error;
|
|
91
|
+
} else {
|
|
92
|
+
console.error(error);
|
|
93
|
+
}
|
|
81
94
|
} finally {
|
|
95
|
+
this._settledCount++;
|
|
82
96
|
this._activeItems.delete(nextFn);
|
|
83
|
-
this._options.onItemsChange(this);
|
|
97
|
+
(_f = (_e = this._options).onItemsChange) == null ? void 0 : _f.call(_e, this);
|
|
98
|
+
(_h = (_g = this._options).onSettled) == null ? void 0 : _h.call(_g, this);
|
|
84
99
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this._onErrorCallbacks.forEach((cb) => cb(error));
|
|
89
|
-
}
|
|
90
|
-
this._onSettledCallbacks.forEach((cb) => cb(success ? res : error));
|
|
91
|
-
if (this._options.wait > 0) {
|
|
92
|
-
setTimeout(() => this.tick(), this._options.wait);
|
|
100
|
+
const wait = this.getWait();
|
|
101
|
+
if (wait > 0) {
|
|
102
|
+
setTimeout(() => this.tick(), wait);
|
|
93
103
|
return;
|
|
94
104
|
}
|
|
95
105
|
this.tick();
|
|
@@ -101,6 +111,7 @@ class AsyncQueuer {
|
|
|
101
111
|
* Checks for and removes expired items from the queuer
|
|
102
112
|
*/
|
|
103
113
|
checkExpiredItems() {
|
|
114
|
+
var _a, _b, _c, _d;
|
|
104
115
|
if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
|
|
105
116
|
return;
|
|
106
117
|
const now = Date.now();
|
|
@@ -123,22 +134,23 @@ class AsyncQueuer {
|
|
|
123
134
|
this._items.splice(index, 1);
|
|
124
135
|
this._itemTimestamps.splice(index, 1);
|
|
125
136
|
this._expirationCount++;
|
|
126
|
-
this._options.onExpire(expiredItem, this);
|
|
137
|
+
(_b = (_a = this._options).onExpire) == null ? void 0 : _b.call(_a, expiredItem, this);
|
|
127
138
|
}
|
|
128
139
|
if (expiredIndices.length > 0) {
|
|
129
|
-
this._options.onItemsChange(this);
|
|
140
|
+
(_d = (_c = this._options).onItemsChange) == null ? void 0 : _d.call(_c, this);
|
|
130
141
|
}
|
|
131
142
|
}
|
|
132
143
|
/**
|
|
133
144
|
* Starts the queuer and processes items
|
|
134
145
|
*/
|
|
135
146
|
start() {
|
|
147
|
+
var _a, _b;
|
|
136
148
|
this._running = true;
|
|
137
149
|
if (!this._pendingTick && !this.getIsEmpty()) {
|
|
138
150
|
this._pendingTick = true;
|
|
139
151
|
this.tick();
|
|
140
152
|
}
|
|
141
|
-
this._options.onIsRunningChange(this);
|
|
153
|
+
(_b = (_a = this._options).onIsRunningChange) == null ? void 0 : _b.call(_a, this);
|
|
142
154
|
return new Promise((resolve) => {
|
|
143
155
|
const checkIdle = () => {
|
|
144
156
|
if (this.getIsIdle()) {
|
|
@@ -154,23 +166,27 @@ class AsyncQueuer {
|
|
|
154
166
|
* Stops the queuer from processing items
|
|
155
167
|
*/
|
|
156
168
|
stop() {
|
|
169
|
+
var _a, _b;
|
|
157
170
|
this._running = false;
|
|
158
171
|
this._pendingTick = false;
|
|
159
|
-
this._options.onIsRunningChange(this);
|
|
172
|
+
(_b = (_a = this._options).onIsRunningChange) == null ? void 0 : _b.call(_a, this);
|
|
160
173
|
}
|
|
161
174
|
/**
|
|
162
175
|
* Removes all items from the queuer
|
|
163
176
|
*/
|
|
164
177
|
clear() {
|
|
178
|
+
var _a, _b;
|
|
165
179
|
this._items = [];
|
|
166
|
-
this._options.onItemsChange(this);
|
|
180
|
+
(_b = (_a = this._options).onItemsChange) == null ? void 0 : _b.call(_a, this);
|
|
167
181
|
}
|
|
168
182
|
/**
|
|
169
183
|
* Resets the queuer to its initial state
|
|
170
184
|
*/
|
|
171
185
|
reset(withInitialItems) {
|
|
172
186
|
this.clear();
|
|
173
|
-
this.
|
|
187
|
+
this._successCount = 0;
|
|
188
|
+
this._errorCount = 0;
|
|
189
|
+
this._settledCount = 0;
|
|
174
190
|
if (withInitialItems) {
|
|
175
191
|
this._items = [...this._options.initialItems];
|
|
176
192
|
}
|
|
@@ -179,61 +195,48 @@ class AsyncQueuer {
|
|
|
179
195
|
/**
|
|
180
196
|
* Adds a task to the queuer
|
|
181
197
|
*/
|
|
182
|
-
addItem(fn, position = this._options.addItemsTo,
|
|
198
|
+
addItem(fn, position = this._options.addItemsTo, runOnItemsChange = true) {
|
|
199
|
+
var _a, _b, _c, _d;
|
|
183
200
|
if (this.getIsFull()) {
|
|
184
201
|
this._rejectionCount++;
|
|
185
|
-
this._options.onReject(fn, this);
|
|
186
|
-
return
|
|
202
|
+
(_b = (_a = this._options).onReject) == null ? void 0 : _b.call(_a, fn, this);
|
|
203
|
+
return;
|
|
187
204
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
throw error;
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
{ priority: fn.priority ?? void 0 }
|
|
201
|
-
);
|
|
202
|
-
const priority = this._options.getPriority !== defaultOptions.getPriority ? this._options.getPriority(task) : task.priority;
|
|
203
|
-
if (priority !== void 0) {
|
|
204
|
-
const insertIndex = this._items.findIndex((existing) => {
|
|
205
|
-
const existingPriority = this._options.getPriority !== defaultOptions.getPriority ? this._options.getPriority(existing) : existing.priority;
|
|
206
|
-
return existingPriority > priority;
|
|
207
|
-
});
|
|
208
|
-
if (insertIndex === -1) {
|
|
209
|
-
this._items.push(task);
|
|
210
|
-
this._itemTimestamps.push(Date.now());
|
|
211
|
-
} else {
|
|
212
|
-
this._items.splice(insertIndex, 0, task);
|
|
213
|
-
this._itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
214
|
-
}
|
|
205
|
+
const priority = this._options.getPriority !== defaultOptions.getPriority ? this._options.getPriority(fn) : fn.priority;
|
|
206
|
+
if (priority !== void 0) {
|
|
207
|
+
const insertIndex = this._items.findIndex((existing) => {
|
|
208
|
+
const existingPriority = this._options.getPriority !== defaultOptions.getPriority ? this._options.getPriority(existing) : existing.priority;
|
|
209
|
+
return existingPriority > priority;
|
|
210
|
+
});
|
|
211
|
+
if (insertIndex === -1) {
|
|
212
|
+
this._items.push(fn);
|
|
213
|
+
this._itemTimestamps.push(Date.now());
|
|
215
214
|
} else {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
this._itemTimestamps.unshift(Date.now());
|
|
219
|
-
} else {
|
|
220
|
-
this._items.push(task);
|
|
221
|
-
this._itemTimestamps.push(Date.now());
|
|
222
|
-
}
|
|
215
|
+
this._items.splice(insertIndex, 0, fn);
|
|
216
|
+
this._itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
223
217
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
this.
|
|
218
|
+
} else {
|
|
219
|
+
if (position === "front") {
|
|
220
|
+
this._items.unshift(fn);
|
|
221
|
+
this._itemTimestamps.unshift(Date.now());
|
|
222
|
+
} else {
|
|
223
|
+
this._items.push(fn);
|
|
224
|
+
this._itemTimestamps.push(Date.now());
|
|
230
225
|
}
|
|
231
|
-
}
|
|
226
|
+
}
|
|
227
|
+
if (runOnItemsChange) {
|
|
228
|
+
(_d = (_c = this._options).onItemsChange) == null ? void 0 : _d.call(_c, this);
|
|
229
|
+
}
|
|
230
|
+
if (this._running && !this._pendingTick) {
|
|
231
|
+
this._pendingTick = true;
|
|
232
|
+
this.tick();
|
|
233
|
+
}
|
|
232
234
|
}
|
|
233
235
|
/**
|
|
234
236
|
* Removes and returns an item from the queuer
|
|
235
237
|
*/
|
|
236
238
|
getNextItem(position = this._options.getItemsFrom) {
|
|
239
|
+
var _a, _b, _c, _d;
|
|
237
240
|
let item;
|
|
238
241
|
if (position === "front") {
|
|
239
242
|
item = this._items.shift();
|
|
@@ -243,9 +246,8 @@ class AsyncQueuer {
|
|
|
243
246
|
this._itemTimestamps.pop();
|
|
244
247
|
}
|
|
245
248
|
if (item !== void 0) {
|
|
246
|
-
this.
|
|
247
|
-
this._options.
|
|
248
|
-
this._options.onGetNextItem(item, this);
|
|
249
|
+
(_b = (_a = this._options).onItemsChange) == null ? void 0 : _b.call(_a, this);
|
|
250
|
+
(_d = (_c = this._options).onGetNextItem) == null ? void 0 : _d.call(_c, item, this);
|
|
249
251
|
}
|
|
250
252
|
return item;
|
|
251
253
|
}
|
|
@@ -295,10 +297,22 @@ class AsyncQueuer {
|
|
|
295
297
|
return [...this._items];
|
|
296
298
|
}
|
|
297
299
|
/**
|
|
298
|
-
* Returns the number of items that have been
|
|
300
|
+
* Returns the number of items that have been successfully processed
|
|
301
|
+
*/
|
|
302
|
+
getSuccessCount() {
|
|
303
|
+
return this._successCount;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Returns the number of items that have failed processing
|
|
307
|
+
*/
|
|
308
|
+
getErrorCount() {
|
|
309
|
+
return this._errorCount;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Returns the number of items that have completed processing (success or error)
|
|
299
313
|
*/
|
|
300
|
-
|
|
301
|
-
return this.
|
|
314
|
+
getSettledCount() {
|
|
315
|
+
return this._settledCount;
|
|
302
316
|
}
|
|
303
317
|
/**
|
|
304
318
|
* Returns the number of items that have been rejected from the queuer
|
|
@@ -318,37 +332,6 @@ class AsyncQueuer {
|
|
|
318
332
|
getIsIdle() {
|
|
319
333
|
return this._running && this.getIsEmpty() && this._activeItems.size === 0;
|
|
320
334
|
}
|
|
321
|
-
/**
|
|
322
|
-
* Adds a callback to be called when a task succeeds
|
|
323
|
-
*/
|
|
324
|
-
onSuccess(cb) {
|
|
325
|
-
this._onSuccessCallbacks.push(cb);
|
|
326
|
-
return () => {
|
|
327
|
-
this._onSuccessCallbacks = this._onSuccessCallbacks.filter(
|
|
328
|
-
(d) => d !== cb
|
|
329
|
-
);
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Adds a callback to be called when a task errors
|
|
334
|
-
*/
|
|
335
|
-
onError(cb) {
|
|
336
|
-
this._onErrorCallbacks.push(cb);
|
|
337
|
-
return () => {
|
|
338
|
-
this._onErrorCallbacks = this._onErrorCallbacks.filter((d) => d !== cb);
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Adds a callback to be called when a task is settled
|
|
343
|
-
*/
|
|
344
|
-
onSettled(cb) {
|
|
345
|
-
this._onSettledCallbacks.push(cb);
|
|
346
|
-
return () => {
|
|
347
|
-
this._onSettledCallbacks = this._onSettledCallbacks.filter(
|
|
348
|
-
(d) => d !== cb
|
|
349
|
-
);
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
335
|
/**
|
|
353
336
|
* Returns the number of items that have expired from the queuer
|
|
354
337
|
*/
|
|
@@ -356,7 +339,7 @@ class AsyncQueuer {
|
|
|
356
339
|
return this._expirationCount;
|
|
357
340
|
}
|
|
358
341
|
}
|
|
359
|
-
function asyncQueue(options
|
|
342
|
+
function asyncQueue(options) {
|
|
360
343
|
const queuer = new AsyncQueuer(options);
|
|
361
344
|
return queuer.addItem.bind(queuer);
|
|
362
345
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-queuer.js","sources":["../../src/async-queuer.ts"],"sourcesContent":["import type { QueuePosition } from './queuer'\n\nexport interface AsyncQueuerOptions<TValue> {\n /**\n * Default position to add items to the queuer\n * @default 'back'\n */\n addItemsTo?: QueuePosition\n /**\n * Maximum number of concurrent tasks to process\n */\n concurrency?: number\n /**\n * Maximum time in milliseconds that an item can stay in the queue\n * If not provided, items will never expire\n */\n expirationDuration?: number\n /**\n * Function to determine if an item has expired\n * If provided, this overrides the expirationDuration behavior\n */\n getIsExpired?: (item: () => Promise<TValue>, addedAt: number) => boolean\n /**\n * Default position to get items from during processing\n * @default 'front'\n */\n getItemsFrom?: QueuePosition\n /**\n * Function to determine priority of items in the queuer\n * Higher priority items will be processed first\n * If not provided, will use static priority values attached to tasks\n */\n getPriority?: (item: () => Promise<TValue>) => number\n /**\n * Initial items to populate the queuer with\n */\n initialItems?: Array<(() => Promise<TValue>) & { priority?: number }>\n /**\n * Maximum number of items allowed in the queuer\n */\n maxSize?: number\n /**\n * Callback fired whenever an item is removed from the queuer\n */\n onGetNextItem?: (\n item: () => Promise<TValue>,\n queuer: AsyncQueuer<TValue>,\n ) => void\n /**\n * Callback fired whenever the queuer's running state changes\n */\n onIsRunningChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onItemsChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is rejected from being added to the queuer\n */\n onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item expires in the queuer\n */\n onExpire?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void\n /**\n * Whether the queuer should start processing tasks immediately or not.\n */\n started?: boolean\n /**\n * Time in milliseconds to wait between processing items\n */\n wait?: number\n}\n\nconst defaultOptions: Required<AsyncQueuerOptions<any>> = {\n addItemsTo: 'back',\n concurrency: 1,\n expirationDuration: Infinity,\n getIsExpired: () => false,\n getItemsFrom: 'front',\n getPriority: (item) => (item as any)?.priority ?? 0,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onIsRunningChange: () => {},\n onItemsChange: () => {},\n onReject: () => {},\n onExpire: () => {},\n started: true,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queue that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queue support via getPriority option\n * - Configurable concurrency limit\n * - Task success/error/completion callbacks\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Pause/resume task processing\n * - Task cancellation\n * - Item expiration to clear stale items from the queue\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if below the concurrency limit.\n *\n * @example\n * ```ts\n * const asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });\n *\n * asyncQueuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * asyncQueuer.start();\n *\n * asyncQueuer.onSuccess((result) => {\n * console.log(result); // 'Hello'\n * });\n * ```\n */\nexport class AsyncQueuer<TValue> {\n private _options: Required<AsyncQueuerOptions<TValue>>\n private _activeItems: Set<() => Promise<TValue>> = new Set()\n private _executionCount = 0\n private _rejectionCount = 0\n private _expirationCount = 0\n private _items: Array<() => Promise<TValue>> = []\n private _itemTimestamps: Array<number> = []\n private _onErrorCallbacks: Array<(error: Error) => void> = []\n private _onSettledCallbacks: Array<(result: TValue | Error) => void> = []\n private _onSuccessCallbacks: Array<(result: TValue) => void> = []\n private _pendingTick = false\n private _running: boolean\n\n constructor(initialOptions: AsyncQueuerOptions<TValue> = defaultOptions) {\n this._options = { ...defaultOptions, ...initialOptions }\n this._running = this._options.started\n\n for (let i = 0; i < this._options.initialItems.length; i++) {\n const item = this._options.initialItems[i]!\n const isLast = i === this._options.initialItems.length - 1\n this.addItem(item, this._options.addItemsTo, isLast)\n }\n }\n\n /**\n * Updates the queuer options\n * Returns the new options state\n */\n setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current queuer options\n */\n getOptions(): Required<AsyncQueuerOptions<TValue>> {\n return this._options\n }\n\n /**\n * Processes items in the queuer\n */\n private tick() {\n if (!this._running) {\n this._pendingTick = false\n return\n }\n\n // Check for expired items\n this.checkExpiredItems()\n\n while (\n this._activeItems.size < this._options.concurrency &&\n !this.getIsEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n this._activeItems.add(nextFn)\n this._options.onItemsChange(this)\n ;(async () => {\n let success = false\n let res!: TValue\n let error: Error | undefined\n\n try {\n res = await nextFn()\n success = true\n } catch (e) {\n error = e as Error\n } finally {\n this._activeItems.delete(nextFn)\n this._options.onItemsChange(this)\n }\n\n if (success) {\n this._onSuccessCallbacks.forEach((cb) => cb(res))\n } else {\n this._onErrorCallbacks.forEach((cb) => cb(error!))\n }\n this._onSettledCallbacks.forEach((cb) => cb(success ? res : error!))\n\n if (this._options.wait > 0) {\n setTimeout(() => this.tick(), this._options.wait)\n return\n }\n\n this.tick()\n })()\n }\n\n this._pendingTick = false\n }\n\n /**\n * Checks for and removes expired items from the queuer\n */\n private checkExpiredItems() {\n if (\n this._options.expirationDuration === Infinity &&\n this._options.getIsExpired === defaultOptions.getIsExpired\n )\n return\n\n const now = Date.now()\n const expiredIndices: Array<number> = []\n\n // Find indices of expired items\n for (let i = 0; i < this._items.length; i++) {\n const timestamp = this._itemTimestamps[i]\n if (timestamp === undefined) continue\n\n const item = this._items[i]\n if (item === undefined) continue\n\n const isExpired =\n this._options.getIsExpired !== defaultOptions.getIsExpired\n ? this._options.getIsExpired(item, timestamp)\n : now - timestamp > this._options.expirationDuration\n\n if (isExpired) {\n expiredIndices.push(i)\n }\n }\n\n // Remove expired items from back to front to maintain indices\n for (let i = expiredIndices.length - 1; i >= 0; i--) {\n const index = expiredIndices[i]\n if (index === undefined) continue\n\n const expiredItem = this._items[index]\n if (expiredItem === undefined) continue\n\n this._items.splice(index, 1)\n this._itemTimestamps.splice(index, 1)\n this._expirationCount++\n this._options.onExpire(expiredItem, this)\n }\n\n if (expiredIndices.length > 0) {\n this._options.onItemsChange(this)\n }\n }\n\n /**\n * Starts the queuer and processes items\n */\n start(): Promise<void> {\n this._running = true\n if (!this._pendingTick && !this.getIsEmpty()) {\n this._pendingTick = true\n this.tick()\n }\n this._options.onIsRunningChange(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.getIsIdle()) {\n resolve()\n } else {\n setTimeout(checkIdle, 100)\n }\n }\n checkIdle()\n })\n }\n\n /**\n * Stops the queuer from processing items\n */\n stop(): void {\n this._running = false\n this._pendingTick = false\n this._options.onIsRunningChange(this)\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this._items = []\n this._options.onItemsChange(this)\n }\n\n /**\n * Resets the queuer to its initial state\n */\n reset(withInitialItems?: boolean): void {\n this.clear()\n this._executionCount = 0\n if (withInitialItems) {\n this._items = [...this._options.initialItems]\n }\n this._running = this._options.started\n }\n\n /**\n * Adds a task to the queuer\n */\n addItem(\n fn: (() => Promise<TValue>) & { priority?: number },\n position: QueuePosition = this._options.addItemsTo,\n runOnUpdate: boolean = true,\n ): Promise<TValue> {\n if (this.getIsFull()) {\n this._rejectionCount++\n this._options.onReject(fn, this)\n return Promise.reject(new Error('Queuer is full'))\n }\n\n return new Promise<TValue>((resolve, reject) => {\n const task = Object.assign(\n async () => {\n try {\n const result = await fn()\n resolve(result)\n return result\n } catch (error) {\n reject(error)\n throw error\n }\n },\n { priority: fn.priority ?? undefined },\n )\n\n // Get priority either from the function or from getPriority option\n const priority =\n this._options.getPriority !== defaultOptions.getPriority\n ? this._options.getPriority(task)\n : task.priority\n\n if (priority !== undefined) {\n // Insert based on priority\n const insertIndex = this._items.findIndex((existing) => {\n const existingPriority =\n this._options.getPriority !== defaultOptions.getPriority\n ? this._options.getPriority(existing)\n : (existing as any).priority\n return existingPriority > priority\n })\n\n if (insertIndex === -1) {\n this._items.push(task)\n this._itemTimestamps.push(Date.now())\n } else {\n this._items.splice(insertIndex, 0, task)\n this._itemTimestamps.splice(insertIndex, 0, Date.now())\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this._items.unshift(task)\n this._itemTimestamps.unshift(Date.now())\n } else {\n this._items.push(task)\n this._itemTimestamps.push(Date.now())\n }\n }\n\n if (runOnUpdate) {\n this._options.onItemsChange(this)\n }\n\n if (this._running && !this._pendingTick) {\n this._pendingTick = true\n this.tick()\n }\n })\n }\n\n /**\n * Removes and returns an item from the queuer\n */\n getNextItem(\n position: QueuePosition = this._options.getItemsFrom,\n ): (() => Promise<TValue>) | undefined {\n let item: (() => Promise<TValue>) | undefined\n\n if (position === 'front') {\n item = this._items.shift()\n this._itemTimestamps.shift()\n } else {\n item = this._items.pop()\n this._itemTimestamps.pop()\n }\n\n if (item !== undefined) {\n this._executionCount++\n this._options.onItemsChange(this)\n this._options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n getPeek(\n position: QueuePosition = 'front',\n ): (() => Promise<TValue>) | undefined {\n if (position === 'front') {\n return this._items[0]\n }\n return this._items[this._items.length - 1]\n }\n\n /**\n * Returns true if the queuer is empty\n */\n getIsEmpty(): boolean {\n return this._items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n getIsFull(): boolean {\n return this._items.length >= this._options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n getSize(): number {\n return this._items.length\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<() => Promise<TValue>> {\n return [...this.getActiveItems(), ...this.getPendingItems()]\n }\n\n /**\n * Returns the active items\n */\n getActiveItems(): Array<() => Promise<TValue>> {\n return Array.from(this._activeItems)\n }\n\n /**\n * Returns the pending items\n */\n getPendingItems(): Array<() => Promise<TValue>> {\n return [...this._items]\n }\n\n /**\n * Returns the number of items that have been removed from the queuer\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns the number of items that have been rejected from the queuer\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns true if the queuer is running\n */\n getIsRunning(): boolean {\n return this._running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n getIsIdle(): boolean {\n return this._running && this.getIsEmpty() && this._activeItems.size === 0\n }\n\n /**\n * Adds a callback to be called when a task succeeds\n */\n onSuccess(cb: (result: TValue) => void) {\n this._onSuccessCallbacks.push(cb)\n return () => {\n this._onSuccessCallbacks = this._onSuccessCallbacks.filter(\n (d) => d !== cb,\n )\n }\n }\n\n /**\n * Adds a callback to be called when a task errors\n */\n onError(cb: (error: Error) => void) {\n this._onErrorCallbacks.push(cb)\n return () => {\n this._onErrorCallbacks = this._onErrorCallbacks.filter((d) => d !== cb)\n }\n }\n\n /**\n * Adds a callback to be called when a task is settled\n */\n onSettled(cb: (result: TValue | Error) => void) {\n this._onSettledCallbacks.push(cb)\n return () => {\n this._onSettledCallbacks = this._onSettledCallbacks.filter(\n (d) => d !== cb,\n )\n }\n }\n\n /**\n * Returns the number of items that have expired from the queuer\n */\n getExpirationCount(): number {\n return this._expirationCount\n }\n}\n\n/**\n * Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.\n * The queuer is automatically started and ready to process items.\n *\n * @example\n * ```ts\n * const enqueue = asyncQueue<string>();\n *\n * // Add items to be processed\n * enqueue(async () => {\n * return 'Hello';\n * });\n * ```\n *\n * @param options - Configuration options for the AsyncQueuer\n * @returns A bound addItem function that can be used to add tasks to the queuer\n */\nexport function asyncQueue<TValue>(\n options: Omit<AsyncQueuerOptions<TValue>,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AAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EAAA;AAEhB;AAmBgB,SAAA,WACd,UAAuD,IACvD;AACM,QAAA,SAAS,IAAI,YAAoB,OAAO;AACvC,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
1
|
+
{"version":3,"file":"async-queuer.js","sources":["../../src/async-queuer.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyAsyncFunction, OptionalKeys } from './types'\nimport type { QueuePosition } from './queuer'\n\nexport type AsyncQueuerFn = AnyAsyncFunction & { priority?: number }\n\nexport interface AsyncQueuerOptions<TFn extends AsyncQueuerFn> {\n /**\n * Default position to add items to the queuer\n * @default 'back'\n */\n addItemsTo?: QueuePosition\n /**\n * Maximum number of concurrent tasks to process.\n * Can be a number or a function that returns a number.\n * @default 1\n */\n concurrency?: number | ((queuer: AsyncQueuer<TFn>) => number)\n /**\n * Maximum time in milliseconds that an item can stay in the queue\n * If not provided, items will never expire\n */\n expirationDuration?: number\n /**\n * Function to determine if an item has expired\n * If provided, this overrides the expirationDuration behavior\n */\n getIsExpired?: (item: TFn, addedAt: number) => boolean\n /**\n * Default position to get items from during processing\n * @default 'front'\n */\n getItemsFrom?: QueuePosition\n /**\n * Function to determine priority of items in the queuer\n * Higher priority items will be processed first\n * If not provided, will use static priority values attached to tasks\n */\n getPriority?: (item: TFn) => number\n /**\n * Initial items to populate the queuer with\n */\n initialItems?: Array<TFn & { priority?: number }>\n /**\n * Maximum number of items allowed in the queuer\n */\n maxSize?: number\n /**\n * Optional error handler for when a task throws.\n * If provided, the handler will be called with the error and queuer instance.\n * This can be used alongside throwOnError - the handler will be called before any error is thrown.\n */\n onError?: (error: unknown, queuer: AsyncQueuer<TFn>) => void\n /**\n * Callback fired whenever an item expires in the queuer\n */\n onExpire?: (item: TFn, queuer: AsyncQueuer<TFn>) => void\n /**\n * Callback fired whenever an item is removed from the queuer\n */\n onGetNextItem?: (item: TFn, queuer: AsyncQueuer<TFn>) => void\n /**\n * Callback fired whenever the queuer's running state changes\n */\n onIsRunningChange?: (queuer: AsyncQueuer<TFn>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onItemsChange?: (queuer: AsyncQueuer<TFn>) => void\n /**\n * Callback fired whenever an item is rejected from being added to the queuer\n */\n onReject?: (item: TFn, queuer: AsyncQueuer<TFn>) => void\n /**\n * Optional callback to call when a task is settled\n */\n onSettled?: (queuer: AsyncQueuer<TFn>) => void\n /**\n * Optional callback to call when a task succeeds\n */\n onSuccess?: (result: TFn, queuer: AsyncQueuer<TFn>) => void\n /**\n * Whether the queuer should start processing tasks immediately or not.\n */\n started?: boolean\n /**\n * Whether to throw errors when they occur.\n * Defaults to true if no onError handler is provided, false if an onError handler is provided.\n * Can be explicitly set to override these defaults.\n */\n throwOnError?: boolean\n /**\n * Time in milliseconds to wait between processing items.\n * Can be a number or a function that returns a number.\n * @default 0\n */\n wait?: number | ((queuer: AsyncQueuer<TFn>) => number)\n}\n\ntype AsyncQueuerOptionsWithOptionalCallbacks = OptionalKeys<\n Required<AsyncQueuerOptions<any>>,\n | 'onError'\n | 'onExpire'\n | 'onGetNextItem'\n | 'onIsRunningChange'\n | 'onItemsChange'\n | 'onReject'\n | 'onSettled'\n | 'onSuccess'\n | 'throwOnError'\n>\n\nconst defaultOptions: AsyncQueuerOptionsWithOptionalCallbacks = {\n addItemsTo: 'back',\n concurrency: 1,\n expirationDuration: Infinity,\n getIsExpired: () => false,\n getItemsFrom: 'front',\n getPriority: (item: any) => item?.priority ?? 0,\n initialItems: [],\n maxSize: Infinity,\n started: true,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queue that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queue support via getPriority option\n * - Configurable concurrency limit\n * - Task success/error/completion callbacks\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Pause/resume task processing\n * - Task cancellation\n * - Item expiration to clear stale items from the queue\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if below the concurrency limit.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and queuer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncQueuer instance\n *\n * @example\n * ```ts\n * const asyncQueuer = new AsyncQueuer<string>({\n * concurrency: 2,\n * onSuccess: (result) => {\n * console.log(result); // 'Hello'\n * }\n * });\n *\n * asyncQueuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * asyncQueuer.start();\n * ```\n */\nexport class AsyncQueuer<TFn extends AsyncQueuerFn> {\n private _options: AsyncQueuerOptionsWithOptionalCallbacks\n private _activeItems: Set<TFn> = new Set()\n private _successCount = 0\n private _errorCount = 0\n private _settledCount = 0\n private _rejectionCount = 0\n private _expirationCount = 0\n private _items: Array<TFn> = []\n private _itemTimestamps: Array<number> = []\n private _pendingTick = false\n private _running: boolean\n\n constructor(initialOptions: AsyncQueuerOptions<TFn> = defaultOptions) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n throwOnError: initialOptions.throwOnError ?? !initialOptions.onError,\n }\n this._running = this._options.started\n\n for (let i = 0; i < this._options.initialItems.length; i++) {\n const item = this._options.initialItems[i]!\n const isLast = i === this._options.initialItems.length - 1\n this.addItem(item, this._options.addItemsTo, isLast)\n }\n }\n\n /**\n * Updates the queuer options\n * Returns the new options state\n */\n setOptions(newOptions: Partial<AsyncQueuerOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current queuer options\n */\n getOptions(): AsyncQueuerOptions<TFn> {\n return this._options\n }\n\n /**\n * Returns the current wait time between processing items\n */\n getWait(): number {\n return parseFunctionOrValue(this._options.wait, this)\n }\n\n /**\n * Returns the current concurrency limit\n */\n getConcurrency(): number {\n return parseFunctionOrValue(this._options.concurrency, this)\n }\n\n /**\n * Processes items in the queuer\n */\n private tick() {\n if (!this._running) {\n this._pendingTick = false\n return\n }\n\n // Check for expired items\n this.checkExpiredItems()\n\n while (\n this._activeItems.size < this.getConcurrency() &&\n !this.getIsEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n this._activeItems.add(nextFn)\n this._options.onItemsChange?.(this)\n ;(async () => {\n let res!: TFn\n\n try {\n res = await nextFn()\n this._successCount++\n this._options.onSuccess?.(res, this)\n } catch (error) {\n this._errorCount++\n this._options.onError?.(error, this)\n if (this._options.throwOnError) {\n throw error\n } else {\n console.error(error)\n }\n } finally {\n this._settledCount++\n this._activeItems.delete(nextFn)\n this._options.onItemsChange?.(this)\n this._options.onSettled?.(this)\n }\n\n const wait = this.getWait()\n if (wait > 0) {\n setTimeout(() => this.tick(), wait)\n return\n }\n\n this.tick()\n })()\n }\n\n this._pendingTick = false\n }\n\n /**\n * Checks for and removes expired items from the queuer\n */\n private checkExpiredItems(): void {\n if (\n this._options.expirationDuration === Infinity &&\n this._options.getIsExpired === defaultOptions.getIsExpired\n )\n return\n\n const now = Date.now()\n const expiredIndices: Array<number> = []\n\n // Find indices of expired items\n for (let i = 0; i < this._items.length; i++) {\n const timestamp = this._itemTimestamps[i]\n if (timestamp === undefined) continue\n\n const item = this._items[i]\n if (item === undefined) continue\n\n const isExpired =\n this._options.getIsExpired !== defaultOptions.getIsExpired\n ? this._options.getIsExpired(item, timestamp)\n : now - timestamp > this._options.expirationDuration\n\n if (isExpired) {\n expiredIndices.push(i)\n }\n }\n\n // Remove expired items from back to front to maintain indices\n for (let i = expiredIndices.length - 1; i >= 0; i--) {\n const index = expiredIndices[i]\n if (index === undefined) continue\n\n const expiredItem = this._items[index]\n if (expiredItem === undefined) continue\n\n this._items.splice(index, 1)\n this._itemTimestamps.splice(index, 1)\n this._expirationCount++\n this._options.onExpire?.(expiredItem, this)\n }\n\n if (expiredIndices.length > 0) {\n this._options.onItemsChange?.(this)\n }\n }\n\n /**\n * Starts the queuer and processes items\n */\n start(): Promise<void> {\n this._running = true\n if (!this._pendingTick && !this.getIsEmpty()) {\n this._pendingTick = true\n this.tick()\n }\n this._options.onIsRunningChange?.(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.getIsIdle()) {\n resolve()\n } else {\n setTimeout(checkIdle, 100)\n }\n }\n checkIdle()\n })\n }\n\n /**\n * Stops the queuer from processing items\n */\n stop(): void {\n this._running = false\n this._pendingTick = false\n this._options.onIsRunningChange?.(this)\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this._items = []\n this._options.onItemsChange?.(this)\n }\n\n /**\n * Resets the queuer to its initial state\n */\n reset(withInitialItems?: boolean): void {\n this.clear()\n this._successCount = 0\n this._errorCount = 0\n this._settledCount = 0\n if (withInitialItems) {\n this._items = [...this._options.initialItems]\n }\n this._running = this._options.started\n }\n\n /**\n * Adds a task to the queuer\n */\n addItem(\n fn: TFn,\n position: QueuePosition = this._options.addItemsTo,\n runOnItemsChange: boolean = true,\n ): void {\n if (this.getIsFull()) {\n this._rejectionCount++\n this._options.onReject?.(fn, this)\n return\n }\n\n // Get priority either from the function or from getPriority option\n const priority =\n this._options.getPriority !== defaultOptions.getPriority\n ? this._options.getPriority(fn)\n : fn.priority\n\n if (priority !== undefined) {\n // Insert based on priority\n const insertIndex = this._items.findIndex((existing) => {\n const existingPriority =\n this._options.getPriority !== defaultOptions.getPriority\n ? this._options.getPriority(existing)\n : (existing as any).priority\n return existingPriority > priority\n })\n\n if (insertIndex === -1) {\n this._items.push(fn)\n this._itemTimestamps.push(Date.now())\n } else {\n this._items.splice(insertIndex, 0, fn)\n this._itemTimestamps.splice(insertIndex, 0, Date.now())\n }\n } else {\n if (position === 'front') {\n // Default FIFO/LIFO behavior\n this._items.unshift(fn)\n this._itemTimestamps.unshift(Date.now())\n } else {\n // LIFO\n this._items.push(fn)\n this._itemTimestamps.push(Date.now())\n }\n }\n\n if (runOnItemsChange) {\n this._options.onItemsChange?.(this)\n }\n\n if (this._running && !this._pendingTick) {\n this._pendingTick = true\n this.tick()\n }\n }\n\n /**\n * Removes and returns an item from the queuer\n */\n getNextItem(\n position: QueuePosition = this._options.getItemsFrom,\n ): TFn | undefined {\n let item: TFn | undefined\n\n if (position === 'front') {\n item = this._items.shift()\n this._itemTimestamps.shift()\n } else {\n item = this._items.pop()\n this._itemTimestamps.pop()\n }\n\n if (item !== undefined) {\n this._options.onItemsChange?.(this)\n this._options.onGetNextItem?.(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n getPeek(position: QueuePosition = 'front'): TFn | undefined {\n if (position === 'front') {\n return this._items[0]\n }\n return this._items[this._items.length - 1]\n }\n\n /**\n * Returns true if the queuer is empty\n */\n getIsEmpty(): boolean {\n return this._items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n getIsFull(): boolean {\n return this._items.length >= this._options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n getSize(): number {\n return this._items.length\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<TFn> {\n return [...this.getActiveItems(), ...this.getPendingItems()]\n }\n\n /**\n * Returns the active items\n */\n getActiveItems(): Array<TFn> {\n return Array.from(this._activeItems)\n }\n\n /**\n * Returns the pending items\n */\n getPendingItems(): Array<TFn> {\n return [...this._items]\n }\n\n /**\n * Returns the number of items that have been successfully processed\n */\n getSuccessCount(): number {\n return this._successCount\n }\n\n /**\n * Returns the number of items that have failed processing\n */\n getErrorCount(): number {\n return this._errorCount\n }\n\n /**\n * Returns the number of items that have completed processing (success or error)\n */\n getSettledCount(): number {\n return this._settledCount\n }\n\n /**\n * Returns the number of items that have been rejected from the queuer\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns true if the queuer is running\n */\n getIsRunning(): boolean {\n return this._running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n getIsIdle(): boolean {\n return this._running && this.getIsEmpty() && this._activeItems.size === 0\n }\n\n /**\n * Returns the number of items that have expired from the queuer\n */\n getExpirationCount(): number {\n return this._expirationCount\n }\n}\n\n/**\n * Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.\n * The queuer is automatically started and ready to process items.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and queuer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncQueuer instance\n *\n * @example\n * ```ts\n * const enqueue = asyncQueue<string>();\n *\n * // Add items to be processed\n * enqueue(async () => {\n * return 'Hello';\n * });\n * ```\n *\n * @param options - Configuration options for the AsyncQueuer\n * @returns A bound addItem function that can be used to add tasks to the queuer\n */\nexport function asyncQueue<TFn extends AsyncQueuerFn>(\n options: AsyncQueuerOptions<TFn>,\n) {\n const queuer = new AsyncQueuer<TFn>(options)\n return queuer.addItem.bind(queuer)\n}\n"],"names":["_b","_a"],"mappings":";AAgHA,MAAM,iBAA0D;AAAA,EAC9D,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,oBAAoB;AAAA,EACpB,cAAc,MAAM;AAAA,EACpB,cAAc;AAAA,EACd,aAAa,CAAC,UAAc,6BAAM,aAAY;AAAA,EAC9C,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,SAAS;AAAA,EACT,MAAM;AACR;AAwCO,MAAM,YAAuC;AAAA,EAalD,YAAY,iBAA0C,gBAAgB;AAX9D,SAAA,mCAA6B,IAAI;AACzC,SAAQ,gBAAgB;AACxB,SAAQ,cAAc;AACtB,SAAQ,gBAAgB;AACxB,SAAQ,kBAAkB;AAC1B,SAAQ,mBAAmB;AAC3B,SAAQ,SAAqB,CAAC;AAC9B,SAAQ,kBAAiC,CAAC;AAC1C,SAAQ,eAAe;AAIrB,SAAK,WAAW;AAAA,MACd,GAAG;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B;AAC3B,WAAO,KAAK;AAAA,EAAA;AAEhB;AA0BO,SAAS,WACd,SACA;AACM,QAAA,SAAS,IAAI,YAAiB,OAAO;AACpC,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
@@ -5,15 +5,19 @@ import { AnyAsyncFunction } from './types.js';
|
|
|
5
5
|
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
|
|
6
6
|
/**
|
|
7
7
|
* Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
8
|
+
* Can be a boolean or a function that returns a boolean.
|
|
8
9
|
* Defaults to true.
|
|
9
10
|
*/
|
|
10
|
-
enabled?: boolean;
|
|
11
|
+
enabled?: boolean | ((rateLimiter: AsyncRateLimiter<TFn>) => boolean);
|
|
11
12
|
/**
|
|
12
|
-
* Maximum number of executions allowed within the time window
|
|
13
|
+
* Maximum number of executions allowed within the time window.
|
|
14
|
+
* Can be a number or a function that returns a number.
|
|
13
15
|
*/
|
|
14
|
-
limit: number;
|
|
16
|
+
limit: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
|
|
15
17
|
/**
|
|
16
|
-
* Optional error handler for when the rate-limited function throws
|
|
18
|
+
* Optional error handler for when the rate-limited function throws.
|
|
19
|
+
* If provided, the handler will be called with the error and rate limiter instance.
|
|
20
|
+
* This can be used alongside throwOnError - the handler will be called before any error is thrown.
|
|
17
21
|
*/
|
|
18
22
|
onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
19
23
|
/**
|
|
@@ -29,9 +33,16 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
|
|
|
29
33
|
*/
|
|
30
34
|
onSuccess?: (result: ReturnType<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
31
35
|
/**
|
|
32
|
-
*
|
|
36
|
+
* Whether to throw errors when they occur.
|
|
37
|
+
* Defaults to true if no onError handler is provided, false if an onError handler is provided.
|
|
38
|
+
* Can be explicitly set to override these defaults.
|
|
33
39
|
*/
|
|
34
|
-
|
|
40
|
+
throwOnError?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Time window in milliseconds within which the limit applies.
|
|
43
|
+
* Can be a number or a function that returns a number.
|
|
44
|
+
*/
|
|
45
|
+
window: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
|
|
35
46
|
/**
|
|
36
47
|
* Type of window to use for rate limiting
|
|
37
48
|
* - 'fixed': Uses a fixed window that resets after the window period
|
|
@@ -64,11 +75,29 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
|
|
|
64
75
|
* Rate limiting is best used for hard API limits or resource constraints. For UI updates or
|
|
65
76
|
* smoothing out frequent events, throttling or debouncing usually provide better user experience.
|
|
66
77
|
*
|
|
78
|
+
* Error Handling:
|
|
79
|
+
* - If an `onError` handler is provided, it will be called with the error and rate limiter instance
|
|
80
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
81
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
82
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
83
|
+
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
84
|
+
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
85
|
+
*
|
|
67
86
|
* @example
|
|
68
87
|
* ```ts
|
|
69
88
|
* const rateLimiter = new AsyncRateLimiter(
|
|
70
89
|
* async (id: string) => await api.getData(id),
|
|
71
|
-
* {
|
|
90
|
+
* {
|
|
91
|
+
* limit: 5,
|
|
92
|
+
* window: 1000,
|
|
93
|
+
* windowType: 'sliding',
|
|
94
|
+
* onError: (error) => {
|
|
95
|
+
* console.error('API call failed:', error);
|
|
96
|
+
* },
|
|
97
|
+
* onReject: (limiter) => {
|
|
98
|
+
* console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
72
101
|
* );
|
|
73
102
|
*
|
|
74
103
|
* // Will execute immediately until limit reached, then block
|
|
@@ -95,12 +124,37 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
|
95
124
|
/**
|
|
96
125
|
* Returns the current rate limiter options
|
|
97
126
|
*/
|
|
98
|
-
getOptions():
|
|
127
|
+
getOptions(): AsyncRateLimiterOptions<TFn>;
|
|
128
|
+
/**
|
|
129
|
+
* Returns the current enabled state of the rate limiter
|
|
130
|
+
*/
|
|
131
|
+
getEnabled(): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Returns the current limit of executions allowed within the time window
|
|
134
|
+
*/
|
|
135
|
+
getLimit(): number;
|
|
136
|
+
/**
|
|
137
|
+
* Returns the current time window in milliseconds
|
|
138
|
+
*/
|
|
139
|
+
getWindow(): number;
|
|
99
140
|
/**
|
|
100
141
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
101
142
|
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
102
143
|
* If execution is allowed, waits for any previous execution to complete before proceeding.
|
|
103
144
|
*
|
|
145
|
+
* Error Handling:
|
|
146
|
+
* - If the rate-limited function throws and no `onError` handler is configured,
|
|
147
|
+
* the error will be thrown from this method.
|
|
148
|
+
* - If an `onError` handler is configured, errors will be caught and passed to the handler,
|
|
149
|
+
* and this method will return undefined.
|
|
150
|
+
* - If the rate limit is exceeded, the execution will be rejected and the `onReject` handler
|
|
151
|
+
* will be called if configured.
|
|
152
|
+
* - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.
|
|
153
|
+
* - Rate limit rejections can be tracked using `getRejectionCount()`.
|
|
154
|
+
*
|
|
155
|
+
* @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
|
|
156
|
+
* @throws The error from the rate-limited function if no onError handler is configured
|
|
157
|
+
*
|
|
104
158
|
* @example
|
|
105
159
|
* ```ts
|
|
106
160
|
* const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
|
|
@@ -172,6 +226,14 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
|
172
226
|
* Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically
|
|
173
227
|
* need to enforce a hard limit on the number of executions within a time period.
|
|
174
228
|
*
|
|
229
|
+
* Error Handling:
|
|
230
|
+
* - If an `onError` handler is provided, it will be called with the error and rate limiter instance
|
|
231
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
232
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
233
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
234
|
+
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
235
|
+
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
236
|
+
*
|
|
175
237
|
* @example
|
|
176
238
|
* ```ts
|
|
177
239
|
* // Rate limit to 5 calls per minute with a sliding window
|
|
@@ -179,6 +241,9 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
|
179
241
|
* limit: 5,
|
|
180
242
|
* window: 60000,
|
|
181
243
|
* windowType: 'sliding',
|
|
244
|
+
* onError: (error) => {
|
|
245
|
+
* console.error('API call failed:', error);
|
|
246
|
+
* },
|
|
182
247
|
* onReject: (rateLimiter) => {
|
|
183
248
|
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
184
249
|
* }
|
|
@@ -193,4 +258,4 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
|
193
258
|
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
194
259
|
* ```
|
|
195
260
|
*/
|
|
196
|
-
export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions:
|
|
261
|
+
export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
|