@tanstack/pacer 0.21.1 → 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 (101) hide show
  1. package/README.md +21 -6
  2. package/dist/async-batcher.d.ts +11 -14
  3. package/dist/async-batcher.js +186 -132
  4. package/dist/async-debouncer.d.ts +10 -13
  5. package/dist/async-debouncer.js +187 -146
  6. package/dist/async-queuer.d.ts +12 -14
  7. package/dist/async-queuer.js +384 -259
  8. package/dist/async-rate-limiter.d.ts +9 -12
  9. package/dist/async-rate-limiter.js +198 -147
  10. package/dist/async-retryer.d.ts +10 -10
  11. package/dist/async-retryer.js +209 -188
  12. package/dist/async-throttler.d.ts +10 -13
  13. package/dist/async-throttler.js +215 -161
  14. package/dist/batcher.d.ts +5 -8
  15. package/dist/batcher.js +107 -87
  16. package/dist/debouncer.d.ts +6 -9
  17. package/dist/debouncer.js +99 -86
  18. package/dist/event-client.d.ts +8 -11
  19. package/dist/event-client.js +1 -2
  20. package/dist/index.js +1 -1
  21. package/dist/queuer.d.ts +8 -10
  22. package/dist/queuer.js +277 -207
  23. package/dist/rate-limiter.d.ts +6 -9
  24. package/dist/rate-limiter.js +127 -109
  25. package/dist/throttler.d.ts +6 -9
  26. package/dist/throttler.js +127 -90
  27. package/dist/types.d.ts +4 -6
  28. package/dist/utils.d.ts +3 -6
  29. package/dist/utils.js +1 -2
  30. package/package.json +23 -70
  31. package/dist/async-batcher.cjs +0 -337
  32. package/dist/async-batcher.cjs.map +0 -1
  33. package/dist/async-batcher.d.cts +0 -344
  34. package/dist/async-batcher.js.map +0 -1
  35. package/dist/async-debouncer.cjs +0 -330
  36. package/dist/async-debouncer.cjs.map +0 -1
  37. package/dist/async-debouncer.d.cts +0 -300
  38. package/dist/async-debouncer.js.map +0 -1
  39. package/dist/async-queuer.cjs +0 -484
  40. package/dist/async-queuer.cjs.map +0 -1
  41. package/dist/async-queuer.d.cts +0 -440
  42. package/dist/async-queuer.js.map +0 -1
  43. package/dist/async-rate-limiter.cjs +0 -373
  44. package/dist/async-rate-limiter.cjs.map +0 -1
  45. package/dist/async-rate-limiter.d.cts +0 -357
  46. package/dist/async-rate-limiter.js.map +0 -1
  47. package/dist/async-retryer.cjs +0 -374
  48. package/dist/async-retryer.cjs.map +0 -1
  49. package/dist/async-retryer.d.cts +0 -319
  50. package/dist/async-retryer.js.map +0 -1
  51. package/dist/async-throttler.cjs +0 -347
  52. package/dist/async-throttler.cjs.map +0 -1
  53. package/dist/async-throttler.d.cts +0 -320
  54. package/dist/async-throttler.js.map +0 -1
  55. package/dist/batcher.cjs +0 -200
  56. package/dist/batcher.cjs.map +0 -1
  57. package/dist/batcher.d.cts +0 -180
  58. package/dist/batcher.js.map +0 -1
  59. package/dist/debouncer.cjs +0 -203
  60. package/dist/debouncer.cjs.map +0 -1
  61. package/dist/debouncer.d.cts +0 -167
  62. package/dist/debouncer.js.map +0 -1
  63. package/dist/event-client.cjs +0 -64
  64. package/dist/event-client.cjs.map +0 -1
  65. package/dist/event-client.d.cts +0 -66
  66. package/dist/event-client.js.map +0 -1
  67. package/dist/index.cjs +0 -52
  68. package/dist/index.d.cts +0 -15
  69. package/dist/queuer.cjs +0 -401
  70. package/dist/queuer.cjs.map +0 -1
  71. package/dist/queuer.d.cts +0 -345
  72. package/dist/queuer.js.map +0 -1
  73. package/dist/rate-limiter.cjs +0 -263
  74. package/dist/rate-limiter.cjs.map +0 -1
  75. package/dist/rate-limiter.d.cts +0 -215
  76. package/dist/rate-limiter.js.map +0 -1
  77. package/dist/throttler.cjs +0 -215
  78. package/dist/throttler.cjs.map +0 -1
  79. package/dist/throttler.d.cts +0 -207
  80. package/dist/throttler.js.map +0 -1
  81. package/dist/types.cjs +0 -0
  82. package/dist/types.d.cts +0 -13
  83. package/dist/utils.cjs +0 -14
  84. package/dist/utils.cjs.map +0 -1
  85. package/dist/utils.d.cts +0 -8
  86. package/dist/utils.js.map +0 -1
  87. package/src/async-batcher.ts +0 -594
  88. package/src/async-debouncer.ts +0 -565
  89. package/src/async-queuer.ts +0 -925
  90. package/src/async-rate-limiter.ts +0 -647
  91. package/src/async-retryer.ts +0 -684
  92. package/src/async-throttler.ts +0 -633
  93. package/src/batcher.ts +0 -329
  94. package/src/debouncer.ts +0 -334
  95. package/src/event-client.ts +0 -129
  96. package/src/index.ts +0 -24
  97. package/src/queuer.ts +0 -740
  98. package/src/rate-limiter.ts +0 -429
  99. package/src/throttler.ts +0 -380
  100. package/src/types.ts +0 -12
  101. 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 };
@@ -1,5 +1,4 @@
1
1
  import { EventClient } from "@tanstack/devtools-event-client";
2
-
3
2
  //#region src/event-client.d.ts
4
3
  /**
5
4
  * Payload on the devtools event bus must be JSON-serializable: `ClientEventBus`
@@ -8,20 +7,20 @@ import { EventClient } from "@tanstack/devtools-event-client";
8
7
  *
9
8
  * Live instances are kept in {@link pacerDevtoolsInstancesByKey} for the panel.
10
9
  */
11
- interface PacerDevtoolsWirePayload {
10
+ export interface PacerDevtoolsWirePayload {
12
11
  key: string;
13
12
  store: {
14
13
  state: unknown;
15
14
  };
16
15
  options: unknown;
17
16
  }
18
- declare function registerPacerDevtoolsInstance(key: string, instance: unknown): void;
19
- declare function getPacerDevtoolsInstance(key: string): unknown;
17
+ export declare function registerPacerDevtoolsInstance(key: string, instance: unknown): void;
18
+ export declare function getPacerDevtoolsInstance(key: string): unknown;
20
19
  /**
21
20
  * Suffix-only keys: {@link EventClient} prepends `pluginId:` (`pacer:`) at runtime
22
21
  * for `emit` / `on`. Wire `type` values are `pacer:${key}`.
23
22
  */
24
- interface PacerEventMap {
23
+ export interface PacerEventMap {
25
24
  'd-AsyncBatcher': PacerDevtoolsWirePayload;
26
25
  'd-AsyncDebouncer': PacerDevtoolsWirePayload;
27
26
  'd-AsyncQueuer': PacerDevtoolsWirePayload;
@@ -45,7 +44,7 @@ interface PacerEventMap {
45
44
  RateLimiter: PacerDevtoolsWirePayload;
46
45
  Throttler: PacerDevtoolsWirePayload;
47
46
  }
48
- type PacerEventName = keyof PacerEventMap;
47
+ export type PacerEventName = keyof PacerEventMap;
49
48
  type PacerUtilEmitSource = {
50
49
  key?: string;
51
50
  store: {
@@ -59,8 +58,6 @@ declare class PacerEventClient extends EventClient<PacerEventMap> {
59
58
  debug: boolean;
60
59
  });
61
60
  }
62
- declare const emitChange: <TEvent extends keyof PacerEventMap>(event: TEvent, instance: PacerUtilEmitSource) => void;
63
- declare const pacerEventClient: PacerEventClient;
64
- //#endregion
65
- export { PacerDevtoolsWirePayload, PacerEventMap, PacerEventName, emitChange, getPacerDevtoolsInstance, pacerEventClient, registerPacerDevtoolsInstance };
66
- //# 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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isFunction, parseFunctionOrValue } from "./utils.js";
2
- import { emitChange, getPacerDevtoolsInstance, pacerEventClient } from "./event-client.js";
3
2
  import { AsyncRetryer, asyncRetry, asyncRetryerOptions } from "./async-retryer.js";
3
+ import { emitChange, getPacerDevtoolsInstance, pacerEventClient } from "./event-client.js";
4
4
  import { AsyncBatcher, asyncBatch, asyncBatcherOptions } from "./async-batcher.js";
5
5
  import { AsyncDebouncer, asyncDebounce, asyncDebouncerOptions } from "./async-debouncer.js";
6
6
  import { AsyncQueuer, asyncQueue, asyncQueuerOptions } from "./async-queuer.js";
package/dist/queuer.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Store } from "@tanstack/store";
2
-
3
2
  //#region src/queuer.d.ts
4
- interface QueuerState<TValue> {
3
+ export interface QueuerState<TValue> {
5
4
  /**
6
5
  * Number of times addItem has been called (for reduction calculations)
7
6
  */
@@ -60,7 +59,7 @@ interface QueuerState<TValue> {
60
59
  *
61
60
  * These options control queue behavior, item expiration, callbacks, and more.
62
61
  */
63
- interface QueuerOptions<TValue> {
62
+ export interface QueuerOptions<TValue> {
64
63
  /**
65
64
  * Default position to add items to the queuer
66
65
  * @default 'back'
@@ -133,14 +132,14 @@ interface QueuerOptions<TValue> {
133
132
  /**
134
133
  * Utility function for sharing common `QueuerOptions` options between different `Queuer` instances.
135
134
  */
136
- 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;
137
136
  /**
138
137
  * Position type for addItem and getNextItem operations.
139
138
  *
140
139
  * - 'front': Operate on the front of the queue (FIFO)
141
140
  * - 'back': Operate on the back of the queue (LIFO)
142
141
  */
143
- type QueuePosition = 'front' | 'back';
142
+ export type QueuePosition = 'front' | 'back';
144
143
  /**
145
144
  * A flexible queue that processes items with configurable wait times, expiration, and priority.
146
145
  *
@@ -215,7 +214,7 @@ type QueuePosition = 'front' | 'back';
215
214
  * manualQueue.getNextItem(); // returns 2, queue is empty
216
215
  * ```
217
216
  */
218
- declare class Queuer<TValue> {
217
+ export declare class Queuer<TValue> {
219
218
  #private;
220
219
  fn: (item: TValue) => void;
221
220
  readonly store: Store<Readonly<QueuerState<TValue>>>;
@@ -228,6 +227,7 @@ declare class Queuer<TValue> {
228
227
  setOptions: (newOptions: Partial<QueuerOptions<TValue>>) => void;
229
228
  /**
230
229
  * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
230
+ * `undefined` cannot be queued (it is the internal "no item" sentinel) and is always rejected.
231
231
  * Items can be inserted based on priority or at the front/back depending on configuration.
232
232
  *
233
233
  * Returns true if the item was added, false if the queue is full.
@@ -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