@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,26 +1,29 @@
|
|
|
1
|
-
import { AnyFunction } from
|
|
1
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/lite-throttler.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Options for configuring a lite throttled function
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
8
|
+
interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {
|
|
9
|
+
/**
|
|
10
|
+
* Whether to execute on the leading edge of the timeout.
|
|
11
|
+
* Defaults to true.
|
|
12
|
+
*/
|
|
13
|
+
leading?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Callback function that is called after the function is executed
|
|
16
|
+
*/
|
|
17
|
+
onExecute?: (args: Parameters<TFn>, throttler: LiteThrottler<TFn>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to execute on the trailing edge of the timeout.
|
|
20
|
+
* Defaults to true.
|
|
21
|
+
*/
|
|
22
|
+
trailing?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Time window in milliseconds during which the function can only be executed once.
|
|
25
|
+
*/
|
|
26
|
+
wait: number;
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
29
|
* A lightweight class that creates a throttled function.
|
|
@@ -62,39 +65,39 @@ export interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {
|
|
|
62
65
|
* });
|
|
63
66
|
* ```
|
|
64
67
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
68
|
+
declare class LiteThrottler<TFn extends AnyFunction> {
|
|
69
|
+
fn: TFn;
|
|
70
|
+
options: LiteThrottlerOptions<TFn>;
|
|
71
|
+
private timeoutId;
|
|
72
|
+
private lastArgs;
|
|
73
|
+
private lastExecutionTime;
|
|
74
|
+
private isPending;
|
|
75
|
+
constructor(fn: TFn, options: LiteThrottlerOptions<TFn>);
|
|
76
|
+
/**
|
|
77
|
+
* Attempts to execute the throttled function. The execution behavior depends on the throttler options:
|
|
78
|
+
*
|
|
79
|
+
* - If enough time has passed since the last execution (>= wait period):
|
|
80
|
+
* - With leading=true: Executes immediately
|
|
81
|
+
* - With leading=false: Waits for the next trailing execution
|
|
82
|
+
*
|
|
83
|
+
* - If within the wait period:
|
|
84
|
+
* - With trailing=true: Schedules execution for end of wait period
|
|
85
|
+
* - With trailing=false: Drops the execution
|
|
86
|
+
*/
|
|
87
|
+
maybeExecute: (...args: Parameters<TFn>) => void;
|
|
88
|
+
private execute;
|
|
89
|
+
/**
|
|
90
|
+
* Processes the current pending execution immediately.
|
|
91
|
+
* If there's a pending execution, it will be executed right away
|
|
92
|
+
* and the timeout will be cleared.
|
|
93
|
+
*/
|
|
94
|
+
flush: () => void;
|
|
95
|
+
/**
|
|
96
|
+
* Cancels any pending trailing execution and clears internal state.
|
|
97
|
+
* If a trailing execution is scheduled, this will prevent that execution from occurring.
|
|
98
|
+
*/
|
|
99
|
+
cancel: () => void;
|
|
100
|
+
private clearTimeout;
|
|
98
101
|
}
|
|
99
102
|
/**
|
|
100
103
|
* Creates a lightweight throttled function that limits how often the provided function can execute.
|
|
@@ -125,4 +128,7 @@ export declare class LiteThrottler<TFn extends AnyFunction> {
|
|
|
125
128
|
* }, { wait: 250, leading: true, trailing: false });
|
|
126
129
|
* ```
|
|
127
130
|
*/
|
|
128
|
-
|
|
131
|
+
declare function liteThrottle<TFn extends AnyFunction>(fn: TFn, options: LiteThrottlerOptions<TFn>): (...args: Parameters<TFn>) => void;
|
|
132
|
+
//#endregion
|
|
133
|
+
export { LiteThrottler, LiteThrottlerOptions, liteThrottle };
|
|
134
|
+
//# sourceMappingURL=lite-throttler.d.cts.map
|
|
@@ -1,26 +1,29 @@
|
|
|
1
|
-
import { AnyFunction } from
|
|
1
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/lite-throttler.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Options for configuring a lite throttled function
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
8
|
+
interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {
|
|
9
|
+
/**
|
|
10
|
+
* Whether to execute on the leading edge of the timeout.
|
|
11
|
+
* Defaults to true.
|
|
12
|
+
*/
|
|
13
|
+
leading?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Callback function that is called after the function is executed
|
|
16
|
+
*/
|
|
17
|
+
onExecute?: (args: Parameters<TFn>, throttler: LiteThrottler<TFn>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to execute on the trailing edge of the timeout.
|
|
20
|
+
* Defaults to true.
|
|
21
|
+
*/
|
|
22
|
+
trailing?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Time window in milliseconds during which the function can only be executed once.
|
|
25
|
+
*/
|
|
26
|
+
wait: number;
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
29
|
* A lightweight class that creates a throttled function.
|
|
@@ -62,39 +65,39 @@ export interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {
|
|
|
62
65
|
* });
|
|
63
66
|
* ```
|
|
64
67
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
68
|
+
declare class LiteThrottler<TFn extends AnyFunction> {
|
|
69
|
+
fn: TFn;
|
|
70
|
+
options: LiteThrottlerOptions<TFn>;
|
|
71
|
+
private timeoutId;
|
|
72
|
+
private lastArgs;
|
|
73
|
+
private lastExecutionTime;
|
|
74
|
+
private isPending;
|
|
75
|
+
constructor(fn: TFn, options: LiteThrottlerOptions<TFn>);
|
|
76
|
+
/**
|
|
77
|
+
* Attempts to execute the throttled function. The execution behavior depends on the throttler options:
|
|
78
|
+
*
|
|
79
|
+
* - If enough time has passed since the last execution (>= wait period):
|
|
80
|
+
* - With leading=true: Executes immediately
|
|
81
|
+
* - With leading=false: Waits for the next trailing execution
|
|
82
|
+
*
|
|
83
|
+
* - If within the wait period:
|
|
84
|
+
* - With trailing=true: Schedules execution for end of wait period
|
|
85
|
+
* - With trailing=false: Drops the execution
|
|
86
|
+
*/
|
|
87
|
+
maybeExecute: (...args: Parameters<TFn>) => void;
|
|
88
|
+
private execute;
|
|
89
|
+
/**
|
|
90
|
+
* Processes the current pending execution immediately.
|
|
91
|
+
* If there's a pending execution, it will be executed right away
|
|
92
|
+
* and the timeout will be cleared.
|
|
93
|
+
*/
|
|
94
|
+
flush: () => void;
|
|
95
|
+
/**
|
|
96
|
+
* Cancels any pending trailing execution and clears internal state.
|
|
97
|
+
* If a trailing execution is scheduled, this will prevent that execution from occurring.
|
|
98
|
+
*/
|
|
99
|
+
cancel: () => void;
|
|
100
|
+
private clearTimeout;
|
|
98
101
|
}
|
|
99
102
|
/**
|
|
100
103
|
* Creates a lightweight throttled function that limits how often the provided function can execute.
|
|
@@ -125,4 +128,7 @@ export declare class LiteThrottler<TFn extends AnyFunction> {
|
|
|
125
128
|
* }, { wait: 250, leading: true, trailing: false });
|
|
126
129
|
* ```
|
|
127
130
|
*/
|
|
128
|
-
|
|
131
|
+
declare function liteThrottle<TFn extends AnyFunction>(fn: TFn, options: LiteThrottlerOptions<TFn>): (...args: Parameters<TFn>) => void;
|
|
132
|
+
//#endregion
|
|
133
|
+
export { LiteThrottler, LiteThrottlerOptions, liteThrottle };
|
|
134
|
+
//# sourceMappingURL=lite-throttler.d.ts.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
//#region src/lite-throttler.ts
|
|
2
|
+
/**
|
|
3
|
+
* A lightweight class that creates a throttled function.
|
|
4
|
+
*
|
|
5
|
+
* This is an alternative to the Throttler in the core @tanstack/pacer package, but is more
|
|
6
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core Throttler,
|
|
7
|
+
* this version does not use TanStack Store for state management, has no devtools integration,
|
|
8
|
+
* and provides only essential throttling functionality.
|
|
9
|
+
*
|
|
10
|
+
* Throttling ensures a function is called at most once within a specified time window.
|
|
11
|
+
* Unlike debouncing which waits for a pause in calls, throttling guarantees consistent
|
|
12
|
+
* execution timing regardless of call frequency.
|
|
13
|
+
*
|
|
14
|
+
* Supports both leading and trailing edge execution:
|
|
15
|
+
* - Leading: Execute immediately on first call (default: true)
|
|
16
|
+
* - Trailing: Execute after wait period if called during throttle (default: true)
|
|
17
|
+
*
|
|
18
|
+
* Features:
|
|
19
|
+
* - Zero dependencies - no external libraries required
|
|
20
|
+
* - Minimal API surface - only essential methods (maybeExecute, flush, cancel)
|
|
21
|
+
* - Simple state management - uses basic private properties instead of reactive stores
|
|
22
|
+
* - Callback support for monitoring execution events
|
|
23
|
+
* - Lightweight - designed for use in npm packages where bundle size matters
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const throttler = new LiteThrottler((scrollY: number) => {
|
|
28
|
+
* updateScrollPosition(scrollY);
|
|
29
|
+
* }, {
|
|
30
|
+
* wait: 100,
|
|
31
|
+
* onExecute: (args, throttler) => {
|
|
32
|
+
* console.log('Updated scroll position:', args[0]);
|
|
33
|
+
* }
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Will execute at most once per 100ms
|
|
37
|
+
* window.addEventListener('scroll', () => {
|
|
38
|
+
* throttler.maybeExecute(window.scrollY);
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
var LiteThrottler = class {
|
|
43
|
+
constructor(fn, options) {
|
|
44
|
+
this.fn = fn;
|
|
45
|
+
this.options = options;
|
|
46
|
+
this.lastExecutionTime = 0;
|
|
47
|
+
this.isPending = false;
|
|
48
|
+
this.maybeExecute = (...args) => {
|
|
49
|
+
const timeSinceLastExecution = Date.now() - this.lastExecutionTime;
|
|
50
|
+
if (this.options.leading && timeSinceLastExecution >= this.options.wait) this.execute(...args);
|
|
51
|
+
else {
|
|
52
|
+
this.lastArgs = args;
|
|
53
|
+
if (!this.timeoutId && this.options.trailing) {
|
|
54
|
+
const timeoutDuration = this.options.wait - timeSinceLastExecution;
|
|
55
|
+
this.isPending = true;
|
|
56
|
+
this.timeoutId = setTimeout(() => {
|
|
57
|
+
if (this.lastArgs !== void 0) this.execute(...this.lastArgs);
|
|
58
|
+
}, timeoutDuration);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
this.execute = (...args) => {
|
|
63
|
+
this.fn(...args);
|
|
64
|
+
this.options.onExecute?.(args, this);
|
|
65
|
+
this.lastExecutionTime = Date.now();
|
|
66
|
+
this.clearTimeout();
|
|
67
|
+
this.lastArgs = void 0;
|
|
68
|
+
this.isPending = false;
|
|
69
|
+
};
|
|
70
|
+
this.flush = () => {
|
|
71
|
+
if (this.isPending && this.lastArgs) this.execute(...this.lastArgs);
|
|
72
|
+
};
|
|
73
|
+
this.cancel = () => {
|
|
74
|
+
this.clearTimeout();
|
|
75
|
+
this.lastArgs = void 0;
|
|
76
|
+
this.isPending = false;
|
|
77
|
+
};
|
|
78
|
+
this.clearTimeout = () => {
|
|
79
|
+
if (this.timeoutId) {
|
|
80
|
+
clearTimeout(this.timeoutId);
|
|
81
|
+
this.timeoutId = void 0;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
if (this.options.leading === void 0 && this.options.trailing === void 0) {
|
|
85
|
+
this.options.leading = true;
|
|
86
|
+
this.options.trailing = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Creates a lightweight throttled function that limits how often the provided function can execute.
|
|
92
|
+
*
|
|
93
|
+
* This is an alternative to the throttle function in the core @tanstack/pacer package, but is more
|
|
94
|
+
* suitable for libraries and npm packages that need minimal overhead. Unlike the core version,
|
|
95
|
+
* this function creates a throttler with no external dependencies, devtools integration, or reactive state.
|
|
96
|
+
*
|
|
97
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
98
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
99
|
+
* expensive operations or UI updates.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const throttledScroll = liteThrottle(() => {
|
|
104
|
+
* updateScrollIndicator();
|
|
105
|
+
* }, { wait: 100 });
|
|
106
|
+
*
|
|
107
|
+
* // Will execute at most once per 100ms
|
|
108
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* // Leading edge execution - fires immediately then throttles
|
|
114
|
+
* const throttledResize = liteThrottle(() => {
|
|
115
|
+
* recalculateLayout();
|
|
116
|
+
* }, { wait: 250, leading: true, trailing: false });
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
function liteThrottle(fn, options) {
|
|
120
|
+
return new LiteThrottler(fn, options).maybeExecute;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
export { LiteThrottler, liteThrottle };
|
|
125
|
+
//# sourceMappingURL=lite-throttler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lite-throttler.js","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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/pacer-lite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Lightweight utilities for debouncing, throttling, and more - designed for npm packages.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,69 +23,33 @@
|
|
|
23
23
|
"minimal"
|
|
24
24
|
],
|
|
25
25
|
"type": "module",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.cts",
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"require":
|
|
36
|
-
|
|
37
|
-
"default": "./dist/cjs/index.cjs"
|
|
38
|
-
}
|
|
31
|
+
"require": "./dist/index.cjs",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./lite-batcher": {
|
|
35
|
+
"require": "./dist/lite-batcher.cjs",
|
|
36
|
+
"import": "./dist/lite-batcher.js"
|
|
39
37
|
},
|
|
40
38
|
"./lite-debouncer": {
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
"default": "./dist/esm/lite-debouncer.js"
|
|
44
|
-
},
|
|
45
|
-
"require": {
|
|
46
|
-
"types": "./dist/cjs/lite-debouncer.d.cts",
|
|
47
|
-
"default": "./dist/cjs/lite-debouncer.cjs"
|
|
48
|
-
}
|
|
39
|
+
"require": "./dist/lite-debouncer.cjs",
|
|
40
|
+
"import": "./dist/lite-debouncer.js"
|
|
49
41
|
},
|
|
50
|
-
"./lite-
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
"default": "./dist/esm/lite-throttler.js"
|
|
54
|
-
},
|
|
55
|
-
"require": {
|
|
56
|
-
"types": "./dist/cjs/lite-throttler.d.cts",
|
|
57
|
-
"default": "./dist/cjs/lite-throttler.cjs"
|
|
58
|
-
}
|
|
42
|
+
"./lite-queuer": {
|
|
43
|
+
"require": "./dist/lite-queuer.cjs",
|
|
44
|
+
"import": "./dist/lite-queuer.js"
|
|
59
45
|
},
|
|
60
46
|
"./lite-rate-limiter": {
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
"default": "./dist/esm/lite-rate-limiter.js"
|
|
64
|
-
},
|
|
65
|
-
"require": {
|
|
66
|
-
"types": "./dist/cjs/lite-rate-limiter.d.cts",
|
|
67
|
-
"default": "./dist/cjs/lite-rate-limiter.cjs"
|
|
68
|
-
}
|
|
47
|
+
"require": "./dist/lite-rate-limiter.cjs",
|
|
48
|
+
"import": "./dist/lite-rate-limiter.js"
|
|
69
49
|
},
|
|
70
|
-
"./lite-
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
"default": "./dist/esm/lite-queuer.js"
|
|
74
|
-
},
|
|
75
|
-
"require": {
|
|
76
|
-
"types": "./dist/cjs/lite-queuer.d.cts",
|
|
77
|
-
"default": "./dist/cjs/lite-queuer.cjs"
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
"./lite-batcher": {
|
|
81
|
-
"import": {
|
|
82
|
-
"types": "./dist/esm/lite-batcher.d.ts",
|
|
83
|
-
"default": "./dist/esm/lite-batcher.js"
|
|
84
|
-
},
|
|
85
|
-
"require": {
|
|
86
|
-
"types": "./dist/cjs/lite-batcher.d.cts",
|
|
87
|
-
"default": "./dist/cjs/lite-batcher.cjs"
|
|
88
|
-
}
|
|
50
|
+
"./lite-throttler": {
|
|
51
|
+
"require": "./dist/lite-throttler.cjs",
|
|
52
|
+
"import": "./dist/lite-throttler.js"
|
|
89
53
|
},
|
|
90
54
|
"./package.json": "./package.json"
|
|
91
55
|
},
|
|
@@ -98,7 +62,7 @@
|
|
|
98
62
|
"src"
|
|
99
63
|
],
|
|
100
64
|
"devDependencies": {
|
|
101
|
-
"@tanstack/pacer": "0.
|
|
65
|
+
"@tanstack/pacer": "0.17.0"
|
|
102
66
|
},
|
|
103
67
|
"scripts": {
|
|
104
68
|
"clean": "premove ./build ./dist",
|
|
@@ -107,7 +71,6 @@
|
|
|
107
71
|
"test:lib": "vitest",
|
|
108
72
|
"test:lib:dev": "pnpm test:lib --watch",
|
|
109
73
|
"test:types": "tsc",
|
|
110
|
-
"
|
|
111
|
-
"build": "vite build"
|
|
74
|
+
"build": "tsdown"
|
|
112
75
|
}
|
|
113
76
|
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const liteDebouncer = require("./lite-debouncer.cjs");
|
|
4
|
-
const liteThrottler = require("./lite-throttler.cjs");
|
|
5
|
-
const liteRateLimiter = require("./lite-rate-limiter.cjs");
|
|
6
|
-
const liteQueuer = require("./lite-queuer.cjs");
|
|
7
|
-
const liteBatcher = require("./lite-batcher.cjs");
|
|
8
|
-
exports.LiteDebouncer = liteDebouncer.LiteDebouncer;
|
|
9
|
-
exports.liteDebounce = liteDebouncer.liteDebounce;
|
|
10
|
-
exports.LiteThrottler = liteThrottler.LiteThrottler;
|
|
11
|
-
exports.liteThrottle = liteThrottler.liteThrottle;
|
|
12
|
-
exports.LiteRateLimiter = liteRateLimiter.LiteRateLimiter;
|
|
13
|
-
exports.liteRateLimit = liteRateLimiter.liteRateLimit;
|
|
14
|
-
exports.LiteQueuer = liteQueuer.LiteQueuer;
|
|
15
|
-
exports.liteQueue = liteQueuer.liteQueue;
|
|
16
|
-
exports.LiteBatcher = liteBatcher.LiteBatcher;
|
|
17
|
-
exports.liteBatch = liteBatcher.liteBatch;
|
|
18
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
class LiteBatcher {
|
|
4
|
-
constructor(fn, options = {}) {
|
|
5
|
-
this.fn = fn;
|
|
6
|
-
this.options = options;
|
|
7
|
-
this.items = [];
|
|
8
|
-
this.timeoutId = null;
|
|
9
|
-
this._isPending = false;
|
|
10
|
-
this.addItem = (item) => {
|
|
11
|
-
this.items.push(item);
|
|
12
|
-
this._isPending = this.options.wait !== Infinity;
|
|
13
|
-
this.options.onItemsChange?.(this);
|
|
14
|
-
const shouldProcess = this.items.length >= this.options.maxSize || this.options.getShouldExecute(this.items, this);
|
|
15
|
-
if (shouldProcess) {
|
|
16
|
-
this.execute();
|
|
17
|
-
} else if (this.options.wait !== Infinity) {
|
|
18
|
-
this.clearTimeout();
|
|
19
|
-
this.timeoutId = setTimeout(() => this.execute(), this.getWait());
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
this.execute = () => {
|
|
23
|
-
if (this.items.length === 0) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const batch = this.peekAllItems();
|
|
27
|
-
this.clear();
|
|
28
|
-
this.fn(batch);
|
|
29
|
-
this.options.onExecute?.(batch, this);
|
|
30
|
-
};
|
|
31
|
-
this.flush = () => {
|
|
32
|
-
this.clearTimeout();
|
|
33
|
-
this.execute();
|
|
34
|
-
};
|
|
35
|
-
this.peekAllItems = () => {
|
|
36
|
-
return [...this.items];
|
|
37
|
-
};
|
|
38
|
-
this.clearTimeout = () => {
|
|
39
|
-
if (this.timeoutId) {
|
|
40
|
-
clearTimeout(this.timeoutId);
|
|
41
|
-
this.timeoutId = null;
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
this.clear = () => {
|
|
45
|
-
const hadItems = this.items.length > 0;
|
|
46
|
-
this.items = [];
|
|
47
|
-
this._isPending = false;
|
|
48
|
-
if (hadItems) {
|
|
49
|
-
this.options.onItemsChange?.(this);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
this.cancel = () => {
|
|
53
|
-
this.clearTimeout();
|
|
54
|
-
this._isPending = false;
|
|
55
|
-
};
|
|
56
|
-
this.options.maxSize = this.options.maxSize ?? Infinity;
|
|
57
|
-
this.options.started = this.options.started ?? true;
|
|
58
|
-
this.options.wait = this.options.wait ?? Infinity;
|
|
59
|
-
this.options.getShouldExecute = this.options.getShouldExecute ?? (() => false);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Number of items currently in the batch
|
|
63
|
-
*/
|
|
64
|
-
get size() {
|
|
65
|
-
return this.items.length;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Whether the batch has no items to process (items array is empty)
|
|
69
|
-
*/
|
|
70
|
-
get isEmpty() {
|
|
71
|
-
return this.items.length === 0;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Whether the batcher is waiting for the timeout to trigger batch processing
|
|
75
|
-
*/
|
|
76
|
-
get isPending() {
|
|
77
|
-
return this._isPending;
|
|
78
|
-
}
|
|
79
|
-
getWait() {
|
|
80
|
-
if (typeof this.options.wait === "function") {
|
|
81
|
-
return this.options.wait(this);
|
|
82
|
-
}
|
|
83
|
-
return this.options.wait;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
function liteBatch(fn, options = {}) {
|
|
87
|
-
const batcher = new LiteBatcher(fn, options);
|
|
88
|
-
return batcher.addItem;
|
|
89
|
-
}
|
|
90
|
-
exports.LiteBatcher = LiteBatcher;
|
|
91
|
-
exports.liteBatch = liteBatch;
|
|
92
|
-
//# sourceMappingURL=lite-batcher.cjs.map
|