@tanstack/pacer 0.16.2 → 0.16.4
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/README.md +165 -0
- package/dist/async-batcher.cjs +355 -0
- package/dist/async-batcher.cjs.map +1 -0
- package/dist/async-batcher.d.cts +344 -0
- package/dist/async-batcher.d.ts +344 -0
- package/dist/async-batcher.js +353 -0
- package/dist/async-batcher.js.map +1 -0
- package/dist/async-debouncer.cjs +340 -0
- package/dist/async-debouncer.cjs.map +1 -0
- package/dist/async-debouncer.d.cts +300 -0
- package/dist/async-debouncer.d.ts +300 -0
- package/dist/async-debouncer.js +338 -0
- package/dist/async-debouncer.js.map +1 -0
- package/dist/async-queuer.cjs +496 -0
- package/dist/async-queuer.cjs.map +1 -0
- package/dist/async-queuer.d.cts +440 -0
- package/dist/async-queuer.d.ts +440 -0
- package/dist/async-queuer.js +494 -0
- package/dist/async-queuer.js.map +1 -0
- package/dist/async-rate-limiter.cjs +381 -0
- package/dist/async-rate-limiter.cjs.map +1 -0
- package/dist/{cjs/async-rate-limiter.d.cts → async-rate-limiter.d.cts} +190 -185
- package/dist/{esm/async-rate-limiter.d.ts → async-rate-limiter.d.ts} +190 -185
- package/dist/async-rate-limiter.js +379 -0
- package/dist/async-rate-limiter.js.map +1 -0
- package/dist/async-retryer.cjs +378 -0
- package/dist/async-retryer.cjs.map +1 -0
- package/dist/async-retryer.d.cts +312 -0
- package/dist/async-retryer.d.ts +312 -0
- package/dist/async-retryer.js +376 -0
- package/dist/async-retryer.js.map +1 -0
- package/dist/async-throttler.cjs +362 -0
- package/dist/async-throttler.cjs.map +1 -0
- package/dist/async-throttler.d.cts +320 -0
- package/dist/async-throttler.d.ts +320 -0
- package/dist/async-throttler.js +360 -0
- package/dist/async-throttler.js.map +1 -0
- package/dist/batcher.cjs +194 -0
- package/dist/batcher.cjs.map +1 -0
- package/dist/batcher.d.cts +180 -0
- package/dist/batcher.d.ts +180 -0
- package/dist/batcher.js +193 -0
- package/dist/batcher.js.map +1 -0
- package/dist/debouncer.cjs +197 -0
- package/dist/debouncer.cjs.map +1 -0
- package/dist/debouncer.d.cts +167 -0
- package/dist/debouncer.d.ts +167 -0
- package/dist/debouncer.js +195 -0
- package/dist/debouncer.js.map +1 -0
- package/dist/event-client.cjs +20 -0
- package/dist/event-client.cjs.map +1 -0
- package/dist/event-client.d.cts +49 -0
- package/dist/event-client.d.ts +49 -0
- package/dist/event-client.js +19 -0
- package/dist/event-client.js.map +1 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/{esm/index.js → index.js} +5 -41
- package/dist/queuer.cjs +393 -0
- package/dist/queuer.cjs.map +1 -0
- package/dist/queuer.d.cts +345 -0
- package/dist/queuer.d.ts +345 -0
- package/dist/queuer.js +391 -0
- package/dist/queuer.js.map +1 -0
- package/dist/rate-limiter.cjs +251 -0
- package/dist/rate-limiter.cjs.map +1 -0
- package/dist/{cjs/rate-limiter.d.cts → rate-limiter.d.cts} +113 -108
- package/dist/{esm/rate-limiter.d.ts → rate-limiter.d.ts} +113 -108
- package/dist/rate-limiter.js +249 -0
- package/dist/rate-limiter.js.map +1 -0
- package/dist/throttler.cjs +209 -0
- package/dist/throttler.cjs.map +1 -0
- package/dist/throttler.d.cts +207 -0
- package/dist/throttler.d.ts +207 -0
- package/dist/throttler.js +207 -0
- package/dist/throttler.js.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/utils.cjs +13 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +11 -0
- package/dist/utils.js.map +1 -0
- package/package.json +38 -122
- package/src/async-batcher.ts +7 -5
- package/src/async-debouncer.ts +7 -5
- package/src/async-queuer.ts +7 -5
- package/src/async-rate-limiter.ts +7 -5
- package/src/async-retryer.ts +7 -5
- package/src/async-throttler.ts +7 -5
- package/src/batcher.ts +7 -5
- package/src/debouncer.ts +7 -5
- package/src/queuer.ts +7 -5
- package/src/rate-limiter.ts +7 -5
- package/src/throttler.ts +7 -5
- package/dist/cjs/async-batcher.cjs +0 -220
- package/dist/cjs/async-batcher.cjs.map +0 -1
- package/dist/cjs/async-batcher.d.cts +0 -340
- package/dist/cjs/async-debouncer.cjs +0 -231
- package/dist/cjs/async-debouncer.cjs.map +0 -1
- package/dist/cjs/async-debouncer.d.cts +0 -295
- package/dist/cjs/async-queuer.cjs +0 -397
- package/dist/cjs/async-queuer.cjs.map +0 -1
- package/dist/cjs/async-queuer.d.cts +0 -435
- package/dist/cjs/async-rate-limiter.cjs +0 -252
- package/dist/cjs/async-rate-limiter.cjs.map +0 -1
- package/dist/cjs/async-retryer.cjs +0 -285
- package/dist/cjs/async-retryer.cjs.map +0 -1
- package/dist/cjs/async-retryer.d.cts +0 -308
- package/dist/cjs/async-throttler.cjs +0 -261
- package/dist/cjs/async-throttler.cjs.map +0 -1
- package/dist/cjs/async-throttler.d.cts +0 -315
- package/dist/cjs/batcher.cjs +0 -130
- package/dist/cjs/batcher.cjs.map +0 -1
- package/dist/cjs/batcher.d.cts +0 -176
- package/dist/cjs/debouncer.cjs +0 -137
- package/dist/cjs/debouncer.cjs.map +0 -1
- package/dist/cjs/debouncer.d.cts +0 -162
- package/dist/cjs/event-client.cjs +0 -20
- package/dist/cjs/event-client.cjs.map +0 -1
- package/dist/cjs/event-client.d.cts +0 -45
- package/dist/cjs/index.cjs +0 -51
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -15
- package/dist/cjs/queuer.cjs +0 -306
- package/dist/cjs/queuer.cjs.map +0 -1
- package/dist/cjs/queuer.d.cts +0 -340
- package/dist/cjs/rate-limiter.cjs +0 -179
- package/dist/cjs/rate-limiter.cjs.map +0 -1
- package/dist/cjs/throttler.cjs +0 -151
- package/dist/cjs/throttler.cjs.map +0 -1
- package/dist/cjs/throttler.d.cts +0 -202
- package/dist/cjs/types.d.cts +0 -9
- package/dist/cjs/utils.cjs +0 -11
- package/dist/cjs/utils.cjs.map +0 -1
- package/dist/cjs/utils.d.cts +0 -3
- package/dist/esm/async-batcher.d.ts +0 -340
- package/dist/esm/async-batcher.js +0 -220
- package/dist/esm/async-batcher.js.map +0 -1
- package/dist/esm/async-debouncer.d.ts +0 -295
- package/dist/esm/async-debouncer.js +0 -231
- package/dist/esm/async-debouncer.js.map +0 -1
- package/dist/esm/async-queuer.d.ts +0 -435
- package/dist/esm/async-queuer.js +0 -397
- package/dist/esm/async-queuer.js.map +0 -1
- package/dist/esm/async-rate-limiter.js +0 -252
- package/dist/esm/async-rate-limiter.js.map +0 -1
- package/dist/esm/async-retryer.d.ts +0 -308
- package/dist/esm/async-retryer.js +0 -285
- package/dist/esm/async-retryer.js.map +0 -1
- package/dist/esm/async-throttler.d.ts +0 -315
- package/dist/esm/async-throttler.js +0 -261
- package/dist/esm/async-throttler.js.map +0 -1
- package/dist/esm/batcher.d.ts +0 -176
- package/dist/esm/batcher.js +0 -130
- package/dist/esm/batcher.js.map +0 -1
- package/dist/esm/debouncer.d.ts +0 -162
- package/dist/esm/debouncer.js +0 -137
- package/dist/esm/debouncer.js.map +0 -1
- package/dist/esm/event-client.d.ts +0 -45
- package/dist/esm/event-client.js +0 -20
- package/dist/esm/event-client.js.map +0 -1
- package/dist/esm/index.d.ts +0 -15
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/queuer.d.ts +0 -340
- package/dist/esm/queuer.js +0 -306
- package/dist/esm/queuer.js.map +0 -1
- package/dist/esm/rate-limiter.js +0 -179
- package/dist/esm/rate-limiter.js.map +0 -1
- package/dist/esm/throttler.d.ts +0 -202
- package/dist/esm/throttler.js +0 -151
- package/dist/esm/throttler.js.map +0 -1
- package/dist/esm/types.d.ts +0 -9
- package/dist/esm/utils.d.ts +0 -3
- package/dist/esm/utils.js +0 -11
- package/dist/esm/utils.js.map +0 -1
|
@@ -1,117 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AsyncRetryer, AsyncRetryerOptions } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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
|
-
|
|
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
|