@tanstack/pacer 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-debouncer.cjs +112 -63
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +66 -25
- package/dist/cjs/async-queuer.cjs +198 -124
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +91 -49
- package/dist/cjs/async-rate-limiter.cjs +83 -55
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +55 -28
- package/dist/cjs/async-throttler.cjs +121 -70
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +75 -25
- package/dist/cjs/debouncer.cjs +45 -23
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +27 -12
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/queuer.cjs +161 -101
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +80 -38
- package/dist/cjs/rate-limiter.cjs +52 -44
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +38 -46
- package/dist/cjs/throttler.cjs +57 -44
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +35 -23
- package/dist/cjs/types.d.cts +8 -0
- package/dist/cjs/utils.cjs +13 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +1 -0
- package/dist/esm/async-debouncer.d.ts +66 -25
- package/dist/esm/async-debouncer.js +112 -63
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +91 -49
- package/dist/esm/async-queuer.js +198 -124
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +55 -28
- package/dist/esm/async-rate-limiter.js +83 -55
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +75 -25
- package/dist/esm/async-throttler.js +121 -70
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +27 -12
- package/dist/esm/debouncer.js +45 -23
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +80 -38
- package/dist/esm/queuer.js +161 -101
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +38 -46
- package/dist/esm/rate-limiter.js +52 -44
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +35 -23
- package/dist/esm/throttler.js +57 -44
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +8 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +13 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +8 -1
- package/src/async-debouncer.ts +157 -88
- package/src/async-queuer.ts +266 -148
- package/src/async-rate-limiter.ts +123 -83
- package/src/async-throttler.ts +173 -89
- package/src/debouncer.ts +71 -42
- package/src/index.ts +2 -0
- package/src/queuer.ts +219 -114
- package/src/rate-limiter.ts +74 -88
- package/src/throttler.ts +83 -65
- package/src/types.ts +9 -0
- package/src/utils.ts +13 -0
|
@@ -1,28 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
* Information about a rate limit rejection
|
|
3
|
-
*/
|
|
4
|
-
export interface RateLimitRejectionInfo {
|
|
5
|
-
/**
|
|
6
|
-
* Number of milliseconds until the next execution will be possible
|
|
7
|
-
*/
|
|
8
|
-
msUntilNextWindow: number;
|
|
9
|
-
/**
|
|
10
|
-
* Current number of executions in the window
|
|
11
|
-
*/
|
|
12
|
-
currentExecutions: number;
|
|
13
|
-
/**
|
|
14
|
-
* Maximum allowed executions per window
|
|
15
|
-
*/
|
|
16
|
-
limit: number;
|
|
17
|
-
/**
|
|
18
|
-
* Total number of rejections that have occurred
|
|
19
|
-
*/
|
|
20
|
-
rejectionCount: number;
|
|
21
|
-
}
|
|
1
|
+
import { AnyFunction } from './types.js';
|
|
22
2
|
/**
|
|
23
3
|
* Options for configuring a rate-limited function
|
|
24
4
|
*/
|
|
25
|
-
export interface RateLimiterOptions {
|
|
5
|
+
export interface RateLimiterOptions<TFn extends AnyFunction> {
|
|
26
6
|
/**
|
|
27
7
|
* Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
28
8
|
* Defaults to true.
|
|
@@ -33,13 +13,17 @@ export interface RateLimiterOptions {
|
|
|
33
13
|
*/
|
|
34
14
|
limit: number;
|
|
35
15
|
/**
|
|
36
|
-
*
|
|
16
|
+
* Callback function that is called after the function is executed
|
|
37
17
|
*/
|
|
38
|
-
|
|
18
|
+
onExecute?: (rateLimiter: RateLimiter<TFn>) => void;
|
|
39
19
|
/**
|
|
40
20
|
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
41
21
|
*/
|
|
42
|
-
onReject?: (
|
|
22
|
+
onReject?: (rateLimiter: RateLimiter<TFn>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Time window in milliseconds within which the limit applies
|
|
25
|
+
*/
|
|
26
|
+
window: number;
|
|
43
27
|
}
|
|
44
28
|
/**
|
|
45
29
|
* A class that creates a rate-limited function.
|
|
@@ -66,30 +50,22 @@ export interface RateLimiterOptions {
|
|
|
66
50
|
* rateLimiter.maybeExecute('123');
|
|
67
51
|
* ```
|
|
68
52
|
*/
|
|
69
|
-
export declare class RateLimiter<TFn extends
|
|
53
|
+
export declare class RateLimiter<TFn extends AnyFunction> {
|
|
70
54
|
private fn;
|
|
71
|
-
private
|
|
72
|
-
private
|
|
73
|
-
private
|
|
74
|
-
private
|
|
75
|
-
constructor(fn: TFn, initialOptions: RateLimiterOptions);
|
|
55
|
+
private _executionCount;
|
|
56
|
+
private _rejectionCount;
|
|
57
|
+
private _executionTimes;
|
|
58
|
+
private _options;
|
|
59
|
+
constructor(fn: TFn, initialOptions: RateLimiterOptions<TFn>);
|
|
76
60
|
/**
|
|
77
61
|
* Updates the rate limiter options
|
|
78
62
|
* Returns the new options state
|
|
79
63
|
*/
|
|
80
|
-
setOptions(newOptions: Partial<RateLimiterOptions
|
|
81
|
-
/**
|
|
82
|
-
* Returns the number of times the function has been executed
|
|
83
|
-
*/
|
|
84
|
-
getExecutionCount(): number;
|
|
64
|
+
setOptions(newOptions: Partial<RateLimiterOptions<TFn>>): void;
|
|
85
65
|
/**
|
|
86
|
-
* Returns the
|
|
66
|
+
* Returns the current rate limiter options
|
|
87
67
|
*/
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns the number of remaining executions allowed in the current window
|
|
91
|
-
*/
|
|
92
|
-
getRemainingInWindow(): number;
|
|
68
|
+
getOptions(): Required<RateLimiterOptions<TFn>>;
|
|
93
69
|
/**
|
|
94
70
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
95
71
|
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
@@ -105,10 +81,26 @@ export declare class RateLimiter<TFn extends (...args: Array<any>) => any, TArgs
|
|
|
105
81
|
* rateLimiter.maybeExecute('arg1', 'arg2'); // false
|
|
106
82
|
* ```
|
|
107
83
|
*/
|
|
108
|
-
maybeExecute(...args:
|
|
84
|
+
maybeExecute(...args: Parameters<TFn>): boolean;
|
|
109
85
|
private executeFunction;
|
|
110
86
|
private rejectFunction;
|
|
111
87
|
private cleanupOldExecutions;
|
|
88
|
+
/**
|
|
89
|
+
* Returns the number of times the function has been executed
|
|
90
|
+
*/
|
|
91
|
+
getExecutionCount(): number;
|
|
92
|
+
/**
|
|
93
|
+
* Returns the number of times the function has been rejected
|
|
94
|
+
*/
|
|
95
|
+
getRejectionCount(): number;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the number of remaining executions allowed in the current window
|
|
98
|
+
*/
|
|
99
|
+
getRemainingInWindow(): number;
|
|
100
|
+
/**
|
|
101
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
102
|
+
*/
|
|
103
|
+
getMsUntilNextWindow(): number;
|
|
112
104
|
/**
|
|
113
105
|
* Resets the rate limiter state
|
|
114
106
|
*/
|
|
@@ -131,8 +123,8 @@ export declare class RateLimiter<TFn extends (...args: Array<any>) => any, TArgs
|
|
|
131
123
|
* const rateLimited = rateLimit(makeApiCall, {
|
|
132
124
|
* limit: 5,
|
|
133
125
|
* window: 60000,
|
|
134
|
-
* onReject: (
|
|
135
|
-
* console.log(`Rate limit exceeded. Try again in ${
|
|
126
|
+
* onReject: (rateLimiter) => {
|
|
127
|
+
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
136
128
|
* }
|
|
137
129
|
* });
|
|
138
130
|
*
|
|
@@ -144,4 +136,4 @@ export declare class RateLimiter<TFn extends (...args: Array<any>) => any, TArgs
|
|
|
144
136
|
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
145
137
|
* ```
|
|
146
138
|
*/
|
|
147
|
-
export declare function rateLimit<TFn extends
|
|
139
|
+
export declare function rateLimit<TFn extends AnyFunction>(fn: TFn, initialOptions: Omit<RateLimiterOptions<TFn>, 'enabled'>): (...args: Parameters<TFn>) => boolean;
|
package/dist/esm/rate-limiter.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
enabled: true,
|
|
3
3
|
limit: 1,
|
|
4
|
+
onExecute: () => {
|
|
5
|
+
},
|
|
6
|
+
onReject: () => {
|
|
7
|
+
},
|
|
4
8
|
window: 0
|
|
5
9
|
};
|
|
6
10
|
class RateLimiter {
|
|
7
11
|
constructor(fn, initialOptions) {
|
|
8
12
|
this.fn = fn;
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
+
this._executionCount = 0;
|
|
14
|
+
this._rejectionCount = 0;
|
|
15
|
+
this._executionTimes = [];
|
|
16
|
+
this._options = {
|
|
13
17
|
...defaultOptions,
|
|
14
18
|
...initialOptions
|
|
15
19
|
};
|
|
@@ -19,30 +23,13 @@ class RateLimiter {
|
|
|
19
23
|
* Returns the new options state
|
|
20
24
|
*/
|
|
21
25
|
setOptions(newOptions) {
|
|
22
|
-
this.
|
|
23
|
-
...this.options,
|
|
24
|
-
...newOptions
|
|
25
|
-
};
|
|
26
|
-
return this.options;
|
|
26
|
+
this._options = { ...this._options, ...newOptions };
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* Returns the
|
|
29
|
+
* Returns the current rate limiter options
|
|
30
30
|
*/
|
|
31
|
-
|
|
32
|
-
return this.
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Returns the number of times the function has been rejected
|
|
36
|
-
*/
|
|
37
|
-
getRejectionCount() {
|
|
38
|
-
return this.rejectionCount;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Returns the number of remaining executions allowed in the current window
|
|
42
|
-
*/
|
|
43
|
-
getRemainingInWindow() {
|
|
44
|
-
this.cleanupOldExecutions();
|
|
45
|
-
return Math.max(0, this.options.limit - this.executionTimes.length);
|
|
31
|
+
getOptions() {
|
|
32
|
+
return this._options;
|
|
46
33
|
}
|
|
47
34
|
/**
|
|
48
35
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
@@ -61,7 +48,7 @@ class RateLimiter {
|
|
|
61
48
|
*/
|
|
62
49
|
maybeExecute(...args) {
|
|
63
50
|
this.cleanupOldExecutions();
|
|
64
|
-
if (this.
|
|
51
|
+
if (this._executionTimes.length < this._options.limit) {
|
|
65
52
|
this.executeFunction(...args);
|
|
66
53
|
return true;
|
|
67
54
|
}
|
|
@@ -69,39 +56,60 @@ class RateLimiter {
|
|
|
69
56
|
return false;
|
|
70
57
|
}
|
|
71
58
|
executeFunction(...args) {
|
|
72
|
-
|
|
59
|
+
var _a, _b;
|
|
60
|
+
if (!this._options.enabled) return;
|
|
73
61
|
const now = Date.now();
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
62
|
+
this._executionCount++;
|
|
63
|
+
this._executionTimes.push(now);
|
|
76
64
|
this.fn(...args);
|
|
65
|
+
(_b = (_a = this._options).onExecute) == null ? void 0 : _b.call(_a, this);
|
|
77
66
|
}
|
|
78
67
|
rejectFunction() {
|
|
79
|
-
this.
|
|
80
|
-
if (this.
|
|
81
|
-
|
|
82
|
-
const msUntilNextWindow = oldestExecution + this.options.window - Date.now();
|
|
83
|
-
this.options.onReject({
|
|
84
|
-
msUntilNextWindow,
|
|
85
|
-
currentExecutions: this.executionTimes.length,
|
|
86
|
-
limit: this.options.limit,
|
|
87
|
-
rejectionCount: this.rejectionCount
|
|
88
|
-
});
|
|
68
|
+
this._rejectionCount++;
|
|
69
|
+
if (this._options.onReject) {
|
|
70
|
+
this._options.onReject(this);
|
|
89
71
|
}
|
|
90
72
|
}
|
|
91
73
|
cleanupOldExecutions() {
|
|
92
74
|
const now = Date.now();
|
|
93
|
-
const windowStart = now - this.
|
|
94
|
-
this.
|
|
75
|
+
const windowStart = now - this._options.window;
|
|
76
|
+
this._executionTimes = this._executionTimes.filter(
|
|
95
77
|
(time) => time > windowStart
|
|
96
78
|
);
|
|
97
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the number of times the function has been executed
|
|
82
|
+
*/
|
|
83
|
+
getExecutionCount() {
|
|
84
|
+
return this._executionCount;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns the number of times the function has been rejected
|
|
88
|
+
*/
|
|
89
|
+
getRejectionCount() {
|
|
90
|
+
return this._rejectionCount;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the number of remaining executions allowed in the current window
|
|
94
|
+
*/
|
|
95
|
+
getRemainingInWindow() {
|
|
96
|
+
this.cleanupOldExecutions();
|
|
97
|
+
return Math.max(0, this._options.limit - this._executionTimes.length);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
101
|
+
*/
|
|
102
|
+
getMsUntilNextWindow() {
|
|
103
|
+
const oldestExecution = Math.min(...this._executionTimes);
|
|
104
|
+
return oldestExecution + this._options.window - Date.now();
|
|
105
|
+
}
|
|
98
106
|
/**
|
|
99
107
|
* Resets the rate limiter state
|
|
100
108
|
*/
|
|
101
109
|
reset() {
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
110
|
+
this._executionTimes = [];
|
|
111
|
+
this._executionCount = 0;
|
|
112
|
+
this._rejectionCount = 0;
|
|
105
113
|
}
|
|
106
114
|
}
|
|
107
115
|
function rateLimit(fn, initialOptions) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiter.js","sources":["../../src/rate-limiter.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"rate-limiter.js","sources":["../../src/rate-limiter.ts"],"sourcesContent":["import type { AnyFunction } from './types'\n\n/**\n * Options for configuring a rate-limited function\n */\nexport interface RateLimiterOptions<TFn extends AnyFunction> {\n /**\n * Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Maximum number of executions allowed within the time window\n */\n limit: number\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (rateLimiter: RateLimiter<TFn>) => void\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: RateLimiter<TFn>) => void\n /**\n * Time window in milliseconds within which the limit applies\n */\n window: number\n}\n\nconst defaultOptions: Required<RateLimiterOptions<any>> = {\n enabled: true,\n limit: 1,\n onExecute: () => {},\n onReject: () => {},\n window: 0,\n}\n\n/**\n * A class that creates a rate-limited function.\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * @example\n * ```ts\n * const rateLimiter = new RateLimiter(\n * (id: string) => api.getData(id),\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Will execute immediately until limit reached, then block\n * rateLimiter.maybeExecute('123');\n * ```\n */\nexport class RateLimiter<TFn extends AnyFunction> {\n private _executionCount = 0\n private _rejectionCount = 0\n private _executionTimes: Array<number> = []\n private _options: RateLimiterOptions<TFn>\n\n constructor(\n private fn: TFn,\n initialOptions: RateLimiterOptions<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<RateLimiterOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current rate limiter options\n */\n getOptions(): Required<RateLimiterOptions<TFn>> {\n return this._options as Required<RateLimiterOptions<TFn>>\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 *\n * @example\n * ```ts\n * const rateLimiter = new RateLimiter(fn, { limit: 5, window: 1000 });\n *\n * // First 5 calls will return true\n * rateLimiter.maybeExecute('arg1', 'arg2'); // true\n *\n * // Additional calls within the window will return false\n * rateLimiter.maybeExecute('arg1', 'arg2'); // false\n * ```\n */\n maybeExecute(...args: Parameters<TFn>): boolean {\n this.cleanupOldExecutions()\n\n if (this._executionTimes.length < this._options.limit) {\n this.executeFunction(...args)\n return true\n }\n\n this.rejectFunction()\n\n return false\n }\n\n private executeFunction(...args: Parameters<TFn>): void {\n if (!this._options.enabled) return\n const now = Date.now()\n this._executionCount++\n this._executionTimes.push(now)\n this.fn(...args) // execute the function\n this._options.onExecute?.(this)\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._options.window\n this._executionTimes = this._executionTimes.filter(\n (time) => time > windowStart,\n )\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns the number of times the function has been rejected\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns the number of remaining executions allowed in the current window\n */\n getRemainingInWindow(): number {\n this.cleanupOldExecutions()\n return Math.max(0, this._options.limit - this._executionTimes.length)\n }\n\n /**\n * Returns the number of milliseconds until the next execution will be possible\n */\n getMsUntilNextWindow(): number {\n const oldestExecution = Math.min(...this._executionTimes)\n return oldestExecution + this._options.window - Date.now()\n }\n\n /**\n * Resets the rate limiter state\n */\n reset(): void {\n this._executionTimes = []\n this._executionCount = 0\n this._rejectionCount = 0\n }\n}\n\n/**\n * Creates a rate-limited function that will execute the provided function up to a maximum number of times within a time window.\n *\n * Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:\n * - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets\n * - A throttler ensures even spacing between executions, which can be better for consistent performance\n * - A debouncer collapses multiple calls into one, which is better for handling bursts of events\n *\n * Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically\n * need to enforce a hard limit on the number of executions within a time period.\n *\n * @example\n * ```ts\n * // Rate limit to 5 calls per minute\n * const rateLimited = rateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\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 * 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 rateLimit<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: Omit<RateLimiterOptions<TFn>, 'enabled'>,\n) {\n const rateLimiter = new RateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":[],"mappings":"AA6BA,MAAM,iBAAoD;AAAA,EACxD,SAAS;AAAA,EACT,OAAO;AAAA,EACP,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,QAAQ;AACV;AA2BO,MAAM,YAAqC;AAAA,EAMhD,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AANV,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAiC,CAAC;AAOxC,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WAAW,YAAoD;AAC7D,SAAK,WAAW,EAAE,GAAG,KAAK,UAAU,GAAG,WAAW;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMpD,aAAgD;AAC9C,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBd,gBAAgB,MAAgC;AAC9C,SAAK,qBAAqB;AAE1B,QAAI,KAAK,gBAAgB,SAAS,KAAK,SAAS,OAAO;AAChD,WAAA,gBAAgB,GAAG,IAAI;AACrB,aAAA;AAAA,IAAA;AAGT,SAAK,eAAe;AAEb,WAAA;AAAA,EAAA;AAAA,EAGD,mBAAmB,MAA6B;AA5F1D;AA6FQ,QAAA,CAAC,KAAK,SAAS,QAAS;AACtB,UAAA,MAAM,KAAK,IAAI;AAChB,SAAA;AACA,SAAA,gBAAgB,KAAK,GAAG;AACxB,SAAA,GAAG,GAAG,IAAI;AACV,qBAAA,UAAS,cAAT,4BAAqB;AAAA,EAAI;AAAA,EAGxB,iBAAuB;AACxB,SAAA;AACD,QAAA,KAAK,SAAS,UAAU;AACrB,WAAA,SAAS,SAAS,IAAI;AAAA,IAAA;AAAA,EAC7B;AAAA,EAGM,uBAA6B;AAC7B,UAAA,MAAM,KAAK,IAAI;AACf,UAAA,cAAc,MAAM,KAAK,SAAS;AACnC,SAAA,kBAAkB,KAAK,gBAAgB;AAAA,MAC1C,CAAC,SAAS,OAAO;AAAA,IACnB;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,uBAA+B;AAC7B,SAAK,qBAAqB;AACnB,WAAA,KAAK,IAAI,GAAG,KAAK,SAAS,QAAQ,KAAK,gBAAgB,MAAM;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMtE,uBAA+B;AAC7B,UAAM,kBAAkB,KAAK,IAAI,GAAG,KAAK,eAAe;AACxD,WAAO,kBAAkB,KAAK,SAAS,SAAS,KAAK,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM3D,QAAc;AACZ,SAAK,kBAAkB,CAAC;AACxB,SAAK,kBAAkB;AACvB,SAAK,kBAAkB;AAAA,EAAA;AAE3B;AAgCgB,SAAA,UACd,IACA,gBACA;AACA,QAAM,cAAc,IAAI,YAAY,IAAI,cAAc;AAC/C,SAAA,YAAY,aAAa,KAAK,WAAW;AAClD;"}
|
package/dist/esm/throttler.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { AnyFunction } from './types.js';
|
|
1
2
|
/**
|
|
2
3
|
* Options for configuring a throttled function
|
|
3
4
|
*/
|
|
4
|
-
export interface ThrottlerOptions {
|
|
5
|
+
export interface ThrottlerOptions<TFn extends AnyFunction> {
|
|
5
6
|
/**
|
|
6
7
|
* Whether the throttler is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
7
8
|
* Defaults to true.
|
|
@@ -12,6 +13,10 @@ export interface ThrottlerOptions {
|
|
|
12
13
|
* Defaults to true.
|
|
13
14
|
*/
|
|
14
15
|
leading?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Callback function that is called after the function is executed
|
|
18
|
+
*/
|
|
19
|
+
onExecute?: (throttler: Throttler<TFn>) => void;
|
|
15
20
|
/**
|
|
16
21
|
* Whether to execute on the trailing edge of the timeout.
|
|
17
22
|
* Defaults to true.
|
|
@@ -33,8 +38,7 @@ export interface ThrottlerOptions {
|
|
|
33
38
|
* - Leading: Execute immediately on first call (default: true)
|
|
34
39
|
* - Trailing: Execute after wait period if called during throttle (default: true)
|
|
35
40
|
*
|
|
36
|
-
* For
|
|
37
|
-
* For collapsing rapid-fire events, consider using Debouncer.
|
|
41
|
+
* For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.
|
|
38
42
|
*
|
|
39
43
|
* @example
|
|
40
44
|
* ```ts
|
|
@@ -50,31 +54,23 @@ export interface ThrottlerOptions {
|
|
|
50
54
|
* throttler.maybeExecute('123'); // Throttled
|
|
51
55
|
* ```
|
|
52
56
|
*/
|
|
53
|
-
export declare class Throttler<TFn extends
|
|
57
|
+
export declare class Throttler<TFn extends AnyFunction> {
|
|
54
58
|
private fn;
|
|
55
|
-
private
|
|
56
|
-
private
|
|
57
|
-
private
|
|
58
|
-
private
|
|
59
|
-
private
|
|
60
|
-
constructor(fn: TFn, initialOptions: ThrottlerOptions);
|
|
59
|
+
private _executionCount;
|
|
60
|
+
private _lastArgs;
|
|
61
|
+
private _lastExecutionTime;
|
|
62
|
+
private _options;
|
|
63
|
+
private _timeoutId;
|
|
64
|
+
constructor(fn: TFn, initialOptions: ThrottlerOptions<TFn>);
|
|
61
65
|
/**
|
|
62
66
|
* Updates the throttler options
|
|
63
67
|
* Returns the new options state
|
|
64
68
|
*/
|
|
65
|
-
setOptions(newOptions: Partial<ThrottlerOptions
|
|
69
|
+
setOptions(newOptions: Partial<ThrottlerOptions<TFn>>): void;
|
|
66
70
|
/**
|
|
67
|
-
* Returns the
|
|
71
|
+
* Returns the current throttler options
|
|
68
72
|
*/
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Returns the last execution time
|
|
72
|
-
*/
|
|
73
|
-
getLastExecutionTime(): number;
|
|
74
|
-
/**
|
|
75
|
-
* Returns the next execution time
|
|
76
|
-
*/
|
|
77
|
-
getNextExecutionTime(): number;
|
|
73
|
+
getOptions(): Required<ThrottlerOptions<TFn>>;
|
|
78
74
|
/**
|
|
79
75
|
* Attempts to execute the throttled function. The execution behavior depends on the throttler options:
|
|
80
76
|
*
|
|
@@ -97,7 +93,7 @@ export declare class Throttler<TFn extends (...args: Array<any>) => any, TArgs e
|
|
|
97
93
|
* throttled.maybeExecute('c', 'd');
|
|
98
94
|
* ```
|
|
99
95
|
*/
|
|
100
|
-
maybeExecute(...args:
|
|
96
|
+
maybeExecute(...args: Parameters<TFn>): void;
|
|
101
97
|
private executeFunction;
|
|
102
98
|
/**
|
|
103
99
|
* Cancels any pending trailing execution and clears internal state.
|
|
@@ -109,6 +105,22 @@ export declare class Throttler<TFn extends (...args: Array<any>) => any, TArgs e
|
|
|
109
105
|
* Has no effect if there is no pending execution.
|
|
110
106
|
*/
|
|
111
107
|
cancel(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the last execution time
|
|
110
|
+
*/
|
|
111
|
+
getLastExecutionTime(): number;
|
|
112
|
+
/**
|
|
113
|
+
* Returns the next execution time
|
|
114
|
+
*/
|
|
115
|
+
getNextExecutionTime(): number;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the number of times the function has been executed
|
|
118
|
+
*/
|
|
119
|
+
getExecutionCount(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Returns `true` if there is a pending execution
|
|
122
|
+
*/
|
|
123
|
+
getIsPending(): boolean;
|
|
112
124
|
}
|
|
113
125
|
/**
|
|
114
126
|
* Creates a throttled function that limits how often the provided function can execute.
|
|
@@ -136,4 +148,4 @@ export declare class Throttler<TFn extends (...args: Array<any>) => any, TArgs e
|
|
|
136
148
|
* });
|
|
137
149
|
* ```
|
|
138
150
|
*/
|
|
139
|
-
export declare function throttle<TFn extends
|
|
151
|
+
export declare function throttle<TFn extends AnyFunction>(fn: TFn, initialOptions: Omit<ThrottlerOptions<TFn>, 'enabled'>): (...args: Parameters<TFn>) => void;
|
package/dist/esm/throttler.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
enabled: true,
|
|
3
3
|
leading: true,
|
|
4
|
+
onExecute: () => {
|
|
5
|
+
},
|
|
4
6
|
trailing: true,
|
|
5
7
|
wait: 0
|
|
6
8
|
};
|
|
7
9
|
class Throttler {
|
|
8
10
|
constructor(fn, initialOptions) {
|
|
9
11
|
this.fn = fn;
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
12
|
+
this._executionCount = 0;
|
|
13
|
+
this._lastExecutionTime = 0;
|
|
14
|
+
this._options = {
|
|
13
15
|
...defaultOptions,
|
|
14
16
|
...initialOptions
|
|
15
17
|
};
|
|
@@ -19,29 +21,16 @@ class Throttler {
|
|
|
19
21
|
* Returns the new options state
|
|
20
22
|
*/
|
|
21
23
|
setOptions(newOptions) {
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return this.options;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Returns the number of times the function has been executed
|
|
30
|
-
*/
|
|
31
|
-
getExecutionCount() {
|
|
32
|
-
return this.executionCount;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Returns the last execution time
|
|
36
|
-
*/
|
|
37
|
-
getLastExecutionTime() {
|
|
38
|
-
return this.lastExecutionTime;
|
|
24
|
+
this._options = { ...this._options, ...newOptions };
|
|
25
|
+
if (!this._options.enabled) {
|
|
26
|
+
this.cancel();
|
|
27
|
+
}
|
|
39
28
|
}
|
|
40
29
|
/**
|
|
41
|
-
* Returns the
|
|
30
|
+
* Returns the current throttler options
|
|
42
31
|
*/
|
|
43
|
-
|
|
44
|
-
return this.
|
|
32
|
+
getOptions() {
|
|
33
|
+
return this._options;
|
|
45
34
|
}
|
|
46
35
|
/**
|
|
47
36
|
* Attempts to execute the throttled function. The execution behavior depends on the throttler options:
|
|
@@ -67,30 +56,30 @@ class Throttler {
|
|
|
67
56
|
*/
|
|
68
57
|
maybeExecute(...args) {
|
|
69
58
|
const now = Date.now();
|
|
70
|
-
const timeSinceLastExecution = now - this.
|
|
71
|
-
if (timeSinceLastExecution >= this.
|
|
72
|
-
|
|
73
|
-
this.executeFunction(...args);
|
|
74
|
-
}
|
|
75
|
-
this.lastExecutionTime = now;
|
|
59
|
+
const timeSinceLastExecution = now - this._lastExecutionTime;
|
|
60
|
+
if (this._options.leading && timeSinceLastExecution >= this._options.wait) {
|
|
61
|
+
this.executeFunction(...args);
|
|
76
62
|
} else {
|
|
77
|
-
this.
|
|
78
|
-
if (!this.
|
|
79
|
-
this.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
63
|
+
this._lastArgs = args;
|
|
64
|
+
if (!this._timeoutId && this._options.trailing) {
|
|
65
|
+
const _timeSinceLastExecution = this._lastExecutionTime ? now - this._lastExecutionTime : 0;
|
|
66
|
+
const timeoutDuration = this._options.wait - _timeSinceLastExecution;
|
|
67
|
+
this._timeoutId = setTimeout(() => {
|
|
68
|
+
if (this._lastArgs !== void 0) {
|
|
69
|
+
this.executeFunction(...this._lastArgs);
|
|
83
70
|
}
|
|
84
|
-
|
|
85
|
-
this.timeoutId = void 0;
|
|
86
|
-
}, this.options.wait - timeSinceLastExecution);
|
|
71
|
+
}, timeoutDuration);
|
|
87
72
|
}
|
|
88
73
|
}
|
|
89
74
|
}
|
|
90
75
|
executeFunction(...args) {
|
|
91
|
-
if (!this.
|
|
92
|
-
this.executionCount++;
|
|
76
|
+
if (!this._options.enabled) return;
|
|
93
77
|
this.fn(...args);
|
|
78
|
+
this._executionCount++;
|
|
79
|
+
this._lastExecutionTime = Date.now();
|
|
80
|
+
this._timeoutId = void 0;
|
|
81
|
+
this._lastArgs = void 0;
|
|
82
|
+
this._options.onExecute(this);
|
|
94
83
|
}
|
|
95
84
|
/**
|
|
96
85
|
* Cancels any pending trailing execution and clears internal state.
|
|
@@ -102,12 +91,36 @@ class Throttler {
|
|
|
102
91
|
* Has no effect if there is no pending execution.
|
|
103
92
|
*/
|
|
104
93
|
cancel() {
|
|
105
|
-
if (this.
|
|
106
|
-
clearTimeout(this.
|
|
107
|
-
this.
|
|
108
|
-
this.
|
|
94
|
+
if (this._timeoutId) {
|
|
95
|
+
clearTimeout(this._timeoutId);
|
|
96
|
+
this._timeoutId = void 0;
|
|
97
|
+
this._lastArgs = void 0;
|
|
109
98
|
}
|
|
110
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns the last execution time
|
|
102
|
+
*/
|
|
103
|
+
getLastExecutionTime() {
|
|
104
|
+
return this._lastExecutionTime;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns the next execution time
|
|
108
|
+
*/
|
|
109
|
+
getNextExecutionTime() {
|
|
110
|
+
return this._lastExecutionTime + this._options.wait;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns the number of times the function has been executed
|
|
114
|
+
*/
|
|
115
|
+
getExecutionCount() {
|
|
116
|
+
return this._executionCount;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns `true` if there is a pending execution
|
|
120
|
+
*/
|
|
121
|
+
getIsPending() {
|
|
122
|
+
return this._options.enabled && !!this._timeoutId;
|
|
123
|
+
}
|
|
111
124
|
}
|
|
112
125
|
function throttle(fn, initialOptions) {
|
|
113
126
|
const throttler = new Throttler(fn, initialOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throttler.js","sources":["../../src/throttler.ts"],"sourcesContent":["/**\n * Options for configuring a throttled function\n */\nexport interface ThrottlerOptions {\n /**\n * Whether the throttler is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to true.\n */\n leading?: boolean\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once\n */\n wait: number\n}\n\nconst defaultOptions: Required<ThrottlerOptions
|
|
1
|
+
{"version":3,"file":"throttler.js","sources":["../../src/throttler.ts"],"sourcesContent":["import type { AnyFunction } from './types'\n\n/**\n * Options for configuring a throttled function\n */\nexport interface ThrottlerOptions<TFn extends AnyFunction> {\n /**\n * Whether the throttler is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to true.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (throttler: Throttler<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once\n */\n wait: number\n}\n\nconst defaultOptions: Required<ThrottlerOptions<any>> = {\n enabled: true,\n leading: true,\n onExecute: () => {},\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a throttled function.\n *\n * Throttling ensures a function is called at most once within a specified time window.\n * Unlike debouncing which waits for a pause in calls, throttling guarantees consistent\n * execution timing regardless of call frequency.\n *\n * Supports both leading and trailing edge execution:\n * - Leading: Execute immediately on first call (default: true)\n * - Trailing: Execute after wait period if called during throttle (default: true)\n *\n * For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.\n *\n * @example\n * ```ts\n * const throttler = new Throttler(\n * (id: string) => api.getData(id),\n * { wait: 1000 } // Execute at most once per second\n * );\n *\n * // First call executes immediately\n * throttler.maybeExecute('123');\n *\n * // Subsequent calls within 1000ms are throttled\n * throttler.maybeExecute('123'); // Throttled\n * ```\n */\nexport class Throttler<TFn extends AnyFunction> {\n private _executionCount = 0\n private _lastArgs: Parameters<TFn> | undefined\n private _lastExecutionTime = 0\n private _options: Required<ThrottlerOptions<TFn>>\n private _timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n private fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the throttler options\n * Returns the new options state\n */\n setOptions(newOptions: Partial<ThrottlerOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n\n // End the pending state if the debouncer is disabled\n if (!this._options.enabled) {\n this.cancel()\n }\n }\n\n /**\n * Returns the current throttler options\n */\n getOptions(): Required<ThrottlerOptions<TFn>> {\n return this._options\n }\n\n /**\n * Attempts to execute the throttled function. The execution behavior depends on the throttler options:\n *\n * - If enough time has passed since the last execution (>= wait period):\n * - With leading=true: Executes immediately\n * - With leading=false: Waits for the next trailing execution\n *\n * - If within the wait period:\n * - With trailing=true: Schedules execution for end of wait period\n * - With trailing=false: Drops the execution\n *\n * @example\n * ```ts\n * const throttled = new Throttler(fn, { wait: 1000 });\n *\n * // First call executes immediately\n * throttled.maybeExecute('a', 'b');\n *\n * // Call during wait period - gets throttled\n * throttled.maybeExecute('c', 'd');\n * ```\n */\n maybeExecute(...args: Parameters<TFn>): void {\n const now = Date.now()\n const timeSinceLastExecution = now - this._lastExecutionTime\n\n // Handle leading execution\n if (this._options.leading && timeSinceLastExecution >= this._options.wait) {\n this.executeFunction(...args)\n } else {\n // Store the most recent arguments for potential trailing execution\n this._lastArgs = args\n\n // Set up trailing execution if not already scheduled\n if (!this._timeoutId && this._options.trailing) {\n const _timeSinceLastExecution = this._lastExecutionTime\n ? now - this._lastExecutionTime\n : 0\n const timeoutDuration = this._options.wait - _timeSinceLastExecution\n this._timeoutId = setTimeout(() => {\n if (this._lastArgs !== undefined) {\n this.executeFunction(...this._lastArgs)\n }\n }, timeoutDuration)\n }\n }\n }\n\n private executeFunction(...args: Parameters<TFn>): void {\n if (!this._options.enabled) return\n this.fn(...args) // EXECUTE!\n this._executionCount++\n this._lastExecutionTime = Date.now()\n this._timeoutId = undefined\n this._lastArgs = undefined\n this._options.onExecute(this)\n }\n\n /**\n * Cancels any pending trailing execution and clears internal state.\n *\n * If a trailing execution is scheduled (due to throttling with trailing=true),\n * this will prevent that execution from occurring. The internal timeout and\n * stored arguments will be cleared.\n *\n * Has no effect if there is no pending execution.\n */\n cancel(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n this._timeoutId = undefined\n this._lastArgs = undefined\n }\n }\n\n /**\n * Returns the last execution time\n */\n getLastExecutionTime(): number {\n return this._lastExecutionTime\n }\n\n /**\n * Returns the next execution time\n */\n getNextExecutionTime(): number {\n return this._lastExecutionTime + this._options.wait\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns `true` if there is a pending execution\n */\n getIsPending(): boolean {\n return this._options.enabled && !!this._timeoutId\n }\n}\n\n/**\n * Creates a throttled function that limits how often the provided function can execute.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * The throttled function can be configured to execute on the leading and/or trailing\n * edge of the throttle window via options.\n *\n * For handling bursts of events, consider using debounce() instead. For hard execution\n * limits, consider using rateLimit().\n *\n * @example\n * ```ts\n * // Basic throttling - max once per second\n * const throttled = throttle(updateUI, { wait: 1000 });\n *\n * // Configure leading/trailing execution\n * const throttled = throttle(saveData, {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: true // Execute again after delay if called during wait\n * });\n * ```\n */\nexport function throttle<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: Omit<ThrottlerOptions<TFn>, 'enabled'>,\n) {\n const throttler = new Throttler(fn, initialOptions)\n return throttler.maybeExecute.bind(throttler)\n}\n"],"names":[],"mappings":"AA+BA,MAAM,iBAAkD;AAAA,EACtD,SAAS;AAAA,EACT,SAAS;AAAA,EACT,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,UAAU;AAAA,EACV,MAAM;AACR;AA6BO,MAAM,UAAmC;AAAA,EAO9C,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AAPV,SAAQ,kBAAkB;AAE1B,SAAQ,qBAAqB;AAQ3B,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WAAW,YAAkD;AAC3D,SAAK,WAAW,EAAE,GAAG,KAAK,UAAU,GAAG,WAAW;AAG9C,QAAA,CAAC,KAAK,SAAS,SAAS;AAC1B,WAAK,OAAO;AAAA,IAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAMF,aAA8C;AAC5C,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBd,gBAAgB,MAA6B;AACrC,UAAA,MAAM,KAAK,IAAI;AACf,UAAA,yBAAyB,MAAM,KAAK;AAG1C,QAAI,KAAK,SAAS,WAAW,0BAA0B,KAAK,SAAS,MAAM;AACpE,WAAA,gBAAgB,GAAG,IAAI;AAAA,IAAA,OACvB;AAEL,WAAK,YAAY;AAGjB,UAAI,CAAC,KAAK,cAAc,KAAK,SAAS,UAAU;AAC9C,cAAM,0BAA0B,KAAK,qBACjC,MAAM,KAAK,qBACX;AACE,cAAA,kBAAkB,KAAK,SAAS,OAAO;AACxC,aAAA,aAAa,WAAW,MAAM;AAC7B,cAAA,KAAK,cAAc,QAAW;AAC3B,iBAAA,gBAAgB,GAAG,KAAK,SAAS;AAAA,UAAA;AAAA,WAEvC,eAAe;AAAA,MAAA;AAAA,IACpB;AAAA,EACF;AAAA,EAGM,mBAAmB,MAA6B;AAClD,QAAA,CAAC,KAAK,SAAS,QAAS;AACvB,SAAA,GAAG,GAAG,IAAI;AACV,SAAA;AACA,SAAA,qBAAqB,KAAK,IAAI;AACnC,SAAK,aAAa;AAClB,SAAK,YAAY;AACZ,SAAA,SAAS,UAAU,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,SAAe;AACb,QAAI,KAAK,YAAY;AACnB,mBAAa,KAAK,UAAU;AAC5B,WAAK,aAAa;AAClB,WAAK,YAAY;AAAA,IAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA,EAMF,uBAA+B;AAC7B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,uBAA+B;AACtB,WAAA,KAAK,qBAAqB,KAAK,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMjD,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WAAO,KAAK,SAAS,WAAW,CAAC,CAAC,KAAK;AAAA,EAAA;AAE3C;AA4BgB,SAAA,SACd,IACA,gBACA;AACA,QAAM,YAAY,IAAI,UAAU,IAAI,cAAc;AAC3C,SAAA,UAAU,aAAa,KAAK,SAAS;AAC9C;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a function that can be called with any arguments and returns any value.
|
|
3
|
+
*/
|
|
4
|
+
export type AnyFunction = (...args: Array<any>) => any;
|
|
5
|
+
/**
|
|
6
|
+
* Represents an asynchronous function that can be called with any arguments and returns a promise.
|
|
7
|
+
*/
|
|
8
|
+
export type AnyAsyncFunction = (...args: Array<any>) => Promise<any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function bindInstanceMethods<T extends Record<string, any>>(instance: T): T;
|