@tanstack/pacer 0.2.0 → 0.4.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 +78 -45
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +60 -24
- package/dist/cjs/async-queuer.cjs +53 -3
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +28 -3
- package/dist/cjs/async-rate-limiter.cjs +75 -38
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +76 -24
- package/dist/cjs/async-throttler.cjs +85 -57
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +66 -25
- package/dist/cjs/debouncer.cjs +12 -14
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +10 -9
- package/dist/cjs/queuer.cjs +51 -1
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +30 -1
- package/dist/cjs/rate-limiter.cjs +19 -9
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +31 -11
- package/dist/cjs/throttler.cjs +29 -36
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +16 -17
- package/dist/cjs/types.d.cts +2 -6
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +1 -1
- package/dist/esm/async-debouncer.d.ts +60 -24
- package/dist/esm/async-debouncer.js +78 -45
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +28 -3
- package/dist/esm/async-queuer.js +53 -3
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +76 -24
- package/dist/esm/async-rate-limiter.js +75 -38
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +66 -25
- package/dist/esm/async-throttler.js +85 -57
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +10 -9
- package/dist/esm/debouncer.js +12 -14
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/queuer.d.ts +30 -1
- package/dist/esm/queuer.js +51 -1
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +31 -11
- package/dist/esm/rate-limiter.js +19 -9
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +16 -17
- package/dist/esm/throttler.js +29 -36
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +2 -6
- package/dist/esm/utils.d.ts +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/async-debouncer.ts +130 -81
- package/src/async-queuer.ts +93 -8
- package/src/async-rate-limiter.ts +141 -67
- package/src/async-throttler.ts +152 -94
- package/src/debouncer.ts +26 -33
- package/src/queuer.ts +92 -4
- package/src/rate-limiter.ts +56 -32
- package/src/throttler.ts +45 -53
- package/src/types.ts +2 -10
- package/src/utils.ts +1 -1
package/dist/esm/async-queuer.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
addItemsTo: "back",
|
|
3
3
|
concurrency: 1,
|
|
4
|
+
expirationDuration: Infinity,
|
|
5
|
+
getIsExpired: () => false,
|
|
4
6
|
getItemsFrom: "front",
|
|
5
7
|
getPriority: (item) => (item == null ? void 0 : item.priority) ?? 0,
|
|
6
8
|
initialItems: [],
|
|
@@ -13,7 +15,9 @@ const defaultOptions = {
|
|
|
13
15
|
},
|
|
14
16
|
onReject: () => {
|
|
15
17
|
},
|
|
16
|
-
|
|
18
|
+
onExpire: () => {
|
|
19
|
+
},
|
|
20
|
+
started: true,
|
|
17
21
|
wait: 0
|
|
18
22
|
};
|
|
19
23
|
class AsyncQueuer {
|
|
@@ -21,7 +25,9 @@ class AsyncQueuer {
|
|
|
21
25
|
this._activeItems = /* @__PURE__ */ new Set();
|
|
22
26
|
this._executionCount = 0;
|
|
23
27
|
this._rejectionCount = 0;
|
|
28
|
+
this._expirationCount = 0;
|
|
24
29
|
this._items = [];
|
|
30
|
+
this._itemTimestamps = [];
|
|
25
31
|
this._onErrorCallbacks = [];
|
|
26
32
|
this._onSettledCallbacks = [];
|
|
27
33
|
this._onSuccessCallbacks = [];
|
|
@@ -40,7 +46,6 @@ class AsyncQueuer {
|
|
|
40
46
|
*/
|
|
41
47
|
setOptions(newOptions) {
|
|
42
48
|
this._options = { ...this._options, ...newOptions };
|
|
43
|
-
return this._options;
|
|
44
49
|
}
|
|
45
50
|
/**
|
|
46
51
|
* Returns the current queuer options
|
|
@@ -56,6 +61,7 @@ class AsyncQueuer {
|
|
|
56
61
|
this._pendingTick = false;
|
|
57
62
|
return;
|
|
58
63
|
}
|
|
64
|
+
this.checkExpiredItems();
|
|
59
65
|
while (this._activeItems.size < this._options.concurrency && !this.getIsEmpty()) {
|
|
60
66
|
const nextFn = this.getNextItem();
|
|
61
67
|
if (!nextFn) {
|
|
@@ -91,6 +97,38 @@ class AsyncQueuer {
|
|
|
91
97
|
}
|
|
92
98
|
this._pendingTick = false;
|
|
93
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Checks for and removes expired items from the queuer
|
|
102
|
+
*/
|
|
103
|
+
checkExpiredItems() {
|
|
104
|
+
if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
|
|
105
|
+
return;
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const expiredIndices = [];
|
|
108
|
+
for (let i = 0; i < this._items.length; i++) {
|
|
109
|
+
const timestamp = this._itemTimestamps[i];
|
|
110
|
+
if (timestamp === void 0) continue;
|
|
111
|
+
const item = this._items[i];
|
|
112
|
+
if (item === void 0) continue;
|
|
113
|
+
const isExpired = this._options.getIsExpired !== defaultOptions.getIsExpired ? this._options.getIsExpired(item, timestamp) : now - timestamp > this._options.expirationDuration;
|
|
114
|
+
if (isExpired) {
|
|
115
|
+
expiredIndices.push(i);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (let i = expiredIndices.length - 1; i >= 0; i--) {
|
|
119
|
+
const index = expiredIndices[i];
|
|
120
|
+
if (index === void 0) continue;
|
|
121
|
+
const expiredItem = this._items[index];
|
|
122
|
+
if (expiredItem === void 0) continue;
|
|
123
|
+
this._items.splice(index, 1);
|
|
124
|
+
this._itemTimestamps.splice(index, 1);
|
|
125
|
+
this._expirationCount++;
|
|
126
|
+
this._options.onExpire(expiredItem, this);
|
|
127
|
+
}
|
|
128
|
+
if (expiredIndices.length > 0) {
|
|
129
|
+
this._options.onItemsChange(this);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
94
132
|
/**
|
|
95
133
|
* Starts the queuer and processes items
|
|
96
134
|
*/
|
|
@@ -169,14 +207,18 @@ class AsyncQueuer {
|
|
|
169
207
|
});
|
|
170
208
|
if (insertIndex === -1) {
|
|
171
209
|
this._items.push(task);
|
|
210
|
+
this._itemTimestamps.push(Date.now());
|
|
172
211
|
} else {
|
|
173
212
|
this._items.splice(insertIndex, 0, task);
|
|
213
|
+
this._itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
174
214
|
}
|
|
175
215
|
} else {
|
|
176
216
|
if (position === "front") {
|
|
177
217
|
this._items.unshift(task);
|
|
218
|
+
this._itemTimestamps.unshift(Date.now());
|
|
178
219
|
} else {
|
|
179
220
|
this._items.push(task);
|
|
221
|
+
this._itemTimestamps.push(Date.now());
|
|
180
222
|
}
|
|
181
223
|
}
|
|
182
224
|
if (runOnUpdate) {
|
|
@@ -195,8 +237,10 @@ class AsyncQueuer {
|
|
|
195
237
|
let item;
|
|
196
238
|
if (position === "front") {
|
|
197
239
|
item = this._items.shift();
|
|
240
|
+
this._itemTimestamps.shift();
|
|
198
241
|
} else {
|
|
199
242
|
item = this._items.pop();
|
|
243
|
+
this._itemTimestamps.pop();
|
|
200
244
|
}
|
|
201
245
|
if (item !== void 0) {
|
|
202
246
|
this._executionCount++;
|
|
@@ -305,9 +349,15 @@ class AsyncQueuer {
|
|
|
305
349
|
);
|
|
306
350
|
};
|
|
307
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* Returns the number of items that have expired from the queuer
|
|
354
|
+
*/
|
|
355
|
+
getExpirationCount() {
|
|
356
|
+
return this._expirationCount;
|
|
357
|
+
}
|
|
308
358
|
}
|
|
309
359
|
function asyncQueue(options = {}) {
|
|
310
|
-
const queuer = new AsyncQueuer(
|
|
360
|
+
const queuer = new AsyncQueuer(options);
|
|
311
361
|
return queuer.addItem.bind(queuer);
|
|
312
362
|
}
|
|
313
363
|
export {
|
|
@@ -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 * 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 * Whether the queuer should start processing tasks immediately\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 getItemsFrom: 'front',\n getPriority: (item) => (item as any)?.priority ?? 0,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onIsRunningChange: () => {},\n onItemsChange: () => {},\n onReject: () => {},\n started: false,\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 *\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 _items: Array<() => Promise<TValue>> = []\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(\n newOptions: Partial<AsyncQueuerOptions<TValue>>,\n ): AsyncQueuerOptions<TValue> {\n this._options = { ...this._options, ...newOptions }\n return this._options\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 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 * 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 } else {\n this._items.splice(insertIndex, 0, task)\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this._items.unshift(task)\n } else {\n this._items.push(task)\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 } else {\n item = this._items.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/**\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>(options: AsyncQueuerOptions<TValue> = {}) {\n const queuer = new AsyncQueuer<TValue>({ ...options, started: true })\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":"AA4DA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,cAAc;AAAA,EACd,aAAa,CAAC,UAAU,6BAAc,aAAY;AAAA,EAClD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS;AAAA,EACT,MAAM;AACR;AA+BO,MAAM,YAAoB;AAAA,EAY/B,YAAY,iBAA6C,gBAAgB;AAVjE,SAAA,mCAA+C,IAAI;AAC3D,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAkB;AAC1B,SAAQ,SAAuC,CAAC;AAChD,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,sBAA+D,CAAC;AACxE,SAAQ,sBAAuD,CAAC;AAChE,SAAQ,eAAe;AAIrB,SAAK,WAAW,EAAE,GAAG,gBAAgB,GAAG,eAAe;AAClD,SAAA,WAAW,KAAK,SAAS;AAE9B,aAAS,IAAI,GAAG,IAAI,KAAK,SAAS,aAAa,QAAQ,KAAK;AAC1D,YAAM,OAAO,KAAK,SAAS,aAAa,CAAC;AACzC,YAAM,SAAS,MAAM,KAAK,SAAS,aAAa,SAAS;AACzD,WAAK,QAAQ,MAAM,KAAK,SAAS,YAAY,MAAM;AAAA,IAAA;AAAA,EACrD;AAAA;AAAA;AAAA;AAA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AAA;AAAA,EAMF,UAAU,IAAsC;AACzC,SAAA,oBAAoB,KAAK,EAAE;AAChC,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAEJ;AAmBgB,SAAA,WAAmB,UAAsC,IAAI;AACrE,QAAA,SAAS,IAAI,YAAoB,EAAE,GAAG,SAAS,SAAS,MAAM;AAC7D,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
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>, 'started'> = {},\n) {\n const queuer = new AsyncQueuer<TValue>(options)\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":"AA0EA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,oBAAoB;AAAA,EACpB,cAAc,MAAM;AAAA,EACpB,cAAc;AAAA,EACd,aAAa,CAAC,UAAU,6BAAc,aAAY;AAAA,EAClD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS;AAAA,EACT,MAAM;AACR;AAgCO,MAAM,YAAoB;AAAA,EAc/B,YAAY,iBAA6C,gBAAgB;AAZjE,SAAA,mCAA+C,IAAI;AAC3D,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAkB;AAC1B,SAAQ,mBAAmB;AAC3B,SAAQ,SAAuC,CAAC;AAChD,SAAQ,kBAAiC,CAAC;AAC1C,SAAQ,oBAAmD,CAAC;AAC5D,SAAQ,sBAA+D,CAAC;AACxE,SAAQ,sBAAuD,CAAC;AAChE,SAAQ,eAAe;AAIrB,SAAK,WAAW,EAAE,GAAG,gBAAgB,GAAG,eAAe;AAClD,SAAA,WAAW,KAAK,SAAS;AAE9B,aAAS,IAAI,GAAG,IAAI,KAAK,SAAS,aAAa,QAAQ,KAAK;AAC1D,YAAM,OAAO,KAAK,SAAS,aAAa,CAAC;AACzC,YAAM,SAAS,MAAM,KAAK,SAAS,aAAa,SAAS;AACzD,WAAK,QAAQ,MAAM,KAAK,SAAS,YAAY,MAAM;AAAA,IAAA;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;A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C,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;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;"}
|
|
@@ -2,7 +2,7 @@ import { AnyAsyncFunction } from './types.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Options for configuring an async rate-limited function
|
|
4
4
|
*/
|
|
5
|
-
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction
|
|
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
8
|
* Defaults to true.
|
|
@@ -15,19 +15,30 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
15
15
|
/**
|
|
16
16
|
* Optional error handler for when the rate-limited function throws
|
|
17
17
|
*/
|
|
18
|
-
onError?: (error: unknown) => void;
|
|
18
|
+
onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
21
|
+
*/
|
|
22
|
+
onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
19
23
|
/**
|
|
20
24
|
* Optional function to call when the rate-limited function is executed
|
|
21
25
|
*/
|
|
22
|
-
|
|
26
|
+
onSettled?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
23
27
|
/**
|
|
24
|
-
* Optional
|
|
28
|
+
* Optional function to call when the rate-limited function is executed
|
|
25
29
|
*/
|
|
26
|
-
|
|
30
|
+
onSuccess?: (result: ReturnType<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
27
31
|
/**
|
|
28
32
|
* Time window in milliseconds within which the limit applies
|
|
29
33
|
*/
|
|
30
34
|
window: number;
|
|
35
|
+
/**
|
|
36
|
+
* Type of window to use for rate limiting
|
|
37
|
+
* - 'fixed': Uses a fixed window that resets after the window period
|
|
38
|
+
* - 'sliding': Uses a sliding window that allows executions as old ones expire
|
|
39
|
+
* Defaults to 'fixed'
|
|
40
|
+
*/
|
|
41
|
+
windowType?: 'fixed' | 'sliding';
|
|
31
42
|
}
|
|
32
43
|
/**
|
|
33
44
|
* A class that creates an async rate-limited function.
|
|
@@ -36,6 +47,16 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
36
47
|
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
37
48
|
* all executions happen immediately, followed by a complete block.
|
|
38
49
|
*
|
|
50
|
+
* The rate limiter supports two types of windows:
|
|
51
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
52
|
+
* towards the limit, and the window resets completely after the period.
|
|
53
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
54
|
+
* consistent rate of execution over time.
|
|
55
|
+
*
|
|
56
|
+
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
57
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
58
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
59
|
+
*
|
|
39
60
|
* For smoother execution patterns, consider using:
|
|
40
61
|
* - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)
|
|
41
62
|
* - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)
|
|
@@ -47,29 +68,34 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
47
68
|
* ```ts
|
|
48
69
|
* const rateLimiter = new AsyncRateLimiter(
|
|
49
70
|
* async (id: string) => await api.getData(id),
|
|
50
|
-
* { limit: 5, window: 1000 } // 5 calls per second
|
|
71
|
+
* { limit: 5, window: 1000, windowType: 'sliding' } // 5 calls per second with sliding window
|
|
51
72
|
* );
|
|
52
73
|
*
|
|
53
74
|
* // Will execute immediately until limit reached, then block
|
|
54
|
-
*
|
|
75
|
+
* // Returns the API response directly
|
|
76
|
+
* const data = await rateLimiter.maybeExecute('123');
|
|
55
77
|
* ```
|
|
56
78
|
*/
|
|
57
|
-
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
79
|
+
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
58
80
|
private fn;
|
|
59
|
-
private _executionCount;
|
|
60
|
-
private _executionTimes;
|
|
61
81
|
private _options;
|
|
82
|
+
private _errorCount;
|
|
83
|
+
private _executionTimes;
|
|
84
|
+
private _lastResult;
|
|
62
85
|
private _rejectionCount;
|
|
63
|
-
|
|
86
|
+
private _settleCount;
|
|
87
|
+
private _successCount;
|
|
88
|
+
private _isExecuting;
|
|
89
|
+
constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>);
|
|
64
90
|
/**
|
|
65
91
|
* Updates the rate limiter options
|
|
66
92
|
* Returns the new options state
|
|
67
93
|
*/
|
|
68
|
-
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn
|
|
94
|
+
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>): void;
|
|
69
95
|
/**
|
|
70
96
|
* Returns the current rate limiter options
|
|
71
97
|
*/
|
|
72
|
-
getOptions(): Required<AsyncRateLimiterOptions<TFn
|
|
98
|
+
getOptions(): Required<AsyncRateLimiterOptions<TFn>>;
|
|
73
99
|
/**
|
|
74
100
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
75
101
|
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
@@ -86,26 +112,40 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
86
112
|
* await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
|
|
87
113
|
* ```
|
|
88
114
|
*/
|
|
89
|
-
maybeExecute(...args:
|
|
115
|
+
maybeExecute(...args: Parameters<TFn>): Promise<ReturnType<TFn> | undefined>;
|
|
90
116
|
private executeFunction;
|
|
91
117
|
private rejectFunction;
|
|
92
118
|
private cleanupOldExecutions;
|
|
119
|
+
/**
|
|
120
|
+
* Returns the number of remaining executions allowed in the current window
|
|
121
|
+
*/
|
|
122
|
+
getRemainingInWindow(): number;
|
|
123
|
+
/**
|
|
124
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
125
|
+
* For fixed windows, this is the time until the current window resets
|
|
126
|
+
* For sliding windows, this is the time until the oldest execution expires
|
|
127
|
+
*/
|
|
128
|
+
getMsUntilNextWindow(): number;
|
|
93
129
|
/**
|
|
94
130
|
* Returns the number of times the function has been executed
|
|
95
131
|
*/
|
|
96
|
-
|
|
132
|
+
getSuccessCount(): number;
|
|
97
133
|
/**
|
|
98
|
-
* Returns the number of times the function has been
|
|
134
|
+
* Returns the number of times the function has been settled
|
|
99
135
|
*/
|
|
100
|
-
|
|
136
|
+
getSettleCount(): number;
|
|
101
137
|
/**
|
|
102
|
-
* Returns the number of
|
|
138
|
+
* Returns the number of times the function has errored
|
|
103
139
|
*/
|
|
104
|
-
|
|
140
|
+
getErrorCount(): number;
|
|
105
141
|
/**
|
|
106
|
-
* Returns the number of
|
|
142
|
+
* Returns the number of times the function has been rejected
|
|
107
143
|
*/
|
|
108
|
-
|
|
144
|
+
getRejectionCount(): number;
|
|
145
|
+
/**
|
|
146
|
+
* Returns whether the function is currently executing
|
|
147
|
+
*/
|
|
148
|
+
getIsExecuting(): boolean;
|
|
109
149
|
/**
|
|
110
150
|
* Resets the rate limiter state
|
|
111
151
|
*/
|
|
@@ -114,6 +154,16 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
114
154
|
/**
|
|
115
155
|
* Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
116
156
|
*
|
|
157
|
+
* Unlike the non-async rate limiter, this async version supports returning values from the rate-limited function,
|
|
158
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
159
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
160
|
+
*
|
|
161
|
+
* The rate limiter supports two types of windows:
|
|
162
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
163
|
+
* towards the limit, and the window resets completely after the period.
|
|
164
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
165
|
+
* consistent rate of execution over time.
|
|
166
|
+
*
|
|
117
167
|
* Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:
|
|
118
168
|
* - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets
|
|
119
169
|
* - A throttler ensures even spacing between executions, which can be better for consistent performance
|
|
@@ -124,10 +174,11 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
124
174
|
*
|
|
125
175
|
* @example
|
|
126
176
|
* ```ts
|
|
127
|
-
* // Rate limit to 5 calls per minute
|
|
177
|
+
* // Rate limit to 5 calls per minute with a sliding window
|
|
128
178
|
* const rateLimited = asyncRateLimit(makeApiCall, {
|
|
129
179
|
* limit: 5,
|
|
130
180
|
* window: 60000,
|
|
181
|
+
* windowType: 'sliding',
|
|
131
182
|
* onReject: (rateLimiter) => {
|
|
132
183
|
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
133
184
|
* }
|
|
@@ -135,10 +186,11 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
135
186
|
*
|
|
136
187
|
* // First 5 calls will execute immediately
|
|
137
188
|
* // Additional calls will be rejected until the minute window resets
|
|
138
|
-
*
|
|
189
|
+
* // Returns the API response directly
|
|
190
|
+
* const result = await rateLimited();
|
|
139
191
|
*
|
|
140
192
|
* // For more even execution, consider using throttle instead:
|
|
141
193
|
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
142
194
|
* ```
|
|
143
195
|
*/
|
|
144
|
-
export declare function asyncRateLimit<TFn extends AnyAsyncFunction
|
|
196
|
+
export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: Omit<AsyncRateLimiterOptions<TFn>, 'enabled'>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
enabled: true,
|
|
3
|
+
onError: () => {
|
|
4
|
+
},
|
|
3
5
|
onReject: () => {
|
|
4
6
|
},
|
|
5
|
-
|
|
7
|
+
onSettled: () => {
|
|
6
8
|
},
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
+
onSuccess: () => {
|
|
10
|
+
},
|
|
11
|
+
windowType: "fixed"
|
|
9
12
|
};
|
|
10
13
|
class AsyncRateLimiter {
|
|
11
14
|
constructor(fn, initialOptions) {
|
|
12
15
|
this.fn = fn;
|
|
13
|
-
this.
|
|
16
|
+
this._errorCount = 0;
|
|
14
17
|
this._executionTimes = [];
|
|
15
18
|
this._rejectionCount = 0;
|
|
19
|
+
this._settleCount = 0;
|
|
20
|
+
this._successCount = 0;
|
|
21
|
+
this._isExecuting = false;
|
|
16
22
|
this._options = {
|
|
17
23
|
...defaultOptions,
|
|
18
24
|
...initialOptions
|
|
@@ -23,11 +29,7 @@ class AsyncRateLimiter {
|
|
|
23
29
|
* Returns the new options state
|
|
24
30
|
*/
|
|
25
31
|
setOptions(newOptions) {
|
|
26
|
-
this._options = {
|
|
27
|
-
...this._options,
|
|
28
|
-
...newOptions
|
|
29
|
-
};
|
|
30
|
-
return this._options;
|
|
32
|
+
this._options = { ...this._options, ...newOptions };
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
35
|
* Returns the current rate limiter options
|
|
@@ -53,33 +55,42 @@ class AsyncRateLimiter {
|
|
|
53
55
|
*/
|
|
54
56
|
async maybeExecute(...args) {
|
|
55
57
|
this.cleanupOldExecutions();
|
|
56
|
-
if (this.
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
if (this._options.windowType === "sliding") {
|
|
59
|
+
if (this._executionTimes.length < this._options.limit) {
|
|
60
|
+
await this.executeFunction(...args);
|
|
61
|
+
return this._lastResult;
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
const now = Date.now();
|
|
65
|
+
const oldestExecution = Math.min(...this._executionTimes);
|
|
66
|
+
const isNewWindow = oldestExecution + this._options.window <= now;
|
|
67
|
+
if (isNewWindow || this._executionTimes.length < this._options.limit) {
|
|
68
|
+
await this.executeFunction(...args);
|
|
69
|
+
return this._lastResult;
|
|
70
|
+
}
|
|
59
71
|
}
|
|
60
72
|
this.rejectFunction();
|
|
61
|
-
return
|
|
73
|
+
return void 0;
|
|
62
74
|
}
|
|
63
75
|
async executeFunction(...args) {
|
|
64
|
-
var _a, _b;
|
|
76
|
+
var _a, _b, _c, _d, _e, _f;
|
|
65
77
|
if (!this._options.enabled) return;
|
|
78
|
+
this._isExecuting = true;
|
|
66
79
|
const now = Date.now();
|
|
67
|
-
this._executionCount++;
|
|
68
80
|
this._executionTimes.push(now);
|
|
69
81
|
try {
|
|
70
|
-
await this.fn(...args);
|
|
82
|
+
this._lastResult = await this.fn(...args);
|
|
83
|
+
this._successCount++;
|
|
84
|
+
(_b = (_a = this._options).onSuccess) == null ? void 0 : _b.call(_a, this._lastResult, this);
|
|
71
85
|
} catch (error) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this._options.onError(error);
|
|
75
|
-
} catch (error2) {
|
|
76
|
-
console.error("Error in onError handler", error2);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
throw error;
|
|
86
|
+
this._errorCount++;
|
|
87
|
+
(_d = (_c = this._options).onError) == null ? void 0 : _d.call(_c, error, this);
|
|
80
88
|
} finally {
|
|
81
|
-
|
|
89
|
+
this._isExecuting = false;
|
|
90
|
+
this._settleCount++;
|
|
91
|
+
(_f = (_e = this._options).onSettled) == null ? void 0 : _f.call(_e, this);
|
|
82
92
|
}
|
|
93
|
+
return this._lastResult;
|
|
83
94
|
}
|
|
84
95
|
rejectFunction() {
|
|
85
96
|
this._rejectionCount++;
|
|
@@ -94,38 +105,64 @@ class AsyncRateLimiter {
|
|
|
94
105
|
(time) => time > windowStart
|
|
95
106
|
);
|
|
96
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns the number of remaining executions allowed in the current window
|
|
110
|
+
*/
|
|
111
|
+
getRemainingInWindow() {
|
|
112
|
+
this.cleanupOldExecutions();
|
|
113
|
+
return Math.max(0, this._options.limit - this._executionTimes.length);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
117
|
+
* For fixed windows, this is the time until the current window resets
|
|
118
|
+
* For sliding windows, this is the time until the oldest execution expires
|
|
119
|
+
*/
|
|
120
|
+
getMsUntilNextWindow() {
|
|
121
|
+
if (this.getRemainingInWindow() > 0) {
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
const oldestExecution = Math.min(...this._executionTimes);
|
|
125
|
+
return oldestExecution + this._options.window - Date.now();
|
|
126
|
+
}
|
|
97
127
|
/**
|
|
98
128
|
* Returns the number of times the function has been executed
|
|
99
129
|
*/
|
|
100
|
-
|
|
101
|
-
return this.
|
|
130
|
+
getSuccessCount() {
|
|
131
|
+
return this._successCount;
|
|
102
132
|
}
|
|
103
133
|
/**
|
|
104
|
-
* Returns the number of times the function has been
|
|
134
|
+
* Returns the number of times the function has been settled
|
|
105
135
|
*/
|
|
106
|
-
|
|
107
|
-
return this.
|
|
136
|
+
getSettleCount() {
|
|
137
|
+
return this._settleCount;
|
|
108
138
|
}
|
|
109
139
|
/**
|
|
110
|
-
* Returns the number of
|
|
140
|
+
* Returns the number of times the function has errored
|
|
111
141
|
*/
|
|
112
|
-
|
|
113
|
-
this.
|
|
114
|
-
return Math.max(0, this._options.limit - this._executionTimes.length);
|
|
142
|
+
getErrorCount() {
|
|
143
|
+
return this._errorCount;
|
|
115
144
|
}
|
|
116
145
|
/**
|
|
117
|
-
* Returns the number of
|
|
146
|
+
* Returns the number of times the function has been rejected
|
|
118
147
|
*/
|
|
119
|
-
|
|
120
|
-
return this.
|
|
148
|
+
getRejectionCount() {
|
|
149
|
+
return this._rejectionCount;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Returns whether the function is currently executing
|
|
153
|
+
*/
|
|
154
|
+
getIsExecuting() {
|
|
155
|
+
return this._isExecuting;
|
|
121
156
|
}
|
|
122
157
|
/**
|
|
123
158
|
* Resets the rate limiter state
|
|
124
159
|
*/
|
|
125
160
|
reset() {
|
|
126
161
|
this._executionTimes = [];
|
|
127
|
-
this.
|
|
162
|
+
this._successCount = 0;
|
|
163
|
+
this._errorCount = 0;
|
|
128
164
|
this._rejectionCount = 0;
|
|
165
|
+
this._settleCount = 0;
|
|
129
166
|
}
|
|
130
167
|
}
|
|
131
168
|
function asyncRateLimit(fn, initialOptions) {
|