@tanstack/pacer 0.16.3 → 0.17.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 (169) hide show
  1. package/README.md +165 -0
  2. package/dist/async-batcher.cjs +355 -0
  3. package/dist/async-batcher.cjs.map +1 -0
  4. package/dist/async-batcher.d.cts +344 -0
  5. package/dist/async-batcher.d.ts +344 -0
  6. package/dist/async-batcher.js +353 -0
  7. package/dist/async-batcher.js.map +1 -0
  8. package/dist/async-debouncer.cjs +340 -0
  9. package/dist/async-debouncer.cjs.map +1 -0
  10. package/dist/async-debouncer.d.cts +300 -0
  11. package/dist/async-debouncer.d.ts +300 -0
  12. package/dist/async-debouncer.js +338 -0
  13. package/dist/async-debouncer.js.map +1 -0
  14. package/dist/async-queuer.cjs +496 -0
  15. package/dist/async-queuer.cjs.map +1 -0
  16. package/dist/async-queuer.d.cts +440 -0
  17. package/dist/async-queuer.d.ts +440 -0
  18. package/dist/async-queuer.js +494 -0
  19. package/dist/async-queuer.js.map +1 -0
  20. package/dist/async-rate-limiter.cjs +381 -0
  21. package/dist/async-rate-limiter.cjs.map +1 -0
  22. package/dist/{cjs/async-rate-limiter.d.cts → async-rate-limiter.d.cts} +190 -185
  23. package/dist/{esm/async-rate-limiter.d.ts → async-rate-limiter.d.ts} +190 -185
  24. package/dist/async-rate-limiter.js +379 -0
  25. package/dist/async-rate-limiter.js.map +1 -0
  26. package/dist/async-retryer.cjs +378 -0
  27. package/dist/async-retryer.cjs.map +1 -0
  28. package/dist/async-retryer.d.cts +312 -0
  29. package/dist/async-retryer.d.ts +312 -0
  30. package/dist/async-retryer.js +376 -0
  31. package/dist/async-retryer.js.map +1 -0
  32. package/dist/async-throttler.cjs +362 -0
  33. package/dist/async-throttler.cjs.map +1 -0
  34. package/dist/async-throttler.d.cts +320 -0
  35. package/dist/async-throttler.d.ts +320 -0
  36. package/dist/async-throttler.js +360 -0
  37. package/dist/async-throttler.js.map +1 -0
  38. package/dist/batcher.cjs +194 -0
  39. package/dist/batcher.cjs.map +1 -0
  40. package/dist/batcher.d.cts +180 -0
  41. package/dist/batcher.d.ts +180 -0
  42. package/dist/batcher.js +193 -0
  43. package/dist/batcher.js.map +1 -0
  44. package/dist/debouncer.cjs +197 -0
  45. package/dist/debouncer.cjs.map +1 -0
  46. package/dist/debouncer.d.cts +167 -0
  47. package/dist/debouncer.d.ts +167 -0
  48. package/dist/debouncer.js +195 -0
  49. package/dist/debouncer.js.map +1 -0
  50. package/dist/event-client.cjs +20 -0
  51. package/dist/event-client.cjs.map +1 -0
  52. package/dist/event-client.d.cts +49 -0
  53. package/dist/event-client.d.ts +49 -0
  54. package/dist/event-client.js +19 -0
  55. package/dist/event-client.js.map +1 -0
  56. package/dist/index.cjs +49 -0
  57. package/dist/index.d.cts +15 -0
  58. package/dist/index.d.ts +15 -0
  59. package/dist/{esm/index.js → index.js} +5 -41
  60. package/dist/queuer.cjs +393 -0
  61. package/dist/queuer.cjs.map +1 -0
  62. package/dist/queuer.d.cts +345 -0
  63. package/dist/queuer.d.ts +345 -0
  64. package/dist/queuer.js +391 -0
  65. package/dist/queuer.js.map +1 -0
  66. package/dist/rate-limiter.cjs +251 -0
  67. package/dist/rate-limiter.cjs.map +1 -0
  68. package/dist/{cjs/rate-limiter.d.cts → rate-limiter.d.cts} +113 -108
  69. package/dist/{esm/rate-limiter.d.ts → rate-limiter.d.ts} +113 -108
  70. package/dist/rate-limiter.js +249 -0
  71. package/dist/rate-limiter.js.map +1 -0
  72. package/dist/throttler.cjs +209 -0
  73. package/dist/throttler.cjs.map +1 -0
  74. package/dist/throttler.d.cts +207 -0
  75. package/dist/throttler.d.ts +207 -0
  76. package/dist/throttler.js +207 -0
  77. package/dist/throttler.js.map +1 -0
  78. package/dist/types.cjs +0 -0
  79. package/dist/types.d.cts +13 -0
  80. package/dist/types.d.ts +13 -0
  81. package/dist/types.js +1 -0
  82. package/dist/utils.cjs +13 -0
  83. package/dist/utils.cjs.map +1 -0
  84. package/dist/utils.d.cts +8 -0
  85. package/dist/utils.d.ts +8 -0
  86. package/dist/utils.js +11 -0
  87. package/dist/utils.js.map +1 -0
  88. package/package.json +39 -123
  89. package/dist/cjs/async-batcher.cjs +0 -222
  90. package/dist/cjs/async-batcher.cjs.map +0 -1
  91. package/dist/cjs/async-batcher.d.cts +0 -340
  92. package/dist/cjs/async-debouncer.cjs +0 -233
  93. package/dist/cjs/async-debouncer.cjs.map +0 -1
  94. package/dist/cjs/async-debouncer.d.cts +0 -295
  95. package/dist/cjs/async-queuer.cjs +0 -399
  96. package/dist/cjs/async-queuer.cjs.map +0 -1
  97. package/dist/cjs/async-queuer.d.cts +0 -435
  98. package/dist/cjs/async-rate-limiter.cjs +0 -254
  99. package/dist/cjs/async-rate-limiter.cjs.map +0 -1
  100. package/dist/cjs/async-retryer.cjs +0 -287
  101. package/dist/cjs/async-retryer.cjs.map +0 -1
  102. package/dist/cjs/async-retryer.d.cts +0 -308
  103. package/dist/cjs/async-throttler.cjs +0 -263
  104. package/dist/cjs/async-throttler.cjs.map +0 -1
  105. package/dist/cjs/async-throttler.d.cts +0 -315
  106. package/dist/cjs/batcher.cjs +0 -132
  107. package/dist/cjs/batcher.cjs.map +0 -1
  108. package/dist/cjs/batcher.d.cts +0 -176
  109. package/dist/cjs/debouncer.cjs +0 -139
  110. package/dist/cjs/debouncer.cjs.map +0 -1
  111. package/dist/cjs/debouncer.d.cts +0 -162
  112. package/dist/cjs/event-client.cjs +0 -20
  113. package/dist/cjs/event-client.cjs.map +0 -1
  114. package/dist/cjs/event-client.d.cts +0 -45
  115. package/dist/cjs/index.cjs +0 -51
  116. package/dist/cjs/index.cjs.map +0 -1
  117. package/dist/cjs/index.d.cts +0 -15
  118. package/dist/cjs/queuer.cjs +0 -308
  119. package/dist/cjs/queuer.cjs.map +0 -1
  120. package/dist/cjs/queuer.d.cts +0 -340
  121. package/dist/cjs/rate-limiter.cjs +0 -181
  122. package/dist/cjs/rate-limiter.cjs.map +0 -1
  123. package/dist/cjs/throttler.cjs +0 -153
  124. package/dist/cjs/throttler.cjs.map +0 -1
  125. package/dist/cjs/throttler.d.cts +0 -202
  126. package/dist/cjs/types.d.cts +0 -9
  127. package/dist/cjs/utils.cjs +0 -11
  128. package/dist/cjs/utils.cjs.map +0 -1
  129. package/dist/cjs/utils.d.cts +0 -3
  130. package/dist/esm/async-batcher.d.ts +0 -340
  131. package/dist/esm/async-batcher.js +0 -222
  132. package/dist/esm/async-batcher.js.map +0 -1
  133. package/dist/esm/async-debouncer.d.ts +0 -295
  134. package/dist/esm/async-debouncer.js +0 -233
  135. package/dist/esm/async-debouncer.js.map +0 -1
  136. package/dist/esm/async-queuer.d.ts +0 -435
  137. package/dist/esm/async-queuer.js +0 -399
  138. package/dist/esm/async-queuer.js.map +0 -1
  139. package/dist/esm/async-rate-limiter.js +0 -254
  140. package/dist/esm/async-rate-limiter.js.map +0 -1
  141. package/dist/esm/async-retryer.d.ts +0 -308
  142. package/dist/esm/async-retryer.js +0 -287
  143. package/dist/esm/async-retryer.js.map +0 -1
  144. package/dist/esm/async-throttler.d.ts +0 -315
  145. package/dist/esm/async-throttler.js +0 -263
  146. package/dist/esm/async-throttler.js.map +0 -1
  147. package/dist/esm/batcher.d.ts +0 -176
  148. package/dist/esm/batcher.js +0 -132
  149. package/dist/esm/batcher.js.map +0 -1
  150. package/dist/esm/debouncer.d.ts +0 -162
  151. package/dist/esm/debouncer.js +0 -139
  152. package/dist/esm/debouncer.js.map +0 -1
  153. package/dist/esm/event-client.d.ts +0 -45
  154. package/dist/esm/event-client.js +0 -20
  155. package/dist/esm/event-client.js.map +0 -1
  156. package/dist/esm/index.d.ts +0 -15
  157. package/dist/esm/index.js.map +0 -1
  158. package/dist/esm/queuer.d.ts +0 -340
  159. package/dist/esm/queuer.js +0 -308
  160. package/dist/esm/queuer.js.map +0 -1
  161. package/dist/esm/rate-limiter.js +0 -181
  162. package/dist/esm/rate-limiter.js.map +0 -1
  163. package/dist/esm/throttler.d.ts +0 -202
  164. package/dist/esm/throttler.js +0 -153
  165. package/dist/esm/throttler.js.map +0 -1
  166. package/dist/esm/types.d.ts +0 -9
  167. package/dist/esm/utils.d.ts +0 -3
  168. package/dist/esm/utils.js +0 -11
  169. package/dist/esm/utils.js.map +0 -1
@@ -1,117 +1,119 @@
1
- import { Store } from '@tanstack/store';
2
- import { AsyncRetryer, AsyncRetryerOptions } from './async-retryer.js';
3
- import { AnyAsyncFunction } from './types.js';
4
- export interface AsyncRateLimiterState<TFn extends AnyAsyncFunction> {
5
- /**
6
- * Number of function executions that have resulted in errors
7
- */
8
- errorCount: number;
9
- /**
10
- * Array of timestamps when executions occurred for rate limiting calculations
11
- */
12
- executionTimes: Array<number>;
13
- /**
14
- * Whether the rate limiter has exceeded the limit
15
- */
16
- isExceeded: boolean;
17
- /**
18
- * Whether the rate-limited function is currently executing asynchronously
19
- */
20
- isExecuting: boolean;
21
- /**
22
- * The result from the most recent successful function execution
23
- */
24
- lastResult: ReturnType<TFn> | undefined;
25
- /**
26
- * Number of function executions that have been rejected due to rate limiting
27
- */
28
- rejectionCount: number;
29
- /**
30
- * Number of function executions that have completed (either successfully or with errors)
31
- */
32
- settleCount: number;
33
- /**
34
- * Current execution status - 'disabled' when not active, 'executing' when executing, 'idle' when not executing, 'exceeded' when rate limit is exceeded
35
- */
36
- status: 'disabled' | 'executing' | 'exceeded' | 'idle';
37
- /**
38
- * Number of function executions that have completed successfully
39
- */
40
- successCount: number;
41
- /**
42
- * Number of times maybeExecute has been called (for reduction calculations)
43
- */
44
- maybeExecuteCount: number;
1
+ import { AnyAsyncFunction } from "./types.js";
2
+ import { AsyncRetryer, AsyncRetryerOptions } from "./async-retryer.js";
3
+ import { Store } from "@tanstack/store";
4
+
5
+ //#region src/async-rate-limiter.d.ts
6
+ interface AsyncRateLimiterState<TFn extends AnyAsyncFunction> {
7
+ /**
8
+ * Number of function executions that have resulted in errors
9
+ */
10
+ errorCount: number;
11
+ /**
12
+ * Array of timestamps when executions occurred for rate limiting calculations
13
+ */
14
+ executionTimes: Array<number>;
15
+ /**
16
+ * Whether the rate limiter has exceeded the limit
17
+ */
18
+ isExceeded: boolean;
19
+ /**
20
+ * Whether the rate-limited function is currently executing asynchronously
21
+ */
22
+ isExecuting: boolean;
23
+ /**
24
+ * The result from the most recent successful function execution
25
+ */
26
+ lastResult: ReturnType<TFn> | undefined;
27
+ /**
28
+ * Number of function executions that have been rejected due to rate limiting
29
+ */
30
+ rejectionCount: number;
31
+ /**
32
+ * Number of function executions that have completed (either successfully or with errors)
33
+ */
34
+ settleCount: number;
35
+ /**
36
+ * Current execution status - 'disabled' when not active, 'executing' when executing, 'idle' when not executing, 'exceeded' when rate limit is exceeded
37
+ */
38
+ status: 'disabled' | 'executing' | 'exceeded' | 'idle';
39
+ /**
40
+ * Number of function executions that have completed successfully
41
+ */
42
+ successCount: number;
43
+ /**
44
+ * Number of times maybeExecute has been called (for reduction calculations)
45
+ */
46
+ maybeExecuteCount: number;
45
47
  }
46
48
  /**
47
49
  * Options for configuring an async rate-limited function
48
50
  */
49
- export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
50
- /**
51
- * Options for configuring the underlying async retryer
52
- */
53
- asyncRetryerOptions?: AsyncRetryerOptions<TFn>;
54
- /**
55
- * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
56
- * Can be a boolean or a function that returns a boolean.
57
- * Defaults to true.
58
- */
59
- enabled?: boolean | ((rateLimiter: AsyncRateLimiter<TFn>) => boolean);
60
- /**
61
- * Initial state for the rate limiter
62
- */
63
- initialState?: Partial<AsyncRateLimiterState<TFn>>;
64
- /**
65
- * Optional key to identify this async rate limiter instance.
66
- * If provided, the async rate limiter will be identified by this key in the devtools and PacerProvider if applicable.
67
- */
68
- key?: string;
69
- /**
70
- * Maximum number of executions allowed within the time window.
71
- * Can be a number or a function that returns a number.
72
- */
73
- limit: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
74
- /**
75
- * Optional error handler for when the rate-limited function throws.
76
- * If provided, the handler will be called with the error and rate limiter instance.
77
- * This can be used alongside throwOnError - the handler will be called before any error is thrown.
78
- */
79
- onError?: (error: Error, args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
80
- /**
81
- * Optional callback function that is called when an execution is rejected due to rate limiting
82
- */
83
- onReject?: (args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
84
- /**
85
- * Optional function to call when the rate-limited function is executed
86
- */
87
- onSettled?: (args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
88
- /**
89
- * Optional function to call when the rate-limited function is executed
90
- */
91
- onSuccess?: (result: ReturnType<TFn>, args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
92
- /**
93
- * Whether to throw errors when they occur.
94
- * Defaults to true if no onError handler is provided, false if an onError handler is provided.
95
- * Can be explicitly set to override these defaults.
96
- */
97
- throwOnError?: boolean;
98
- /**
99
- * Time window in milliseconds within which the limit applies.
100
- * Can be a number or a function that returns a number.
101
- */
102
- window: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
103
- /**
104
- * Type of window to use for rate limiting
105
- * - 'fixed': Uses a fixed window that resets after the window period
106
- * - 'sliding': Uses a sliding window that allows executions as old ones expire
107
- * Defaults to 'fixed'
108
- */
109
- windowType?: 'fixed' | 'sliding';
51
+ interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
52
+ /**
53
+ * Options for configuring the underlying async retryer
54
+ */
55
+ asyncRetryerOptions?: AsyncRetryerOptions<TFn>;
56
+ /**
57
+ * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
58
+ * Can be a boolean or a function that returns a boolean.
59
+ * Defaults to true.
60
+ */
61
+ enabled?: boolean | ((rateLimiter: AsyncRateLimiter<TFn>) => boolean);
62
+ /**
63
+ * Initial state for the rate limiter
64
+ */
65
+ initialState?: Partial<AsyncRateLimiterState<TFn>>;
66
+ /**
67
+ * Optional key to identify this async rate limiter instance.
68
+ * If provided, the async rate limiter will be identified by this key in the devtools and PacerProvider if applicable.
69
+ */
70
+ key?: string;
71
+ /**
72
+ * Maximum number of executions allowed within the time window.
73
+ * Can be a number or a function that returns a number.
74
+ */
75
+ limit: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
76
+ /**
77
+ * Optional error handler for when the rate-limited function throws.
78
+ * If provided, the handler will be called with the error and rate limiter instance.
79
+ * This can be used alongside throwOnError - the handler will be called before any error is thrown.
80
+ */
81
+ onError?: (error: Error, args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
82
+ /**
83
+ * Optional callback function that is called when an execution is rejected due to rate limiting
84
+ */
85
+ onReject?: (args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
86
+ /**
87
+ * Optional function to call when the rate-limited function is executed
88
+ */
89
+ onSettled?: (args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
90
+ /**
91
+ * Optional function to call when the rate-limited function is executed
92
+ */
93
+ onSuccess?: (result: ReturnType<TFn>, args: Parameters<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
94
+ /**
95
+ * Whether to throw errors when they occur.
96
+ * Defaults to true if no onError handler is provided, false if an onError handler is provided.
97
+ * Can be explicitly set to override these defaults.
98
+ */
99
+ throwOnError?: boolean;
100
+ /**
101
+ * Time window in milliseconds within which the limit applies.
102
+ * Can be a number or a function that returns a number.
103
+ */
104
+ window: number | ((rateLimiter: AsyncRateLimiter<TFn>) => number);
105
+ /**
106
+ * Type of window to use for rate limiting
107
+ * - 'fixed': Uses a fixed window that resets after the window period
108
+ * - 'sliding': Uses a sliding window that allows executions as old ones expire
109
+ * Defaults to 'fixed'
110
+ */
111
+ windowType?: 'fixed' | 'sliding';
110
112
  }
111
113
  /**
112
114
  * Utility function for sharing common `AsyncRateLimiterOptions` options between different `AsyncRateLimiter` instances.
113
115
  */
114
- export declare function asyncRateLimiterOptions<TFn extends AnyAsyncFunction = AnyAsyncFunction, TOptions extends Partial<AsyncRateLimiterOptions<TFn>> = Partial<AsyncRateLimiterOptions<TFn>>>(options: TOptions): TOptions;
116
+ declare function asyncRateLimiterOptions<TFn extends AnyAsyncFunction = AnyAsyncFunction, TOptions extends Partial<AsyncRateLimiterOptions<TFn>> = Partial<AsyncRateLimiterOptions<TFn>>>(options: TOptions): TOptions;
115
117
  /**
116
118
  * A class that creates an async rate-limited function.
117
119
  *
@@ -186,84 +188,84 @@ export declare function asyncRateLimiterOptions<TFn extends AnyAsyncFunction = A
186
188
  * const data = await rateLimiter.maybeExecute('123');
187
189
  * ```
188
190
  */
189
- export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
190
- #private;
191
- fn: TFn;
192
- readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>;
193
- key: string | undefined;
194
- options: AsyncRateLimiterOptions<TFn>;
195
- asyncRetryers: Map<number, AsyncRetryer<TFn>>;
196
- constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>);
197
- /**
198
- * Updates the async rate limiter options
199
- */
200
- setOptions: (newOptions: Partial<AsyncRateLimiterOptions<TFn>>) => void;
201
- /**
202
- * Attempts to execute the rate-limited function if within the configured limits.
203
- * Will reject execution if the number of calls in the current window exceeds the limit.
204
- *
205
- * Error Handling:
206
- * - If the rate-limited function throws and no `onError` handler is configured,
207
- * the error will be thrown from this method.
208
- * - If an `onError` handler is configured, errors will be caught and passed to the handler,
209
- * and this method will return undefined.
210
- * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.
211
- *
212
- * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
213
- * @throws The error from the rate-limited function if no onError handler is configured
214
- *
215
- * @example
216
- * ```ts
217
- * const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
218
- *
219
- * // First 5 calls will return a promise that resolves with the result
220
- * const result = await rateLimiter.maybeExecute('arg1', 'arg2');
221
- *
222
- * // Additional calls within the window will return undefined
223
- * const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined
224
- * ```
225
- */
226
- maybeExecute: (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
227
- /**
228
- * Returns the number of remaining executions allowed in the current window
229
- */
230
- getRemainingInWindow: () => number;
231
- /**
232
- * Returns the number of milliseconds until the next execution will be possible
233
- * For fixed windows, this is the time until the current window resets
234
- * For sliding windows, this is the time until the oldest execution expires
235
- */
236
- getMsUntilNextWindow: () => number;
237
- /**
238
- * Returns the AbortSignal for a specific execution.
239
- * If no maybeExecuteCount is provided, returns the signal for the most recent execution.
240
- * Returns null if no execution is found or not currently executing.
241
- *
242
- * @param maybeExecuteCount - Optional specific execution to get signal for
243
- * @example
244
- * ```typescript
245
- * const rateLimiter = new AsyncRateLimiter(
246
- * async (userId: string) => {
247
- * const signal = rateLimiter.getAbortSignal()
248
- * if (signal) {
249
- * const response = await fetch(`/api/users/${userId}`, { signal })
250
- * return response.json()
251
- * }
252
- * },
253
- * { limit: 5, window: 1000 }
254
- * )
255
- * ```
256
- */
257
- getAbortSignal(maybeExecuteCount?: number): AbortSignal | null;
258
- /**
259
- * Aborts all ongoing executions with the internal abort controllers.
260
- * Does NOT clear out the execution times or reset the rate limiter.
261
- */
262
- abort: () => void;
263
- /**
264
- * Resets the rate limiter state
265
- */
266
- reset: () => void;
191
+ declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
192
+ #private;
193
+ fn: TFn;
194
+ readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>;
195
+ key: string | undefined;
196
+ options: AsyncRateLimiterOptions<TFn>;
197
+ asyncRetryers: Map<number, AsyncRetryer<TFn>>;
198
+ constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>);
199
+ /**
200
+ * Updates the async rate limiter options
201
+ */
202
+ setOptions: (newOptions: Partial<AsyncRateLimiterOptions<TFn>>) => void;
203
+ /**
204
+ * Attempts to execute the rate-limited function if within the configured limits.
205
+ * Will reject execution if the number of calls in the current window exceeds the limit.
206
+ *
207
+ * Error Handling:
208
+ * - If the rate-limited function throws and no `onError` handler is configured,
209
+ * the error will be thrown from this method.
210
+ * - If an `onError` handler is configured, errors will be caught and passed to the handler,
211
+ * and this method will return undefined.
212
+ * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.
213
+ *
214
+ * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
215
+ * @throws The error from the rate-limited function if no onError handler is configured
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });
220
+ *
221
+ * // First 5 calls will return a promise that resolves with the result
222
+ * const result = await rateLimiter.maybeExecute('arg1', 'arg2');
223
+ *
224
+ * // Additional calls within the window will return undefined
225
+ * const result2 = await rateLimiter.maybeExecute('arg1', 'arg2'); // undefined
226
+ * ```
227
+ */
228
+ maybeExecute: (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
229
+ /**
230
+ * Returns the number of remaining executions allowed in the current window
231
+ */
232
+ getRemainingInWindow: () => number;
233
+ /**
234
+ * Returns the number of milliseconds until the next execution will be possible
235
+ * For fixed windows, this is the time until the current window resets
236
+ * For sliding windows, this is the time until the oldest execution expires
237
+ */
238
+ getMsUntilNextWindow: () => number;
239
+ /**
240
+ * Returns the AbortSignal for a specific execution.
241
+ * If no maybeExecuteCount is provided, returns the signal for the most recent execution.
242
+ * Returns null if no execution is found or not currently executing.
243
+ *
244
+ * @param maybeExecuteCount - Optional specific execution to get signal for
245
+ * @example
246
+ * ```typescript
247
+ * const rateLimiter = new AsyncRateLimiter(
248
+ * async (userId: string) => {
249
+ * const signal = rateLimiter.getAbortSignal()
250
+ * if (signal) {
251
+ * const response = await fetch(`/api/users/${userId}`, { signal })
252
+ * return response.json()
253
+ * }
254
+ * },
255
+ * { limit: 5, window: 1000 }
256
+ * )
257
+ * ```
258
+ */
259
+ getAbortSignal(maybeExecuteCount?: number): AbortSignal | null;
260
+ /**
261
+ * Aborts all ongoing executions with the internal abort controllers.
262
+ * Does NOT clear out the execution times or reset the rate limiter.
263
+ */
264
+ abort: () => void;
265
+ /**
266
+ * Resets the rate limiter state
267
+ */
268
+ reset: () => void;
267
269
  }
268
270
  /**
269
271
  * Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.
@@ -349,4 +351,7 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
349
351
  * const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
350
352
  * ```
351
353
  */
352
- export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
354
+ declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
355
+ //#endregion
356
+ export { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, asyncRateLimit, asyncRateLimiterOptions };
357
+ //# sourceMappingURL=async-rate-limiter.d.ts.map