@tanstack/pacer 0.22.0 → 0.23.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 (100) hide show
  1. package/README.md +4 -4
  2. package/dist/async-batcher.d.ts +11 -13
  3. package/dist/async-batcher.js +185 -131
  4. package/dist/async-debouncer.d.ts +6 -8
  5. package/dist/async-debouncer.js +186 -142
  6. package/dist/async-queuer.d.ts +11 -13
  7. package/dist/async-queuer.js +383 -290
  8. package/dist/async-rate-limiter.d.ts +6 -8
  9. package/dist/async-rate-limiter.js +197 -143
  10. package/dist/async-retryer.d.ts +6 -8
  11. package/dist/async-retryer.js +209 -179
  12. package/dist/async-throttler.d.ts +6 -8
  13. package/dist/async-throttler.js +214 -157
  14. package/dist/batcher.d.ts +5 -7
  15. package/dist/batcher.js +107 -87
  16. package/dist/debouncer.d.ts +6 -8
  17. package/dist/debouncer.js +99 -86
  18. package/dist/event-client.d.ts +8 -10
  19. package/dist/event-client.js +1 -2
  20. package/dist/queuer.d.ts +7 -9
  21. package/dist/queuer.js +277 -212
  22. package/dist/rate-limiter.d.ts +6 -8
  23. package/dist/rate-limiter.js +127 -109
  24. package/dist/throttler.d.ts +6 -8
  25. package/dist/throttler.js +127 -90
  26. package/dist/types.d.ts +4 -6
  27. package/dist/utils.d.ts +3 -5
  28. package/dist/utils.js +1 -2
  29. package/package.json +21 -68
  30. package/dist/async-batcher.cjs +0 -337
  31. package/dist/async-batcher.cjs.map +0 -1
  32. package/dist/async-batcher.d.cts +0 -343
  33. package/dist/async-batcher.js.map +0 -1
  34. package/dist/async-debouncer.cjs +0 -327
  35. package/dist/async-debouncer.cjs.map +0 -1
  36. package/dist/async-debouncer.d.cts +0 -299
  37. package/dist/async-debouncer.js.map +0 -1
  38. package/dist/async-queuer.cjs +0 -516
  39. package/dist/async-queuer.cjs.map +0 -1
  40. package/dist/async-queuer.d.cts +0 -440
  41. package/dist/async-queuer.js.map +0 -1
  42. package/dist/async-rate-limiter.cjs +0 -370
  43. package/dist/async-rate-limiter.cjs.map +0 -1
  44. package/dist/async-rate-limiter.d.cts +0 -356
  45. package/dist/async-rate-limiter.js.map +0 -1
  46. package/dist/async-retryer.cjs +0 -365
  47. package/dist/async-retryer.cjs.map +0 -1
  48. package/dist/async-retryer.d.cts +0 -321
  49. package/dist/async-retryer.js.map +0 -1
  50. package/dist/async-throttler.cjs +0 -344
  51. package/dist/async-throttler.cjs.map +0 -1
  52. package/dist/async-throttler.d.cts +0 -319
  53. package/dist/async-throttler.js.map +0 -1
  54. package/dist/batcher.cjs +0 -200
  55. package/dist/batcher.cjs.map +0 -1
  56. package/dist/batcher.d.cts +0 -179
  57. package/dist/batcher.js.map +0 -1
  58. package/dist/debouncer.cjs +0 -203
  59. package/dist/debouncer.cjs.map +0 -1
  60. package/dist/debouncer.d.cts +0 -166
  61. package/dist/debouncer.js.map +0 -1
  62. package/dist/event-client.cjs +0 -64
  63. package/dist/event-client.cjs.map +0 -1
  64. package/dist/event-client.d.cts +0 -65
  65. package/dist/event-client.js.map +0 -1
  66. package/dist/index.cjs +0 -52
  67. package/dist/index.d.cts +0 -15
  68. package/dist/queuer.cjs +0 -406
  69. package/dist/queuer.cjs.map +0 -1
  70. package/dist/queuer.d.cts +0 -345
  71. package/dist/queuer.js.map +0 -1
  72. package/dist/rate-limiter.cjs +0 -263
  73. package/dist/rate-limiter.cjs.map +0 -1
  74. package/dist/rate-limiter.d.cts +0 -214
  75. package/dist/rate-limiter.js.map +0 -1
  76. package/dist/throttler.cjs +0 -215
  77. package/dist/throttler.cjs.map +0 -1
  78. package/dist/throttler.d.cts +0 -206
  79. package/dist/throttler.js.map +0 -1
  80. package/dist/types.cjs +0 -0
  81. package/dist/types.d.cts +0 -13
  82. package/dist/utils.cjs +0 -13
  83. package/dist/utils.cjs.map +0 -1
  84. package/dist/utils.d.cts +0 -7
  85. package/dist/utils.js.map +0 -1
  86. package/src/async-batcher.ts +0 -594
  87. package/src/async-debouncer.ts +0 -566
  88. package/src/async-queuer.ts +0 -988
  89. package/src/async-rate-limiter.ts +0 -648
  90. package/src/async-retryer.ts +0 -673
  91. package/src/async-throttler.ts +0 -634
  92. package/src/batcher.ts +0 -329
  93. package/src/debouncer.ts +0 -334
  94. package/src/event-client.ts +0 -129
  95. package/src/index.ts +0 -24
  96. package/src/queuer.ts +0 -751
  97. package/src/rate-limiter.ts +0 -429
  98. package/src/throttler.ts +0 -380
  99. package/src/types.ts +0 -12
  100. package/src/utils.ts +0 -12
package/dist/debouncer.js CHANGED
@@ -58,88 +58,13 @@ const defaultOptions = {
58
58
  * ```
59
59
  */
60
60
  var Debouncer = class {
61
+ fn;
62
+ store = new Store(getDefaultDebouncerState());
63
+ key;
64
+ options;
61
65
  #timeoutId;
62
66
  constructor(fn, initialOptions) {
63
67
  this.fn = fn;
64
- this.store = new Store(getDefaultDebouncerState());
65
- this.setOptions = (newOptions) => {
66
- this.options = {
67
- ...this.options,
68
- ...newOptions
69
- };
70
- if (!this.#getEnabled()) this.cancel();
71
- };
72
- this.#setState = (newState) => {
73
- this.store.setState((state) => {
74
- const combinedState = {
75
- ...state,
76
- ...newState
77
- };
78
- const { isPending } = combinedState;
79
- return {
80
- ...combinedState,
81
- status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : "idle"
82
- };
83
- });
84
- emitChange("Debouncer", this);
85
- };
86
- this.#getEnabled = () => {
87
- return !!parseFunctionOrValue(this.options.enabled, this);
88
- };
89
- this.#getWait = () => {
90
- return parseFunctionOrValue(this.options.wait, this);
91
- };
92
- this.maybeExecute = (...args) => {
93
- if (!this.#getEnabled()) return void 0;
94
- this.#setState({ maybeExecuteCount: this.store.state.maybeExecuteCount + 1 });
95
- let _didLeadingExecute = false;
96
- if (this.options.leading && this.store.state.canLeadingExecute) {
97
- this.#setState({ canLeadingExecute: false });
98
- _didLeadingExecute = true;
99
- this.#execute(...args);
100
- }
101
- if (this.options.trailing) this.#setState({
102
- isPending: true,
103
- lastArgs: args
104
- });
105
- if (this.#timeoutId) clearTimeout(this.#timeoutId);
106
- this.#timeoutId = setTimeout(() => {
107
- this.#setState({ canLeadingExecute: true });
108
- if (this.options.trailing && !_didLeadingExecute) this.#execute(...args);
109
- }, this.#getWait());
110
- };
111
- this.#execute = (...args) => {
112
- if (!this.#getEnabled()) return void 0;
113
- this.fn(...args);
114
- this.#setState({
115
- executionCount: this.store.state.executionCount + 1,
116
- isPending: false,
117
- lastArgs: void 0
118
- });
119
- this.options.onExecute?.(args, this);
120
- };
121
- this.flush = () => {
122
- if (this.store.state.isPending && this.store.state.lastArgs) {
123
- this.#clearTimeout();
124
- this.#execute(...this.store.state.lastArgs);
125
- }
126
- };
127
- this.#clearTimeout = () => {
128
- if (this.#timeoutId) {
129
- clearTimeout(this.#timeoutId);
130
- this.#timeoutId = void 0;
131
- }
132
- };
133
- this.cancel = () => {
134
- this.#clearTimeout();
135
- this.#setState({
136
- canLeadingExecute: true,
137
- isPending: false
138
- });
139
- };
140
- this.reset = () => {
141
- this.#setState(getDefaultDebouncerState());
142
- };
143
68
  this.key = initialOptions.key;
144
69
  this.options = {
145
70
  ...defaultOptions,
@@ -152,17 +77,106 @@ var Debouncer = class {
152
77
  this.setOptions(event.payload.options);
153
78
  });
154
79
  }
155
- #setState;
80
+ /**
81
+ * Updates the debouncer options
82
+ */
83
+ setOptions = (newOptions) => {
84
+ this.options = {
85
+ ...this.options,
86
+ ...newOptions
87
+ };
88
+ if (!this.#getEnabled()) this.cancel();
89
+ };
90
+ #setState = (newState) => {
91
+ this.store.setState((state) => {
92
+ const combinedState = {
93
+ ...state,
94
+ ...newState
95
+ };
96
+ const { isPending } = combinedState;
97
+ return {
98
+ ...combinedState,
99
+ status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : "idle"
100
+ };
101
+ });
102
+ emitChange("Debouncer", this);
103
+ };
156
104
  /**
157
105
  * Returns the current enabled state of the debouncer
158
106
  */
159
- #getEnabled;
107
+ #getEnabled = () => {
108
+ return !!parseFunctionOrValue(this.options.enabled, this);
109
+ };
160
110
  /**
161
111
  * Returns the current wait time in milliseconds
162
112
  */
163
- #getWait;
164
- #execute;
165
- #clearTimeout;
113
+ #getWait = () => {
114
+ return parseFunctionOrValue(this.options.wait, this);
115
+ };
116
+ /**
117
+ * Attempts to execute the debounced function
118
+ * If a call is already in progress, it will be queued
119
+ */
120
+ maybeExecute = (...args) => {
121
+ if (!this.#getEnabled()) return void 0;
122
+ this.#setState({ maybeExecuteCount: this.store.state.maybeExecuteCount + 1 });
123
+ let _didLeadingExecute = false;
124
+ if (this.options.leading && this.store.state.canLeadingExecute) {
125
+ this.#setState({ canLeadingExecute: false });
126
+ _didLeadingExecute = true;
127
+ this.#execute(...args);
128
+ }
129
+ if (this.options.trailing) this.#setState({
130
+ isPending: true,
131
+ lastArgs: args
132
+ });
133
+ if (this.#timeoutId) clearTimeout(this.#timeoutId);
134
+ this.#timeoutId = setTimeout(() => {
135
+ this.#setState({ canLeadingExecute: true });
136
+ if (this.options.trailing && !_didLeadingExecute) this.#execute(...args);
137
+ }, this.#getWait());
138
+ };
139
+ #execute = (...args) => {
140
+ if (!this.#getEnabled()) return void 0;
141
+ this.fn(...args);
142
+ this.#setState({
143
+ executionCount: this.store.state.executionCount + 1,
144
+ isPending: false,
145
+ lastArgs: void 0
146
+ });
147
+ this.options.onExecute?.(args, this);
148
+ };
149
+ /**
150
+ * Processes the current pending execution immediately
151
+ */
152
+ flush = () => {
153
+ if (this.store.state.isPending && this.store.state.lastArgs) {
154
+ this.#clearTimeout();
155
+ this.#execute(...this.store.state.lastArgs);
156
+ }
157
+ };
158
+ #clearTimeout = () => {
159
+ if (this.#timeoutId) {
160
+ clearTimeout(this.#timeoutId);
161
+ this.#timeoutId = void 0;
162
+ }
163
+ };
164
+ /**
165
+ * Cancels any pending execution
166
+ */
167
+ cancel = () => {
168
+ this.#clearTimeout();
169
+ this.#setState({
170
+ canLeadingExecute: true,
171
+ isPending: false
172
+ });
173
+ };
174
+ /**
175
+ * Resets the debouncer state to its default values
176
+ */
177
+ reset = () => {
178
+ this.#setState(getDefaultDebouncerState());
179
+ };
166
180
  };
167
181
  /**
168
182
  * Creates a debounced function that delays invoking the provided function until after a specified wait time.
@@ -196,5 +210,4 @@ function debounce(fn, initialOptions) {
196
210
  }
197
211
 
198
212
  //#endregion
199
- export { Debouncer, debounce, debouncerOptions };
200
- //# sourceMappingURL=debouncer.js.map
213
+ export { Debouncer, debounce, debouncerOptions };
@@ -7,20 +7,20 @@ import { EventClient } from "@tanstack/devtools-event-client";
7
7
  *
8
8
  * Live instances are kept in {@link pacerDevtoolsInstancesByKey} for the panel.
9
9
  */
10
- interface PacerDevtoolsWirePayload {
10
+ export interface PacerDevtoolsWirePayload {
11
11
  key: string;
12
12
  store: {
13
13
  state: unknown;
14
14
  };
15
15
  options: unknown;
16
16
  }
17
- declare function registerPacerDevtoolsInstance(key: string, instance: unknown): void;
18
- declare function getPacerDevtoolsInstance(key: string): unknown;
17
+ export declare function registerPacerDevtoolsInstance(key: string, instance: unknown): void;
18
+ export declare function getPacerDevtoolsInstance(key: string): unknown;
19
19
  /**
20
20
  * Suffix-only keys: {@link EventClient} prepends `pluginId:` (`pacer:`) at runtime
21
21
  * for `emit` / `on`. Wire `type` values are `pacer:${key}`.
22
22
  */
23
- interface PacerEventMap {
23
+ export interface PacerEventMap {
24
24
  'd-AsyncBatcher': PacerDevtoolsWirePayload;
25
25
  'd-AsyncDebouncer': PacerDevtoolsWirePayload;
26
26
  'd-AsyncQueuer': PacerDevtoolsWirePayload;
@@ -44,7 +44,7 @@ interface PacerEventMap {
44
44
  RateLimiter: PacerDevtoolsWirePayload;
45
45
  Throttler: PacerDevtoolsWirePayload;
46
46
  }
47
- type PacerEventName = keyof PacerEventMap;
47
+ export type PacerEventName = keyof PacerEventMap;
48
48
  type PacerUtilEmitSource = {
49
49
  key?: string;
50
50
  store: {
@@ -58,8 +58,6 @@ declare class PacerEventClient extends EventClient<PacerEventMap> {
58
58
  debug: boolean;
59
59
  });
60
60
  }
61
- declare const emitChange: <TEvent extends keyof PacerEventMap>(event: TEvent, instance: PacerUtilEmitSource) => void;
62
- declare const pacerEventClient: PacerEventClient;
63
- //#endregion
64
- export { PacerDevtoolsWirePayload, PacerEventMap, PacerEventName, emitChange, getPacerDevtoolsInstance, pacerEventClient, registerPacerDevtoolsInstance };
65
- //# sourceMappingURL=event-client.d.ts.map
61
+ export declare const emitChange: <TEvent extends keyof PacerEventMap>(event: TEvent, instance: PacerUtilEmitSource) => void;
62
+ export declare const pacerEventClient: PacerEventClient;
63
+ //#endregion
@@ -56,5 +56,4 @@ const emitChange = (event, instance) => {
56
56
  const pacerEventClient = new PacerEventClient();
57
57
 
58
58
  //#endregion
59
- export { emitChange, getPacerDevtoolsInstance, pacerEventClient, registerPacerDevtoolsInstance };
60
- //# sourceMappingURL=event-client.js.map
59
+ export { emitChange, getPacerDevtoolsInstance, pacerEventClient, registerPacerDevtoolsInstance };
package/dist/queuer.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Store } from "@tanstack/store";
2
2
  //#region src/queuer.d.ts
3
- interface QueuerState<TValue> {
3
+ export interface QueuerState<TValue> {
4
4
  /**
5
5
  * Number of times addItem has been called (for reduction calculations)
6
6
  */
@@ -59,7 +59,7 @@ interface QueuerState<TValue> {
59
59
  *
60
60
  * These options control queue behavior, item expiration, callbacks, and more.
61
61
  */
62
- interface QueuerOptions<TValue> {
62
+ export interface QueuerOptions<TValue> {
63
63
  /**
64
64
  * Default position to add items to the queuer
65
65
  * @default 'back'
@@ -132,14 +132,14 @@ interface QueuerOptions<TValue> {
132
132
  /**
133
133
  * Utility function for sharing common `QueuerOptions` options between different `Queuer` instances.
134
134
  */
135
- declare function queuerOptions<TValue = any, TOptions extends Partial<QueuerOptions<TValue>> = Partial<QueuerOptions<TValue>>>(options: TOptions): TOptions;
135
+ export declare function queuerOptions<TValue = any, TOptions extends Partial<QueuerOptions<TValue>> = Partial<QueuerOptions<TValue>>>(options: TOptions): TOptions;
136
136
  /**
137
137
  * Position type for addItem and getNextItem operations.
138
138
  *
139
139
  * - 'front': Operate on the front of the queue (FIFO)
140
140
  * - 'back': Operate on the back of the queue (LIFO)
141
141
  */
142
- type QueuePosition = 'front' | 'back';
142
+ export type QueuePosition = 'front' | 'back';
143
143
  /**
144
144
  * A flexible queue that processes items with configurable wait times, expiration, and priority.
145
145
  *
@@ -214,7 +214,7 @@ type QueuePosition = 'front' | 'back';
214
214
  * manualQueue.getNextItem(); // returns 2, queue is empty
215
215
  * ```
216
216
  */
217
- declare class Queuer<TValue> {
217
+ export declare class Queuer<TValue> {
218
218
  #private;
219
219
  fn: (item: TValue) => void;
220
220
  readonly store: Store<Readonly<QueuerState<TValue>>>;
@@ -339,7 +339,5 @@ declare class Queuer<TValue> {
339
339
  * processPriority(3); // Processed before 1
340
340
  * ```
341
341
  */
342
- declare function queue<TValue>(fn: (item: TValue) => void, initialOptions: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
343
- //#endregion
344
- export { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions };
345
- //# sourceMappingURL=queuer.d.ts.map
342
+ export declare function queue<TValue>(fn: (item: TValue) => void, initialOptions: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
343
+ //#endregion