@tanstack/pacer 0.2.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 +78 -45
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +44 -16
- package/dist/cjs/async-queuer.cjs +53 -3
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +28 -3
- package/dist/cjs/async-rate-limiter.cjs +46 -35
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +34 -19
- package/dist/cjs/async-throttler.cjs +85 -57
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +49 -17
- package/dist/cjs/debouncer.cjs +12 -14
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +10 -9
- package/dist/cjs/queuer.cjs +51 -1
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +30 -1
- package/dist/cjs/rate-limiter.cjs +1 -5
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +9 -9
- package/dist/cjs/throttler.cjs +29 -36
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +16 -17
- package/dist/cjs/types.d.cts +2 -6
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +1 -1
- package/dist/esm/async-debouncer.d.ts +44 -16
- package/dist/esm/async-debouncer.js +78 -45
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +28 -3
- package/dist/esm/async-queuer.js +53 -3
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +34 -19
- package/dist/esm/async-rate-limiter.js +46 -35
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +49 -17
- package/dist/esm/async-throttler.js +85 -57
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +10 -9
- package/dist/esm/debouncer.js +12 -14
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/queuer.d.ts +30 -1
- package/dist/esm/queuer.js +51 -1
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +9 -9
- package/dist/esm/rate-limiter.js +1 -5
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +16 -17
- package/dist/esm/throttler.js +29 -36
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +2 -6
- package/dist/esm/utils.d.ts +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/async-debouncer.ts +114 -73
- package/src/async-queuer.ts +93 -8
- package/src/async-rate-limiter.ts +74 -60
- package/src/async-throttler.ts +135 -86
- package/src/debouncer.ts +26 -33
- package/src/queuer.ts +92 -4
- package/src/rate-limiter.ts +14 -26
- package/src/throttler.ts +45 -53
- package/src/types.ts +2 -10
- package/src/utils.ts +1 -1
|
@@ -2,19 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const defaultOptions = {
|
|
4
4
|
enabled: true,
|
|
5
|
+
onError: () => {
|
|
6
|
+
},
|
|
5
7
|
onReject: () => {
|
|
6
8
|
},
|
|
7
|
-
|
|
9
|
+
onSettled: () => {
|
|
8
10
|
},
|
|
9
|
-
|
|
11
|
+
onSuccess: () => {
|
|
10
12
|
}
|
|
11
13
|
};
|
|
12
14
|
class AsyncRateLimiter {
|
|
13
15
|
constructor(fn, initialOptions) {
|
|
14
16
|
this.fn = fn;
|
|
15
|
-
this.
|
|
17
|
+
this._errorCount = 0;
|
|
16
18
|
this._executionTimes = [];
|
|
17
19
|
this._rejectionCount = 0;
|
|
20
|
+
this._settleCount = 0;
|
|
21
|
+
this._successCount = 0;
|
|
18
22
|
this._options = {
|
|
19
23
|
...defaultOptions,
|
|
20
24
|
...initialOptions
|
|
@@ -25,11 +29,7 @@ class AsyncRateLimiter {
|
|
|
25
29
|
* Returns the new options state
|
|
26
30
|
*/
|
|
27
31
|
setOptions(newOptions) {
|
|
28
|
-
this._options = {
|
|
29
|
-
...this._options,
|
|
30
|
-
...newOptions
|
|
31
|
-
};
|
|
32
|
-
return this._options;
|
|
32
|
+
this._options = { ...this._options, ...newOptions };
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Returns the current rate limiter options
|
|
@@ -57,31 +57,28 @@ class AsyncRateLimiter {
|
|
|
57
57
|
this.cleanupOldExecutions();
|
|
58
58
|
if (this._executionTimes.length < this._options.limit) {
|
|
59
59
|
await this.executeFunction(...args);
|
|
60
|
-
return
|
|
60
|
+
return this._lastResult;
|
|
61
61
|
}
|
|
62
62
|
this.rejectFunction();
|
|
63
|
-
return
|
|
63
|
+
return void 0;
|
|
64
64
|
}
|
|
65
65
|
async executeFunction(...args) {
|
|
66
|
-
var _a, _b;
|
|
66
|
+
var _a, _b, _c, _d, _e, _f;
|
|
67
67
|
if (!this._options.enabled) return;
|
|
68
68
|
const now = Date.now();
|
|
69
|
-
this._executionCount++;
|
|
70
69
|
this._executionTimes.push(now);
|
|
71
70
|
try {
|
|
72
|
-
await this.fn(...args);
|
|
71
|
+
this._lastResult = await this.fn(...args);
|
|
72
|
+
this._successCount++;
|
|
73
|
+
(_b = (_a = this._options).onSuccess) == null ? void 0 : _b.call(_a, this._lastResult, this);
|
|
73
74
|
} catch (error) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this._options.onError(error);
|
|
77
|
-
} catch (error2) {
|
|
78
|
-
console.error("Error in onError handler", error2);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
throw error;
|
|
75
|
+
this._errorCount++;
|
|
76
|
+
(_d = (_c = this._options).onError) == null ? void 0 : _d.call(_c, error, this);
|
|
82
77
|
} finally {
|
|
83
|
-
|
|
78
|
+
this._settleCount++;
|
|
79
|
+
(_f = (_e = this._options).onSettled) == null ? void 0 : _f.call(_e, this);
|
|
84
80
|
}
|
|
81
|
+
return this._lastResult;
|
|
85
82
|
}
|
|
86
83
|
rejectFunction() {
|
|
87
84
|
this._rejectionCount++;
|
|
@@ -96,18 +93,6 @@ class AsyncRateLimiter {
|
|
|
96
93
|
(time) => time > windowStart
|
|
97
94
|
);
|
|
98
95
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Returns the number of times the function has been executed
|
|
101
|
-
*/
|
|
102
|
-
getExecutionCount() {
|
|
103
|
-
return this._executionCount;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Returns the number of times the function has been rejected
|
|
107
|
-
*/
|
|
108
|
-
getRejectionCount() {
|
|
109
|
-
return this._rejectionCount;
|
|
110
|
-
}
|
|
111
96
|
/**
|
|
112
97
|
* Returns the number of remaining executions allowed in the current window
|
|
113
98
|
*/
|
|
@@ -121,13 +106,39 @@ class AsyncRateLimiter {
|
|
|
121
106
|
getMsUntilNextWindow() {
|
|
122
107
|
return this.getRemainingInWindow() * this._options.window;
|
|
123
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns the number of times the function has been executed
|
|
111
|
+
*/
|
|
112
|
+
getSuccessCount() {
|
|
113
|
+
return this._successCount;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns the number of times the function has been settled
|
|
117
|
+
*/
|
|
118
|
+
getSettleCount() {
|
|
119
|
+
return this._settleCount;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Returns the number of times the function has errored
|
|
123
|
+
*/
|
|
124
|
+
getErrorCount() {
|
|
125
|
+
return this._errorCount;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns the number of times the function has been rejected
|
|
129
|
+
*/
|
|
130
|
+
getRejectionCount() {
|
|
131
|
+
return this._rejectionCount;
|
|
132
|
+
}
|
|
124
133
|
/**
|
|
125
134
|
* Resets the rate limiter state
|
|
126
135
|
*/
|
|
127
136
|
reset() {
|
|
128
137
|
this._executionTimes = [];
|
|
129
|
-
this.
|
|
138
|
+
this._successCount = 0;
|
|
139
|
+
this._errorCount = 0;
|
|
130
140
|
this._rejectionCount = 0;
|
|
141
|
+
this._settleCount = 0;
|
|
131
142
|
}
|
|
132
143
|
}
|
|
133
144
|
function asyncRateLimit(fn, initialOptions) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-rate-limiter.cjs","sources":["../../src/async-rate-limiter.ts"],"sourcesContent":["import type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async rate-limited function\n */\nexport interface AsyncRateLimiterOptions<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\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 * Optional error handler for when the rate-limited function throws\n */\n onError?: (error: unknown) => void\n /**\n * Optional function to call when the rate-limited function is executed\n */\n onExecute?: (rateLimiter: AsyncRateLimiter<TFn, TArgs>) => void\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: AsyncRateLimiter<TFn, TArgs>) => void\n /**\n * Time window in milliseconds within which the limit applies\n */\n window: number\n}\n\nconst defaultOptions: Required<\n Omit<AsyncRateLimiterOptions<any, any>, 'limit' | 'window'>\n> = {\n enabled: true,\n onReject: () => {},\n onError: () => {},\n onExecute: () => {},\n}\n\n/**\n * A class that creates an async rate-limited function.\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(\n * async (id: string) => await api.getData(id),\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Will execute immediately until limit reached, then block\n * await rateLimiter.maybeExecute('123');\n * ```\n */\nexport class AsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n private _executionCount = 0\n private _executionTimes: Array<number> = []\n private _options: AsyncRateLimiterOptions<TFn, TArgs>\n private _rejectionCount = 0\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn, TArgs>,\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(\n newOptions: Partial<AsyncRateLimiterOptions<TFn, TArgs>>,\n ): AsyncRateLimiterOptions<TFn, TArgs> {\n this._options = {\n ...this._options,\n ...newOptions,\n }\n return this._options\n }\n\n /**\n * Returns the current rate limiter options\n */\n getOptions(): Required<AsyncRateLimiterOptions<TFn, TArgs>> {\n return this._options as Required<AsyncRateLimiterOptions<TFn, TArgs>>\n }\n\n /**\n * Attempts to execute the rate-limited function if within the configured limits.\n * Will reject execution if the number of calls in the current window exceeds the limit.\n * If execution is allowed, waits for any previous execution to complete before proceeding.\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(fn, { limit: 5, window: 1000 });\n *\n * // First 5 calls will execute\n * await rateLimiter.maybeExecute('arg1', 'arg2');\n *\n * // Additional calls within the window will be rejected\n * await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected\n * ```\n */\n async maybeExecute(...args: TArgs): Promise<boolean> {\n this.cleanupOldExecutions()\n\n if (this._executionTimes.length < this._options.limit) {\n await this.executeFunction(...args)\n return true\n }\n\n this.rejectFunction()\n return false\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this._options.enabled) return\n const now = Date.now()\n this._executionCount++\n this._executionTimes.push(now)\n\n try {\n await this.fn(...args)\n } catch (error) {\n if (this._options.onError) {\n try {\n this._options.onError(error)\n } catch (error) {\n console.error('Error in onError handler', error)\n }\n }\n throw error\n } finally {\n this._options.onExecute?.(this)\n }\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 return this.getRemainingInWindow() * this._options.window\n }\n\n /**\n * Resets the rate limiter state\n */\n reset(): void {\n this._executionTimes = []\n this._executionCount = 0\n this._rejectionCount = 0\n }\n}\n\n/**\n * Creates an async rate-limited function that will execute the provided function up to a maximum number of times within a time window.\n *\n * Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:\n * - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets\n * - A throttler ensures even spacing between executions, which can be better for consistent performance\n * - A debouncer collapses multiple calls into one, which is better for handling bursts of events\n *\n * Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically\n * need to enforce a hard limit on the number of executions within a time period.\n *\n * @example\n * ```ts\n * // Rate limit to 5 calls per minute\n * const rateLimited = asyncRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\n * onReject: (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 * await rateLimited();\n *\n * // For more even execution, consider using throttle instead:\n * const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds\n * ```\n */\nexport function asyncRateLimit<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n>(\n fn: TFn,\n initialOptions: Omit<AsyncRateLimiterOptions<TFn, TArgs>, 'enabled'>,\n) {\n const rateLimiter = new AsyncRateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":["error"],"mappings":";;AAoCA,MAAM,iBAEF;AAAA,EACF,SAAS;AAAA,EACT,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAA;AACnB;AA2BO,MAAM,iBAGX;AAAA,EAMA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AANV,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAiC,CAAC;AAE1C,SAAQ,kBAAkB;AAMxB,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YACqC;AACrC,SAAK,WAAW;AAAA,MACd,GAAG,KAAK;AAAA,MACR,GAAG;AAAA,IACL;AACA,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,aAA4D;AAC1D,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBd,MAAM,gBAAgB,MAA+B;AACnD,SAAK,qBAAqB;AAE1B,QAAI,KAAK,gBAAgB,SAAS,KAAK,SAAS,OAAO;AAC/C,YAAA,KAAK,gBAAgB,GAAG,IAAI;AAC3B,aAAA;AAA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xB,SAAK,kBAAkB;AACvB,SAAK,kBAAkB;AAAA,EAAA;AAE3B;AAgCgB,SAAA,eAId,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 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 * Defaults to true.\n */\n enabled?: boolean\n /**\n * Maximum number of executions allowed within the time window\n */\n limit: 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 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 * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void\n /**\n * Time window in milliseconds within which the limit applies\n */\n window: number\n}\n\nconst defaultOptions: Required<\n Omit<AsyncRateLimiterOptions<any>, 'limit' | 'window'>\n> = {\n enabled: true,\n onError: () => {},\n onReject: () => {},\n onSettled: () => {},\n onSuccess: () => {},\n}\n\n/**\n * A class that creates an async rate-limited function.\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * @example\n * ```ts\n * const rateLimiter = new AsyncRateLimiter(\n * async (id: string) => await api.getData(id),\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Will execute immediately until limit reached, then block\n * await rateLimiter.maybeExecute('123');\n * ```\n */\nexport class AsyncRateLimiter<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\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 * 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 if (this._executionTimes.length < this._options.limit) {\n await this.executeFunction(...args)\n return this._lastResult\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._options.enabled) return\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._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._options.window\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._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 return this.getRemainingInWindow() * this._options.window\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 * 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 * Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:\n * - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets\n * - A throttler ensures even spacing between executions, which can be better for consistent performance\n * - A debouncer collapses multiple calls into one, which is better for handling bursts of events\n *\n * Consider using throttle() or debounce() if you need more intelligent execution control. Use rate limiting when you specifically\n * need to enforce a hard limit on the number of executions within a time period.\n *\n * @example\n * ```ts\n * // Rate limit to 5 calls per minute\n * const rateLimited = asyncRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000,\n * onReject: (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 * 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: Omit<AsyncRateLimiterOptions<TFn>, 'enabled'>,\n) {\n const rateLimiter = new AsyncRateLimiter(fn, initialOptions)\n return rateLimiter.maybeExecute.bind(rateLimiter)\n}\n"],"names":[],"mappings":";;AAwCA,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,EAAA;AACnB;AA2BO,MAAM,iBAA+C;AAAA,EAS1D,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AARV,SAAQ,cAAc;AACtB,SAAQ,kBAAiC,CAAC;AAE1C,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AACvB,SAAQ,gBAAgB;AAMtB,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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A;AAAA;AAAA;AAAA;AAAA,EAMd,iBAAyB;AACvB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,gBAAwB;AACtB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,QAAc;AACZ,SAAK,kBAAkB,CAAC;AACxB,SAAK,gBAAgB;AACrB,SAAK,cAAc;AACnB,SAAK,kBAAkB;AACvB,SAAK,eAAe;AAAA,EAAA;AAExB;AAgCgB,SAAA,eACd,IACA,gBACA;AACA,QAAM,cAAc,IAAI,iBAAiB,IAAI,cAAc;AACpD,SAAA,YAAY,aAAa,KAAK,WAAW;AAClD;;;"}
|
|
@@ -2,7 +2,7 @@ import { AnyAsyncFunction } from './types.cjs';
|
|
|
2
2
|
/**
|
|
3
3
|
* Options for configuring an async rate-limited function
|
|
4
4
|
*/
|
|
5
|
-
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction
|
|
5
|
+
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
|
|
6
6
|
/**
|
|
7
7
|
* Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
8
8
|
* Defaults to true.
|
|
@@ -15,15 +15,19 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
15
15
|
/**
|
|
16
16
|
* Optional error handler for when the rate-limited function throws
|
|
17
17
|
*/
|
|
18
|
-
onError?: (error: unknown) => void;
|
|
18
|
+
onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
19
19
|
/**
|
|
20
20
|
* Optional function to call when the rate-limited function is executed
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
onSettled?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Optional function to call when the rate-limited function is executed
|
|
25
|
+
*/
|
|
26
|
+
onSuccess?: (result: ReturnType<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
23
27
|
/**
|
|
24
28
|
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
25
29
|
*/
|
|
26
|
-
onReject?: (rateLimiter: AsyncRateLimiter<TFn
|
|
30
|
+
onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
27
31
|
/**
|
|
28
32
|
* Time window in milliseconds within which the limit applies
|
|
29
33
|
*/
|
|
@@ -54,22 +58,25 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
54
58
|
* await rateLimiter.maybeExecute('123');
|
|
55
59
|
* ```
|
|
56
60
|
*/
|
|
57
|
-
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
61
|
+
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
58
62
|
private fn;
|
|
59
|
-
private _executionCount;
|
|
60
|
-
private _executionTimes;
|
|
61
63
|
private _options;
|
|
64
|
+
private _errorCount;
|
|
65
|
+
private _executionTimes;
|
|
66
|
+
private _lastResult;
|
|
62
67
|
private _rejectionCount;
|
|
63
|
-
|
|
68
|
+
private _settleCount;
|
|
69
|
+
private _successCount;
|
|
70
|
+
constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>);
|
|
64
71
|
/**
|
|
65
72
|
* Updates the rate limiter options
|
|
66
73
|
* Returns the new options state
|
|
67
74
|
*/
|
|
68
|
-
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn
|
|
75
|
+
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>): void;
|
|
69
76
|
/**
|
|
70
77
|
* Returns the current rate limiter options
|
|
71
78
|
*/
|
|
72
|
-
getOptions(): Required<AsyncRateLimiterOptions<TFn
|
|
79
|
+
getOptions(): Required<AsyncRateLimiterOptions<TFn>>;
|
|
73
80
|
/**
|
|
74
81
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
75
82
|
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
@@ -86,26 +93,34 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
86
93
|
* await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
|
|
87
94
|
* ```
|
|
88
95
|
*/
|
|
89
|
-
maybeExecute(...args:
|
|
96
|
+
maybeExecute(...args: Parameters<TFn>): Promise<ReturnType<TFn> | undefined>;
|
|
90
97
|
private executeFunction;
|
|
91
98
|
private rejectFunction;
|
|
92
99
|
private cleanupOldExecutions;
|
|
100
|
+
/**
|
|
101
|
+
* Returns the number of remaining executions allowed in the current window
|
|
102
|
+
*/
|
|
103
|
+
getRemainingInWindow(): number;
|
|
104
|
+
/**
|
|
105
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
106
|
+
*/
|
|
107
|
+
getMsUntilNextWindow(): number;
|
|
93
108
|
/**
|
|
94
109
|
* Returns the number of times the function has been executed
|
|
95
110
|
*/
|
|
96
|
-
|
|
111
|
+
getSuccessCount(): number;
|
|
97
112
|
/**
|
|
98
|
-
* Returns the number of times the function has been
|
|
113
|
+
* Returns the number of times the function has been settled
|
|
99
114
|
*/
|
|
100
|
-
|
|
115
|
+
getSettleCount(): number;
|
|
101
116
|
/**
|
|
102
|
-
* Returns the number of
|
|
117
|
+
* Returns the number of times the function has errored
|
|
103
118
|
*/
|
|
104
|
-
|
|
119
|
+
getErrorCount(): number;
|
|
105
120
|
/**
|
|
106
|
-
* Returns the number of
|
|
121
|
+
* Returns the number of times the function has been rejected
|
|
107
122
|
*/
|
|
108
|
-
|
|
123
|
+
getRejectionCount(): number;
|
|
109
124
|
/**
|
|
110
125
|
* Resets the rate limiter state
|
|
111
126
|
*/
|
|
@@ -141,4 +156,4 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
141
156
|
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
142
157
|
* ```
|
|
143
158
|
*/
|
|
144
|
-
export declare function asyncRateLimit<TFn extends AnyAsyncFunction
|
|
159
|
+
export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: Omit<AsyncRateLimiterOptions<TFn>, 'enabled'>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
|
|
@@ -2,21 +2,27 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const defaultOptions = {
|
|
4
4
|
enabled: true,
|
|
5
|
+
leading: true,
|
|
5
6
|
onError: () => {
|
|
6
7
|
},
|
|
7
|
-
|
|
8
|
+
onSettled: () => {
|
|
8
9
|
},
|
|
10
|
+
onSuccess: () => {
|
|
11
|
+
},
|
|
12
|
+
trailing: true,
|
|
9
13
|
wait: 0
|
|
10
14
|
};
|
|
11
15
|
class AsyncThrottler {
|
|
12
16
|
constructor(fn, initialOptions) {
|
|
13
17
|
this.fn = fn;
|
|
14
18
|
this._abortController = null;
|
|
15
|
-
this.
|
|
19
|
+
this._errorCount = 0;
|
|
16
20
|
this._isExecuting = false;
|
|
17
|
-
this._isPending = false;
|
|
18
21
|
this._lastExecutionTime = 0;
|
|
19
22
|
this._nextExecutionTime = 0;
|
|
23
|
+
this._settleCount = 0;
|
|
24
|
+
this._successCount = 0;
|
|
25
|
+
this._timeoutId = null;
|
|
20
26
|
this._options = {
|
|
21
27
|
...defaultOptions,
|
|
22
28
|
...initialOptions
|
|
@@ -27,11 +33,10 @@ class AsyncThrottler {
|
|
|
27
33
|
* Returns the new options state
|
|
28
34
|
*/
|
|
29
35
|
setOptions(newOptions) {
|
|
30
|
-
this._options = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
return this._options;
|
|
36
|
+
this._options = { ...this._options, ...newOptions };
|
|
37
|
+
if (!this._options.enabled) {
|
|
38
|
+
this.cancel();
|
|
39
|
+
}
|
|
35
40
|
}
|
|
36
41
|
/**
|
|
37
42
|
* Returns the current options
|
|
@@ -44,72 +49,65 @@ class AsyncThrottler {
|
|
|
44
49
|
* If a call is already in progress, it may be blocked or queued depending on the `wait` option
|
|
45
50
|
*/
|
|
46
51
|
async maybeExecute(...args) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
this.
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
const timeSinceLastExecution = now - this._lastExecutionTime;
|
|
54
|
+
if (this._options.leading && timeSinceLastExecution >= this._options.wait) {
|
|
55
|
+
await this.executeFunction(...args);
|
|
56
|
+
return this._lastResult;
|
|
57
|
+
} else {
|
|
58
|
+
this._lastArgs = args;
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
if (this._timeoutId) {
|
|
61
|
+
clearTimeout(this._timeoutId);
|
|
62
|
+
}
|
|
63
|
+
if (this._options.trailing) {
|
|
64
|
+
const _timeSinceLastExecution = this._lastExecutionTime ? now - this._lastExecutionTime : 0;
|
|
65
|
+
const timeoutDuration = this._options.wait - _timeSinceLastExecution;
|
|
66
|
+
this._timeoutId = setTimeout(async () => {
|
|
67
|
+
if (this._lastArgs !== void 0) {
|
|
68
|
+
await this.executeFunction(...this._lastArgs);
|
|
69
|
+
}
|
|
70
|
+
resolve(this._lastResult);
|
|
71
|
+
}, timeoutDuration);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async executeFunction(...args) {
|
|
77
|
+
if (!this._options.enabled || this._isExecuting) return void 0;
|
|
50
78
|
this._abortController = new AbortController();
|
|
51
|
-
const signal = this._abortController.signal;
|
|
52
79
|
try {
|
|
53
|
-
while (this._isExecuting) {
|
|
54
|
-
await this.delay(this._options.wait, signal);
|
|
55
|
-
}
|
|
56
|
-
while (Date.now() < this._nextExecutionTime) {
|
|
57
|
-
await this.delay(this._nextExecutionTime - Date.now(), signal);
|
|
58
|
-
}
|
|
59
|
-
this._isPending = false;
|
|
60
80
|
this._isExecuting = true;
|
|
61
|
-
await this.
|
|
81
|
+
this._lastResult = await this.fn(...args);
|
|
82
|
+
this._successCount++;
|
|
83
|
+
this._options.onSuccess(this._lastResult, this);
|
|
62
84
|
} catch (error) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
this._options.onError(error);
|
|
68
|
-
} catch {
|
|
69
|
-
}
|
|
85
|
+
this._errorCount++;
|
|
86
|
+
this._options.onError(error, this);
|
|
70
87
|
} finally {
|
|
71
|
-
this._lastExecutionTime = Date.now();
|
|
72
|
-
this._nextExecutionTime = this._lastExecutionTime + this._options.wait;
|
|
73
88
|
this._isExecuting = false;
|
|
89
|
+
this._settleCount++;
|
|
74
90
|
this._abortController = null;
|
|
91
|
+
this._lastExecutionTime = Date.now();
|
|
92
|
+
this._nextExecutionTime = this._lastExecutionTime + this._options.wait;
|
|
93
|
+
this._options.onSettled(this);
|
|
75
94
|
}
|
|
76
|
-
|
|
77
|
-
delay(ms, signal) {
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
const timeout = setTimeout(resolve, ms);
|
|
80
|
-
signal.addEventListener(
|
|
81
|
-
"abort",
|
|
82
|
-
() => {
|
|
83
|
-
clearTimeout(timeout);
|
|
84
|
-
reject(new Error("AbortError"));
|
|
85
|
-
},
|
|
86
|
-
{ once: true }
|
|
87
|
-
);
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async executeFunction(...args) {
|
|
91
|
-
if (!this._options.enabled) return;
|
|
92
|
-
this._executionCount++;
|
|
93
|
-
await this.fn(...args);
|
|
94
|
-
this._options.onExecute(this);
|
|
95
|
+
return this._lastResult;
|
|
95
96
|
}
|
|
96
97
|
/**
|
|
97
|
-
* Cancels any pending execution
|
|
98
|
+
* Cancels any pending execution or aborts any execution in progress
|
|
98
99
|
*/
|
|
99
100
|
cancel() {
|
|
101
|
+
if (this._timeoutId) {
|
|
102
|
+
clearTimeout(this._timeoutId);
|
|
103
|
+
this._timeoutId = null;
|
|
104
|
+
}
|
|
100
105
|
if (this._abortController) {
|
|
101
106
|
this._abortController.abort();
|
|
102
107
|
this._abortController = null;
|
|
103
108
|
}
|
|
104
|
-
this._isPending = false;
|
|
105
109
|
this._lastArgs = void 0;
|
|
106
110
|
}
|
|
107
|
-
/**
|
|
108
|
-
* Returns the number of times the function has been executed
|
|
109
|
-
*/
|
|
110
|
-
getExecutionCount() {
|
|
111
|
-
return this._executionCount;
|
|
112
|
-
}
|
|
113
111
|
/**
|
|
114
112
|
* Returns the last execution time
|
|
115
113
|
*/
|
|
@@ -122,11 +120,41 @@ class AsyncThrottler {
|
|
|
122
120
|
getNextExecutionTime() {
|
|
123
121
|
return this._nextExecutionTime;
|
|
124
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Returns the last result of the debounced function
|
|
125
|
+
*/
|
|
126
|
+
getLastResult() {
|
|
127
|
+
return this._lastResult;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns the number of times the function has been executed successfully
|
|
131
|
+
*/
|
|
132
|
+
getSuccessCount() {
|
|
133
|
+
return this._successCount;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns the number of times the function has settled (completed or errored)
|
|
137
|
+
*/
|
|
138
|
+
getSettleCount() {
|
|
139
|
+
return this._settleCount;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns the number of times the function has errored
|
|
143
|
+
*/
|
|
144
|
+
getErrorCount() {
|
|
145
|
+
return this._errorCount;
|
|
146
|
+
}
|
|
125
147
|
/**
|
|
126
148
|
* Returns the current pending state
|
|
127
149
|
*/
|
|
128
150
|
getIsPending() {
|
|
129
|
-
return this.
|
|
151
|
+
return this._options.enabled && !!this._timeoutId;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Returns the current executing state
|
|
155
|
+
*/
|
|
156
|
+
getIsExecuting() {
|
|
157
|
+
return this._isExecuting;
|
|
130
158
|
}
|
|
131
159
|
}
|
|
132
160
|
function asyncThrottle(fn, initialOptions) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-throttler.cjs","sources":["../../src/async-throttler.ts"],"sourcesContent":["import type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async throttled function\n */\nexport interface AsyncThrottlerOptions<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\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 * Optional error handler for when the throttled function throws\n */\n onError?: (error: unknown) => void\n /**\n * Optional function to call when the throttled function is executed\n */\n onExecute?: (throttler: AsyncThrottler<TFn, TArgs>) => void\n /**\n * Time window in milliseconds during which the function can only be executed once\n * Defaults to 0ms\n */\n wait: number\n}\n\nconst defaultOptions: Required<AsyncThrottlerOptions<any, any>> = {\n enabled: true,\n onError: () => {},\n onExecute: () => {},\n wait: 0,\n}\n\n/**\n * A class that creates an async throttled function.\n *\n * Throttling limits how often a function can be executed, allowing only one execution within a specified time window.\n * Unlike debouncing which resets the delay timer on each call, throttling ensures the function executes at a\n * regular interval regardless of how often it's called.\n *\n * This is useful for rate-limiting API calls, handling scroll/resize events, or any scenario where you want to\n * ensure a maximum execution frequency.\n *\n * @example\n * ```ts\n * const throttler = new AsyncThrottler(async (value: string) => {\n * await saveToAPI(value);\n * }, { wait: 1000 });\n *\n * // Will only execute once per second no matter how often called\n * inputElement.addEventListener('input', () => {\n * throttler.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncThrottler<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n private _options: Required<AsyncThrottlerOptions<TFn, TArgs>>\n private _abortController: AbortController | null = null\n private _executionCount = 0\n private _isExecuting = false\n private _isPending = false\n private _lastArgs: TArgs | undefined\n private _lastExecutionTime = 0\n private _nextExecutionTime = 0\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncThrottlerOptions<TFn, TArgs>,\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(\n newOptions: Partial<AsyncThrottlerOptions<TFn, TArgs>>,\n ): Required<AsyncThrottlerOptions<TFn, TArgs>> {\n this._options = {\n ...this._options,\n ...newOptions,\n }\n return this._options\n }\n\n /**\n * Returns the current options\n */\n getOptions(): Required<AsyncThrottlerOptions<TFn, TArgs>> {\n return this._options\n }\n\n /**\n * Attempts to execute the throttled function\n * If a call is already in progress, it may be blocked or queued depending on the `wait` option\n */\n async maybeExecute(...args: TArgs): Promise<void> {\n this._lastArgs = args\n if (this._isPending) return\n this._isPending = true\n\n this._abortController = new AbortController()\n const signal = this._abortController.signal\n\n try {\n while (this._isExecuting) {\n await this.delay(this._options.wait, signal)\n }\n\n while (Date.now() < this._nextExecutionTime) {\n await this.delay(this._nextExecutionTime - Date.now(), signal)\n }\n\n this._isPending = false\n this._isExecuting = true\n\n await this.executeFunction(...this._lastArgs)\n } catch (error) {\n if (error instanceof Error && error.name === 'AbortError') {\n return // Silent return on cancellation\n }\n try {\n this._options.onError(error)\n } catch {\n // Ignore errors from error handler\n }\n } finally {\n this._lastExecutionTime = Date.now()\n this._nextExecutionTime = this._lastExecutionTime + this._options.wait\n this._isExecuting = false\n this._abortController = null\n }\n }\n\n private delay(ms: number, signal: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const timeout = setTimeout(resolve, ms)\n signal.addEventListener(\n 'abort',\n () => {\n clearTimeout(timeout)\n reject(new Error('AbortError'))\n },\n { once: true },\n )\n })\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this._options.enabled) return\n this._executionCount++\n await this.fn(...args)\n this._options.onExecute(this)\n }\n\n /**\n * Cancels any pending execution\n */\n cancel(): void {\n if (this._abortController) {\n this._abortController.abort()\n this._abortController = null\n }\n this._isPending = false\n this._lastArgs = undefined\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 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._nextExecutionTime\n }\n\n /**\n * Returns the current pending state\n */\n getIsPending(): boolean {\n return this._isPending\n }\n}\n\n/**\n * Creates an async throttled function that limits how often the function can execute.\n * The throttled function will execute at most once per wait period, even if called multiple times.\n * If called while executing, it will wait until execution completes before scheduling the next call.\n *\n * @example\n * ```ts\n * const throttled = asyncThrottle(async () => {\n * await someAsyncOperation();\n * }, { wait: 1000 });\n *\n * // This will execute at most once per second\n * await throttled();\n * await throttled(); // Waits 1 second before executing\n * ```\n */\nexport function asyncThrottle<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n>(fn: TFn, initialOptions: Omit<AsyncThrottlerOptions<TFn, TArgs>, 'enabled'>) {\n const asyncThrottler = new AsyncThrottler(fn, initialOptions)\n return asyncThrottler.maybeExecute.bind(asyncThrottler)\n}\n"],"names":[],"mappings":";;AA6BA,MAAM,iBAA4D;AAAA,EAChE,SAAS;AAAA,EACT,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,MAAM;AACR;AAwBO,MAAM,eAGX;AAAA,EAUA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,mBAA2C;AACnD,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AACvB,SAAQ,aAAa;AAErB,SAAQ,qBAAqB;AAC7B,SAAQ,qBAAqB;AAM3B,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YAC6C;AAC7C,SAAK,WAAW;AAAA,MACd,GAAG,KAAK;AAAA,MACR,GAAG;AAAA,IACL;AACA,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,aAA0D;AACxD,WAAO,KAAK;AAAA,EAAA;AA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AA;AAAA;AAAA;AAAA,EAM9B,SAAe;AACb,QAAI,KAAK,kBAAkB;AACzB,WAAK,iBAAiB,MAAM;AAC5B,WAAK,mBAAmB;AAAA,IAAA;AAE1B,SAAK,aAAa;AAClB,SAAK,YAAY;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMnB,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,uBAA+B;AAC7B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,uBAA+B;AAC7B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WAAO,KAAK;AAAA,EAAA;AAEhB;AAkBgB,SAAA,cAGd,IAAS,gBAAoE;AAC7E,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AACrD,SAAA,eAAe,aAAa,KAAK,cAAc;AACxD;;;"}
|
|
1
|
+
{"version":3,"file":"async-throttler.cjs","sources":["../../src/async-throttler.ts"],"sourcesContent":["import type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async throttled function\n */\nexport interface AsyncThrottlerOptions<TFn extends AnyAsyncFunction> {\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 the function immediately when called\n * Defaults to true\n */\n leading?: boolean\n /**\n * Optional error handler for when the throttled function throws\n */\n onError?: (error: unknown, asyncThrottler: AsyncThrottler<TFn>) => void\n /**\n * Optional function to call when the throttled function is executed\n */\n onSettled?: (asyncThrottler: AsyncThrottler<TFn>) => void\n /**\n * Optional function to call when the throttled function is executed\n */\n onSuccess?: (\n result: ReturnType<TFn>,\n asyncThrottler: AsyncThrottler<TFn>,\n ) => void\n /**\n * Whether to execute the function on the trailing edge of the wait period\n * Defaults to true\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once\n * Defaults to 0ms\n */\n wait: number\n}\n\nconst defaultOptions: Required<AsyncThrottlerOptions<any>> = {\n enabled: true,\n leading: true,\n onError: () => {},\n onSettled: () => {},\n onSuccess: () => {},\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates an async throttled function.\n *\n * Throttling limits how often a function can be executed, allowing only one execution within a specified time window.\n * Unlike debouncing which resets the delay timer on each call, throttling ensures the function executes at a\n * regular interval regardless of how often it's called.\n *\n * This is useful for rate-limiting API calls, handling scroll/resize events, or any scenario where you want to\n * ensure a maximum execution frequency.\n *\n * @example\n * ```ts\n * const throttler = new AsyncThrottler(async (value: string) => {\n * await saveToAPI(value);\n * }, { wait: 1000 });\n *\n * // Will only execute once per second no matter how often called\n * inputElement.addEventListener('input', () => {\n * throttler.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncThrottler<TFn extends AnyAsyncFunction> {\n private _options: Required<AsyncThrottlerOptions<TFn>>\n private _abortController: AbortController | null = null\n private _errorCount = 0\n private _isExecuting = false\n private _lastArgs: Parameters<TFn> | undefined\n private _lastExecutionTime = 0\n private _lastResult: ReturnType<TFn> | undefined\n private _nextExecutionTime = 0\n private _settleCount = 0\n private _successCount = 0\n private _timeoutId: NodeJS.Timeout | null = null\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncThrottlerOptions<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<AsyncThrottlerOptions<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 options\n */\n getOptions(): Required<AsyncThrottlerOptions<TFn>> {\n return this._options\n }\n\n /**\n * Attempts to execute the throttled function\n * If a call is already in progress, it may be blocked or queued depending on the `wait` option\n */\n async maybeExecute(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\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 await this.executeFunction(...args)\n return this._lastResult\n } else {\n // Store the most recent arguments for potential trailing execution\n this._lastArgs = args\n\n return new Promise((resolve) => {\n // Clear any existing timeout to ensure we use the latest arguments\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n }\n\n // Set up trailing execution if enabled\n if (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(async () => {\n if (this._lastArgs !== undefined) {\n await this.executeFunction(...this._lastArgs)\n }\n resolve(this._lastResult)\n }, timeoutDuration)\n }\n })\n }\n }\n\n private async executeFunction(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n if (!this._options.enabled || this._isExecuting) return undefined\n this._abortController = new AbortController()\n try {\n this._isExecuting = true\n this._lastResult = await this.fn(...args) // EXECUTE!\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._abortController = null\n this._lastExecutionTime = Date.now()\n this._nextExecutionTime = this._lastExecutionTime + this._options.wait\n this._options.onSettled(this)\n }\n return this._lastResult\n }\n\n /**\n * Cancels any pending execution or aborts any execution in progress\n */\n cancel(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n this._timeoutId = null\n }\n if (this._abortController) {\n this._abortController.abort()\n this._abortController = null\n }\n this._lastArgs = undefined\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._nextExecutionTime\n }\n\n /**\n * Returns the last result of the debounced function\n */\n getLastResult(): ReturnType<TFn> | undefined {\n return this._lastResult\n }\n\n /**\n * Returns the number of times the function has been executed successfully\n */\n getSuccessCount(): number {\n return this._successCount\n }\n\n /**\n * Returns the number of times the function has settled (completed or errored)\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 current pending state\n */\n getIsPending(): boolean {\n return this._options.enabled && !!this._timeoutId\n }\n\n /**\n * Returns the current executing state\n */\n getIsExecuting(): boolean {\n return this._isExecuting\n }\n}\n\n/**\n * Creates an async throttled function that limits how often the function can execute.\n * The throttled function will execute at most once per wait period, even if called multiple times.\n * If called while executing, it will wait until execution completes before scheduling the next call.\n *\n * @example\n * ```ts\n * const throttled = asyncThrottle(async () => {\n * await someAsyncOperation();\n * }, { wait: 1000 });\n *\n * // This will execute at most once per second\n * await throttled();\n * await throttled(); // Waits 1 second before executing\n * ```\n */\nexport function asyncThrottle<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: Omit<AsyncThrottlerOptions<TFn>, 'enabled'>,\n) {\n const asyncThrottler = new AsyncThrottler(fn, initialOptions)\n return asyncThrottler.maybeExecute.bind(asyncThrottler)\n}\n"],"names":[],"mappings":";;AA2CA,MAAM,iBAAuD;AAAA,EAC3D,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,UAAU;AAAA,EACV,MAAM;AACR;AAwBO,MAAM,eAA6C;AAAA,EAaxD,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AAZV,SAAQ,mBAA2C;AACnD,SAAQ,cAAc;AACtB,SAAQ,eAAe;AAEvB,SAAQ,qBAAqB;AAE7B,SAAQ,qBAAqB;AAC7B,SAAQ,eAAe;AACvB,SAAQ,gBAAgB;AACxB,SAAQ,aAAoC;AAM1C,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAA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