@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.
- package/README.md +21 -6
- package/dist/async-batcher.d.ts +11 -14
- package/dist/async-batcher.js +186 -132
- package/dist/async-debouncer.d.ts +10 -13
- package/dist/async-debouncer.js +187 -146
- package/dist/async-queuer.d.ts +12 -14
- package/dist/async-queuer.js +384 -259
- package/dist/async-rate-limiter.d.ts +9 -12
- package/dist/async-rate-limiter.js +198 -147
- package/dist/async-retryer.d.ts +10 -10
- package/dist/async-retryer.js +209 -188
- package/dist/async-throttler.d.ts +10 -13
- package/dist/async-throttler.js +215 -161
- package/dist/batcher.d.ts +5 -8
- package/dist/batcher.js +107 -87
- package/dist/debouncer.d.ts +6 -9
- package/dist/debouncer.js +99 -86
- package/dist/event-client.d.ts +8 -11
- package/dist/event-client.js +1 -2
- package/dist/index.js +1 -1
- package/dist/queuer.d.ts +8 -10
- package/dist/queuer.js +277 -207
- package/dist/rate-limiter.d.ts +6 -9
- package/dist/rate-limiter.js +127 -109
- package/dist/throttler.d.ts +6 -9
- package/dist/throttler.js +127 -90
- package/dist/types.d.ts +4 -6
- package/dist/utils.d.ts +3 -6
- package/dist/utils.js +1 -2
- package/package.json +23 -70
- package/dist/async-batcher.cjs +0 -337
- package/dist/async-batcher.cjs.map +0 -1
- package/dist/async-batcher.d.cts +0 -344
- package/dist/async-batcher.js.map +0 -1
- package/dist/async-debouncer.cjs +0 -330
- package/dist/async-debouncer.cjs.map +0 -1
- package/dist/async-debouncer.d.cts +0 -300
- package/dist/async-debouncer.js.map +0 -1
- package/dist/async-queuer.cjs +0 -484
- package/dist/async-queuer.cjs.map +0 -1
- package/dist/async-queuer.d.cts +0 -440
- package/dist/async-queuer.js.map +0 -1
- package/dist/async-rate-limiter.cjs +0 -373
- package/dist/async-rate-limiter.cjs.map +0 -1
- package/dist/async-rate-limiter.d.cts +0 -357
- package/dist/async-rate-limiter.js.map +0 -1
- package/dist/async-retryer.cjs +0 -374
- package/dist/async-retryer.cjs.map +0 -1
- package/dist/async-retryer.d.cts +0 -319
- package/dist/async-retryer.js.map +0 -1
- package/dist/async-throttler.cjs +0 -347
- package/dist/async-throttler.cjs.map +0 -1
- package/dist/async-throttler.d.cts +0 -320
- package/dist/async-throttler.js.map +0 -1
- package/dist/batcher.cjs +0 -200
- package/dist/batcher.cjs.map +0 -1
- package/dist/batcher.d.cts +0 -180
- package/dist/batcher.js.map +0 -1
- package/dist/debouncer.cjs +0 -203
- package/dist/debouncer.cjs.map +0 -1
- package/dist/debouncer.d.cts +0 -167
- package/dist/debouncer.js.map +0 -1
- package/dist/event-client.cjs +0 -64
- package/dist/event-client.cjs.map +0 -1
- package/dist/event-client.d.cts +0 -66
- package/dist/event-client.js.map +0 -1
- package/dist/index.cjs +0 -52
- package/dist/index.d.cts +0 -15
- package/dist/queuer.cjs +0 -401
- package/dist/queuer.cjs.map +0 -1
- package/dist/queuer.d.cts +0 -345
- package/dist/queuer.js.map +0 -1
- package/dist/rate-limiter.cjs +0 -263
- package/dist/rate-limiter.cjs.map +0 -1
- package/dist/rate-limiter.d.cts +0 -215
- package/dist/rate-limiter.js.map +0 -1
- package/dist/throttler.cjs +0 -215
- package/dist/throttler.cjs.map +0 -1
- package/dist/throttler.d.cts +0 -207
- package/dist/throttler.js.map +0 -1
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +0 -13
- package/dist/utils.cjs +0 -14
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.cts +0 -8
- package/dist/utils.js.map +0 -1
- package/src/async-batcher.ts +0 -594
- package/src/async-debouncer.ts +0 -565
- package/src/async-queuer.ts +0 -925
- package/src/async-rate-limiter.ts +0 -647
- package/src/async-retryer.ts +0 -684
- package/src/async-throttler.ts +0 -633
- package/src/batcher.ts +0 -329
- package/src/debouncer.ts +0 -334
- package/src/event-client.ts +0 -129
- package/src/index.ts +0 -24
- package/src/queuer.ts +0 -740
- package/src/rate-limiter.ts +0 -429
- package/src/throttler.ts +0 -380
- package/src/types.ts +0 -12
- package/src/utils.ts +0 -12
package/dist/async-debouncer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parseFunctionOrValue } from "./utils.js";
|
|
2
|
-
import { emitChange, pacerEventClient } from "./event-client.js";
|
|
3
2
|
import { AsyncRetryer } from "./async-retryer.js";
|
|
3
|
+
import { emitChange, pacerEventClient } from "./event-client.js";
|
|
4
4
|
import { Store } from "@tanstack/store";
|
|
5
5
|
|
|
6
6
|
//#region src/async-debouncer.ts
|
|
@@ -86,144 +86,15 @@ const defaultOptions = {
|
|
|
86
86
|
* ```
|
|
87
87
|
*/
|
|
88
88
|
var AsyncDebouncer = class {
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
fn;
|
|
90
|
+
store = new Store(getDefaultAsyncDebouncerState());
|
|
91
|
+
key;
|
|
92
|
+
options;
|
|
93
|
+
asyncRetryers = /* @__PURE__ */ new Map();
|
|
94
|
+
#timeoutId = null;
|
|
95
|
+
#resolvePreviousPromise = null;
|
|
91
96
|
constructor(fn, initialOptions) {
|
|
92
97
|
this.fn = fn;
|
|
93
|
-
this.store = new Store(getDefaultAsyncDebouncerState());
|
|
94
|
-
this.asyncRetryers = /* @__PURE__ */ new Map();
|
|
95
|
-
this.#timeoutId = null;
|
|
96
|
-
this.#resolvePreviousPromise = null;
|
|
97
|
-
this.setOptions = (newOptions) => {
|
|
98
|
-
this.options = {
|
|
99
|
-
...this.options,
|
|
100
|
-
...newOptions
|
|
101
|
-
};
|
|
102
|
-
if (!this.#getEnabled()) this.cancel();
|
|
103
|
-
};
|
|
104
|
-
this.#setState = (newState) => {
|
|
105
|
-
this.store.setState((state) => {
|
|
106
|
-
const combinedState = {
|
|
107
|
-
...state,
|
|
108
|
-
...newState
|
|
109
|
-
};
|
|
110
|
-
const { isPending, isExecuting, settleCount } = combinedState;
|
|
111
|
-
return {
|
|
112
|
-
...combinedState,
|
|
113
|
-
status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : isExecuting ? "executing" : settleCount > 0 ? "settled" : "idle"
|
|
114
|
-
};
|
|
115
|
-
});
|
|
116
|
-
emitChange("AsyncDebouncer", this);
|
|
117
|
-
};
|
|
118
|
-
this.#getEnabled = () => {
|
|
119
|
-
return !!parseFunctionOrValue(this.options.enabled, this);
|
|
120
|
-
};
|
|
121
|
-
this.#getWait = () => {
|
|
122
|
-
return parseFunctionOrValue(this.options.wait, this);
|
|
123
|
-
};
|
|
124
|
-
this.maybeExecute = async (...args) => {
|
|
125
|
-
if (!this.#getEnabled()) return void 0;
|
|
126
|
-
this.#cancelPendingExecution();
|
|
127
|
-
this.#setState({
|
|
128
|
-
lastArgs: args,
|
|
129
|
-
maybeExecuteCount: this.store.state.maybeExecuteCount + 1
|
|
130
|
-
});
|
|
131
|
-
if (this.options.leading && this.store.state.canLeadingExecute) {
|
|
132
|
-
this.#setState({ canLeadingExecute: false });
|
|
133
|
-
await this.#execute(...args);
|
|
134
|
-
return this.store.state.lastResult;
|
|
135
|
-
}
|
|
136
|
-
if (this.options.trailing && this.#getEnabled()) this.#setState({ isPending: true });
|
|
137
|
-
return new Promise((resolve, reject) => {
|
|
138
|
-
this.#resolvePreviousPromise = resolve;
|
|
139
|
-
this.#timeoutId = setTimeout(async () => {
|
|
140
|
-
if (this.options.trailing && this.store.state.lastArgs) try {
|
|
141
|
-
await this.#execute(...this.store.state.lastArgs);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
reject(error);
|
|
144
|
-
}
|
|
145
|
-
this.#setState({ canLeadingExecute: true });
|
|
146
|
-
this.#resolvePreviousPromise = null;
|
|
147
|
-
resolve(this.store.state.lastResult);
|
|
148
|
-
}, this.#getWait());
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
this.#execute = async (...args) => {
|
|
152
|
-
if (!this.#getEnabled()) return void 0;
|
|
153
|
-
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1;
|
|
154
|
-
try {
|
|
155
|
-
this.#setState({ isExecuting: true });
|
|
156
|
-
const currentAsyncRetryer = new AsyncRetryer(this.fn, {
|
|
157
|
-
...this.options.asyncRetryerOptions,
|
|
158
|
-
key: `${this.key}-retryer-${currentMaybeExecuteCount}`
|
|
159
|
-
});
|
|
160
|
-
this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer);
|
|
161
|
-
const result = await currentAsyncRetryer.execute(...args);
|
|
162
|
-
this.#setState({
|
|
163
|
-
lastResult: result,
|
|
164
|
-
successCount: this.store.state.successCount + 1
|
|
165
|
-
});
|
|
166
|
-
this.options.onSuccess?.(result, args, this);
|
|
167
|
-
} catch (error) {
|
|
168
|
-
this.#setState({ errorCount: this.store.state.errorCount + 1 });
|
|
169
|
-
this.options.onError?.(error, args, this);
|
|
170
|
-
if (this.options.throwOnError) throw error;
|
|
171
|
-
} finally {
|
|
172
|
-
this.asyncRetryers.delete(currentMaybeExecuteCount);
|
|
173
|
-
this.#setState({
|
|
174
|
-
isExecuting: false,
|
|
175
|
-
isPending: false,
|
|
176
|
-
lastArgs: void 0,
|
|
177
|
-
settleCount: this.store.state.settleCount + 1
|
|
178
|
-
});
|
|
179
|
-
this.options.onSettled?.(args, this);
|
|
180
|
-
}
|
|
181
|
-
return this.store.state.lastResult;
|
|
182
|
-
};
|
|
183
|
-
this.flush = async () => {
|
|
184
|
-
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
185
|
-
const { lastArgs } = this.store.state;
|
|
186
|
-
this.#cancelPendingExecution();
|
|
187
|
-
return await this.#execute(...lastArgs);
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
this.#resolvePreviousPromiseInternal = () => {
|
|
191
|
-
if (this.#resolvePreviousPromise) {
|
|
192
|
-
this.#resolvePreviousPromise(this.store.state.lastResult);
|
|
193
|
-
this.#resolvePreviousPromise = null;
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
this.#clearTimeout = () => {
|
|
197
|
-
if (this.#timeoutId) {
|
|
198
|
-
clearTimeout(this.#timeoutId);
|
|
199
|
-
this.#timeoutId = null;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
this.#cancelPendingExecution = () => {
|
|
203
|
-
this.#clearTimeout();
|
|
204
|
-
this.#resolvePreviousPromiseInternal();
|
|
205
|
-
this.#setState({
|
|
206
|
-
isPending: false,
|
|
207
|
-
lastArgs: void 0
|
|
208
|
-
});
|
|
209
|
-
};
|
|
210
|
-
this.getAbortSignal = (maybeExecuteCount) => {
|
|
211
|
-
const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount;
|
|
212
|
-
return this.asyncRetryers.get(count)?.getAbortSignal() ?? null;
|
|
213
|
-
};
|
|
214
|
-
this.abort = () => {
|
|
215
|
-
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
216
|
-
this.asyncRetryers.clear();
|
|
217
|
-
this.#setState({ isExecuting: false });
|
|
218
|
-
};
|
|
219
|
-
this.cancel = () => {
|
|
220
|
-
this.#cancelPendingExecution();
|
|
221
|
-
this.#setState({ canLeadingExecute: true });
|
|
222
|
-
};
|
|
223
|
-
this.reset = () => {
|
|
224
|
-
this.#setState(getDefaultAsyncDebouncerState());
|
|
225
|
-
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
226
|
-
};
|
|
227
98
|
this.key = initialOptions.key;
|
|
228
99
|
this.options = {
|
|
229
100
|
...defaultOptions,
|
|
@@ -237,22 +108,193 @@ var AsyncDebouncer = class {
|
|
|
237
108
|
this.setOptions(event.payload.options);
|
|
238
109
|
});
|
|
239
110
|
}
|
|
240
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Updates the async debouncer options
|
|
113
|
+
*/
|
|
114
|
+
setOptions = (newOptions) => {
|
|
115
|
+
this.options = {
|
|
116
|
+
...this.options,
|
|
117
|
+
...newOptions
|
|
118
|
+
};
|
|
119
|
+
if (!this.#getEnabled()) this.cancel();
|
|
120
|
+
};
|
|
121
|
+
#setState = (newState) => {
|
|
122
|
+
this.store.setState((state) => {
|
|
123
|
+
const combinedState = {
|
|
124
|
+
...state,
|
|
125
|
+
...newState
|
|
126
|
+
};
|
|
127
|
+
const { isPending, isExecuting, settleCount } = combinedState;
|
|
128
|
+
return {
|
|
129
|
+
...combinedState,
|
|
130
|
+
status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : isExecuting ? "executing" : settleCount > 0 ? "settled" : "idle"
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
emitChange("AsyncDebouncer", this);
|
|
134
|
+
};
|
|
241
135
|
/**
|
|
242
136
|
* Returns the current debouncer enabled state
|
|
243
137
|
*/
|
|
244
|
-
#getEnabled
|
|
138
|
+
#getEnabled = () => {
|
|
139
|
+
return !!parseFunctionOrValue(this.options.enabled, this);
|
|
140
|
+
};
|
|
245
141
|
/**
|
|
246
142
|
* Returns the current debouncer wait state
|
|
247
143
|
*/
|
|
248
|
-
#getWait
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
144
|
+
#getWait = () => {
|
|
145
|
+
return parseFunctionOrValue(this.options.wait, this);
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Attempts to execute the debounced function.
|
|
149
|
+
* If a call is already in progress, it will be queued.
|
|
150
|
+
*
|
|
151
|
+
* Error Handling:
|
|
152
|
+
* - If the debounced function throws and no `onError` handler is configured,
|
|
153
|
+
* the error will be thrown from this method.
|
|
154
|
+
* - If an `onError` handler is configured, errors will be caught and passed to the handler,
|
|
155
|
+
* and this method will return undefined.
|
|
156
|
+
* - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.
|
|
157
|
+
*
|
|
158
|
+
* @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError
|
|
159
|
+
* @throws The error from the debounced function if no onError handler is configured
|
|
160
|
+
*/
|
|
161
|
+
maybeExecute = async (...args) => {
|
|
162
|
+
if (!this.#getEnabled()) return void 0;
|
|
163
|
+
this.#cancelPendingExecution();
|
|
164
|
+
this.#setState({
|
|
165
|
+
lastArgs: args,
|
|
166
|
+
maybeExecuteCount: this.store.state.maybeExecuteCount + 1
|
|
167
|
+
});
|
|
168
|
+
if (this.options.leading && this.store.state.canLeadingExecute) {
|
|
169
|
+
this.#setState({ canLeadingExecute: false });
|
|
170
|
+
await this.#execute(...args);
|
|
171
|
+
return this.store.state.lastResult;
|
|
172
|
+
}
|
|
173
|
+
if (this.options.trailing && this.#getEnabled()) this.#setState({ isPending: true });
|
|
174
|
+
return new Promise((resolve, reject) => {
|
|
175
|
+
this.#resolvePreviousPromise = resolve;
|
|
176
|
+
this.#timeoutId = setTimeout(async () => {
|
|
177
|
+
if (this.options.trailing && this.store.state.lastArgs) try {
|
|
178
|
+
await this.#execute(...this.store.state.lastArgs);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
reject(error);
|
|
181
|
+
}
|
|
182
|
+
this.#setState({ canLeadingExecute: true });
|
|
183
|
+
this.#resolvePreviousPromise = null;
|
|
184
|
+
resolve(this.store.state.lastResult);
|
|
185
|
+
}, this.#getWait());
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
#execute = async (...args) => {
|
|
189
|
+
if (!this.#getEnabled()) return void 0;
|
|
190
|
+
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1;
|
|
191
|
+
try {
|
|
192
|
+
this.#setState({ isExecuting: true });
|
|
193
|
+
const currentAsyncRetryer = new AsyncRetryer(this.fn, this.options.asyncRetryerOptions);
|
|
194
|
+
this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer);
|
|
195
|
+
const result = await currentAsyncRetryer.execute(...args);
|
|
196
|
+
this.#setState({
|
|
197
|
+
lastResult: result,
|
|
198
|
+
successCount: this.store.state.successCount + 1
|
|
199
|
+
});
|
|
200
|
+
this.options.onSuccess?.(result, args, this);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
this.#setState({ errorCount: this.store.state.errorCount + 1 });
|
|
203
|
+
this.options.onError?.(error, args, this);
|
|
204
|
+
if (this.options.throwOnError) throw error;
|
|
205
|
+
} finally {
|
|
206
|
+
this.asyncRetryers.delete(currentMaybeExecuteCount);
|
|
207
|
+
this.#setState({
|
|
208
|
+
isExecuting: false,
|
|
209
|
+
isPending: false,
|
|
210
|
+
lastArgs: void 0,
|
|
211
|
+
settleCount: this.store.state.settleCount + 1
|
|
212
|
+
});
|
|
213
|
+
this.options.onSettled?.(args, this);
|
|
214
|
+
}
|
|
215
|
+
return this.store.state.lastResult;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Processes the current pending execution immediately
|
|
219
|
+
*/
|
|
220
|
+
flush = async () => {
|
|
221
|
+
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
222
|
+
const { lastArgs } = this.store.state;
|
|
223
|
+
this.#cancelPendingExecution();
|
|
224
|
+
return await this.#execute(...lastArgs);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
#resolvePreviousPromiseInternal = () => {
|
|
228
|
+
if (this.#resolvePreviousPromise) {
|
|
229
|
+
this.#resolvePreviousPromise(this.store.state.lastResult);
|
|
230
|
+
this.#resolvePreviousPromise = null;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
#clearTimeout = () => {
|
|
234
|
+
if (this.#timeoutId) {
|
|
235
|
+
clearTimeout(this.#timeoutId);
|
|
236
|
+
this.#timeoutId = null;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
252
239
|
/**
|
|
253
240
|
* Internal cancel without resetting the leading execute state
|
|
254
241
|
*/
|
|
255
|
-
#cancelPendingExecution
|
|
242
|
+
#cancelPendingExecution = () => {
|
|
243
|
+
this.#clearTimeout();
|
|
244
|
+
this.#resolvePreviousPromiseInternal();
|
|
245
|
+
this.#setState({
|
|
246
|
+
isPending: false,
|
|
247
|
+
lastArgs: void 0
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Returns the AbortSignal for a specific execution.
|
|
252
|
+
* If no maybeExecuteCount is provided, returns the signal for the most recent execution.
|
|
253
|
+
* Returns null if no execution is found or not currently executing.
|
|
254
|
+
*
|
|
255
|
+
* @param maybeExecuteCount - Optional specific execution to get signal for
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* const debouncer = new AsyncDebouncer(
|
|
259
|
+
* async (searchTerm: string) => {
|
|
260
|
+
* const signal = debouncer.getAbortSignal()
|
|
261
|
+
* if (signal) {
|
|
262
|
+
* const response = await fetch(`/api/search?q=${searchTerm}`, { signal })
|
|
263
|
+
* return response.json()
|
|
264
|
+
* }
|
|
265
|
+
* },
|
|
266
|
+
* { wait: 300 }
|
|
267
|
+
* )
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
getAbortSignal = (maybeExecuteCount) => {
|
|
271
|
+
const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount;
|
|
272
|
+
return this.asyncRetryers.get(count)?.getAbortSignal() ?? null;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Aborts all ongoing executions with the internal abort controllers.
|
|
276
|
+
* Does NOT cancel any pending execution that have not started yet.
|
|
277
|
+
*/
|
|
278
|
+
abort = () => {
|
|
279
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
280
|
+
this.asyncRetryers.clear();
|
|
281
|
+
this.#setState({ isExecuting: false });
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* Cancels any pending execution that have not started yet.
|
|
285
|
+
* Does NOT abort any execution already in progress.
|
|
286
|
+
*/
|
|
287
|
+
cancel = () => {
|
|
288
|
+
this.#cancelPendingExecution();
|
|
289
|
+
this.#setState({ canLeadingExecute: true });
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Resets the debouncer state to its default values
|
|
293
|
+
*/
|
|
294
|
+
reset = () => {
|
|
295
|
+
this.#setState(getDefaultAsyncDebouncerState());
|
|
296
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
297
|
+
};
|
|
256
298
|
};
|
|
257
299
|
/**
|
|
258
300
|
* Creates an async debounced function that delays execution until after a specified wait time.
|
|
@@ -323,5 +365,4 @@ function asyncDebounce(fn, initialOptions) {
|
|
|
323
365
|
}
|
|
324
366
|
|
|
325
367
|
//#endregion
|
|
326
|
-
export { AsyncDebouncer, asyncDebounce, asyncDebouncerOptions };
|
|
327
|
-
//# sourceMappingURL=async-debouncer.js.map
|
|
368
|
+
export { AsyncDebouncer, asyncDebounce, asyncDebouncerOptions };
|
package/dist/async-queuer.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { AsyncRetryer, AsyncRetryerOptions } from "./async-retryer.js";
|
|
2
2
|
import { QueuePosition } from "./queuer.js";
|
|
3
3
|
import { Store } from "@tanstack/store";
|
|
4
|
-
|
|
5
4
|
//#region src/async-queuer.d.ts
|
|
6
|
-
interface AsyncQueuerState<TValue> {
|
|
5
|
+
export interface AsyncQueuerState<TValue> {
|
|
7
6
|
/**
|
|
8
7
|
* Items currently being processed by the queuer
|
|
9
8
|
*/
|
|
@@ -19,7 +18,7 @@ interface AsyncQueuerState<TValue> {
|
|
|
19
18
|
/**
|
|
20
19
|
* Number of times execute has been called
|
|
21
20
|
*/
|
|
22
|
-
|
|
21
|
+
executionCount: number;
|
|
23
22
|
/**
|
|
24
23
|
* Number of items that have been removed from the queue due to expiration
|
|
25
24
|
*/
|
|
@@ -67,7 +66,7 @@ interface AsyncQueuerState<TValue> {
|
|
|
67
66
|
/**
|
|
68
67
|
* Number of task executions that have completed (either successfully or with errors)
|
|
69
68
|
*/
|
|
70
|
-
|
|
69
|
+
settleCount: number;
|
|
71
70
|
/**
|
|
72
71
|
* Number of items currently in the queue
|
|
73
72
|
*/
|
|
@@ -81,7 +80,7 @@ interface AsyncQueuerState<TValue> {
|
|
|
81
80
|
*/
|
|
82
81
|
successCount: number;
|
|
83
82
|
}
|
|
84
|
-
interface AsyncQueuerOptions<TValue> {
|
|
83
|
+
export interface AsyncQueuerOptions<TValue> {
|
|
85
84
|
/**
|
|
86
85
|
* Options for configuring the underlying async retryer
|
|
87
86
|
*/
|
|
@@ -181,7 +180,7 @@ interface AsyncQueuerOptions<TValue> {
|
|
|
181
180
|
/**
|
|
182
181
|
* Utility function for sharing common `AsyncQueuerOptions` options between different `AsyncQueuer` instances.
|
|
183
182
|
*/
|
|
184
|
-
declare function asyncQueuerOptions<TValue = any, TOptions extends Partial<AsyncQueuerOptions<TValue>> = Partial<AsyncQueuerOptions<TValue>>>(options: TOptions): TOptions;
|
|
183
|
+
export declare function asyncQueuerOptions<TValue = any, TOptions extends Partial<AsyncQueuerOptions<TValue>> = Partial<AsyncQueuerOptions<TValue>>>(options: TOptions): TOptions;
|
|
185
184
|
/**
|
|
186
185
|
* A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
|
|
187
186
|
*
|
|
@@ -246,7 +245,7 @@ declare function asyncQueuerOptions<TValue = any, TOptions extends Partial<Async
|
|
|
246
245
|
* asyncQueuer.start();
|
|
247
246
|
* ```
|
|
248
247
|
*/
|
|
249
|
-
declare class AsyncQueuer<TValue> {
|
|
248
|
+
export declare class AsyncQueuer<TValue> {
|
|
250
249
|
#private;
|
|
251
250
|
fn: (item: TValue) => Promise<any>;
|
|
252
251
|
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
|
|
@@ -261,6 +260,7 @@ declare class AsyncQueuer<TValue> {
|
|
|
261
260
|
/**
|
|
262
261
|
* Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
|
|
263
262
|
* Items can be inserted based on priority or at the front/back depending on configuration.
|
|
263
|
+
* `undefined` cannot be queued (it is the internal "no item" sentinel) and is always rejected.
|
|
264
264
|
*
|
|
265
265
|
* @example
|
|
266
266
|
* ```ts
|
|
@@ -340,10 +340,10 @@ declare class AsyncQueuer<TValue> {
|
|
|
340
340
|
clear: () => void;
|
|
341
341
|
/**
|
|
342
342
|
* Returns the AbortSignal for a specific execution.
|
|
343
|
-
* If no
|
|
343
|
+
* If no executionCount is provided, returns the signal for the most recent execution.
|
|
344
344
|
* Returns null if no execution is found or not currently executing.
|
|
345
345
|
*
|
|
346
|
-
* @param
|
|
346
|
+
* @param executionCount - Optional specific execution to get signal for
|
|
347
347
|
* @example
|
|
348
348
|
* ```typescript
|
|
349
349
|
* const queuer = new AsyncQueuer(
|
|
@@ -358,7 +358,7 @@ declare class AsyncQueuer<TValue> {
|
|
|
358
358
|
* )
|
|
359
359
|
* ```
|
|
360
360
|
*/
|
|
361
|
-
getAbortSignal: (
|
|
361
|
+
getAbortSignal: (executionCount?: number) => AbortSignal | null;
|
|
362
362
|
/**
|
|
363
363
|
* Aborts all ongoing executions with the internal abort controllers.
|
|
364
364
|
* Does NOT clear out the items.
|
|
@@ -434,7 +434,5 @@ declare class AsyncQueuer<TValue> {
|
|
|
434
434
|
* enqueue('hello');
|
|
435
435
|
* ```
|
|
436
436
|
*/
|
|
437
|
-
declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
|
|
438
|
-
//#endregion
|
|
439
|
-
export { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, asyncQueue, asyncQueuerOptions };
|
|
440
|
-
//# sourceMappingURL=async-queuer.d.ts.map
|
|
437
|
+
export declare function asyncQueue<TValue>(fn: (value: TValue) => Promise<any>, initialOptions: AsyncQueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
|
|
438
|
+
//#endregion
|