@tanstack/pacer 0.1.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/LICENSE +21 -0
- package/dist/cjs/async-debouncer.cjs +109 -0
- package/dist/cjs/async-debouncer.cjs.map +1 -0
- package/dist/cjs/async-debouncer.d.cts +99 -0
- package/dist/cjs/async-queuer.cjs +293 -0
- package/dist/cjs/async-queuer.cjs.map +1 -0
- package/dist/cjs/async-queuer.d.cts +197 -0
- package/dist/cjs/async-rate-limiter.cjs +122 -0
- package/dist/cjs/async-rate-limiter.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter.d.cts +132 -0
- package/dist/cjs/async-throttler.cjs +115 -0
- package/dist/cjs/async-throttler.cjs.map +1 -0
- package/dist/cjs/async-throttler.d.cts +93 -0
- package/dist/cjs/compare.cjs +72 -0
- package/dist/cjs/compare.cjs.map +1 -0
- package/dist/cjs/compare.d.cts +12 -0
- package/dist/cjs/debouncer.cjs +74 -0
- package/dist/cjs/debouncer.cjs.map +1 -0
- package/dist/cjs/debouncer.d.cts +96 -0
- package/dist/cjs/index.cjs +32 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +9 -0
- package/dist/cjs/queuer.cjs +231 -0
- package/dist/cjs/queuer.cjs.map +1 -0
- package/dist/cjs/queuer.d.cts +221 -0
- package/dist/cjs/rate-limiter.cjs +115 -0
- package/dist/cjs/rate-limiter.cjs.map +1 -0
- package/dist/cjs/rate-limiter.d.cts +147 -0
- package/dist/cjs/throttler.cjs +120 -0
- package/dist/cjs/throttler.cjs.map +1 -0
- package/dist/cjs/throttler.d.cts +139 -0
- package/dist/esm/async-debouncer.d.ts +99 -0
- package/dist/esm/async-debouncer.js +109 -0
- package/dist/esm/async-debouncer.js.map +1 -0
- package/dist/esm/async-queuer.d.ts +197 -0
- package/dist/esm/async-queuer.js +293 -0
- package/dist/esm/async-queuer.js.map +1 -0
- package/dist/esm/async-rate-limiter.d.ts +132 -0
- package/dist/esm/async-rate-limiter.js +122 -0
- package/dist/esm/async-rate-limiter.js.map +1 -0
- package/dist/esm/async-throttler.d.ts +93 -0
- package/dist/esm/async-throttler.js +115 -0
- package/dist/esm/async-throttler.js.map +1 -0
- package/dist/esm/compare.d.ts +12 -0
- package/dist/esm/compare.js +72 -0
- package/dist/esm/compare.js.map +1 -0
- package/dist/esm/debouncer.d.ts +96 -0
- package/dist/esm/debouncer.js +74 -0
- package/dist/esm/debouncer.js.map +1 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/queuer.d.ts +221 -0
- package/dist/esm/queuer.js +231 -0
- package/dist/esm/queuer.js.map +1 -0
- package/dist/esm/rate-limiter.d.ts +147 -0
- package/dist/esm/rate-limiter.js +115 -0
- package/dist/esm/rate-limiter.js.map +1 -0
- package/dist/esm/throttler.d.ts +139 -0
- package/dist/esm/throttler.js +120 -0
- package/dist/esm/throttler.js.map +1 -0
- package/package.json +150 -0
- package/src/async-debouncer.ts +197 -0
- package/src/async-queuer.ts +447 -0
- package/src/async-rate-limiter.ts +228 -0
- package/src/async-throttler.ts +194 -0
- package/src/compare.ts +105 -0
- package/src/debouncer.ts +164 -0
- package/src/index.ts +9 -0
- package/src/queuer.ts +394 -0
- package/src/rate-limiter.ts +236 -0
- package/src/throttler.ts +222 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
addItemsTo: "back",
|
|
3
|
+
concurrency: 1,
|
|
4
|
+
getItemsFrom: "front",
|
|
5
|
+
getPriority: (item) => item.priority ?? 0,
|
|
6
|
+
initialItems: [],
|
|
7
|
+
maxSize: Infinity,
|
|
8
|
+
onGetNextItem: () => {
|
|
9
|
+
},
|
|
10
|
+
onUpdate: () => {
|
|
11
|
+
},
|
|
12
|
+
started: false,
|
|
13
|
+
wait: 0
|
|
14
|
+
};
|
|
15
|
+
class AsyncQueuer {
|
|
16
|
+
constructor(initialOptions = defaultOptions) {
|
|
17
|
+
this.items = [];
|
|
18
|
+
this.activeItems = /* @__PURE__ */ new Set();
|
|
19
|
+
this.onSuccessCallbacks = [];
|
|
20
|
+
this.onErrorCallbacks = [];
|
|
21
|
+
this.onSettledCallbacks = [];
|
|
22
|
+
this.pendingTick = false;
|
|
23
|
+
this.executionCount = 0;
|
|
24
|
+
this.options = { ...defaultOptions, ...initialOptions };
|
|
25
|
+
this.running = this.options.started;
|
|
26
|
+
for (let i = 0; i < this.options.initialItems.length; i++) {
|
|
27
|
+
const item = this.options.initialItems[i];
|
|
28
|
+
const isLast = i === this.options.initialItems.length - 1;
|
|
29
|
+
this.addItem(item, this.options.addItemsTo, isLast);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Updates the queuer options
|
|
34
|
+
* Returns the new options state
|
|
35
|
+
*/
|
|
36
|
+
setOptions(newOptions) {
|
|
37
|
+
this.options = { ...this.options, ...newOptions };
|
|
38
|
+
return this.options;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Processes items in the queuer
|
|
42
|
+
*/
|
|
43
|
+
tick() {
|
|
44
|
+
if (!this.running) {
|
|
45
|
+
this.pendingTick = false;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
while (this.activeItems.size < this.options.concurrency && !this.isEmpty()) {
|
|
49
|
+
const nextFn = this.getNextItem();
|
|
50
|
+
if (!nextFn) {
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
this.activeItems.add(nextFn);
|
|
54
|
+
(async () => {
|
|
55
|
+
let success = false;
|
|
56
|
+
let res;
|
|
57
|
+
let error;
|
|
58
|
+
try {
|
|
59
|
+
res = await nextFn();
|
|
60
|
+
success = true;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
error = e;
|
|
63
|
+
} finally {
|
|
64
|
+
this.options.onUpdate(this);
|
|
65
|
+
}
|
|
66
|
+
this.activeItems.delete(nextFn);
|
|
67
|
+
if (success) {
|
|
68
|
+
this.onSuccessCallbacks.forEach((cb) => cb(res));
|
|
69
|
+
} else {
|
|
70
|
+
this.onErrorCallbacks.forEach((cb) => cb(error));
|
|
71
|
+
}
|
|
72
|
+
this.onSettledCallbacks.forEach((cb) => cb(success ? res : error));
|
|
73
|
+
if (this.options.wait > 0) {
|
|
74
|
+
setTimeout(() => this.tick(), this.options.wait);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.tick();
|
|
78
|
+
})();
|
|
79
|
+
}
|
|
80
|
+
this.pendingTick = false;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Adds a task to the queuer
|
|
84
|
+
*/
|
|
85
|
+
addItem(fn, position = this.options.addItemsTo, runOnUpdate = true) {
|
|
86
|
+
if (this.isFull()) {
|
|
87
|
+
return Promise.reject(new Error("Queuer is full"));
|
|
88
|
+
}
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
const task = Object.assign(
|
|
91
|
+
async () => {
|
|
92
|
+
try {
|
|
93
|
+
const result = await fn();
|
|
94
|
+
resolve(result);
|
|
95
|
+
return result;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
reject(error);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
{ priority: fn.priority ?? void 0 }
|
|
102
|
+
);
|
|
103
|
+
const priority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(task) : task.priority;
|
|
104
|
+
if (priority !== void 0) {
|
|
105
|
+
const insertIndex = this.items.findIndex((existing) => {
|
|
106
|
+
const existingPriority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(existing) : existing.priority;
|
|
107
|
+
return existingPriority > priority;
|
|
108
|
+
});
|
|
109
|
+
if (insertIndex === -1) {
|
|
110
|
+
this.items.push(task);
|
|
111
|
+
} else {
|
|
112
|
+
this.items.splice(insertIndex, 0, task);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
if (position === "front") {
|
|
116
|
+
this.items.unshift(task);
|
|
117
|
+
} else {
|
|
118
|
+
this.items.push(task);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (runOnUpdate) {
|
|
122
|
+
this.options.onUpdate(this);
|
|
123
|
+
}
|
|
124
|
+
if (this.running && !this.pendingTick) {
|
|
125
|
+
this.pendingTick = true;
|
|
126
|
+
this.tick();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Removes and returns an item from the queuer
|
|
132
|
+
*/
|
|
133
|
+
getNextItem(position = this.options.getItemsFrom) {
|
|
134
|
+
let item;
|
|
135
|
+
if (position === "front") {
|
|
136
|
+
item = this.items.shift();
|
|
137
|
+
} else {
|
|
138
|
+
item = this.items.pop();
|
|
139
|
+
}
|
|
140
|
+
if (item !== void 0) {
|
|
141
|
+
this.executionCount++;
|
|
142
|
+
this.options.onUpdate(this);
|
|
143
|
+
this.options.onGetNextItem(item, this);
|
|
144
|
+
}
|
|
145
|
+
return item;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Returns an item without removing it
|
|
149
|
+
*/
|
|
150
|
+
peek(position = "front") {
|
|
151
|
+
if (position === "front") {
|
|
152
|
+
return this.items[0];
|
|
153
|
+
}
|
|
154
|
+
return this.items[this.items.length - 1];
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Returns true if the queuer is empty
|
|
158
|
+
*/
|
|
159
|
+
isEmpty() {
|
|
160
|
+
return this.items.length === 0;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Returns true if the queuer is full
|
|
164
|
+
*/
|
|
165
|
+
isFull() {
|
|
166
|
+
return this.items.length >= this.options.maxSize;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Returns the current size of the queuer
|
|
170
|
+
*/
|
|
171
|
+
size() {
|
|
172
|
+
return this.items.length;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Removes all items from the queuer
|
|
176
|
+
*/
|
|
177
|
+
clear() {
|
|
178
|
+
this.items = [];
|
|
179
|
+
this.options.onUpdate(this);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Resets the queuer to its initial state
|
|
183
|
+
*/
|
|
184
|
+
reset(withInitialItems) {
|
|
185
|
+
this.clear();
|
|
186
|
+
this.executionCount = 0;
|
|
187
|
+
if (withInitialItems) {
|
|
188
|
+
this.items = [...this.options.initialItems];
|
|
189
|
+
}
|
|
190
|
+
this.running = this.options.started;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns a copy of all items in the queuer
|
|
194
|
+
*/
|
|
195
|
+
getAllItems() {
|
|
196
|
+
return [...this.items];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns the number of items that have been removed from the queuer
|
|
200
|
+
*/
|
|
201
|
+
getExecutionCount() {
|
|
202
|
+
return this.executionCount;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Returns the active items
|
|
206
|
+
*/
|
|
207
|
+
getActiveItems() {
|
|
208
|
+
return Array.from(this.activeItems);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns the pending items
|
|
212
|
+
*/
|
|
213
|
+
getPendingItems() {
|
|
214
|
+
return this.getAllItems();
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Adds a callback to be called when a task succeeds
|
|
218
|
+
*/
|
|
219
|
+
onSuccess(cb) {
|
|
220
|
+
this.onSuccessCallbacks.push(cb);
|
|
221
|
+
return () => {
|
|
222
|
+
this.onSuccessCallbacks = this.onSuccessCallbacks.filter((d) => d !== cb);
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Adds a callback to be called when a task errors
|
|
227
|
+
*/
|
|
228
|
+
onError(cb) {
|
|
229
|
+
this.onErrorCallbacks.push(cb);
|
|
230
|
+
return () => {
|
|
231
|
+
this.onErrorCallbacks = this.onErrorCallbacks.filter((d) => d !== cb);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Adds a callback to be called when a task is settled
|
|
236
|
+
*/
|
|
237
|
+
onSettled(cb) {
|
|
238
|
+
this.onSettledCallbacks.push(cb);
|
|
239
|
+
return () => {
|
|
240
|
+
this.onSettledCallbacks = this.onSettledCallbacks.filter((d) => d !== cb);
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Starts the queuer and processes items
|
|
245
|
+
*/
|
|
246
|
+
start() {
|
|
247
|
+
this.running = true;
|
|
248
|
+
if (!this.pendingTick && !this.isEmpty()) {
|
|
249
|
+
this.pendingTick = true;
|
|
250
|
+
this.tick();
|
|
251
|
+
}
|
|
252
|
+
this.options.onUpdate(this);
|
|
253
|
+
return new Promise((resolve) => {
|
|
254
|
+
const checkIdle = () => {
|
|
255
|
+
if (this.isIdle()) {
|
|
256
|
+
resolve();
|
|
257
|
+
} else {
|
|
258
|
+
setTimeout(checkIdle, 100);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
checkIdle();
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Stops the queuer from processing items
|
|
266
|
+
*/
|
|
267
|
+
stop() {
|
|
268
|
+
this.running = false;
|
|
269
|
+
this.pendingTick = false;
|
|
270
|
+
this.options.onUpdate(this);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns true if the queuer is running
|
|
274
|
+
*/
|
|
275
|
+
isRunning() {
|
|
276
|
+
return this.running;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Returns true if the queuer is running but has no items to process
|
|
280
|
+
*/
|
|
281
|
+
isIdle() {
|
|
282
|
+
return this.running && this.isEmpty() && this.activeItems.size === 0;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function asyncQueue(options = {}) {
|
|
286
|
+
const queuer = new AsyncQueuer({ ...options, started: true });
|
|
287
|
+
return queuer.addItem.bind(queuer);
|
|
288
|
+
}
|
|
289
|
+
export {
|
|
290
|
+
AsyncQueuer,
|
|
291
|
+
asyncQueue
|
|
292
|
+
};
|
|
293
|
+
//# sourceMappingURL=async-queuer.js.map
|
|
@@ -0,0 +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 an item is added or removed from the queuer\n */\n onUpdate?: (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 onUpdate: () => {},\n started: false,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queuer that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queuer 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) queuer 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 queuer = new AsyncQueuer<string>({ concurrency: 2 });\n *\n * queuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * queuer.start();\n *\n * queuer.onSuccess((result) => {\n * console.log(result); // 'Hello'\n * });\n * ```\n */\nexport class AsyncQueuer<TValue> {\n protected options: Required<AsyncQueuerOptions<TValue>>\n private items: Array<() => Promise<TValue>> = []\n private activeItems: Set<() => Promise<TValue>> = new Set()\n private onSuccessCallbacks: Array<(result: TValue) => void> = []\n private onErrorCallbacks: Array<(error: Error) => void> = []\n private onSettledCallbacks: Array<(result: TValue | Error) => void> = []\n private running: boolean\n private pendingTick = false\n private executionCount = 0\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 * Processes items in the queuer\n */\n protected tick() {\n if (!this.running) {\n this.pendingTick = false\n return\n }\n\n while (\n this.activeItems.size < this.options.concurrency &&\n !this.isEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n\n this.activeItems.add(nextFn)\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.options.onUpdate(this)\n }\n\n this.activeItems.delete(nextFn)\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 * 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.isFull()) {\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.onUpdate(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.onUpdate(this)\n this.options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n peek(position: QueuePosition = 'front'): (() => 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 isEmpty(): boolean {\n return this.items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n isFull(): boolean {\n return this.items.length >= this.options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n size(): number {\n return this.items.length\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this.items = []\n this.options.onUpdate(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 * Returns a copy of all items in the queuer\n */\n getAllItems(): 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 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.getAllItems()\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((d) => d !== cb)\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((d) => d !== cb)\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.isEmpty()) {\n this.pendingTick = true\n this.tick()\n }\n this.options.onUpdate(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.isIdle()) {\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.onUpdate(this)\n }\n\n /**\n * Returns true if the queuer is running\n */\n isRunning(): boolean {\n return this.running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n isIdle(): boolean {\n return this.running && this.isEmpty() && this.activeItems.size === 0\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":"AAoDA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,cAAc;AAAA,EACd,aAAa,CAAC,SAAU,KAAa,YAAY;AAAA,EACjD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS;AAAA,EACT,MAAM;AACR;AA+BO,MAAM,YAAoB;AAAA,EAW/B,YAAY,iBAA6C,gBAAgB;AATzE,SAAQ,QAAsC,CAAC;AACvC,SAAA,kCAA8C,IAAI;AAC1D,SAAQ,qBAAsD,CAAC;AAC/D,SAAQ,mBAAkD,CAAC;AAC3D,SAAQ,qBAA8D,CAAC;AA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;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMH,OAAa;AACX,SAAK,UAAU;AACf,SAAK,cAAc;AACd,SAAA,QAAQ,SAAS,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM5B,YAAqB;AACnB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,SAAkB;AAChB,WAAO,KAAK,WAAW,KAAK,QAAa,KAAA,KAAK,YAAY,SAAS;AAAA,EAAA;AAEvE;AAmBgB,SAAA,WAAmB,UAAsC,IAAI;AACrE,QAAA,SAAS,IAAI,YAAoB,EAAE,GAAG,SAAS,SAAS,MAAM;AAC7D,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { RateLimitRejectionInfo } from './rate-limiter.js';
|
|
2
|
+
/**
|
|
3
|
+
* Options for configuring an async rate-limited function
|
|
4
|
+
*/
|
|
5
|
+
export interface AsyncRateLimiterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
8
|
+
* Defaults to true.
|
|
9
|
+
*/
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Maximum number of executions allowed within the time window
|
|
13
|
+
*/
|
|
14
|
+
limit: number;
|
|
15
|
+
/**
|
|
16
|
+
* Time window in milliseconds within which the limit applies
|
|
17
|
+
*/
|
|
18
|
+
window: number;
|
|
19
|
+
/**
|
|
20
|
+
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
21
|
+
*/
|
|
22
|
+
onReject?: (info: RateLimitRejectionInfo) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Optional error handler for when the rate-limited function throws
|
|
25
|
+
*/
|
|
26
|
+
onError?: (error: unknown) => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A class that creates an async rate-limited function.
|
|
30
|
+
*
|
|
31
|
+
* Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,
|
|
32
|
+
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
33
|
+
* all executions happen immediately, followed by a complete block.
|
|
34
|
+
*
|
|
35
|
+
* For smoother execution patterns, consider using:
|
|
36
|
+
* - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)
|
|
37
|
+
* - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)
|
|
38
|
+
*
|
|
39
|
+
* Rate limiting is best used for hard API limits or resource constraints. For UI updates or
|
|
40
|
+
* smoothing out frequent events, throttling or debouncing usually provide better user experience.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const rateLimiter = new AsyncRateLimiter(
|
|
45
|
+
* async (id: string) => await api.getData(id),
|
|
46
|
+
* { limit: 5, window: 1000 } // 5 calls per second
|
|
47
|
+
* );
|
|
48
|
+
*
|
|
49
|
+
* // Will execute immediately until limit reached, then block
|
|
50
|
+
* await rateLimiter.maybeExecute('123');
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare class AsyncRateLimiter<TFn extends (...args: Array<any>) => Promise<any>, TArgs extends Parameters<TFn>> {
|
|
54
|
+
private fn;
|
|
55
|
+
private executionCount;
|
|
56
|
+
private rejectionCount;
|
|
57
|
+
private executionTimes;
|
|
58
|
+
private options;
|
|
59
|
+
constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions);
|
|
60
|
+
/**
|
|
61
|
+
* Updates the rate limiter options
|
|
62
|
+
* Returns the new options state
|
|
63
|
+
*/
|
|
64
|
+
setOptions(newOptions: Partial<AsyncRateLimiterOptions>): AsyncRateLimiterOptions;
|
|
65
|
+
/**
|
|
66
|
+
* Returns the number of times the function has been executed
|
|
67
|
+
*/
|
|
68
|
+
getExecutionCount(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the number of times the function has been rejected
|
|
71
|
+
*/
|
|
72
|
+
getRejectionCount(): number;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the number of remaining executions allowed in the current window
|
|
75
|
+
*/
|
|
76
|
+
getRemainingInWindow(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Attempts to execute the rate-limited function if within the configured limits.
|
|
79
|
+
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
80
|
+
* If execution is allowed, waits for any previous execution to complete before proceeding.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
|
|
85
|
+
*
|
|
86
|
+
* // First 5 calls will execute
|
|
87
|
+
* await rateLimiter.maybeExecute('arg1', 'arg2');
|
|
88
|
+
*
|
|
89
|
+
* // Additional calls within the window will be rejected
|
|
90
|
+
* await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
maybeExecute(...args: TArgs): Promise<boolean>;
|
|
94
|
+
private executeFunction;
|
|
95
|
+
private rejectFunction;
|
|
96
|
+
private cleanupOldExecutions;
|
|
97
|
+
/**
|
|
98
|
+
* Resets the rate limiter state
|
|
99
|
+
*/
|
|
100
|
+
reset(): void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
104
|
+
*
|
|
105
|
+
* Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:
|
|
106
|
+
* - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets
|
|
107
|
+
* - A throttler ensures even spacing between executions, which can be better for consistent performance
|
|
108
|
+
* - A debouncer collapses multiple calls into one, which is better for handling bursts of events
|
|
109
|
+
*
|
|
110
|
+
* Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically
|
|
111
|
+
* need to enforce a hard limit on the number of executions within a time period.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* // Rate limit to 5 calls per minute
|
|
116
|
+
* const rateLimited = asyncRateLimit(makeApiCall, {
|
|
117
|
+
* limit: 5,
|
|
118
|
+
* window: 60000,
|
|
119
|
+
* onReject: ({ msUntilNextWindow }) => {
|
|
120
|
+
* console.log(`Rate limit exceeded. Try again in ${msUntilNextWindow}ms`);
|
|
121
|
+
* }
|
|
122
|
+
* });
|
|
123
|
+
*
|
|
124
|
+
* // First 5 calls will execute immediately
|
|
125
|
+
* // Additional calls will be rejected until the minute window resets
|
|
126
|
+
* await rateLimited();
|
|
127
|
+
*
|
|
128
|
+
* // For more even execution, consider using throttle instead:
|
|
129
|
+
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function asyncRateLimit<TFn extends (...args: Array<any>) => Promise<any>>(fn: TFn, initialOptions: Omit<AsyncRateLimiterOptions, 'enabled'>): (...args: Parameters<TFn>) => Promise<boolean>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
class AsyncRateLimiter {
|
|
2
|
+
constructor(fn, initialOptions) {
|
|
3
|
+
this.fn = fn;
|
|
4
|
+
this.executionCount = 0;
|
|
5
|
+
this.rejectionCount = 0;
|
|
6
|
+
this.executionTimes = [];
|
|
7
|
+
this.options = {
|
|
8
|
+
enabled: true,
|
|
9
|
+
...initialOptions
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Updates the rate limiter options
|
|
14
|
+
* Returns the new options state
|
|
15
|
+
*/
|
|
16
|
+
setOptions(newOptions) {
|
|
17
|
+
this.options = {
|
|
18
|
+
...this.options,
|
|
19
|
+
...newOptions
|
|
20
|
+
};
|
|
21
|
+
return this.options;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns the number of times the function has been executed
|
|
25
|
+
*/
|
|
26
|
+
getExecutionCount() {
|
|
27
|
+
return this.executionCount;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the number of times the function has been rejected
|
|
31
|
+
*/
|
|
32
|
+
getRejectionCount() {
|
|
33
|
+
return this.rejectionCount;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the number of remaining executions allowed in the current window
|
|
37
|
+
*/
|
|
38
|
+
getRemainingInWindow() {
|
|
39
|
+
this.cleanupOldExecutions();
|
|
40
|
+
return Math.max(0, this.options.limit - this.executionTimes.length);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Attempts to execute the rate-limited function if within the configured limits.
|
|
44
|
+
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
45
|
+
* If execution is allowed, waits for any previous execution to complete before proceeding.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
|
|
50
|
+
*
|
|
51
|
+
* // First 5 calls will execute
|
|
52
|
+
* await rateLimiter.maybeExecute('arg1', 'arg2');
|
|
53
|
+
*
|
|
54
|
+
* // Additional calls within the window will be rejected
|
|
55
|
+
* await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
async maybeExecute(...args) {
|
|
59
|
+
this.cleanupOldExecutions();
|
|
60
|
+
if (this.executionTimes.length < this.options.limit) {
|
|
61
|
+
await this.executeFunction(...args);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
this.rejectFunction();
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
async executeFunction(...args) {
|
|
68
|
+
if (!this.options.enabled) return;
|
|
69
|
+
const now = Date.now();
|
|
70
|
+
this.executionCount++;
|
|
71
|
+
this.executionTimes.push(now);
|
|
72
|
+
try {
|
|
73
|
+
await this.fn(...args);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (this.options.onError) {
|
|
76
|
+
try {
|
|
77
|
+
this.options.onError(error);
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
} finally {
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
rejectFunction() {
|
|
86
|
+
this.rejectionCount++;
|
|
87
|
+
if (this.options.onReject) {
|
|
88
|
+
const oldestExecution = Math.min(...this.executionTimes);
|
|
89
|
+
const msUntilNextWindow = oldestExecution + this.options.window - Date.now();
|
|
90
|
+
this.options.onReject({
|
|
91
|
+
msUntilNextWindow,
|
|
92
|
+
currentExecutions: this.executionTimes.length,
|
|
93
|
+
limit: this.options.limit,
|
|
94
|
+
rejectionCount: this.rejectionCount
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
cleanupOldExecutions() {
|
|
99
|
+
const now = Date.now();
|
|
100
|
+
const windowStart = now - this.options.window;
|
|
101
|
+
this.executionTimes = this.executionTimes.filter(
|
|
102
|
+
(time) => time > windowStart
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Resets the rate limiter state
|
|
107
|
+
*/
|
|
108
|
+
reset() {
|
|
109
|
+
this.executionTimes = [];
|
|
110
|
+
this.executionCount = 0;
|
|
111
|
+
this.rejectionCount = 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function asyncRateLimit(fn, initialOptions) {
|
|
115
|
+
const rateLimiter = new AsyncRateLimiter(fn, initialOptions);
|
|
116
|
+
return rateLimiter.maybeExecute.bind(rateLimiter);
|
|
117
|
+
}
|
|
118
|
+
export {
|
|
119
|
+
AsyncRateLimiter,
|
|
120
|
+
asyncRateLimit
|
|
121
|
+
};
|
|
122
|
+
//# sourceMappingURL=async-rate-limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-rate-limiter.js","sources":["../../src/async-rate-limiter.ts"],"sourcesContent":["import type { RateLimitRejectionInfo } from './rate-limiter'\n\n/**\n * Options for configuring an async rate-limited function\n */\nexport interface AsyncRateLimiterOptions {\n /**\n * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Maximum number of executions allowed within the time window\n */\n limit: number\n /**\n * Time window in milliseconds within which the limit applies\n */\n window: number\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (info: RateLimitRejectionInfo) => void\n /**\n * Optional error handler for when the rate-limited function throws\n */\n onError?: (error: unknown) => void\n}\n\n/**\n * A class that creates an async rate-limited function.\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(\n * async (id: string) => await api.getData(id),\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Will execute immediately until limit reached, then block\n * await rateLimiter.maybeExecute('123');\n * ```\n */\nexport class AsyncRateLimiter<\n TFn extends (...args: Array<any>) => Promise<any>,\n TArgs extends Parameters<TFn>,\n> {\n private executionCount = 0\n private rejectionCount = 0\n private executionTimes: Array<number> = []\n private options: AsyncRateLimiterOptions\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncRateLimiterOptions,\n ) {\n this.options = {\n enabled: true,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the rate limiter options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<AsyncRateLimiterOptions>,\n ): AsyncRateLimiterOptions {\n this.options = {\n ...this.options,\n ...newOptions,\n }\n return this.options\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this.executionCount\n }\n\n /**\n * Returns the number of times the function has been rejected\n */\n getRejectionCount(): number {\n return this.rejectionCount\n }\n\n /**\n * Returns the number of remaining executions allowed in the current window\n */\n getRemainingInWindow(): number {\n this.cleanupOldExecutions()\n return Math.max(0, this.options.limit - this.executionTimes.length)\n }\n\n /**\n * Attempts to execute the rate-limited function if within the configured limits.\n * Will reject execution if the number of calls in the current window exceeds the limit.\n * If execution is allowed, waits for any previous execution to complete before proceeding.\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });\n *\n * // First 5 calls will execute\n * await rateLimiter.maybeExecute('arg1', 'arg2');\n *\n * // Additional calls within the window will be rejected\n * await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected\n * ```\n */\n async maybeExecute(...args: TArgs): Promise<boolean> {\n this.cleanupOldExecutions()\n\n if (this.executionTimes.length < this.options.limit) {\n await this.executeFunction(...args)\n return true\n }\n\n this.rejectFunction()\n return false\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this.options.enabled) return\n const now = Date.now()\n this.executionCount++\n this.executionTimes.push(now)\n\n try {\n await this.fn(...args)\n } catch (error) {\n if (this.options.onError) {\n try {\n this.options.onError(error)\n } catch {\n // Ignore errors from error handler\n }\n }\n throw error\n } finally {\n }\n }\n\n private rejectFunction(): void {\n this.rejectionCount++\n if (this.options.onReject) {\n const oldestExecution = Math.min(...this.executionTimes)\n const msUntilNextWindow =\n oldestExecution + this.options.window - Date.now()\n\n this.options.onReject({\n msUntilNextWindow,\n currentExecutions: this.executionTimes.length,\n limit: this.options.limit,\n rejectionCount: this.rejectionCount,\n })\n }\n }\n\n private cleanupOldExecutions(): void {\n const now = Date.now()\n const windowStart = now - this.options.window\n this.executionTimes = this.executionTimes.filter(\n (time) => time > windowStart,\n )\n }\n\n /**\n * Resets the rate limiter state\n */\n reset(): void {\n this.executionTimes = []\n this.executionCount = 0\n this.rejectionCount = 0\n }\n}\n\n/**\n * Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.\n *\n * Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:\n * - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets\n * - A throttler ensures even spacing between executions, which can be better for consistent performance\n * - A debouncer collapses multiple calls into one, which is better for handling bursts of events\n *\n * Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically\n * need to enforce a hard limit on the number of executions within a time period.\n *\n * @example\n * ```ts\n * // Rate limit to 5 calls per minute\n * const rateLimited = asyncRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\n * onReject: ({ msUntilNextWindow }) => {\n * console.log(`Rate limit exceeded. Try again in ${msUntilNextWindow}ms`);\n * }\n * });\n *\n * // First 5 calls will execute immediately\n * // Additional calls will be rejected until the minute window resets\n * await rateLimited();\n *\n * // For more even execution, consider using throttle instead:\n * const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds\n * ```\n */\nexport function asyncRateLimit<\n TFn extends (...args: Array<any>) => Promise<any>,\n>(fn: TFn, initialOptions: Omit<AsyncRateLimiterOptions, 'enabled'>) {\n const rateLimiter = new AsyncRateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":[],"mappings":"AAsDO,MAAM,iBAGX;AAAA,EAMA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AANV,SAAQ,iBAAiB;AACzB,SAAQ,iBAAiB;AACzB,SAAQ,iBAAgC,CAAC;AAOvC,SAAK,UAAU;AAAA,MACb,SAAS;AAAA,MACT,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YACyB;AACzB,SAAK,UAAU;AAAA,MACb,GAAG,KAAK;AAAA,MACR,GAAG;AAAA,IACL;AACA,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,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