@tanstack/pacer-lite 0.1.0 → 0.2.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/README.md +1 -1
- package/dist/index.cjs +16 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/{esm/index.js → index.js} +2 -13
- package/dist/lite-batcher.cjs +175 -0
- package/dist/lite-batcher.cjs.map +1 -0
- package/dist/{cjs/lite-batcher.d.cts → lite-batcher.d.cts} +89 -85
- package/dist/{esm/lite-batcher.d.ts → lite-batcher.d.ts} +89 -85
- package/dist/lite-batcher.js +173 -0
- package/dist/lite-batcher.js.map +1 -0
- package/dist/lite-debouncer.cjs +125 -0
- package/dist/lite-debouncer.cjs.map +1 -0
- package/dist/{cjs/lite-debouncer.d.cts → lite-debouncer.d.cts} +53 -47
- package/dist/{esm/lite-debouncer.d.ts → lite-debouncer.d.ts} +53 -47
- package/dist/lite-debouncer.js +123 -0
- package/dist/lite-debouncer.js.map +1 -0
- package/dist/lite-queuer.cjs +208 -0
- package/dist/lite-queuer.cjs.map +1 -0
- package/dist/lite-queuer.d.cts +243 -0
- package/dist/lite-queuer.d.ts +243 -0
- package/dist/lite-queuer.js +206 -0
- package/dist/lite-queuer.js.map +1 -0
- package/dist/lite-rate-limiter.cjs +149 -0
- package/dist/lite-rate-limiter.cjs.map +1 -0
- package/dist/{cjs/lite-rate-limiter.d.cts → lite-rate-limiter.d.cts} +73 -67
- package/dist/{esm/lite-rate-limiter.d.ts → lite-rate-limiter.d.ts} +73 -67
- package/dist/lite-rate-limiter.js +147 -0
- package/dist/lite-rate-limiter.js.map +1 -0
- package/dist/lite-throttler.cjs +127 -0
- package/dist/lite-throttler.cjs.map +1 -0
- package/dist/{cjs/lite-throttler.d.cts → lite-throttler.d.cts} +60 -54
- package/dist/{esm/lite-throttler.d.ts → lite-throttler.d.ts} +60 -54
- package/dist/lite-throttler.js +125 -0
- package/dist/lite-throttler.js.map +1 -0
- package/package.json +22 -59
- package/dist/cjs/index.cjs +0 -18
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -5
- package/dist/cjs/lite-batcher.cjs +0 -92
- package/dist/cjs/lite-batcher.cjs.map +0 -1
- package/dist/cjs/lite-debouncer.cjs +0 -59
- package/dist/cjs/lite-debouncer.cjs.map +0 -1
- package/dist/cjs/lite-queuer.cjs +0 -171
- package/dist/cjs/lite-queuer.cjs.map +0 -1
- package/dist/cjs/lite-queuer.d.cts +0 -239
- package/dist/cjs/lite-rate-limiter.cjs +0 -95
- package/dist/cjs/lite-rate-limiter.cjs.map +0 -1
- package/dist/cjs/lite-throttler.cjs +0 -63
- package/dist/cjs/lite-throttler.cjs.map +0 -1
- package/dist/esm/index.d.ts +0 -5
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/lite-batcher.js +0 -92
- package/dist/esm/lite-batcher.js.map +0 -1
- package/dist/esm/lite-debouncer.js +0 -59
- package/dist/esm/lite-debouncer.js.map +0 -1
- package/dist/esm/lite-queuer.d.ts +0 -239
- package/dist/esm/lite-queuer.js +0 -171
- package/dist/esm/lite-queuer.js.map +0 -1
- package/dist/esm/lite-rate-limiter.js +0 -95
- package/dist/esm/lite-rate-limiter.js.map +0 -1
- package/dist/esm/lite-throttler.js +0 -63
- package/dist/esm/lite-throttler.js.map +0 -1
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
import { AnyFunction } from
|
|
1
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/lite-rate-limiter.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Options for configuring a lite rate-limited function
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
interface LiteRateLimiterOptions<TFn extends AnyFunction = AnyFunction> {
|
|
9
|
+
/**
|
|
10
|
+
* Maximum number of executions allowed within the time window.
|
|
11
|
+
*/
|
|
12
|
+
limit: number;
|
|
13
|
+
/**
|
|
14
|
+
* Callback function that is called after the function is executed
|
|
15
|
+
*/
|
|
16
|
+
onExecute?: (args: Parameters<TFn>, rateLimiter: LiteRateLimiter<TFn>) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
19
|
+
*/
|
|
20
|
+
onReject?: (rateLimiter: LiteRateLimiter<TFn>) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Time window in milliseconds within which the limit applies.
|
|
23
|
+
*/
|
|
24
|
+
window: number;
|
|
25
|
+
/**
|
|
26
|
+
* Type of window to use for rate limiting
|
|
27
|
+
* - 'fixed': Uses a fixed window that resets after the window period
|
|
28
|
+
* - 'sliding': Uses a sliding window that allows executions as old ones expire
|
|
29
|
+
* Defaults to 'fixed'
|
|
30
|
+
*/
|
|
31
|
+
windowType?: 'fixed' | 'sliding';
|
|
29
32
|
}
|
|
30
33
|
/**
|
|
31
34
|
* A lightweight class that creates a rate-limited function.
|
|
@@ -65,47 +68,47 @@ export interface LiteRateLimiterOptions<TFn extends AnyFunction = AnyFunction> {
|
|
|
65
68
|
* }
|
|
66
69
|
* ```
|
|
67
70
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
71
|
+
declare class LiteRateLimiter<TFn extends AnyFunction> {
|
|
72
|
+
fn: TFn;
|
|
73
|
+
options: LiteRateLimiterOptions<TFn>;
|
|
74
|
+
private executionTimes;
|
|
75
|
+
private timeoutIds;
|
|
76
|
+
constructor(fn: TFn, options: LiteRateLimiterOptions<TFn>);
|
|
77
|
+
/**
|
|
78
|
+
* Attempts to execute the rate-limited function if within the configured limits.
|
|
79
|
+
* Returns true if executed, false if rejected due to rate limiting.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const rateLimiter = new LiteRateLimiter(fn, { limit: 5, window: 1000 });
|
|
84
|
+
*
|
|
85
|
+
* // First 5 calls return true
|
|
86
|
+
* rateLimiter.maybeExecute('arg1', 'arg2'); // true
|
|
87
|
+
*
|
|
88
|
+
* // Additional calls within the window return false
|
|
89
|
+
* rateLimiter.maybeExecute('arg1', 'arg2'); // false
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
maybeExecute: (...args: Parameters<TFn>) => boolean;
|
|
93
|
+
private execute;
|
|
94
|
+
private getExecutionTimesInWindow;
|
|
95
|
+
private setCleanupTimeout;
|
|
96
|
+
private clearTimeout;
|
|
97
|
+
private clearTimeouts;
|
|
98
|
+
private cleanupOldExecutions;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the number of remaining executions allowed in the current window.
|
|
101
|
+
*/
|
|
102
|
+
getRemainingInWindow: () => number;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the number of milliseconds until the next execution will be possible.
|
|
105
|
+
* Returns 0 if executions are currently allowed.
|
|
106
|
+
*/
|
|
107
|
+
getMsUntilNextWindow: () => number;
|
|
108
|
+
/**
|
|
109
|
+
* Resets the rate limiter state, clearing all execution history.
|
|
110
|
+
*/
|
|
111
|
+
reset: () => void;
|
|
109
112
|
}
|
|
110
113
|
/**
|
|
111
114
|
* Creates a lightweight rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
@@ -140,4 +143,7 @@ export declare class LiteRateLimiter<TFn extends AnyFunction> {
|
|
|
140
143
|
* });
|
|
141
144
|
* ```
|
|
142
145
|
*/
|
|
143
|
-
|
|
146
|
+
declare function liteRateLimit<TFn extends AnyFunction>(fn: TFn, options: LiteRateLimiterOptions<TFn>): (...args: Parameters<TFn>) => boolean;
|
|
147
|
+
//#endregion
|
|
148
|
+
export { LiteRateLimiter, LiteRateLimiterOptions, liteRateLimit };
|
|
149
|
+
//# sourceMappingURL=lite-rate-limiter.d.cts.map
|
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
import { AnyFunction } from
|
|
1
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/lite-rate-limiter.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Options for configuring a lite rate-limited function
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
interface LiteRateLimiterOptions<TFn extends AnyFunction = AnyFunction> {
|
|
9
|
+
/**
|
|
10
|
+
* Maximum number of executions allowed within the time window.
|
|
11
|
+
*/
|
|
12
|
+
limit: number;
|
|
13
|
+
/**
|
|
14
|
+
* Callback function that is called after the function is executed
|
|
15
|
+
*/
|
|
16
|
+
onExecute?: (args: Parameters<TFn>, rateLimiter: LiteRateLimiter<TFn>) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
19
|
+
*/
|
|
20
|
+
onReject?: (rateLimiter: LiteRateLimiter<TFn>) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Time window in milliseconds within which the limit applies.
|
|
23
|
+
*/
|
|
24
|
+
window: number;
|
|
25
|
+
/**
|
|
26
|
+
* Type of window to use for rate limiting
|
|
27
|
+
* - 'fixed': Uses a fixed window that resets after the window period
|
|
28
|
+
* - 'sliding': Uses a sliding window that allows executions as old ones expire
|
|
29
|
+
* Defaults to 'fixed'
|
|
30
|
+
*/
|
|
31
|
+
windowType?: 'fixed' | 'sliding';
|
|
29
32
|
}
|
|
30
33
|
/**
|
|
31
34
|
* A lightweight class that creates a rate-limited function.
|
|
@@ -65,47 +68,47 @@ export interface LiteRateLimiterOptions<TFn extends AnyFunction = AnyFunction> {
|
|
|
65
68
|
* }
|
|
66
69
|
* ```
|
|
67
70
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
71
|
+
declare class LiteRateLimiter<TFn extends AnyFunction> {
|
|
72
|
+
fn: TFn;
|
|
73
|
+
options: LiteRateLimiterOptions<TFn>;
|
|
74
|
+
private executionTimes;
|
|
75
|
+
private timeoutIds;
|
|
76
|
+
constructor(fn: TFn, options: LiteRateLimiterOptions<TFn>);
|
|
77
|
+
/**
|
|
78
|
+
* Attempts to execute the rate-limited function if within the configured limits.
|
|
79
|
+
* Returns true if executed, false if rejected due to rate limiting.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const rateLimiter = new LiteRateLimiter(fn, { limit: 5, window: 1000 });
|
|
84
|
+
*
|
|
85
|
+
* // First 5 calls return true
|
|
86
|
+
* rateLimiter.maybeExecute('arg1', 'arg2'); // true
|
|
87
|
+
*
|
|
88
|
+
* // Additional calls within the window return false
|
|
89
|
+
* rateLimiter.maybeExecute('arg1', 'arg2'); // false
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
maybeExecute: (...args: Parameters<TFn>) => boolean;
|
|
93
|
+
private execute;
|
|
94
|
+
private getExecutionTimesInWindow;
|
|
95
|
+
private setCleanupTimeout;
|
|
96
|
+
private clearTimeout;
|
|
97
|
+
private clearTimeouts;
|
|
98
|
+
private cleanupOldExecutions;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the number of remaining executions allowed in the current window.
|
|
101
|
+
*/
|
|
102
|
+
getRemainingInWindow: () => number;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the number of milliseconds until the next execution will be possible.
|
|
105
|
+
* Returns 0 if executions are currently allowed.
|
|
106
|
+
*/
|
|
107
|
+
getMsUntilNextWindow: () => number;
|
|
108
|
+
/**
|
|
109
|
+
* Resets the rate limiter state, clearing all execution history.
|
|
110
|
+
*/
|
|
111
|
+
reset: () => void;
|
|
109
112
|
}
|
|
110
113
|
/**
|
|
111
114
|
* Creates a lightweight rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
@@ -140,4 +143,7 @@ export declare class LiteRateLimiter<TFn extends AnyFunction> {
|
|
|
140
143
|
* });
|
|
141
144
|
* ```
|
|
142
145
|
*/
|
|
143
|
-
|
|
146
|
+
declare function liteRateLimit<TFn extends AnyFunction>(fn: TFn, options: LiteRateLimiterOptions<TFn>): (...args: Parameters<TFn>) => boolean;
|
|
147
|
+
//#endregion
|
|
148
|
+
export { LiteRateLimiter, LiteRateLimiterOptions, liteRateLimit };
|
|
149
|
+
//# sourceMappingURL=lite-rate-limiter.d.ts.map
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
//#region src/lite-rate-limiter.ts
|
|
2
|
+
/**
|
|
3
|
+
* A lightweight class that creates a rate-limited function.
|
|
4
|
+
*
|
|
5
|
+
* This is an alternative to the RateLimiter in the core @tanstack/pacer package, but is more
|
|
6
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core RateLimiter,
|
|
7
|
+
* this version does not use TanStack Store for state management, has no devtools integration,
|
|
8
|
+
* and provides only essential rate limiting functionality.
|
|
9
|
+
*
|
|
10
|
+
* Rate limiting allows a function to execute up to a limit within a time window,
|
|
11
|
+
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
12
|
+
* all executions happen immediately, followed by a complete block.
|
|
13
|
+
*
|
|
14
|
+
* The rate limiter supports two types of windows:
|
|
15
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
16
|
+
* towards the limit, and the window resets completely after the period.
|
|
17
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
18
|
+
* consistent rate of execution over time.
|
|
19
|
+
*
|
|
20
|
+
* Features:
|
|
21
|
+
* - Zero dependencies - no external libraries required
|
|
22
|
+
* - Minimal API surface - only essential methods (maybeExecute, getRemainingInWindow, getMsUntilNextWindow, reset)
|
|
23
|
+
* - Simple state management - uses basic private properties instead of reactive stores
|
|
24
|
+
* - Lightweight - designed for use in npm packages where bundle size matters
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const rateLimiter = new LiteRateLimiter((id: string) => {
|
|
29
|
+
* api.getData(id);
|
|
30
|
+
* }, { limit: 5, window: 1000 });
|
|
31
|
+
*
|
|
32
|
+
* // First 5 calls will execute, then block until window resets
|
|
33
|
+
* if (rateLimiter.maybeExecute('123')) {
|
|
34
|
+
* console.log('API call made');
|
|
35
|
+
* } else {
|
|
36
|
+
* console.log('Rate limited - try again in', rateLimiter.getMsUntilNextWindow(), 'ms');
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
var LiteRateLimiter = class {
|
|
41
|
+
constructor(fn, options) {
|
|
42
|
+
this.fn = fn;
|
|
43
|
+
this.options = options;
|
|
44
|
+
this.executionTimes = [];
|
|
45
|
+
this.timeoutIds = /* @__PURE__ */ new Set();
|
|
46
|
+
this.maybeExecute = (...args) => {
|
|
47
|
+
this.cleanupOldExecutions();
|
|
48
|
+
if (this.getExecutionTimesInWindow().length < this.options.limit) {
|
|
49
|
+
this.execute(...args);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
this.options.onReject?.(this);
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
this.execute = (...args) => {
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
this.fn(...args);
|
|
58
|
+
this.options.onExecute?.(args, this);
|
|
59
|
+
this.executionTimes.push(now);
|
|
60
|
+
this.setCleanupTimeout(now);
|
|
61
|
+
};
|
|
62
|
+
this.getExecutionTimesInWindow = () => {
|
|
63
|
+
if (this.options.windowType === "sliding") return this.executionTimes.filter((time) => time > Date.now() - this.options.window);
|
|
64
|
+
else {
|
|
65
|
+
if (this.executionTimes.length === 0) return [];
|
|
66
|
+
const windowStart = Math.min(...this.executionTimes);
|
|
67
|
+
const windowEnd = windowStart + this.options.window;
|
|
68
|
+
if (Date.now() > windowEnd) return [];
|
|
69
|
+
return this.executionTimes.filter((time) => time >= windowStart && time <= windowEnd);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
this.setCleanupTimeout = (executionTime) => {
|
|
73
|
+
if (this.options.windowType === "sliding" || this.timeoutIds.size === 0) {
|
|
74
|
+
const timeUntilExpiration = executionTime - Date.now() + this.options.window + 1;
|
|
75
|
+
const timeoutId = setTimeout(() => {
|
|
76
|
+
this.cleanupOldExecutions();
|
|
77
|
+
this.clearTimeout(timeoutId);
|
|
78
|
+
}, timeUntilExpiration);
|
|
79
|
+
this.timeoutIds.add(timeoutId);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
this.clearTimeout = (timeoutId) => {
|
|
83
|
+
clearTimeout(timeoutId);
|
|
84
|
+
this.timeoutIds.delete(timeoutId);
|
|
85
|
+
};
|
|
86
|
+
this.clearTimeouts = () => {
|
|
87
|
+
this.timeoutIds.forEach((timeoutId) => clearTimeout(timeoutId));
|
|
88
|
+
this.timeoutIds.clear();
|
|
89
|
+
};
|
|
90
|
+
this.cleanupOldExecutions = () => {
|
|
91
|
+
this.executionTimes = this.getExecutionTimesInWindow();
|
|
92
|
+
};
|
|
93
|
+
this.getRemainingInWindow = () => {
|
|
94
|
+
const relevantExecutionTimes = this.getExecutionTimesInWindow();
|
|
95
|
+
return Math.max(0, this.options.limit - relevantExecutionTimes.length);
|
|
96
|
+
};
|
|
97
|
+
this.getMsUntilNextWindow = () => {
|
|
98
|
+
if (this.getRemainingInWindow() > 0) return 0;
|
|
99
|
+
return (this.executionTimes[0] ?? Infinity) + this.options.window - Date.now();
|
|
100
|
+
};
|
|
101
|
+
this.reset = () => {
|
|
102
|
+
this.executionTimes = [];
|
|
103
|
+
this.clearTimeouts();
|
|
104
|
+
};
|
|
105
|
+
if (this.options.windowType === void 0) this.options.windowType = "fixed";
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Creates a lightweight rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
110
|
+
*
|
|
111
|
+
* This is an alternative to the rateLimit function in the core @tanstack/pacer package, but is more
|
|
112
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core version,
|
|
113
|
+
* this function creates a rate limiter with no external dependencies, devtools integration, or reactive state.
|
|
114
|
+
*
|
|
115
|
+
* Rate limiting allows all executions until the limit is reached, then blocks all subsequent calls until the window resets.
|
|
116
|
+
* This differs from throttling (which ensures even spacing) and debouncing (which waits for pauses).
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const rateLimitedApi = liteRateLimit(makeApiCall, {
|
|
121
|
+
* limit: 5,
|
|
122
|
+
* window: 60000, // 1 minute
|
|
123
|
+
* windowType: 'sliding'
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // First 5 calls execute immediately
|
|
127
|
+
* // Additional calls are rejected until window allows
|
|
128
|
+
* rateLimitedApi();
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* // Fixed window - all 10 calls happen in first second, then 10 second wait
|
|
134
|
+
* const rateLimitedFixed = liteRateLimit(logEvent, {
|
|
135
|
+
* limit: 10,
|
|
136
|
+
* window: 10000,
|
|
137
|
+
* windowType: 'fixed'
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
function liteRateLimit(fn, options) {
|
|
142
|
+
return new LiteRateLimiter(fn, options).maybeExecute;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { LiteRateLimiter, liteRateLimit };
|
|
147
|
+
//# sourceMappingURL=lite-rate-limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-rate-limiter.js","names":["fn: TFn","options: LiteRateLimiterOptions<TFn>"],"sources":["../src/lite-rate-limiter.ts"],"sourcesContent":["import type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * Options for configuring a lite rate-limited function\n */\nexport interface LiteRateLimiterOptions<TFn extends AnyFunction = AnyFunction> {\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?: (args: Parameters<TFn>, rateLimiter: LiteRateLimiter<TFn>) => void\n /**\n * Optional callback function that is called when an execution is rejected due to rate limiting\n */\n onReject?: (rateLimiter: LiteRateLimiter<TFn>) => void\n /**\n * Time window in milliseconds within which the limit applies.\n */\n window: number\n /**\n * Type of window to use for rate limiting\n * - 'fixed': Uses a fixed window that resets after the window period\n * - 'sliding': Uses a sliding window that allows executions as old ones expire\n * Defaults to 'fixed'\n */\n windowType?: 'fixed' | 'sliding'\n}\n\n/**\n * A lightweight class that creates a rate-limited function.\n *\n * This is an alternative to the RateLimiter in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core RateLimiter,\n * this version does not use TanStack Store for state management, has no devtools integration,\n * and provides only essential rate limiting functionality.\n *\n * Rate limiting allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Features:\n * - Zero dependencies - no external libraries required\n * - Minimal API surface - only essential methods (maybeExecute, getRemainingInWindow, getMsUntilNextWindow, reset)\n * - Simple state management - uses basic private properties instead of reactive stores\n * - Lightweight - designed for use in npm packages where bundle size matters\n *\n * @example\n * ```ts\n * const rateLimiter = new LiteRateLimiter((id: string) => {\n * api.getData(id);\n * }, { limit: 5, window: 1000 });\n *\n * // First 5 calls will execute, then block until window resets\n * if (rateLimiter.maybeExecute('123')) {\n * console.log('API call made');\n * } else {\n * console.log('Rate limited - try again in', rateLimiter.getMsUntilNextWindow(), 'ms');\n * }\n * ```\n */\nexport class LiteRateLimiter<TFn extends AnyFunction> {\n private executionTimes: Array<number> = []\n private timeoutIds: Set<NodeJS.Timeout> = new Set()\n\n constructor(\n public fn: TFn,\n public options: LiteRateLimiterOptions<TFn>,\n ) {\n // Default windowType to 'fixed' if not specified\n if (this.options.windowType === undefined) {\n this.options.windowType = 'fixed'\n }\n }\n\n /**\n * Attempts to execute the rate-limited function if within the configured limits.\n * Returns true if executed, false if rejected due to rate limiting.\n *\n * @example\n * ```ts\n * const rateLimiter = new LiteRateLimiter(fn, { limit: 5, window: 1000 });\n *\n * // First 5 calls return true\n * rateLimiter.maybeExecute('arg1', 'arg2'); // true\n *\n * // Additional calls within the window return false\n * rateLimiter.maybeExecute('arg1', 'arg2'); // false\n * ```\n */\n maybeExecute = (...args: Parameters<TFn>): boolean => {\n this.cleanupOldExecutions()\n\n const relevantExecutionTimes = this.getExecutionTimesInWindow()\n\n if (relevantExecutionTimes.length < this.options.limit) {\n this.execute(...args)\n return true\n }\n\n this.options.onReject?.(this)\n return false\n }\n\n private execute = (...args: Parameters<TFn>): void => {\n const now = Date.now()\n this.fn(...args)\n this.options.onExecute?.(args, this)\n this.executionTimes.push(now)\n this.setCleanupTimeout(now)\n }\n\n private getExecutionTimesInWindow = (): Array<number> => {\n if (this.options.windowType === 'sliding') {\n // For sliding window, return all executions within the current window\n return this.executionTimes.filter(\n (time) => time > Date.now() - this.options.window,\n )\n } else {\n // For fixed window, return all executions in the current window\n if (this.executionTimes.length === 0) {\n return []\n }\n const oldestExecution = Math.min(...this.executionTimes)\n const windowStart = oldestExecution\n const windowEnd = windowStart + this.options.window\n const now = Date.now()\n\n // If the window has expired, return empty array\n if (now > windowEnd) {\n return []\n }\n\n // Otherwise, return all executions in the current window\n return this.executionTimes.filter(\n (time) => time >= windowStart && time <= windowEnd,\n )\n }\n }\n\n private setCleanupTimeout = (executionTime: number): void => {\n if (\n this.options.windowType === 'sliding' ||\n this.timeoutIds.size === 0 // new fixed window\n ) {\n const now = Date.now()\n const timeUntilExpiration = executionTime - now + this.options.window + 1\n const timeoutId = setTimeout(() => {\n this.cleanupOldExecutions()\n this.clearTimeout(timeoutId)\n }, timeUntilExpiration)\n this.timeoutIds.add(timeoutId)\n }\n }\n\n private clearTimeout = (timeoutId: NodeJS.Timeout): void => {\n clearTimeout(timeoutId)\n this.timeoutIds.delete(timeoutId)\n }\n\n private clearTimeouts = (): void => {\n this.timeoutIds.forEach((timeoutId) => clearTimeout(timeoutId))\n this.timeoutIds.clear()\n }\n\n private cleanupOldExecutions = (): void => {\n this.executionTimes = this.getExecutionTimesInWindow()\n }\n\n /**\n * Returns the number of remaining executions allowed in the current window.\n */\n getRemainingInWindow = (): number => {\n const relevantExecutionTimes = this.getExecutionTimesInWindow()\n return Math.max(0, this.options.limit - relevantExecutionTimes.length)\n }\n\n /**\n * Returns the number of milliseconds until the next execution will be possible.\n * Returns 0 if executions are currently allowed.\n */\n getMsUntilNextWindow = (): number => {\n if (this.getRemainingInWindow() > 0) {\n return 0\n }\n const oldestExecution = this.executionTimes[0] ?? Infinity\n return oldestExecution + this.options.window - Date.now()\n }\n\n /**\n * Resets the rate limiter state, clearing all execution history.\n */\n reset = (): void => {\n this.executionTimes = []\n this.clearTimeouts()\n }\n}\n\n/**\n * Creates a lightweight rate-limited function that will execute the provided function up to a maximum number of times within a time window.\n *\n * This is an alternative to the rateLimit function in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,\n * this function creates a rate limiter with no external dependencies, devtools integration, or reactive state.\n *\n * Rate limiting allows all executions until the limit is reached, then blocks all subsequent calls until the window resets.\n * This differs from throttling (which ensures even spacing) and debouncing (which waits for pauses).\n *\n * @example\n * ```ts\n * const rateLimitedApi = liteRateLimit(makeApiCall, {\n * limit: 5,\n * window: 60000, // 1 minute\n * windowType: 'sliding'\n * });\n *\n * // First 5 calls execute immediately\n * // Additional calls are rejected until window allows\n * rateLimitedApi();\n * ```\n *\n * @example\n * ```ts\n * // Fixed window - all 10 calls happen in first second, then 10 second wait\n * const rateLimitedFixed = liteRateLimit(logEvent, {\n * limit: 10,\n * window: 10000,\n * windowType: 'fixed'\n * });\n * ```\n */\nexport function liteRateLimit<TFn extends AnyFunction>(\n fn: TFn,\n options: LiteRateLimiterOptions<TFn>,\n): (...args: Parameters<TFn>) => boolean {\n const rateLimiter = new LiteRateLimiter(fn, options)\n return rateLimiter.maybeExecute\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEA,IAAa,kBAAb,MAAsD;CAIpD,YACE,AAAOA,IACP,AAAOC,SACP;EAFO;EACA;wBAL+B,EAAE;oCACA,IAAI,KAAK;uBA2BnC,GAAG,SAAmC;AACpD,QAAK,sBAAsB;AAI3B,OAF+B,KAAK,2BAA2B,CAEpC,SAAS,KAAK,QAAQ,OAAO;AACtD,SAAK,QAAQ,GAAG,KAAK;AACrB,WAAO;;AAGT,QAAK,QAAQ,WAAW,KAAK;AAC7B,UAAO;;kBAGU,GAAG,SAAgC;GACpD,MAAM,MAAM,KAAK,KAAK;AACtB,QAAK,GAAG,GAAG,KAAK;AAChB,QAAK,QAAQ,YAAY,MAAM,KAAK;AACpC,QAAK,eAAe,KAAK,IAAI;AAC7B,QAAK,kBAAkB,IAAI;;yCAG4B;AACvD,OAAI,KAAK,QAAQ,eAAe,UAE9B,QAAO,KAAK,eAAe,QACxB,SAAS,OAAO,KAAK,KAAK,GAAG,KAAK,QAAQ,OAC5C;QACI;AAEL,QAAI,KAAK,eAAe,WAAW,EACjC,QAAO,EAAE;IAGX,MAAM,cADkB,KAAK,IAAI,GAAG,KAAK,eAAe;IAExD,MAAM,YAAY,cAAc,KAAK,QAAQ;AAI7C,QAHY,KAAK,KAAK,GAGZ,UACR,QAAO,EAAE;AAIX,WAAO,KAAK,eAAe,QACxB,SAAS,QAAQ,eAAe,QAAQ,UAC1C;;;4BAIwB,kBAAgC;AAC3D,OACE,KAAK,QAAQ,eAAe,aAC5B,KAAK,WAAW,SAAS,GACzB;IAEA,MAAM,sBAAsB,gBADhB,KAAK,KAAK,GAC4B,KAAK,QAAQ,SAAS;IACxE,MAAM,YAAY,iBAAiB;AACjC,UAAK,sBAAsB;AAC3B,UAAK,aAAa,UAAU;OAC3B,oBAAoB;AACvB,SAAK,WAAW,IAAI,UAAU;;;uBAIV,cAAoC;AAC1D,gBAAa,UAAU;AACvB,QAAK,WAAW,OAAO,UAAU;;6BAGC;AAClC,QAAK,WAAW,SAAS,cAAc,aAAa,UAAU,CAAC;AAC/D,QAAK,WAAW,OAAO;;oCAGkB;AACzC,QAAK,iBAAiB,KAAK,2BAA2B;;oCAMnB;GACnC,MAAM,yBAAyB,KAAK,2BAA2B;AAC/D,UAAO,KAAK,IAAI,GAAG,KAAK,QAAQ,QAAQ,uBAAuB,OAAO;;oCAOnC;AACnC,OAAI,KAAK,sBAAsB,GAAG,EAChC,QAAO;AAGT,WADwB,KAAK,eAAe,MAAM,YACzB,KAAK,QAAQ,SAAS,KAAK,KAAK;;qBAMvC;AAClB,QAAK,iBAAiB,EAAE;AACxB,QAAK,eAAe;;AA5HpB,MAAI,KAAK,QAAQ,eAAe,OAC9B,MAAK,QAAQ,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgKhC,SAAgB,cACd,IACA,SACuC;AAEvC,QADoB,IAAI,gBAAgB,IAAI,QAAQ,CACjC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/lite-throttler.ts
|
|
3
|
+
/**
|
|
4
|
+
* A lightweight class that creates a throttled function.
|
|
5
|
+
*
|
|
6
|
+
* This is an alternative to the Throttler in the core @tanstack/pacer package, but is more
|
|
7
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core Throttler,
|
|
8
|
+
* this version does not use TanStack Store for state management, has no devtools integration,
|
|
9
|
+
* and provides only essential throttling functionality.
|
|
10
|
+
*
|
|
11
|
+
* Throttling ensures a function is called at most once within a specified time window.
|
|
12
|
+
* Unlike debouncing which waits for a pause in calls, throttling guarantees consistent
|
|
13
|
+
* execution timing regardless of call frequency.
|
|
14
|
+
*
|
|
15
|
+
* Supports both leading and trailing edge execution:
|
|
16
|
+
* - Leading: Execute immediately on first call (default: true)
|
|
17
|
+
* - Trailing: Execute after wait period if called during throttle (default: true)
|
|
18
|
+
*
|
|
19
|
+
* Features:
|
|
20
|
+
* - Zero dependencies - no external libraries required
|
|
21
|
+
* - Minimal API surface - only essential methods (maybeExecute, flush, cancel)
|
|
22
|
+
* - Simple state management - uses basic private properties instead of reactive stores
|
|
23
|
+
* - Callback support for monitoring execution events
|
|
24
|
+
* - Lightweight - designed for use in npm packages where bundle size matters
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const throttler = new LiteThrottler((scrollY: number) => {
|
|
29
|
+
* updateScrollPosition(scrollY);
|
|
30
|
+
* }, {
|
|
31
|
+
* wait: 100,
|
|
32
|
+
* onExecute: (args, throttler) => {
|
|
33
|
+
* console.log('Updated scroll position:', args[0]);
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Will execute at most once per 100ms
|
|
38
|
+
* window.addEventListener('scroll', () => {
|
|
39
|
+
* throttler.maybeExecute(window.scrollY);
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
var LiteThrottler = class {
|
|
44
|
+
constructor(fn, options) {
|
|
45
|
+
this.fn = fn;
|
|
46
|
+
this.options = options;
|
|
47
|
+
this.lastExecutionTime = 0;
|
|
48
|
+
this.isPending = false;
|
|
49
|
+
this.maybeExecute = (...args) => {
|
|
50
|
+
const timeSinceLastExecution = Date.now() - this.lastExecutionTime;
|
|
51
|
+
if (this.options.leading && timeSinceLastExecution >= this.options.wait) this.execute(...args);
|
|
52
|
+
else {
|
|
53
|
+
this.lastArgs = args;
|
|
54
|
+
if (!this.timeoutId && this.options.trailing) {
|
|
55
|
+
const timeoutDuration = this.options.wait - timeSinceLastExecution;
|
|
56
|
+
this.isPending = true;
|
|
57
|
+
this.timeoutId = setTimeout(() => {
|
|
58
|
+
if (this.lastArgs !== void 0) this.execute(...this.lastArgs);
|
|
59
|
+
}, timeoutDuration);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
this.execute = (...args) => {
|
|
64
|
+
this.fn(...args);
|
|
65
|
+
this.options.onExecute?.(args, this);
|
|
66
|
+
this.lastExecutionTime = Date.now();
|
|
67
|
+
this.clearTimeout();
|
|
68
|
+
this.lastArgs = void 0;
|
|
69
|
+
this.isPending = false;
|
|
70
|
+
};
|
|
71
|
+
this.flush = () => {
|
|
72
|
+
if (this.isPending && this.lastArgs) this.execute(...this.lastArgs);
|
|
73
|
+
};
|
|
74
|
+
this.cancel = () => {
|
|
75
|
+
this.clearTimeout();
|
|
76
|
+
this.lastArgs = void 0;
|
|
77
|
+
this.isPending = false;
|
|
78
|
+
};
|
|
79
|
+
this.clearTimeout = () => {
|
|
80
|
+
if (this.timeoutId) {
|
|
81
|
+
clearTimeout(this.timeoutId);
|
|
82
|
+
this.timeoutId = void 0;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
if (this.options.leading === void 0 && this.options.trailing === void 0) {
|
|
86
|
+
this.options.leading = true;
|
|
87
|
+
this.options.trailing = true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Creates a lightweight throttled function that limits how often the provided function can execute.
|
|
93
|
+
*
|
|
94
|
+
* This is an alternative to the throttle function in the core @tanstack/pacer package, but is more
|
|
95
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core version,
|
|
96
|
+
* this function creates a throttler with no external dependencies, devtools integration, or reactive state.
|
|
97
|
+
*
|
|
98
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
99
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
100
|
+
* expensive operations or UI updates.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const throttledScroll = liteThrottle(() => {
|
|
105
|
+
* updateScrollIndicator();
|
|
106
|
+
* }, { wait: 100 });
|
|
107
|
+
*
|
|
108
|
+
* // Will execute at most once per 100ms
|
|
109
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* // Leading edge execution - fires immediately then throttles
|
|
115
|
+
* const throttledResize = liteThrottle(() => {
|
|
116
|
+
* recalculateLayout();
|
|
117
|
+
* }, { wait: 250, leading: true, trailing: false });
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
function liteThrottle(fn, options) {
|
|
121
|
+
return new LiteThrottler(fn, options).maybeExecute;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
exports.LiteThrottler = LiteThrottler;
|
|
126
|
+
exports.liteThrottle = liteThrottle;
|
|
127
|
+
//# sourceMappingURL=lite-throttler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-throttler.cjs","names":["fn: TFn","options: LiteThrottlerOptions<TFn>"],"sources":["../src/lite-throttler.ts"],"sourcesContent":["import type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * Options for configuring a lite throttled function\n */\nexport interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {\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?: (args: Parameters<TFn>, throttler: LiteThrottler<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\n/**\n * A lightweight class that creates a throttled function.\n *\n * This is an alternative to the Throttler in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core Throttler,\n * this version does not use TanStack Store for state management, has no devtools integration,\n * and provides only essential throttling functionality.\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 * Features:\n * - Zero dependencies - no external libraries required\n * - Minimal API surface - only essential methods (maybeExecute, flush, cancel)\n * - Simple state management - uses basic private properties instead of reactive stores\n * - Callback support for monitoring execution events\n * - Lightweight - designed for use in npm packages where bundle size matters\n *\n * @example\n * ```ts\n * const throttler = new LiteThrottler((scrollY: number) => {\n * updateScrollPosition(scrollY);\n * }, {\n * wait: 100,\n * onExecute: (args, throttler) => {\n * console.log('Updated scroll position:', args[0]);\n * }\n * });\n *\n * // Will execute at most once per 100ms\n * window.addEventListener('scroll', () => {\n * throttler.maybeExecute(window.scrollY);\n * });\n * ```\n */\nexport class LiteThrottler<TFn extends AnyFunction> {\n private timeoutId: NodeJS.Timeout | undefined\n private lastArgs: Parameters<TFn> | undefined\n private lastExecutionTime = 0\n private isPending = false\n\n constructor(\n public fn: TFn,\n public options: LiteThrottlerOptions<TFn>,\n ) {\n // Default both leading and trailing to true if neither is specified\n if (\n this.options.leading === undefined &&\n this.options.trailing === undefined\n ) {\n this.options.leading = true\n this.options.trailing = true\n }\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 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.execute(...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 timeoutDuration = this.options.wait - timeSinceLastExecution\n this.isPending = true\n this.timeoutId = setTimeout(() => {\n if (this.lastArgs !== undefined) {\n this.execute(...this.lastArgs)\n }\n }, timeoutDuration)\n }\n }\n }\n\n private execute = (...args: Parameters<TFn>): void => {\n this.fn(...args)\n this.options.onExecute?.(args, this)\n this.lastExecutionTime = Date.now()\n this.clearTimeout()\n this.lastArgs = undefined\n this.isPending = false\n }\n\n /**\n * Processes the current pending execution immediately.\n * If there's a pending execution, it will be executed right away\n * and the timeout will be cleared.\n */\n flush = (): void => {\n if (this.isPending && this.lastArgs) {\n this.execute(...this.lastArgs)\n }\n }\n\n /**\n * Cancels any pending trailing execution and clears internal state.\n * If a trailing execution is scheduled, this will prevent that execution from occurring.\n */\n cancel = (): void => {\n this.clearTimeout()\n this.lastArgs = undefined\n this.isPending = false\n }\n\n private clearTimeout = (): void => {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n }\n }\n}\n\n/**\n * Creates a lightweight throttled function that limits how often the provided function can execute.\n *\n * This is an alternative to the throttle function in the core @tanstack/pacer package, but is more\n * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,\n * this function creates a throttler with no external dependencies, devtools integration, or reactive state.\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 * @example\n * ```ts\n * const throttledScroll = liteThrottle(() => {\n * updateScrollIndicator();\n * }, { wait: 100 });\n *\n * // Will execute at most once per 100ms\n * window.addEventListener('scroll', throttledScroll);\n * ```\n *\n * @example\n * ```ts\n * // Leading edge execution - fires immediately then throttles\n * const throttledResize = liteThrottle(() => {\n * recalculateLayout();\n * }, { wait: 250, leading: true, trailing: false });\n * ```\n */\nexport function liteThrottle<TFn extends AnyFunction>(\n fn: TFn,\n options: LiteThrottlerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const throttler = new LiteThrottler(fn, options)\n return throttler.maybeExecute\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,gBAAb,MAAoD;CAMlD,YACE,AAAOA,IACP,AAAOC,SACP;EAFO;EACA;2BALmB;mBACR;uBA2BJ,GAAG,SAAgC;GAEjD,MAAM,yBADM,KAAK,KAAK,GACe,KAAK;AAG1C,OAAI,KAAK,QAAQ,WAAW,0BAA0B,KAAK,QAAQ,KACjE,MAAK,QAAQ,GAAG,KAAK;QAChB;AAEL,SAAK,WAAW;AAGhB,QAAI,CAAC,KAAK,aAAa,KAAK,QAAQ,UAAU;KAC5C,MAAM,kBAAkB,KAAK,QAAQ,OAAO;AAC5C,UAAK,YAAY;AACjB,UAAK,YAAY,iBAAiB;AAChC,UAAI,KAAK,aAAa,OACpB,MAAK,QAAQ,GAAG,KAAK,SAAS;QAE/B,gBAAgB;;;;kBAKN,GAAG,SAAgC;AACpD,QAAK,GAAG,GAAG,KAAK;AAChB,QAAK,QAAQ,YAAY,MAAM,KAAK;AACpC,QAAK,oBAAoB,KAAK,KAAK;AACnC,QAAK,cAAc;AACnB,QAAK,WAAW;AAChB,QAAK,YAAY;;qBAQC;AAClB,OAAI,KAAK,aAAa,KAAK,SACzB,MAAK,QAAQ,GAAG,KAAK,SAAS;;sBAQb;AACnB,QAAK,cAAc;AACnB,QAAK,WAAW;AAChB,QAAK,YAAY;;4BAGgB;AACjC,OAAI,KAAK,WAAW;AAClB,iBAAa,KAAK,UAAU;AAC5B,SAAK,YAAY;;;AA7EnB,MACE,KAAK,QAAQ,YAAY,UACzB,KAAK,QAAQ,aAAa,QAC1B;AACA,QAAK,QAAQ,UAAU;AACvB,QAAK,QAAQ,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0G9B,SAAgB,aACd,IACA,SACoC;AAEpC,QADkB,IAAI,cAAc,IAAI,QAAQ,CAC/B"}
|