@tanstack/pacer 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/cjs/async-debouncer.cjs +32 -17
  2. package/dist/cjs/async-debouncer.cjs.map +1 -1
  3. package/dist/cjs/async-debouncer.d.cts +51 -9
  4. package/dist/cjs/async-queuer.cjs +189 -191
  5. package/dist/cjs/async-queuer.cjs.map +1 -1
  6. package/dist/cjs/async-queuer.d.cts +151 -94
  7. package/dist/cjs/async-rate-limiter.cjs +23 -13
  8. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  9. package/dist/cjs/async-rate-limiter.d.cts +54 -5
  10. package/dist/cjs/async-throttler.cjs +38 -17
  11. package/dist/cjs/async-throttler.cjs.map +1 -1
  12. package/dist/cjs/async-throttler.d.cts +51 -8
  13. package/dist/cjs/batcher.cjs +138 -0
  14. package/dist/cjs/batcher.cjs.map +1 -0
  15. package/dist/cjs/batcher.d.cts +149 -0
  16. package/dist/cjs/debouncer.cjs +3 -4
  17. package/dist/cjs/debouncer.cjs.map +1 -1
  18. package/dist/cjs/debouncer.d.cts +1 -2
  19. package/dist/cjs/index.cjs +3 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/cjs/index.d.cts +1 -0
  22. package/dist/cjs/queuer.cjs +68 -41
  23. package/dist/cjs/queuer.cjs.map +1 -1
  24. package/dist/cjs/queuer.d.cts +123 -92
  25. package/dist/cjs/rate-limiter.cjs +3 -4
  26. package/dist/cjs/rate-limiter.cjs.map +1 -1
  27. package/dist/cjs/rate-limiter.d.cts +1 -2
  28. package/dist/cjs/throttler.cjs +3 -4
  29. package/dist/cjs/throttler.cjs.map +1 -1
  30. package/dist/cjs/throttler.d.cts +1 -2
  31. package/dist/cjs/types.d.cts +1 -0
  32. package/dist/esm/async-debouncer.d.ts +51 -9
  33. package/dist/esm/async-debouncer.js +32 -17
  34. package/dist/esm/async-debouncer.js.map +1 -1
  35. package/dist/esm/async-queuer.d.ts +151 -94
  36. package/dist/esm/async-queuer.js +189 -191
  37. package/dist/esm/async-queuer.js.map +1 -1
  38. package/dist/esm/async-rate-limiter.d.ts +54 -5
  39. package/dist/esm/async-rate-limiter.js +23 -13
  40. package/dist/esm/async-rate-limiter.js.map +1 -1
  41. package/dist/esm/async-throttler.d.ts +51 -8
  42. package/dist/esm/async-throttler.js +38 -17
  43. package/dist/esm/async-throttler.js.map +1 -1
  44. package/dist/esm/batcher.d.ts +149 -0
  45. package/dist/esm/batcher.js +138 -0
  46. package/dist/esm/batcher.js.map +1 -0
  47. package/dist/esm/debouncer.d.ts +1 -2
  48. package/dist/esm/debouncer.js +3 -4
  49. package/dist/esm/debouncer.js.map +1 -1
  50. package/dist/esm/index.d.ts +1 -0
  51. package/dist/esm/index.js +3 -0
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/queuer.d.ts +123 -92
  54. package/dist/esm/queuer.js +68 -41
  55. package/dist/esm/queuer.js.map +1 -1
  56. package/dist/esm/rate-limiter.d.ts +1 -2
  57. package/dist/esm/rate-limiter.js +3 -4
  58. package/dist/esm/rate-limiter.js.map +1 -1
  59. package/dist/esm/throttler.d.ts +1 -2
  60. package/dist/esm/throttler.js +3 -4
  61. package/dist/esm/throttler.js.map +1 -1
  62. package/dist/esm/types.d.ts +1 -0
  63. package/package.json +11 -1
  64. package/src/async-debouncer.ts +76 -20
  65. package/src/async-queuer.ts +309 -275
  66. package/src/async-rate-limiter.ts +73 -16
  67. package/src/async-throttler.ts +84 -20
  68. package/src/batcher.ts +253 -0
  69. package/src/debouncer.ts +3 -4
  70. package/src/index.ts +1 -0
  71. package/src/queuer.ts +142 -98
  72. package/src/rate-limiter.ts +3 -4
  73. package/src/throttler.ts +3 -4
  74. package/src/types.ts +3 -0
@@ -1,4 +1,3 @@
1
- import { AnyAsyncFunction } from './types.cjs';
2
1
  import { QueuePosition } from './queuer.cjs';
3
2
  export interface AsyncQueuerOptions<TValue> {
4
3
  /**
@@ -21,7 +20,7 @@ export interface AsyncQueuerOptions<TValue> {
21
20
  * Function to determine if an item has expired
22
21
  * If provided, this overrides the expirationDuration behavior
23
22
  */
24
- getIsExpired?: (item: () => Promise<TValue>, addedAt: number) => boolean;
23
+ getIsExpired?: (item: TValue, addedAt: number) => boolean;
25
24
  /**
26
25
  * Default position to get items from during processing
27
26
  * @default 'front'
@@ -32,21 +31,25 @@ export interface AsyncQueuerOptions<TValue> {
32
31
  * Higher priority items will be processed first
33
32
  * If not provided, will use static priority values attached to tasks
34
33
  */
35
- getPriority?: (item: () => Promise<TValue>) => number;
34
+ getPriority?: (item: TValue) => number;
36
35
  /**
37
36
  * Initial items to populate the queuer with
38
37
  */
39
- initialItems?: Array<(() => Promise<TValue>) & {
40
- priority?: number;
41
- }>;
38
+ initialItems?: Array<TValue>;
42
39
  /**
43
40
  * Maximum number of items allowed in the queuer
44
41
  */
45
42
  maxSize?: number;
46
43
  /**
47
- * Callback fired whenever an item is removed from the queuer
44
+ * Optional error handler for when a task throws.
45
+ * If provided, the handler will be called with the error and queuer instance.
46
+ * This can be used alongside throwOnError - the handler will be called before any error is thrown.
47
+ */
48
+ onError?: (error: unknown, queuer: AsyncQueuer<TValue>) => void;
49
+ /**
50
+ * Callback fired whenever an item expires in the queuer
48
51
  */
49
- onGetNextItem?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
52
+ onExpire?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
50
53
  /**
51
54
  * Callback fired whenever the queuer's running state changes
52
55
  */
@@ -58,15 +61,25 @@ export interface AsyncQueuerOptions<TValue> {
58
61
  /**
59
62
  * Callback fired whenever an item is rejected from being added to the queuer
60
63
  */
61
- onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
64
+ onReject?: (item: TValue, queuer: AsyncQueuer<TValue>) => void;
62
65
  /**
63
- * Callback fired whenever an item expires in the queuer
66
+ * Optional callback to call when a task is settled
67
+ */
68
+ onSettled?: (queuer: AsyncQueuer<TValue>) => void;
69
+ /**
70
+ * Optional callback to call when a task succeeds
64
71
  */
65
- onExpire?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
72
+ onSuccess?: (result: TValue, queuer: AsyncQueuer<TValue>) => void;
66
73
  /**
67
74
  * Whether the queuer should start processing tasks immediately or not.
68
75
  */
69
76
  started?: boolean;
77
+ /**
78
+ * Whether to throw errors when they occur.
79
+ * Defaults to true if no onError handler is provided, false if an onError handler is provided.
80
+ * Can be explicitly set to override these defaults.
81
+ */
82
+ throwOnError?: boolean;
70
83
  /**
71
84
  * Time in milliseconds to wait between processing items.
72
85
  * Can be a number or a function that returns a number.
@@ -75,178 +88,222 @@ export interface AsyncQueuerOptions<TValue> {
75
88
  wait?: number | ((queuer: AsyncQueuer<TValue>) => number);
76
89
  }
77
90
  /**
78
- * A flexible asynchronous queue that processes tasks with configurable concurrency control.
91
+ * A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
79
92
  *
80
93
  * Features:
81
- * - Priority queue support via getPriority option
94
+ * - Priority queue support via the getPriority option
82
95
  * - Configurable concurrency limit
83
- * - Task success/error/completion callbacks
96
+ * - Callbacks for task success, error, completion, and queue state changes
84
97
  * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
85
- * - Pause/resume task processing
98
+ * - Pause and resume processing
86
99
  * - Task cancellation
87
- * - Item expiration to clear stale items from the queue
100
+ * - Item expiration to remove stale items from the queue
88
101
  *
89
102
  * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
90
- * the next pending task is processed if below the concurrency limit.
103
+ * the next pending task is processed if the concurrency limit allows.
91
104
  *
92
- * @example
93
- * ```ts
94
- * const asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });
105
+ * Error Handling:
106
+ * - If an `onError` handler is provided, it will be called with the error and queuer instance
107
+ * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
108
+ * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
109
+ * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
110
+ * - The error state can be checked using the AsyncQueuer instance
95
111
  *
96
- * asyncQueuer.addItem(async () => {
97
- * return 'Hello';
112
+ * Example usage:
113
+ * ```ts
114
+ * const asyncQueuer = new AsyncQueuer<string>(async (item) => {
115
+ * // process item
116
+ * return item.toUpperCase();
117
+ * }, {
118
+ * concurrency: 2,
119
+ * onSuccess: (result) => {
120
+ * console.log(result);
121
+ * }
98
122
  * });
99
123
  *
124
+ * asyncQueuer.addItem('hello');
100
125
  * asyncQueuer.start();
101
- *
102
- * asyncQueuer.onSuccess((result) => {
103
- * console.log(result); // 'Hello'
104
- * });
105
126
  * ```
106
127
  */
107
128
  export declare class AsyncQueuer<TValue> {
129
+ private fn;
108
130
  private _options;
109
131
  private _activeItems;
110
- private _executionCount;
132
+ private _successCount;
133
+ private _errorCount;
134
+ private _settledCount;
111
135
  private _rejectionCount;
112
136
  private _expirationCount;
113
137
  private _items;
114
138
  private _itemTimestamps;
115
- private _onErrorCallbacks;
116
- private _onSettledCallbacks;
117
- private _onSuccessCallbacks;
118
139
  private _pendingTick;
119
140
  private _running;
120
- constructor(initialOptions?: AsyncQueuerOptions<TValue>);
141
+ private _lastResult;
142
+ constructor(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>);
121
143
  /**
122
- * Updates the queuer options
123
- * Returns the new options state
144
+ * Updates the queuer options. New options are merged with existing options.
124
145
  */
125
146
  setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): void;
126
147
  /**
127
- * Returns the current queuer options
148
+ * Returns the current queuer options, including defaults and any overrides.
128
149
  */
129
- getOptions(): Required<AsyncQueuerOptions<TValue>>;
150
+ getOptions(): AsyncQueuerOptions<TValue>;
130
151
  /**
131
- * Returns the current wait time between processing items
152
+ * Returns the current wait time (in milliseconds) between processing items.
153
+ * If a function is provided, it is called with the queuer instance.
132
154
  */
133
155
  getWait(): number;
134
156
  /**
135
- * Returns the current concurrency limit
157
+ * Returns the current concurrency limit for processing items.
158
+ * If a function is provided, it is called with the queuer instance.
136
159
  */
137
160
  getConcurrency(): number;
138
161
  /**
139
- * Processes items in the queuer
162
+ * Processes items in the queue up to the concurrency limit. Internal use only.
140
163
  */
141
164
  private tick;
142
165
  /**
143
- * Checks for and removes expired items from the queuer
166
+ * Starts processing items in the queue. If already running, does nothing.
144
167
  */
145
- private checkExpiredItems;
168
+ start(): void;
146
169
  /**
147
- * Starts the queuer and processes items
148
- */
149
- start(): Promise<void>;
150
- /**
151
- * Stops the queuer from processing items
170
+ * Stops processing items in the queue. Does not clear the queue.
152
171
  */
153
172
  stop(): void;
154
173
  /**
155
- * Removes all items from the queuer
174
+ * Removes all pending items from the queue. Does not affect active tasks.
156
175
  */
157
176
  clear(): void;
158
177
  /**
159
- * Resets the queuer to its initial state
178
+ * Resets the queuer to its initial state. Optionally repopulates with initial items.
179
+ * Does not affect callbacks or options.
160
180
  */
161
181
  reset(withInitialItems?: boolean): void;
162
182
  /**
163
- * Adds a task to the queuer
183
+ * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
184
+ * Items can be inserted based on priority or at the front/back depending on configuration.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * queuer.addItem({ value: 'task', priority: 10 });
189
+ * queuer.addItem('task2', 'front');
190
+ * ```
164
191
  */
165
- addItem(fn: AnyAsyncFunction & {
192
+ addItem(item: TValue & {
166
193
  priority?: number;
167
- }, position?: QueuePosition, runOnItemsChange?: boolean): Promise<TValue>;
168
- /**
169
- * Removes and returns an item from the queuer
194
+ }, position?: QueuePosition, runOnItemsChange?: boolean): void;
195
+ /**
196
+ * Removes and returns the next item from the queue without executing the task function.
197
+ * Use for manual queue management. Normally, use execute() to process items.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * // FIFO
202
+ * queuer.getNextItem();
203
+ * // LIFO
204
+ * queuer.getNextItem('back');
205
+ * ```
206
+ */
207
+ getNextItem(position?: QueuePosition): TValue | undefined;
208
+ /**
209
+ * Removes and returns the next item from the queue and executes the task function with it.
210
+ *
211
+ * @example
212
+ * ```ts
213
+ * queuer.execute();
214
+ * // LIFO
215
+ * queuer.execute('back');
216
+ * ```
217
+ */
218
+ execute(position?: QueuePosition): Promise<any>;
219
+ /**
220
+ * Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
221
+ * Internal use only.
170
222
  */
171
- getNextItem(position?: QueuePosition): (() => Promise<TValue>) | undefined;
223
+ private checkExpiredItems;
172
224
  /**
173
- * Returns an item without removing it
225
+ * Returns the next item in the queue without removing it.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * queuer.getPeek(); // front
230
+ * queuer.getPeek('back'); // back
231
+ * ```
174
232
  */
175
- getPeek(position?: QueuePosition): (() => Promise<TValue>) | undefined;
233
+ getPeek(position?: QueuePosition): TValue | undefined;
176
234
  /**
177
- * Returns true if the queuer is empty
235
+ * Returns true if the queue is empty (no pending items).
178
236
  */
179
237
  getIsEmpty(): boolean;
180
238
  /**
181
- * Returns true if the queuer is full
239
+ * Returns true if the queue is full (reached maxSize).
182
240
  */
183
241
  getIsFull(): boolean;
184
242
  /**
185
- * Returns the current size of the queuer
243
+ * Returns the number of pending items in the queue.
186
244
  */
187
245
  getSize(): number;
188
246
  /**
189
- * Returns a copy of all items in the queuer
247
+ * Returns a copy of all items in the queue, including active and pending items.
190
248
  */
191
- getAllItems(): Array<() => Promise<TValue>>;
249
+ getAllItems(): Array<TValue>;
192
250
  /**
193
- * Returns the active items
251
+ * Returns the items currently being processed (active tasks).
194
252
  */
195
- getActiveItems(): Array<() => Promise<TValue>>;
253
+ getActiveItems(): Array<TValue>;
196
254
  /**
197
- * Returns the pending items
255
+ * Returns the items waiting to be processed (pending tasks).
198
256
  */
199
- getPendingItems(): Array<() => Promise<TValue>>;
257
+ getPendingItems(): Array<TValue>;
200
258
  /**
201
- * Returns the number of items that have been removed from the queuer
259
+ * Returns the number of items that have been successfully processed.
202
260
  */
203
- getExecutionCount(): number;
261
+ getSuccessCount(): number;
204
262
  /**
205
- * Returns the number of items that have been rejected from the queuer
263
+ * Returns the number of items that have failed processing.
206
264
  */
207
- getRejectionCount(): number;
208
- /**
209
- * Returns true if the queuer is running
210
- */
211
- getIsRunning(): boolean;
265
+ getErrorCount(): number;
212
266
  /**
213
- * Returns true if the queuer is running but has no items to process
267
+ * Returns the number of items that have completed processing (success or error).
214
268
  */
215
- getIsIdle(): boolean;
269
+ getSettledCount(): number;
216
270
  /**
217
- * Adds a callback to be called when a task succeeds
271
+ * Returns the number of items that have been rejected from being added to the queue.
218
272
  */
219
- onSuccess(cb: (result: TValue) => void): () => void;
273
+ getRejectionCount(): number;
220
274
  /**
221
- * Adds a callback to be called when a task errors
275
+ * Returns true if the queuer is currently running (processing items).
222
276
  */
223
- onError(cb: (error: Error) => void): () => void;
277
+ getIsRunning(): boolean;
224
278
  /**
225
- * Adds a callback to be called when a task is settled
279
+ * Returns true if the queuer is running but has no items to process and no active tasks.
226
280
  */
227
- onSettled(cb: (result: TValue | Error) => void): () => void;
281
+ getIsIdle(): boolean;
228
282
  /**
229
- * Returns the number of items that have expired from the queuer
283
+ * Returns the number of items that have expired and been removed from the queue.
230
284
  */
231
285
  getExpirationCount(): number;
232
286
  }
233
287
  /**
234
- * Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.
235
- * The queuer is automatically started and ready to process items.
288
+ * Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
289
+ * The queuer is started automatically and ready to process items.
236
290
  *
237
- * @example
291
+ * Error Handling:
292
+ * - If an `onError` handler is provided, it will be called with the error and queuer instance
293
+ * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
294
+ * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
295
+ * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
296
+ * - The error state can be checked using the underlying AsyncQueuer instance
297
+ *
298
+ * Example usage:
238
299
  * ```ts
239
- * const enqueue = asyncQueue<string>();
300
+ * const enqueue = asyncQueue<string>(async (item) => {
301
+ * return item.toUpperCase();
302
+ * }, {...options});
240
303
  *
241
- * // Add items to be processed
242
- * enqueue(async () => {
243
- * return 'Hello';
244
- * });
304
+ * enqueue('hello');
245
305
  * ```
246
- *
247
- * @param options - Configuration options for the AsyncQueuer
248
- * @returns A bound addItem function that can be used to add tasks to the queuer
249
306
  */
250
- export declare function asyncQueue<TValue>(options: AsyncQueuerOptions<TValue>): (fn: AnyAsyncFunction & {
307
+ export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue & {
251
308
  priority?: number;
252
- }, position?: QueuePosition, runOnItemsChange?: boolean) => Promise<TValue>;
309
+ }, position?: QueuePosition, runOnItemsChange?: boolean) => void;
@@ -3,14 +3,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const utils = require("./utils.cjs");
4
4
  const defaultOptions = {
5
5
  enabled: true,
6
- onError: () => {
7
- },
8
- onReject: () => {
9
- },
10
- onSettled: () => {
11
- },
12
- onSuccess: () => {
13
- },
14
6
  windowType: "fixed"
15
7
  };
16
8
  class AsyncRateLimiter {
@@ -24,12 +16,12 @@ class AsyncRateLimiter {
24
16
  this._isExecuting = false;
25
17
  this._options = {
26
18
  ...defaultOptions,
27
- ...initialOptions
19
+ ...initialOptions,
20
+ throwOnError: initialOptions.throwOnError ?? !initialOptions.onError
28
21
  };
29
22
  }
30
23
  /**
31
24
  * Updates the rate limiter options
32
- * Returns the new options state
33
25
  */
34
26
  setOptions(newOptions) {
35
27
  this._options = { ...this._options, ...newOptions };
@@ -63,6 +55,19 @@ class AsyncRateLimiter {
63
55
  * Will reject execution if the number of calls in the current window exceeds the limit.
64
56
  * If execution is allowed, waits for any previous execution to complete before proceeding.
65
57
  *
58
+ * Error Handling:
59
+ * - If the rate-limited function throws and no `onError` handler is configured,
60
+ * the error will be thrown from this method.
61
+ * - If an `onError` handler is configured, errors will be caught and passed to the handler,
62
+ * and this method will return undefined.
63
+ * - If the rate limit is exceeded, the execution will be rejected and the `onReject` handler
64
+ * will be called if configured.
65
+ * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.
66
+ * - Rate limit rejections can be tracked using `getRejectionCount()`.
67
+ *
68
+ * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
69
+ * @throws The error from the rate-limited function if no onError handler is configured
70
+ *
66
71
  * @example
67
72
  * ```ts
68
73
  * const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
@@ -80,7 +85,7 @@ class AsyncRateLimiter {
80
85
  const window = this.getWindow();
81
86
  if (this._options.windowType === "sliding") {
82
87
  if (this._executionTimes.length < limit) {
83
- await this.executeFunction(...args);
88
+ await this.execute(...args);
84
89
  return this._lastResult;
85
90
  }
86
91
  } else {
@@ -88,14 +93,14 @@ class AsyncRateLimiter {
88
93
  const oldestExecution = Math.min(...this._executionTimes);
89
94
  const isNewWindow = oldestExecution + window <= now;
90
95
  if (isNewWindow || this._executionTimes.length < limit) {
91
- await this.executeFunction(...args);
96
+ await this.execute(...args);
92
97
  return this._lastResult;
93
98
  }
94
99
  }
95
100
  this.rejectFunction();
96
101
  return void 0;
97
102
  }
98
- async executeFunction(...args) {
103
+ async execute(...args) {
99
104
  var _a, _b, _c, _d, _e, _f;
100
105
  if (!this.getEnabled()) return;
101
106
  this._isExecuting = true;
@@ -108,6 +113,11 @@ class AsyncRateLimiter {
108
113
  } catch (error) {
109
114
  this._errorCount++;
110
115
  (_d = (_c = this._options).onError) == null ? void 0 : _d.call(_c, error, this);
116
+ if (this._options.throwOnError) {
117
+ throw error;
118
+ } else {
119
+ console.error(error);
120
+ }
111
121
  } finally {
112
122
  this._isExecuting = false;
113
123
  this._settleCount++;
@@ -1 +1 @@
1
- {"version":3,"file":"async-rate-limiter.cjs","sources":["../../src/async-rate-limiter.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async rate-limited function\n */\nexport interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {\n /**\n * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((rateLimiter: AsyncRateLimiter<TFn>) => boolean)\n /**\n * Maximum number of executions allowed within the time window.\n * Can be a number or a function that returns a number.\n */\n limit: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number)\n /**\n * Optional error handler for when the rate-limited function throws\n */\n onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional function to call when the rate-limited function is executed\n */\n onSettled?: (rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional function to call when the rate-limited function is executed\n */\n onSuccess?: (\n result: ReturnType<TFn>,\n rateLimiter: AsyncRateLimiter<TFn>,\n ) => void\n /**\n * Time window in milliseconds within which the limit applies.\n * Can be a number or a function that returns a number.\n */\n window: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number)\n /**\n * Type of window to use for rate limiting\n * - 'fixed': Uses a fixed window that resets after the window period\n * - 'sliding': Uses a sliding window that allows executions as old ones expire\n * Defaults to 'fixed'\n */\n windowType?: 'fixed' | 'sliding'\n}\n\nconst defaultOptions: Required<\n Omit<AsyncRateLimiterOptions<any>, 'limit' | 'window'>\n> = {\n enabled: true,\n onError: () => {},\n onReject: () => {},\n onSettled: () => {},\n onSuccess: () => {},\n windowType: 'fixed',\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 * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\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, windowType: 'sliding' } // 5 calls per second with sliding window\n * );\n *\n * // Will execute immediately until limit reached, then block\n * // Returns the API response directly\n * const data = await rateLimiter.maybeExecute('123');\n * ```\n */\nexport class AsyncRateLimiter<TFn extends AnyAsyncFunction> {\n private _options: AsyncRateLimiterOptions<TFn>\n private _errorCount = 0\n private _executionTimes: Array<number> = []\n private _lastResult: ReturnType<TFn> | undefined\n private _rejectionCount = 0\n private _settleCount = 0\n private _successCount = 0\n private _isExecuting = false\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the rate limiter options\n * Returns the new options state\n */\n setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current rate limiter options\n */\n getOptions(): Required<AsyncRateLimiterOptions<TFn>> {\n return this._options as Required<AsyncRateLimiterOptions<TFn>>\n }\n\n /**\n * Returns the current enabled state of the rate limiter\n */\n getEnabled(): boolean {\n return !!parseFunctionOrValue(this._options.enabled, this)\n }\n\n /**\n * Returns the current limit of executions allowed within the time window\n */\n getLimit(): number {\n return parseFunctionOrValue(this._options.limit, this)\n }\n\n /**\n * Returns the current time window in milliseconds\n */\n getWindow(): number {\n return parseFunctionOrValue(this._options.window, this)\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(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n this.cleanupOldExecutions()\n\n const limit = this.getLimit()\n const window = this.getWindow()\n\n if (this._options.windowType === 'sliding') {\n // For sliding window, we can execute if we have capacity in the current window\n if (this._executionTimes.length < limit) {\n await this.executeFunction(...args)\n return this._lastResult\n }\n } else {\n // For fixed window, we need to check if we're in a new window\n const now = Date.now()\n const oldestExecution = Math.min(...this._executionTimes)\n const isNewWindow = oldestExecution + window <= now\n\n if (isNewWindow || this._executionTimes.length < limit) {\n await this.executeFunction(...args)\n return this._lastResult\n }\n }\n\n this.rejectFunction()\n return undefined\n }\n\n private async executeFunction(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n if (!this.getEnabled()) return\n this._isExecuting = true\n const now = Date.now()\n this._executionTimes.push(now)\n\n try {\n this._lastResult = await this.fn(...args)\n this._successCount++\n this._options.onSuccess?.(this._lastResult!, this)\n } catch (error) {\n this._errorCount++\n this._options.onError?.(error, this)\n } finally {\n this._isExecuting = false\n this._settleCount++\n this._options.onSettled?.(this)\n }\n\n return this._lastResult\n }\n\n private rejectFunction(): void {\n this._rejectionCount++\n if (this._options.onReject) {\n this._options.onReject(this)\n }\n }\n\n private cleanupOldExecutions(): void {\n const now = Date.now()\n const windowStart = now - this.getWindow()\n this._executionTimes = this._executionTimes.filter(\n (time) => time > windowStart,\n )\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.getLimit() - this._executionTimes.length)\n }\n\n /**\n * Returns the number of milliseconds until the next execution will be possible\n * For fixed windows, this is the time until the current window resets\n * For sliding windows, this is the time until the oldest execution expires\n */\n getMsUntilNextWindow(): number {\n if (this.getRemainingInWindow() > 0) {\n return 0\n }\n const oldestExecution = Math.min(...this._executionTimes)\n return oldestExecution + this.getWindow() - Date.now()\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getSuccessCount(): number {\n return this._successCount\n }\n\n /**\n * Returns the number of times the function has been settled\n */\n getSettleCount(): number {\n return this._settleCount\n }\n\n /**\n * Returns the number of times the function has errored\n */\n getErrorCount(): number {\n return this._errorCount\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 whether the function is currently executing\n */\n getIsExecuting(): boolean {\n return this._isExecuting\n }\n\n /**\n * Resets the rate limiter state\n */\n reset(): void {\n this._executionTimes = []\n this._successCount = 0\n this._errorCount = 0\n this._rejectionCount = 0\n this._settleCount = 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 * Unlike the non-async rate limiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\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 with a sliding window\n * const rateLimited = asyncRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // First 5 calls will execute immediately\n * // Additional calls will be rejected until the minute window resets\n * // Returns the API response directly\n * const result = 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<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n) {\n const rateLimiter = new AsyncRateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":["parseFunctionOrValue"],"mappings":";;;AAmDA,MAAM,iBAEF;AAAA,EACF,SAAS;AAAA,EACT,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,YAAY;AACd;AAsCO,MAAM,iBAA+C;AAAA,EAU1D,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,cAAc;AACtB,SAAQ,kBAAiC,CAAC;AAE1C,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AACvB,SAAQ,gBAAgB;AACxB,SAAQ,eAAe;AAMrB,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WAAW,YAAyD;AAClE,SAAK,WAAW,EAAE,GAAG,KAAK,UAAU,GAAG,WAAW;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMpD,aAAqD;AACnD,WAAO,KAAK;AAAA,EAAA;AAA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BAAkB,CAAC;AACxB,SAAK,gBAAgB;AACrB,SAAK,cAAc;AACnB,SAAK,kBAAkB;AACvB,SAAK,eAAe;AAAA,EAAA;AAExB;AA4CgB,SAAA,eACd,IACA,gBACA;AACA,QAAM,cAAc,IAAI,iBAAiB,IAAI,cAAc;AACpD,SAAA,YAAY,aAAa,KAAK,WAAW;AAClD;;;"}
1
+ {"version":3,"file":"async-rate-limiter.cjs","sources":["../../src/async-rate-limiter.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyAsyncFunction, OptionalKeys } from './types'\n\n/**\n * Options for configuring an async rate-limited function\n */\nexport interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {\n /**\n * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((rateLimiter: AsyncRateLimiter<TFn>) => boolean)\n /**\n * Maximum number of executions allowed within the time window.\n * Can be a number or a function that returns a number.\n */\n limit: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number)\n /**\n * Optional error handler for when the rate-limited function throws.\n * If provided, the handler will be called with the error and rate limiter instance.\n * This can be used alongside throwOnError - the handler will be called before any error is thrown.\n */\n onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional function to call when the rate-limited function is executed\n */\n onSettled?: (rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Optional function to call when the rate-limited function is executed\n */\n onSuccess?: (\n result: ReturnType<TFn>,\n rateLimiter: AsyncRateLimiter<TFn>,\n ) => void\n /**\n * Whether to throw errors when they occur.\n * Defaults to true if no onError handler is provided, false if an onError handler is provided.\n * Can be explicitly set to override these defaults.\n */\n throwOnError?: boolean\n /**\n * Time window in milliseconds within which the limit applies.\n * Can be a number or a function that returns a number.\n */\n window: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number)\n /**\n * Type of window to use for rate limiting\n * - 'fixed': Uses a fixed window that resets after the window period\n * - 'sliding': Uses a sliding window that allows executions as old ones expire\n * Defaults to 'fixed'\n */\n windowType?: 'fixed' | 'sliding'\n}\n\ntype AsyncRateLimiterOptionsWithOptionalCallbacks = OptionalKeys<\n AsyncRateLimiterOptions<any>,\n 'onError' | 'onReject' | 'onSettled' | 'onSuccess'\n>\n\nconst defaultOptions: Omit<\n AsyncRateLimiterOptionsWithOptionalCallbacks,\n 'limit' | 'window'\n> = {\n enabled: true,\n windowType: 'fixed',\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 * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\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 * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(\n * async (id: string) => await api.getData(id),\n * {\n * limit: 5,\n * window: 1000,\n * windowType: 'sliding',\n * onError: (error) => {\n * console.error('API call failed:', error);\n * },\n * onReject: (limiter) => {\n * console.log(`Rate limit exceeded. Try again in ${limiter.getMsUntilNextWindow()}ms`);\n * }\n * }\n * );\n *\n * // Will execute immediately until limit reached, then block\n * // Returns the API response directly\n * const data = await rateLimiter.maybeExecute('123');\n * ```\n */\nexport class AsyncRateLimiter<TFn extends AnyAsyncFunction> {\n private _options: AsyncRateLimiterOptionsWithOptionalCallbacks\n private _errorCount = 0\n private _executionTimes: Array<number> = []\n private _lastResult: ReturnType<TFn> | undefined\n private _rejectionCount = 0\n private _settleCount = 0\n private _successCount = 0\n private _isExecuting = false\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n throwOnError: initialOptions.throwOnError ?? !initialOptions.onError,\n }\n }\n\n /**\n * Updates the rate limiter options\n */\n setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current rate limiter options\n */\n getOptions(): AsyncRateLimiterOptions<TFn> {\n return this._options\n }\n\n /**\n * Returns the current enabled state of the rate limiter\n */\n getEnabled(): boolean {\n return !!parseFunctionOrValue(this._options.enabled, this)\n }\n\n /**\n * Returns the current limit of executions allowed within the time window\n */\n getLimit(): number {\n return parseFunctionOrValue(this._options.limit, this)\n }\n\n /**\n * Returns the current time window in milliseconds\n */\n getWindow(): number {\n return parseFunctionOrValue(this._options.window, this)\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 * Error Handling:\n * - If the rate-limited function throws and no `onError` handler is configured,\n * the error will be thrown from this method.\n * - If an `onError` handler is configured, errors will be caught and passed to the handler,\n * and this method will return undefined.\n * - If the rate limit is exceeded, the execution will be rejected and the `onReject` handler\n * will be called if configured.\n * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.\n * - Rate limit rejections can be tracked using `getRejectionCount()`.\n *\n * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError\n * @throws The error from the rate-limited function if no onError handler is configured\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(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n this.cleanupOldExecutions()\n\n const limit = this.getLimit()\n const window = this.getWindow()\n\n if (this._options.windowType === 'sliding') {\n // For sliding window, we can execute if we have capacity in the current window\n if (this._executionTimes.length < limit) {\n await this.execute(...args)\n return this._lastResult\n }\n } else {\n // For fixed window, we need to check if we're in a new window\n const now = Date.now()\n const oldestExecution = Math.min(...this._executionTimes)\n const isNewWindow = oldestExecution + window <= now\n\n if (isNewWindow || this._executionTimes.length < limit) {\n await this.execute(...args)\n return this._lastResult\n }\n }\n\n this.rejectFunction()\n return undefined\n }\n\n private async execute(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n if (!this.getEnabled()) return\n this._isExecuting = true\n const now = Date.now()\n this._executionTimes.push(now)\n\n try {\n this._lastResult = await this.fn(...args)\n this._successCount++\n this._options.onSuccess?.(this._lastResult!, this)\n } catch (error) {\n this._errorCount++\n this._options.onError?.(error, this)\n if (this._options.throwOnError) {\n throw error\n } else {\n console.error(error)\n }\n } finally {\n this._isExecuting = false\n this._settleCount++\n this._options.onSettled?.(this)\n }\n\n return this._lastResult\n }\n\n private rejectFunction(): void {\n this._rejectionCount++\n if (this._options.onReject) {\n this._options.onReject(this)\n }\n }\n\n private cleanupOldExecutions(): void {\n const now = Date.now()\n const windowStart = now - this.getWindow()\n this._executionTimes = this._executionTimes.filter(\n (time) => time > windowStart,\n )\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.getLimit() - this._executionTimes.length)\n }\n\n /**\n * Returns the number of milliseconds until the next execution will be possible\n * For fixed windows, this is the time until the current window resets\n * For sliding windows, this is the time until the oldest execution expires\n */\n getMsUntilNextWindow(): number {\n if (this.getRemainingInWindow() > 0) {\n return 0\n }\n const oldestExecution = Math.min(...this._executionTimes)\n return oldestExecution + this.getWindow() - Date.now()\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getSuccessCount(): number {\n return this._successCount\n }\n\n /**\n * Returns the number of times the function has been settled\n */\n getSettleCount(): number {\n return this._settleCount\n }\n\n /**\n * Returns the number of times the function has errored\n */\n getErrorCount(): number {\n return this._errorCount\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 whether the function is currently executing\n */\n getIsExecuting(): boolean {\n return this._isExecuting\n }\n\n /**\n * Resets the rate limiter state\n */\n reset(): void {\n this._executionTimes = []\n this._successCount = 0\n this._errorCount = 0\n this._rejectionCount = 0\n this._settleCount = 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 * Unlike the non-async rate limiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\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 * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\n *\n * @example\n * ```ts\n * // Rate limit to 5 calls per minute with a sliding window\n * const rateLimited = asyncRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onError: (error) => {\n * console.error('API call failed:', error);\n * },\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // First 5 calls will execute immediately\n * // Additional calls will be rejected until the minute window resets\n * // Returns the API response directly\n * const result = 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<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n) {\n const rateLimiter = new AsyncRateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":["parseFunctionOrValue"],"mappings":";;;AAgEA,MAAM,iBAGF;AAAA,EACF,SAAS;AAAA,EACT,YAAY;AACd;AAwDO,MAAM,iBAA+C;AAAA,EAU1D,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,cAAc;AACtB,SAAQ,kBAAiC,CAAC;AAE1C,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AACvB,SAAQ,gBAAgB;AACxB,SAAQ,eAAe;AA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