@tanstack/pacer-lite 0.2.1 → 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.
Files changed (41) hide show
  1. package/README.md +29 -14
  2. package/dist/lite-batcher.d.ts +4 -6
  3. package/dist/lite-batcher.js +72 -45
  4. package/dist/lite-debouncer.d.ts +5 -9
  5. package/dist/lite-debouncer.js +58 -40
  6. package/dist/lite-queuer.d.ts +5 -7
  7. package/dist/lite-queuer.js +170 -89
  8. package/dist/lite-rate-limiter.d.ts +5 -9
  9. package/dist/lite-rate-limiter.js +89 -63
  10. package/dist/lite-throttler.d.ts +5 -9
  11. package/dist/lite-throttler.js +63 -40
  12. package/dist/pacer/dist/types.d.ts +7 -0
  13. package/package.json +13 -33
  14. package/dist/index.cjs +0 -16
  15. package/dist/index.d.cts +0 -6
  16. package/dist/lite-batcher.cjs +0 -175
  17. package/dist/lite-batcher.cjs.map +0 -1
  18. package/dist/lite-batcher.d.cts +0 -184
  19. package/dist/lite-batcher.js.map +0 -1
  20. package/dist/lite-debouncer.cjs +0 -125
  21. package/dist/lite-debouncer.cjs.map +0 -1
  22. package/dist/lite-debouncer.d.cts +0 -127
  23. package/dist/lite-debouncer.js.map +0 -1
  24. package/dist/lite-queuer.cjs +0 -208
  25. package/dist/lite-queuer.cjs.map +0 -1
  26. package/dist/lite-queuer.d.cts +0 -243
  27. package/dist/lite-queuer.js.map +0 -1
  28. package/dist/lite-rate-limiter.cjs +0 -149
  29. package/dist/lite-rate-limiter.cjs.map +0 -1
  30. package/dist/lite-rate-limiter.d.cts +0 -149
  31. package/dist/lite-rate-limiter.js.map +0 -1
  32. package/dist/lite-throttler.cjs +0 -127
  33. package/dist/lite-throttler.cjs.map +0 -1
  34. package/dist/lite-throttler.d.cts +0 -134
  35. package/dist/lite-throttler.js.map +0 -1
  36. package/src/index.ts +0 -5
  37. package/src/lite-batcher.ts +0 -267
  38. package/src/lite-debouncer.ts +0 -184
  39. package/src/lite-queuer.ts +0 -434
  40. package/src/lite-rate-limiter.ts +0 -246
  41. package/src/lite-throttler.ts +0 -195
@@ -1,195 +0,0 @@
1
- import type { AnyFunction } from '@tanstack/pacer/types'
2
-
3
- /**
4
- * Options for configuring a lite throttled function
5
- */
6
- export interface LiteThrottlerOptions<TFn extends AnyFunction = AnyFunction> {
7
- /**
8
- * Whether to execute on the leading edge of the timeout.
9
- * Defaults to true.
10
- */
11
- leading?: boolean
12
- /**
13
- * Callback function that is called after the function is executed
14
- */
15
- onExecute?: (args: Parameters<TFn>, throttler: LiteThrottler<TFn>) => void
16
- /**
17
- * Whether to execute on the trailing edge of the timeout.
18
- * Defaults to true.
19
- */
20
- trailing?: boolean
21
- /**
22
- * Time window in milliseconds during which the function can only be executed once.
23
- */
24
- wait: number
25
- }
26
-
27
- /**
28
- * A lightweight class that creates a throttled function.
29
- *
30
- * This is an alternative to the Throttler in the core @tanstack/pacer package, but is more
31
- * suitable for libraries and npm packages that need minimal overhead. Unlike the core Throttler,
32
- * this version does not use TanStack Store for state management, has no devtools integration,
33
- * and provides only essential throttling functionality.
34
- *
35
- * Throttling ensures a function is called at most once within a specified time window.
36
- * Unlike debouncing which waits for a pause in calls, throttling guarantees consistent
37
- * execution timing regardless of call frequency.
38
- *
39
- * Supports both leading and trailing edge execution:
40
- * - Leading: Execute immediately on first call (default: true)
41
- * - Trailing: Execute after wait period if called during throttle (default: true)
42
- *
43
- * Features:
44
- * - Zero dependencies - no external libraries required
45
- * - Minimal API surface - only essential methods (maybeExecute, flush, cancel)
46
- * - Simple state management - uses basic private properties instead of reactive stores
47
- * - Callback support for monitoring execution events
48
- * - Lightweight - designed for use in npm packages where bundle size matters
49
- *
50
- * @example
51
- * ```ts
52
- * const throttler = new LiteThrottler((scrollY: number) => {
53
- * updateScrollPosition(scrollY);
54
- * }, {
55
- * wait: 100,
56
- * onExecute: (args, throttler) => {
57
- * console.log('Updated scroll position:', args[0]);
58
- * }
59
- * });
60
- *
61
- * // Will execute at most once per 100ms
62
- * window.addEventListener('scroll', () => {
63
- * throttler.maybeExecute(window.scrollY);
64
- * });
65
- * ```
66
- */
67
- export class LiteThrottler<TFn extends AnyFunction> {
68
- private timeoutId: NodeJS.Timeout | undefined
69
- private lastArgs: Parameters<TFn> | undefined
70
- private lastExecutionTime = 0
71
- private isPending = false
72
-
73
- constructor(
74
- public fn: TFn,
75
- public options: LiteThrottlerOptions<TFn>,
76
- ) {
77
- // Default both leading and trailing to true if neither is specified
78
- if (
79
- this.options.leading === undefined &&
80
- this.options.trailing === undefined
81
- ) {
82
- this.options.leading = true
83
- this.options.trailing = true
84
- }
85
- }
86
-
87
- /**
88
- * Attempts to execute the throttled function. The execution behavior depends on the throttler options:
89
- *
90
- * - If enough time has passed since the last execution (>= wait period):
91
- * - With leading=true: Executes immediately
92
- * - With leading=false: Waits for the next trailing execution
93
- *
94
- * - If within the wait period:
95
- * - With trailing=true: Schedules execution for end of wait period
96
- * - With trailing=false: Drops the execution
97
- */
98
- maybeExecute = (...args: Parameters<TFn>): void => {
99
- const now = Date.now()
100
- const timeSinceLastExecution = now - this.lastExecutionTime
101
-
102
- // Handle leading execution
103
- if (this.options.leading && timeSinceLastExecution >= this.options.wait) {
104
- this.execute(...args)
105
- } else {
106
- // Store the most recent arguments for potential trailing execution
107
- this.lastArgs = args
108
-
109
- // Set up trailing execution if not already scheduled
110
- if (!this.timeoutId && this.options.trailing) {
111
- const timeoutDuration = this.options.wait - timeSinceLastExecution
112
- this.isPending = true
113
- this.timeoutId = setTimeout(() => {
114
- if (this.lastArgs !== undefined) {
115
- this.execute(...this.lastArgs)
116
- }
117
- }, timeoutDuration)
118
- }
119
- }
120
- }
121
-
122
- private execute = (...args: Parameters<TFn>): void => {
123
- this.fn(...args)
124
- this.options.onExecute?.(args, this)
125
- this.lastExecutionTime = Date.now()
126
- this.clearTimeout()
127
- this.lastArgs = undefined
128
- this.isPending = false
129
- }
130
-
131
- /**
132
- * Processes the current pending execution immediately.
133
- * If there's a pending execution, it will be executed right away
134
- * and the timeout will be cleared.
135
- */
136
- flush = (): void => {
137
- if (this.isPending && this.lastArgs) {
138
- this.execute(...this.lastArgs)
139
- }
140
- }
141
-
142
- /**
143
- * Cancels any pending trailing execution and clears internal state.
144
- * If a trailing execution is scheduled, this will prevent that execution from occurring.
145
- */
146
- cancel = (): void => {
147
- this.clearTimeout()
148
- this.lastArgs = undefined
149
- this.isPending = false
150
- }
151
-
152
- private clearTimeout = (): void => {
153
- if (this.timeoutId) {
154
- clearTimeout(this.timeoutId)
155
- this.timeoutId = undefined
156
- }
157
- }
158
- }
159
-
160
- /**
161
- * Creates a lightweight throttled function that limits how often the provided function can execute.
162
- *
163
- * This is an alternative to the throttle function in the core @tanstack/pacer package, but is more
164
- * suitable for libraries and npm packages that need minimal overhead. Unlike the core version,
165
- * this function creates a throttler with no external dependencies, devtools integration, or reactive state.
166
- *
167
- * Throttling ensures a function executes at most once within a specified time window,
168
- * regardless of how many times it is called. This is useful for rate-limiting
169
- * expensive operations or UI updates.
170
- *
171
- * @example
172
- * ```ts
173
- * const throttledScroll = liteThrottle(() => {
174
- * updateScrollIndicator();
175
- * }, { wait: 100 });
176
- *
177
- * // Will execute at most once per 100ms
178
- * window.addEventListener('scroll', throttledScroll);
179
- * ```
180
- *
181
- * @example
182
- * ```ts
183
- * // Leading edge execution - fires immediately then throttles
184
- * const throttledResize = liteThrottle(() => {
185
- * recalculateLayout();
186
- * }, { wait: 250, leading: true, trailing: false });
187
- * ```
188
- */
189
- export function liteThrottle<TFn extends AnyFunction>(
190
- fn: TFn,
191
- options: LiteThrottlerOptions<TFn>,
192
- ): (...args: Parameters<TFn>) => void {
193
- const throttler = new LiteThrottler(fn, options)
194
- return throttler.maybeExecute
195
- }