@tanstack/pacer 0.7.0 → 0.9.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 (91) hide show
  1. package/dist/cjs/async-batcher.cjs +163 -0
  2. package/dist/cjs/async-batcher.cjs.map +1 -0
  3. package/dist/cjs/async-batcher.d.cts +273 -0
  4. package/dist/cjs/async-debouncer.cjs +149 -162
  5. package/dist/cjs/async-debouncer.cjs.map +1 -1
  6. package/dist/cjs/async-debouncer.d.cts +76 -57
  7. package/dist/cjs/async-queuer.cjs +282 -343
  8. package/dist/cjs/async-queuer.cjs.map +1 -1
  9. package/dist/cjs/async-queuer.d.cts +123 -102
  10. package/dist/cjs/async-rate-limiter.cjs +128 -185
  11. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  12. package/dist/cjs/async-rate-limiter.d.cts +72 -61
  13. package/dist/cjs/async-throttler.cjs +168 -178
  14. package/dist/cjs/async-throttler.cjs.map +1 -1
  15. package/dist/cjs/async-throttler.d.cts +97 -69
  16. package/dist/cjs/batcher.cjs +110 -119
  17. package/dist/cjs/batcher.cjs.map +1 -1
  18. package/dist/cjs/batcher.d.cts +76 -51
  19. package/dist/cjs/debouncer.cjs +97 -85
  20. package/dist/cjs/debouncer.cjs.map +1 -1
  21. package/dist/cjs/debouncer.d.cts +54 -26
  22. package/dist/cjs/index.cjs +3 -6
  23. package/dist/cjs/index.cjs.map +1 -1
  24. package/dist/cjs/index.d.cts +1 -1
  25. package/dist/cjs/queuer.cjs +246 -294
  26. package/dist/cjs/queuer.cjs.map +1 -1
  27. package/dist/cjs/queuer.d.cts +105 -84
  28. package/dist/cjs/rate-limiter.cjs +97 -130
  29. package/dist/cjs/rate-limiter.cjs.map +1 -1
  30. package/dist/cjs/rate-limiter.d.cts +50 -37
  31. package/dist/cjs/throttler.cjs +107 -123
  32. package/dist/cjs/throttler.cjs.map +1 -1
  33. package/dist/cjs/throttler.d.cts +59 -35
  34. package/dist/cjs/utils.cjs +0 -13
  35. package/dist/cjs/utils.cjs.map +1 -1
  36. package/dist/cjs/utils.d.cts +0 -1
  37. package/dist/esm/async-batcher.d.ts +273 -0
  38. package/dist/esm/async-batcher.js +163 -0
  39. package/dist/esm/async-batcher.js.map +1 -0
  40. package/dist/esm/async-debouncer.d.ts +76 -57
  41. package/dist/esm/async-debouncer.js +149 -162
  42. package/dist/esm/async-debouncer.js.map +1 -1
  43. package/dist/esm/async-queuer.d.ts +123 -102
  44. package/dist/esm/async-queuer.js +282 -343
  45. package/dist/esm/async-queuer.js.map +1 -1
  46. package/dist/esm/async-rate-limiter.d.ts +72 -61
  47. package/dist/esm/async-rate-limiter.js +128 -185
  48. package/dist/esm/async-rate-limiter.js.map +1 -1
  49. package/dist/esm/async-throttler.d.ts +97 -69
  50. package/dist/esm/async-throttler.js +168 -178
  51. package/dist/esm/async-throttler.js.map +1 -1
  52. package/dist/esm/batcher.d.ts +76 -51
  53. package/dist/esm/batcher.js +110 -119
  54. package/dist/esm/batcher.js.map +1 -1
  55. package/dist/esm/debouncer.d.ts +54 -26
  56. package/dist/esm/debouncer.js +97 -85
  57. package/dist/esm/debouncer.js.map +1 -1
  58. package/dist/esm/index.d.ts +1 -1
  59. package/dist/esm/index.js +4 -7
  60. package/dist/esm/queuer.d.ts +105 -84
  61. package/dist/esm/queuer.js +246 -294
  62. package/dist/esm/queuer.js.map +1 -1
  63. package/dist/esm/rate-limiter.d.ts +50 -37
  64. package/dist/esm/rate-limiter.js +97 -130
  65. package/dist/esm/rate-limiter.js.map +1 -1
  66. package/dist/esm/throttler.d.ts +59 -35
  67. package/dist/esm/throttler.js +107 -123
  68. package/dist/esm/throttler.js.map +1 -1
  69. package/dist/esm/utils.d.ts +0 -1
  70. package/dist/esm/utils.js +0 -13
  71. package/dist/esm/utils.js.map +1 -1
  72. package/package.json +14 -11
  73. package/src/async-batcher.ts +475 -0
  74. package/src/async-debouncer.ts +201 -121
  75. package/src/async-queuer.ts +341 -220
  76. package/src/async-rate-limiter.ts +176 -136
  77. package/src/async-throttler.ts +233 -139
  78. package/src/batcher.ts +159 -93
  79. package/src/debouncer.ts +135 -52
  80. package/src/index.ts +1 -1
  81. package/src/queuer.ts +349 -227
  82. package/src/rate-limiter.ts +125 -80
  83. package/src/throttler.ts +152 -78
  84. package/src/utils.ts +0 -15
  85. package/dist/cjs/compare.cjs +0 -72
  86. package/dist/cjs/compare.cjs.map +0 -1
  87. package/dist/cjs/compare.d.cts +0 -12
  88. package/dist/esm/compare.d.ts +0 -12
  89. package/dist/esm/compare.js +0 -72
  90. package/dist/esm/compare.js.map +0 -1
  91. package/src/compare.ts +0 -105
@@ -1,4 +1,31 @@
1
+ import { Store } from '@tanstack/store';
1
2
  import { AnyFunction } from './types.js';
3
+ export interface ThrottlerState<TFn extends AnyFunction> {
4
+ /**
5
+ * Number of function executions that have been completed
6
+ */
7
+ executionCount: number;
8
+ /**
9
+ * The arguments from the most recent call to maybeExecute
10
+ */
11
+ lastArgs: Parameters<TFn> | undefined;
12
+ /**
13
+ * Timestamp of the last function execution in milliseconds
14
+ */
15
+ lastExecutionTime: number;
16
+ /**
17
+ * Timestamp when the next execution can occur in milliseconds
18
+ */
19
+ nextExecutionTime: number;
20
+ /**
21
+ * Whether the throttler is waiting for the timeout to trigger execution
22
+ */
23
+ isPending: boolean;
24
+ /**
25
+ * Current execution status - 'idle' when not active, 'pending' when waiting for timeout
26
+ */
27
+ status: 'disabled' | 'idle' | 'pending';
28
+ }
2
29
  /**
3
30
  * Options for configuring a throttled function
4
31
  */
@@ -9,6 +36,10 @@ export interface ThrottlerOptions<TFn extends AnyFunction> {
9
36
  * Defaults to true.
10
37
  */
11
38
  enabled?: boolean | ((throttler: Throttler<TFn>) => boolean);
39
+ /**
40
+ * Initial state for the throttler
41
+ */
42
+ initialState?: Partial<ThrottlerState<TFn>>;
12
43
  /**
13
44
  * Whether to execute on the leading edge of the timeout.
14
45
  * Defaults to true.
@@ -43,6 +74,14 @@ export interface ThrottlerOptions<TFn extends AnyFunction> {
43
74
  *
44
75
  * For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.
45
76
  *
77
+ * State Management:
78
+ * - Uses TanStack Store for reactive state management
79
+ * - Use `initialState` to provide initial state values when creating the throttler
80
+ * - Use `onExecute` callback to react to function execution and implement custom logic
81
+ * - The state includes execution count, last execution time, pending status, and more
82
+ * - State can be accessed via `throttler.store.state` when using the class directly
83
+ * - When using framework adapters (React/Solid), state is accessed from `throttler.state`
84
+ *
46
85
  * @example
47
86
  * ```ts
48
87
  * const throttler = new Throttler(
@@ -58,29 +97,15 @@ export interface ThrottlerOptions<TFn extends AnyFunction> {
58
97
  * ```
59
98
  */
60
99
  export declare class Throttler<TFn extends AnyFunction> {
100
+ #private;
61
101
  private fn;
62
- private _executionCount;
63
- private _lastArgs;
64
- private _lastExecutionTime;
65
- private _options;
66
- private _timeoutId;
102
+ readonly store: Store<Readonly<ThrottlerState<TFn>>>;
103
+ options: ThrottlerOptions<TFn>;
67
104
  constructor(fn: TFn, initialOptions: ThrottlerOptions<TFn>);
68
105
  /**
69
106
  * Updates the throttler options
70
107
  */
71
- setOptions(newOptions: Partial<ThrottlerOptions<TFn>>): void;
72
- /**
73
- * Returns the current throttler options
74
- */
75
- getOptions(): Required<ThrottlerOptions<TFn>>;
76
- /**
77
- * Returns the current enabled state of the throttler
78
- */
79
- getEnabled(): boolean;
80
- /**
81
- * Returns the current wait time in milliseconds
82
- */
83
- getWait(): number;
108
+ setOptions: (newOptions: Partial<ThrottlerOptions<TFn>>) => void;
84
109
  /**
85
110
  * Attempts to execute the throttled function. The execution behavior depends on the throttler options:
86
111
  *
@@ -103,8 +128,11 @@ export declare class Throttler<TFn extends AnyFunction> {
103
128
  * throttled.maybeExecute('c', 'd');
104
129
  * ```
105
130
  */
106
- maybeExecute(...args: Parameters<TFn>): void;
107
- private execute;
131
+ maybeExecute: (...args: Parameters<TFn>) => void;
132
+ /**
133
+ * Processes the current pending execution immediately
134
+ */
135
+ flush: () => void;
108
136
  /**
109
137
  * Cancels any pending trailing execution and clears internal state.
110
138
  *
@@ -114,23 +142,11 @@ export declare class Throttler<TFn extends AnyFunction> {
114
142
  *
115
143
  * Has no effect if there is no pending execution.
116
144
  */
117
- cancel(): void;
145
+ cancel: () => void;
118
146
  /**
119
- * Returns the last execution time
147
+ * Resets the throttler state to its default values
120
148
  */
121
- getLastExecutionTime(): number;
122
- /**
123
- * Returns the next execution time
124
- */
125
- getNextExecutionTime(): number;
126
- /**
127
- * Returns the number of times the function has been executed
128
- */
129
- getExecutionCount(): number;
130
- /**
131
- * Returns `true` if there is a pending execution
132
- */
133
- getIsPending(): boolean;
149
+ reset: () => void;
134
150
  }
135
151
  /**
136
152
  * Creates a throttled function that limits how often the provided function can execute.
@@ -145,6 +161,14 @@ export declare class Throttler<TFn extends AnyFunction> {
145
161
  * For handling bursts of events, consider using debounce() instead. For hard execution
146
162
  * limits, consider using rateLimit().
147
163
  *
164
+ * State Management:
165
+ * - Uses TanStack Store for reactive state management
166
+ * - Use `initialState` to provide initial state values when creating the throttler
167
+ * - Use `onExecute` callback to react to function execution and implement custom logic
168
+ * - The state includes execution count, last execution time, pending status, and more
169
+ * - State can be accessed via the underlying Throttler instance's `store.state` property
170
+ * - When using framework adapters (React/Solid), state is accessed from the hook's state property
171
+ *
148
172
  * @example
149
173
  * ```ts
150
174
  * // Basic throttling - max once per second
@@ -1,143 +1,127 @@
1
+ import { Store } from "@tanstack/store";
1
2
  import { parseFunctionOrValue } from "./utils.js";
3
+ function getDefaultThrottlerState() {
4
+ return structuredClone({
5
+ executionCount: 0,
6
+ isPending: false,
7
+ lastArgs: void 0,
8
+ lastExecutionTime: 0,
9
+ nextExecutionTime: 0,
10
+ status: "idle"
11
+ });
12
+ }
2
13
  const defaultOptions = {
3
14
  enabled: true,
4
15
  leading: true,
5
- onExecute: () => {
6
- },
7
16
  trailing: true,
8
17
  wait: 0
9
18
  };
10
19
  class Throttler {
11
20
  constructor(fn, initialOptions) {
12
21
  this.fn = fn;
13
- this._executionCount = 0;
14
- this._lastExecutionTime = 0;
15
- this._options = {
22
+ this.store = new Store(
23
+ getDefaultThrottlerState()
24
+ );
25
+ this.setOptions = (newOptions) => {
26
+ this.options = { ...this.options, ...newOptions };
27
+ if (!this.#getEnabled()) {
28
+ this.cancel();
29
+ }
30
+ };
31
+ this.#setState = (newState) => {
32
+ this.store.setState((state) => {
33
+ const combinedState = {
34
+ ...state,
35
+ ...newState
36
+ };
37
+ const { isPending } = combinedState;
38
+ return {
39
+ ...combinedState,
40
+ status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : "idle"
41
+ };
42
+ });
43
+ };
44
+ this.#getEnabled = () => {
45
+ return !!parseFunctionOrValue(this.options.enabled, this);
46
+ };
47
+ this.#getWait = () => {
48
+ return parseFunctionOrValue(this.options.wait, this);
49
+ };
50
+ this.maybeExecute = (...args) => {
51
+ const now = Date.now();
52
+ const timeSinceLastExecution = now - this.store.state.lastExecutionTime;
53
+ const wait = this.#getWait();
54
+ if (this.options.leading && timeSinceLastExecution >= wait) {
55
+ this.#execute(...args);
56
+ } else {
57
+ this.#setState({
58
+ lastArgs: args
59
+ });
60
+ if (!this.#timeoutId && this.options.trailing) {
61
+ const _timeSinceLastExecution = this.store.state.lastExecutionTime ? now - this.store.state.lastExecutionTime : 0;
62
+ const timeoutDuration = wait - _timeSinceLastExecution;
63
+ this.#setState({ isPending: true });
64
+ this.#timeoutId = setTimeout(() => {
65
+ const { lastArgs } = this.store.state;
66
+ if (lastArgs !== void 0) {
67
+ this.#execute(...lastArgs);
68
+ }
69
+ }, timeoutDuration);
70
+ }
71
+ }
72
+ };
73
+ this.#execute = (...args) => {
74
+ if (!this.#getEnabled()) return;
75
+ this.fn(...args);
76
+ const lastExecutionTime = Date.now();
77
+ const nextExecutionTime = lastExecutionTime + this.#getWait();
78
+ this.#clearTimeout();
79
+ this.#setState({
80
+ executionCount: this.store.state.executionCount + 1,
81
+ lastExecutionTime,
82
+ nextExecutionTime,
83
+ isPending: false,
84
+ lastArgs: void 0
85
+ });
86
+ this.options.onExecute?.(this);
87
+ };
88
+ this.flush = () => {
89
+ if (this.store.state.isPending && this.store.state.lastArgs) {
90
+ this.#execute(...this.store.state.lastArgs);
91
+ }
92
+ };
93
+ this.#clearTimeout = () => {
94
+ if (this.#timeoutId) {
95
+ clearTimeout(this.#timeoutId);
96
+ this.#timeoutId = void 0;
97
+ }
98
+ };
99
+ this.cancel = () => {
100
+ this.#clearTimeout();
101
+ this.#setState({
102
+ lastArgs: void 0,
103
+ isPending: false
104
+ });
105
+ };
106
+ this.reset = () => {
107
+ this.#setState(getDefaultThrottlerState());
108
+ };
109
+ this.options = {
16
110
  ...defaultOptions,
17
111
  ...initialOptions
18
112
  };
113
+ this.#setState(this.options.initialState ?? {});
19
114
  }
20
- /**
21
- * Updates the throttler options
22
- */
23
- setOptions(newOptions) {
24
- this._options = { ...this._options, ...newOptions };
25
- if (!this._options.enabled) {
26
- this.cancel();
27
- }
28
- }
29
- /**
30
- * Returns the current throttler options
31
- */
32
- getOptions() {
33
- return this._options;
34
- }
35
- /**
36
- * Returns the current enabled state of the throttler
37
- */
38
- getEnabled() {
39
- return parseFunctionOrValue(this._options.enabled, this);
40
- }
41
- /**
42
- * Returns the current wait time in milliseconds
43
- */
44
- getWait() {
45
- return parseFunctionOrValue(this._options.wait, this);
46
- }
47
- /**
48
- * Attempts to execute the throttled function. The execution behavior depends on the throttler options:
49
- *
50
- * - If enough time has passed since the last execution (>= wait period):
51
- * - With leading=true: Executes immediately
52
- * - With leading=false: Waits for the next trailing execution
53
- *
54
- * - If within the wait period:
55
- * - With trailing=true: Schedules execution for end of wait period
56
- * - With trailing=false: Drops the execution
57
- *
58
- * @example
59
- * ```ts
60
- * const throttled = new Throttler(fn, { wait: 1000 });
61
- *
62
- * // First call executes immediately
63
- * throttled.maybeExecute('a', 'b');
64
- *
65
- * // Call during wait period - gets throttled
66
- * throttled.maybeExecute('c', 'd');
67
- * ```
68
- */
69
- maybeExecute(...args) {
70
- const now = Date.now();
71
- const timeSinceLastExecution = now - this._lastExecutionTime;
72
- const wait = this.getWait();
73
- if (this._options.leading && timeSinceLastExecution >= wait) {
74
- this.execute(...args);
75
- } else {
76
- this._lastArgs = args;
77
- if (!this._timeoutId && this._options.trailing) {
78
- const _timeSinceLastExecution = this._lastExecutionTime ? now - this._lastExecutionTime : 0;
79
- const timeoutDuration = wait - _timeSinceLastExecution;
80
- this._timeoutId = setTimeout(() => {
81
- if (this._lastArgs !== void 0) {
82
- this.execute(...this._lastArgs);
83
- }
84
- }, timeoutDuration);
85
- }
86
- }
87
- }
88
- execute(...args) {
89
- if (!this.getEnabled()) return;
90
- this.fn(...args);
91
- this._executionCount++;
92
- this._lastExecutionTime = Date.now();
93
- this._timeoutId = void 0;
94
- this._lastArgs = void 0;
95
- this._options.onExecute(this);
96
- }
97
- /**
98
- * Cancels any pending trailing execution and clears internal state.
99
- *
100
- * If a trailing execution is scheduled (due to throttling with trailing=true),
101
- * this will prevent that execution from occurring. The internal timeout and
102
- * stored arguments will be cleared.
103
- *
104
- * Has no effect if there is no pending execution.
105
- */
106
- cancel() {
107
- if (this._timeoutId) {
108
- clearTimeout(this._timeoutId);
109
- this._timeoutId = void 0;
110
- this._lastArgs = void 0;
111
- }
112
- }
113
- /**
114
- * Returns the last execution time
115
- */
116
- getLastExecutionTime() {
117
- return this._lastExecutionTime;
118
- }
119
- /**
120
- * Returns the next execution time
121
- */
122
- getNextExecutionTime() {
123
- return this._lastExecutionTime + this.getWait();
124
- }
125
- /**
126
- * Returns the number of times the function has been executed
127
- */
128
- getExecutionCount() {
129
- return this._executionCount;
130
- }
131
- /**
132
- * Returns `true` if there is a pending execution
133
- */
134
- getIsPending() {
135
- return this.getEnabled() && !!this._timeoutId;
136
- }
115
+ #timeoutId;
116
+ #setState;
117
+ #getEnabled;
118
+ #getWait;
119
+ #execute;
120
+ #clearTimeout;
137
121
  }
138
122
  function throttle(fn, initialOptions) {
139
123
  const throttler = new Throttler(fn, initialOptions);
140
- return throttler.maybeExecute.bind(throttler);
124
+ return throttler.maybeExecute;
141
125
  }
142
126
  export {
143
127
  Throttler,
@@ -1 +1 @@
1
- {"version":3,"file":"throttler.js","sources":["../../src/throttler.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyFunction } from './types'\n\n/**\n * Options for configuring a throttled function\n */\nexport interface ThrottlerOptions<TFn extends AnyFunction> {\n /**\n * Whether the throttler is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((throttler: Throttler<TFn>) => boolean)\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to true.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (throttler: Throttler<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((throttler: Throttler<TFn>) => number)\n}\n\nconst defaultOptions: Required<ThrottlerOptions<any>> = {\n enabled: true,\n leading: true,\n onExecute: () => {},\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a throttled function.\n *\n * Throttling ensures a function is called at most once within a specified time window.\n * Unlike debouncing which waits for a pause in calls, throttling guarantees consistent\n * execution timing regardless of call frequency.\n *\n * Supports both leading and trailing edge execution:\n * - Leading: Execute immediately on first call (default: true)\n * - Trailing: Execute after wait period if called during throttle (default: true)\n *\n * For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.\n *\n * @example\n * ```ts\n * const throttler = new Throttler(\n * (id: string) => api.getData(id),\n * { wait: 1000 } // Execute at most once per second\n * );\n *\n * // First call executes immediately\n * throttler.maybeExecute('123');\n *\n * // Subsequent calls within 1000ms are throttled\n * throttler.maybeExecute('123'); // Throttled\n * ```\n */\nexport class Throttler<TFn extends AnyFunction> {\n private _executionCount = 0\n private _lastArgs: Parameters<TFn> | undefined\n private _lastExecutionTime = 0\n private _options: Required<ThrottlerOptions<TFn>>\n private _timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n private fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the throttler options\n */\n setOptions(newOptions: Partial<ThrottlerOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n\n // End the pending state if the debouncer is disabled\n if (!this._options.enabled) {\n this.cancel()\n }\n }\n\n /**\n * Returns the current throttler options\n */\n getOptions(): Required<ThrottlerOptions<TFn>> {\n return this._options\n }\n\n /**\n * Returns the current enabled state of the throttler\n */\n getEnabled(): boolean {\n return parseFunctionOrValue(this._options.enabled, this)\n }\n\n /**\n * Returns the current wait time in milliseconds\n */\n getWait(): number {\n return parseFunctionOrValue(this._options.wait, this)\n }\n\n /**\n * Attempts to execute the throttled function. The execution behavior depends on the throttler options:\n *\n * - If enough time has passed since the last execution (>= wait period):\n * - With leading=true: Executes immediately\n * - With leading=false: Waits for the next trailing execution\n *\n * - If within the wait period:\n * - With trailing=true: Schedules execution for end of wait period\n * - With trailing=false: Drops the execution\n *\n * @example\n * ```ts\n * const throttled = new Throttler(fn, { wait: 1000 });\n *\n * // First call executes immediately\n * throttled.maybeExecute('a', 'b');\n *\n * // Call during wait period - gets throttled\n * throttled.maybeExecute('c', 'd');\n * ```\n */\n maybeExecute(...args: Parameters<TFn>): void {\n const now = Date.now()\n const timeSinceLastExecution = now - this._lastExecutionTime\n const wait = this.getWait()\n\n // Handle leading execution\n if (this._options.leading && timeSinceLastExecution >= 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 _timeSinceLastExecution = this._lastExecutionTime\n ? now - this._lastExecutionTime\n : 0\n const timeoutDuration = wait - _timeSinceLastExecution\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 if (!this.getEnabled()) return\n this.fn(...args) // EXECUTE!\n this._executionCount++\n this._lastExecutionTime = Date.now()\n this._timeoutId = undefined\n this._lastArgs = undefined\n this._options.onExecute(this)\n }\n\n /**\n * Cancels any pending trailing execution and clears internal state.\n *\n * If a trailing execution is scheduled (due to throttling with trailing=true),\n * this will prevent that execution from occurring. The internal timeout and\n * stored arguments will be cleared.\n *\n * Has no effect if there is no pending execution.\n */\n cancel(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n this._timeoutId = undefined\n this._lastArgs = undefined\n }\n }\n\n /**\n * Returns the last execution time\n */\n getLastExecutionTime(): number {\n return this._lastExecutionTime\n }\n\n /**\n * Returns the next execution time\n */\n getNextExecutionTime(): number {\n return this._lastExecutionTime + this.getWait()\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns `true` if there is a pending execution\n */\n getIsPending(): boolean {\n return this.getEnabled() && !!this._timeoutId\n }\n}\n\n/**\n * Creates a throttled function that limits how often the provided function can execute.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * The throttled function can be configured to execute on the leading and/or trailing\n * edge of the throttle window via options.\n *\n * For handling bursts of events, consider using debounce() instead. For hard execution\n * limits, consider using rateLimit().\n *\n * @example\n * ```ts\n * // Basic throttling - max once per second\n * const throttled = throttle(updateUI, { wait: 1000 });\n *\n * // Configure leading/trailing execution\n * const throttled = throttle(saveData, {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: true // Execute again after delay if called during wait\n * });\n * ```\n */\nexport function throttle<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n) {\n const throttler = new Throttler(fn, initialOptions)\n return throttler.ma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KAAK,qBACjC,MAAM,KAAK,qBACX;AACJ,cAAM,kBAAkB,OAAO;AAC1B,aAAA,aAAa,WAAW,MAAM;AAC7B,cAAA,KAAK,cAAc,QAAW;AAC3B,iBAAA,QAAQ,GAAG,KAAK,SAAS;AAAA,UAAA;AAAA,WAE/B,eAAe;AAAA,MAAA;AAAA,IACpB;AAAA,EACF;AAAA,EAGM,WAAW,MAA6B;AAC1C,QAAA,CAAC,KAAK,aAAc;AACnB,SAAA,GAAG,GAAG,IAAI;AACV,SAAA;AACA,SAAA,qBAAqB,KAAK,IAAI;AACnC,SAAK,aAAa;AAClB,SAAK,YAAY;AACZ,SAAA,SAAS,UAAU,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,SAAe;AACb,QAAI,KAAK,YAAY;AACnB,mBAAa,KAAK,UAAU;AAC5B,WAAK,aAAa;AAClB,WAAK,YAAY;AAAA,IAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA,EAMF,uBAA+B;AAC7B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,uBAA+B;AACtB,WAAA,KAAK,qBAAqB,KAAK,QAAQ;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMhD,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WAAO,KAAK,WAAA,KAAgB,CAAC,CAAC,KAAK;AAAA,EAAA;AAEvC;AA4BgB,SAAA,SACd,IACA,gBACA;AACA,QAAM,YAAY,IAAI,UAAU,IAAI,cAAc;AAC3C,SAAA,UAAU,aAAa,KAAK,SAAS;AAC9C;"}
1
+ {"version":3,"file":"throttler.js","sources":["../../src/throttler.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { parseFunctionOrValue } from './utils'\nimport type { AnyFunction } from './types'\n\nexport interface ThrottlerState<TFn extends AnyFunction> {\n /**\n * Number of function executions that have been completed\n */\n executionCount: number\n /**\n * The arguments from the most recent call to maybeExecute\n */\n lastArgs: Parameters<TFn> | undefined\n /**\n * Timestamp of the last function execution in milliseconds\n */\n lastExecutionTime: number\n /**\n * Timestamp when the next execution can occur in milliseconds\n */\n nextExecutionTime: number\n /**\n * Whether the throttler is waiting for the timeout to trigger execution\n */\n isPending: boolean\n /**\n * Current execution status - 'idle' when not active, 'pending' when waiting for timeout\n */\n status: 'disabled' | 'idle' | 'pending'\n}\n\nfunction getDefaultThrottlerState<\n TFn extends AnyFunction,\n>(): ThrottlerState<TFn> {\n return structuredClone({\n executionCount: 0,\n isPending: false,\n lastArgs: undefined,\n lastExecutionTime: 0,\n nextExecutionTime: 0,\n status: 'idle',\n })\n}\n\n/**\n * Options for configuring a throttled function\n */\nexport interface ThrottlerOptions<TFn extends AnyFunction> {\n /**\n * Whether the throttler is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((throttler: Throttler<TFn>) => boolean)\n /**\n * Initial state for the throttler\n */\n initialState?: Partial<ThrottlerState<TFn>>\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to true.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (throttler: Throttler<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Time window in milliseconds during which the function can only be executed once.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((throttler: Throttler<TFn>) => number)\n}\n\nconst defaultOptions: Omit<\n Required<ThrottlerOptions<any>>,\n 'initialState' | 'onExecute'\n> = {\n enabled: true,\n leading: true,\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a throttled function.\n *\n * Throttling ensures a function is called at most once within a specified time window.\n * Unlike debouncing which waits for a pause in calls, throttling guarantees consistent\n * execution timing regardless of call frequency.\n *\n * Supports both leading and trailing edge execution:\n * - Leading: Execute immediately on first call (default: true)\n * - Trailing: Execute after wait period if called during throttle (default: true)\n *\n * For collapsing rapid-fire events where you only care about the last call, consider using Debouncer.\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the throttler\n * - Use `onExecute` callback to react to function execution and implement custom logic\n * - The state includes execution count, last execution time, pending status, and more\n * - State can be accessed via `throttler.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `throttler.state`\n *\n * @example\n * ```ts\n * const throttler = new Throttler(\n * (id: string) => api.getData(id),\n * { wait: 1000 } // Execute at most once per second\n * );\n *\n * // First call executes immediately\n * throttler.maybeExecute('123');\n *\n * // Subsequent calls within 1000ms are throttled\n * throttler.maybeExecute('123'); // Throttled\n * ```\n */\nexport class Throttler<TFn extends AnyFunction> {\n readonly store: Store<Readonly<ThrottlerState<TFn>>> = new Store(\n getDefaultThrottlerState(),\n )\n options: ThrottlerOptions<TFn>\n #timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n private fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n ) {\n this.options = {\n ...defaultOptions,\n ...initialOptions,\n }\n this.#setState(this.options.initialState ?? {})\n }\n\n /**\n * Updates the throttler options\n */\n setOptions = (newOptions: Partial<ThrottlerOptions<TFn>>): void => {\n this.options = { ...this.options, ...newOptions }\n\n // Cancel pending execution if the throttler is disabled\n if (!this.#getEnabled()) {\n this.cancel()\n }\n }\n\n #setState = (newState: Partial<ThrottlerState<TFn>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isPending } = combinedState\n return {\n ...combinedState,\n status: !this.#getEnabled()\n ? 'disabled'\n : isPending\n ? 'pending'\n : 'idle',\n }\n })\n }\n\n #getEnabled = (): boolean => {\n return !!parseFunctionOrValue(this.options.enabled, this)\n }\n\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Attempts to execute the throttled function. The execution behavior depends on the throttler options:\n *\n * - If enough time has passed since the last execution (>= wait period):\n * - With leading=true: Executes immediately\n * - With leading=false: Waits for the next trailing execution\n *\n * - If within the wait period:\n * - With trailing=true: Schedules execution for end of wait period\n * - With trailing=false: Drops the execution\n *\n * @example\n * ```ts\n * const throttled = new Throttler(fn, { wait: 1000 });\n *\n * // First call executes immediately\n * throttled.maybeExecute('a', 'b');\n *\n * // Call during wait period - gets throttled\n * throttled.maybeExecute('c', 'd');\n * ```\n */\n maybeExecute = (...args: Parameters<TFn>): void => {\n const now = Date.now()\n const timeSinceLastExecution = now - this.store.state.lastExecutionTime\n const wait = this.#getWait()\n\n // Handle leading execution\n if (this.options.leading && timeSinceLastExecution >= wait) {\n this.#execute(...args)\n } else {\n // Store the most recent arguments for potential trailing execution\n this.#setState({\n lastArgs: args,\n })\n // Set up trailing execution if not already scheduled\n if (!this.#timeoutId && this.options.trailing) {\n // prevent large number if lastExecutionTime is undefined\n const _timeSinceLastExecution = this.store.state.lastExecutionTime\n ? now - this.store.state.lastExecutionTime\n : 0\n const timeoutDuration = wait - _timeSinceLastExecution\n this.#setState({ isPending: true })\n this.#timeoutId = setTimeout(() => {\n const { lastArgs } = this.store.state\n if (lastArgs !== undefined) {\n this.#execute(...lastArgs)\n }\n }, timeoutDuration)\n }\n }\n }\n\n #execute = (...args: Parameters<TFn>): void => {\n if (!this.#getEnabled()) return\n this.fn(...args) // EXECUTE!\n const lastExecutionTime = Date.now()\n const nextExecutionTime = lastExecutionTime + this.#getWait()\n this.#clearTimeout()\n this.#setState({\n executionCount: this.store.state.executionCount + 1,\n lastExecutionTime,\n nextExecutionTime,\n isPending: false,\n lastArgs: undefined,\n })\n this.options.onExecute?.(this)\n }\n\n /**\n * Processes the current pending execution immediately\n */\n flush = (): void => {\n if (this.store.state.isPending && this.store.state.lastArgs) {\n this.#execute(...this.store.state.lastArgs)\n }\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = undefined\n }\n }\n\n /**\n * Cancels any pending trailing execution and clears internal state.\n *\n * If a trailing execution is scheduled (due to throttling with trailing=true),\n * this will prevent that execution from occurring. The internal timeout and\n * stored arguments will be cleared.\n *\n * Has no effect if there is no pending execution.\n */\n cancel = (): void => {\n this.#clearTimeout()\n this.#setState({\n lastArgs: undefined,\n isPending: false,\n })\n }\n\n /**\n * Resets the throttler state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultThrottlerState<TFn>())\n }\n}\n\n/**\n * Creates a throttled function that limits how often the provided function can execute.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * The throttled function can be configured to execute on the leading and/or trailing\n * edge of the throttle window via options.\n *\n * For handling bursts of events, consider using debounce() instead. For hard execution\n * limits, consider using rateLimit().\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the throttler\n * - Use `onExecute` callback to react to function execution and implement custom logic\n * - The state includes execution count, last execution time, pending status, and more\n * - State can be accessed via the underlying Throttler instance's `store.state` property\n * - When using framework adapters (React/Solid), state is accessed from the hook's state property\n *\n * @example\n * ```ts\n * // Basic throttling - max once per second\n * const throttled = throttle(updateUI, { wait: 1000 });\n *\n * // Configure leading/trailing execution\n * const throttled = throttle(saveData, {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: true // Execute again after delay if called during wait\n * });\n * ```\n */\nexport function throttle<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n) {\n const throttler = new Throttler(fn, initialOptions)\n return throttler.maybeExecute\n}\n"],"names":[],"mappings":";;AA+BA,SAAS,2BAEgB;AACvB,SAAO,gBAAgB;AAAA,IACrB,gBAAgB;AAAA,IAChB,WAAW;AAAA,IACX,UAAU;AAAA,IACV,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,QAAQ;AAAA,EAAA,CACT;AACH;AAsCA,MAAM,iBAGF;AAAA,EAC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A,EAVhD;AAAA,EAyBA;AAAA,EAkBA;AAAA,EAIA;AAAA,EAyDA;AAAA,EAyBA;AA8BF;AAoCO,SAAS,SACd,IACA,gBACA;AACA,QAAM,YAAY,IAAI,UAAU,IAAI,cAAc;AAClD,SAAO,UAAU;AACnB;"}
@@ -1,4 +1,3 @@
1
1
  import { AnyFunction } from './types.js';
2
2
  export declare function isFunction<T extends AnyFunction>(value: any): value is T;
3
3
  export declare function parseFunctionOrValue<T, TArgs extends Array<any>>(value: T | ((...args: TArgs) => T), ...args: TArgs): T;
4
- export declare function bindInstanceMethods<T extends Record<string, any>>(instance: T): T;
package/dist/esm/utils.js CHANGED
@@ -4,20 +4,7 @@ function isFunction(value) {
4
4
  function parseFunctionOrValue(value, ...args) {
5
5
  return isFunction(value) ? value(...args) : value;
6
6
  }
7
- function bindInstanceMethods(instance) {
8
- return Object.getOwnPropertyNames(Object.getPrototypeOf(instance)).reduce(
9
- (acc, key) => {
10
- const method = instance[key];
11
- if (isFunction(method)) {
12
- acc[key] = method.bind(instance);
13
- }
14
- return acc;
15
- },
16
- instance
17
- );
18
- }
19
7
  export {
20
- bindInstanceMethods,
21
8
  isFunction,
22
9
  parseFunctionOrValue
23
10
  };
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type { AnyFunction } from './types'\n\nexport function isFunction<T extends AnyFunction>(value: any): value is T {\n return typeof value === 'function'\n}\n\nexport function parseFunctionOrValue<T, TArgs extends Array<any>>(\n value: T | ((...args: TArgs) => T),\n ...args: TArgs\n): T {\n return isFunction(value) ? value(...args) : value\n}\n\nexport function bindInstanceMethods<T extends Record<string, any>>(\n instance: T,\n): T {\n return Object.getOwnPropertyNames(Object.getPrototypeOf(instance)).reduce(\n (acc: any, key) => {\n const method = instance[key as keyof T]\n if (isFunction(method)) {\n acc[key] = method.bind(instance)\n }\n return acc\n },\n instance,\n )\n}\n"],"names":[],"mappings":"AAEO,SAAS,WAAkC,OAAwB;AACxE,SAAO,OAAO,UAAU;AAC1B;AAEgB,SAAA,qBACd,UACG,MACA;AACH,SAAO,WAAW,KAAK,IAAI,MAAM,GAAG,IAAI,IAAI;AAC9C;AAEO,SAAS,oBACd,UACG;AACH,SAAO,OAAO,oBAAoB,OAAO,eAAe,QAAQ,CAAC,EAAE;AAAA,IACjE,CAAC,KAAU,QAAQ;AACX,YAAA,SAAS,SAAS,GAAc;AAClC,UAAA,WAAW,MAAM,GAAG;AACtB,YAAI,GAAG,IAAI,OAAO,KAAK,QAAQ;AAAA,MAAA;AAE1B,aAAA;AAAA,IACT;AAAA,IACA;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type { AnyFunction } from './types'\n\nexport function isFunction<T extends AnyFunction>(value: any): value is T {\n return typeof value === 'function'\n}\n\nexport function parseFunctionOrValue<T, TArgs extends Array<any>>(\n value: T | ((...args: TArgs) => T),\n ...args: TArgs\n): T {\n return isFunction(value) ? value(...args) : value\n}\n"],"names":[],"mappings":"AAEO,SAAS,WAAkC,OAAwB;AACxE,SAAO,OAAO,UAAU;AAC1B;AAEO,SAAS,qBACd,UACG,MACA;AACH,SAAO,WAAW,KAAK,IAAI,MAAM,GAAG,IAAI,IAAI;AAC9C;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/pacer",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Utilities for debouncing, throttling, rate-limiting, queuing, and more.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -37,6 +37,16 @@
37
37
  "default": "./dist/cjs/index.cjs"
38
38
  }
39
39
  },
40
+ "./async-batcher": {
41
+ "import": {
42
+ "types": "./dist/esm/async-batcher.d.ts",
43
+ "default": "./dist/esm/async-batcher.js"
44
+ },
45
+ "require": {
46
+ "types": "./dist/cjs/async-batcher.d.cts",
47
+ "default": "./dist/cjs/async-batcher.cjs"
48
+ }
49
+ },
40
50
  "./async-debouncer": {
41
51
  "import": {
42
52
  "types": "./dist/esm/async-debouncer.d.ts",
@@ -87,16 +97,6 @@
87
97
  "default": "./dist/cjs/batcher.cjs"
88
98
  }
89
99
  },
90
- "./compare": {
91
- "import": {
92
- "types": "./dist/esm/compare.d.ts",
93
- "default": "./dist/esm/compare.js"
94
- },
95
- "require": {
96
- "types": "./dist/cjs/compare.d.cts",
97
- "default": "./dist/cjs/compare.cjs"
98
- }
99
- },
100
100
  "./debouncer": {
101
101
  "import": {
102
102
  "types": "./dist/esm/debouncer.d.ts",
@@ -160,6 +160,9 @@
160
160
  "dist/",
161
161
  "src"
162
162
  ],
163
+ "dependencies": {
164
+ "@tanstack/store": "^0.7.2"
165
+ },
163
166
  "scripts": {
164
167
  "clean": "premove ./build ./dist",
165
168
  "lint:fix": "eslint ./src --fix",