@tanstack/pacer 0.15.3 → 0.16.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/dist/cjs/async-batcher.cjs +66 -4
- package/dist/cjs/async-batcher.cjs.map +1 -1
- package/dist/cjs/async-batcher.d.cts +97 -16
- package/dist/cjs/async-debouncer.cjs +53 -18
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +79 -9
- package/dist/cjs/async-queuer.cjs +58 -1
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +103 -9
- package/dist/cjs/async-rate-limiter.cjs +51 -1
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +94 -27
- package/dist/cjs/async-retryer.cjs +286 -0
- package/dist/cjs/async-retryer.cjs.map +1 -0
- package/dist/cjs/async-retryer.d.cts +312 -0
- package/dist/cjs/async-throttler.cjs +101 -52
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +85 -9
- package/dist/cjs/batcher.cjs +5 -0
- package/dist/cjs/batcher.cjs.map +1 -1
- package/dist/cjs/batcher.d.cts +13 -1
- package/dist/cjs/debouncer.cjs +5 -0
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +10 -2
- package/dist/cjs/event-client.cjs.map +1 -1
- package/dist/cjs/event-client.d.cts +3 -0
- package/dist/cjs/index.cjs +13 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +5 -0
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +11 -3
- package/dist/cjs/rate-limiter.cjs +5 -0
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +11 -0
- package/dist/cjs/throttler.cjs +5 -0
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +11 -0
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/esm/async-batcher.d.ts +97 -16
- package/dist/esm/async-batcher.js +67 -5
- package/dist/esm/async-batcher.js.map +1 -1
- package/dist/esm/async-debouncer.d.ts +79 -9
- package/dist/esm/async-debouncer.js +54 -19
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +103 -9
- package/dist/esm/async-queuer.js +59 -2
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +94 -27
- package/dist/esm/async-rate-limiter.js +52 -2
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-retryer.d.ts +312 -0
- package/dist/esm/async-retryer.js +286 -0
- package/dist/esm/async-retryer.js.map +1 -0
- package/dist/esm/async-throttler.d.ts +85 -9
- package/dist/esm/async-throttler.js +102 -53
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +13 -1
- package/dist/esm/batcher.js +5 -0
- package/dist/esm/batcher.js.map +1 -1
- package/dist/esm/debouncer.d.ts +10 -2
- package/dist/esm/debouncer.js +6 -1
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/event-client.d.ts +3 -0
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +23 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +11 -3
- package/dist/esm/queuer.js +6 -1
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +11 -0
- package/dist/esm/rate-limiter.js +6 -1
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +11 -0
- package/dist/esm/throttler.js +6 -1
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +13 -3
- package/src/async-batcher.ts +146 -19
- package/src/async-debouncer.ts +120 -30
- package/src/async-queuer.ts +149 -11
- package/src/async-rate-limiter.ts +131 -30
- package/src/async-retryer.ts +669 -0
- package/src/async-throttler.ts +198 -72
- package/src/batcher.ts +18 -1
- package/src/debouncer.ts +19 -2
- package/src/event-client.ts +3 -0
- package/src/index.ts +2 -0
- package/src/queuer.ts +21 -3
- package/src/rate-limiter.ts +20 -0
- package/src/throttler.ts +20 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from '@tanstack/store';
|
|
2
|
+
import { AsyncRetryer, AsyncRetryerOptions } from './async-retryer.js';
|
|
2
3
|
import { AnyAsyncFunction } from './types.js';
|
|
3
4
|
export interface AsyncDebouncerState<TFn extends AnyAsyncFunction> {
|
|
4
5
|
/**
|
|
@@ -46,6 +47,10 @@ export interface AsyncDebouncerState<TFn extends AnyAsyncFunction> {
|
|
|
46
47
|
* Options for configuring an async debounced function
|
|
47
48
|
*/
|
|
48
49
|
export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {
|
|
50
|
+
/**
|
|
51
|
+
* Options for configuring the underlying async retryer
|
|
52
|
+
*/
|
|
53
|
+
asyncRetryerOptions?: AsyncRetryerOptions<TFn>;
|
|
49
54
|
/**
|
|
50
55
|
* Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
51
56
|
* Can be a boolean or a function that returns a boolean.
|
|
@@ -71,7 +76,7 @@ export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {
|
|
|
71
76
|
* If provided, the handler will be called with the error and debouncer instance.
|
|
72
77
|
* This can be used alongside throwOnError - the handler will be called before any error is thrown.
|
|
73
78
|
*/
|
|
74
|
-
onError?: (error:
|
|
79
|
+
onError?: (error: Error, args: Parameters<TFn>, debouncer: AsyncDebouncer<TFn>) => void;
|
|
75
80
|
/**
|
|
76
81
|
* Optional callback to call when the debounced function is executed
|
|
77
82
|
*/
|
|
@@ -98,9 +103,26 @@ export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {
|
|
|
98
103
|
*/
|
|
99
104
|
wait: number | ((debouncer: AsyncDebouncer<TFn>) => number);
|
|
100
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Utility function for sharing common `AsyncDebouncerOptions` options between different `AsyncDebouncer` instances.
|
|
108
|
+
*/
|
|
109
|
+
export declare function asyncDebouncerOptions<TFn extends AnyAsyncFunction = AnyAsyncFunction, TOptions extends Partial<AsyncDebouncerOptions<TFn>> = Partial<AsyncDebouncerOptions<TFn>>>(options: TOptions): TOptions;
|
|
101
110
|
/**
|
|
102
111
|
* A class that creates an async debounced function.
|
|
103
112
|
*
|
|
113
|
+
* Async vs Sync Versions:
|
|
114
|
+
* The async version provides advanced features over the sync Debouncer:
|
|
115
|
+
* - Returns promises that can be awaited for debounced function results
|
|
116
|
+
* - Built-in retry support via AsyncRetryer integration
|
|
117
|
+
* - Abort support to cancel in-flight executions
|
|
118
|
+
* - Cancel support to prevent pending executions from starting
|
|
119
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
120
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
121
|
+
*
|
|
122
|
+
* The sync Debouncer is lighter weight and simpler when you don't need async features,
|
|
123
|
+
* return values, or execution control.
|
|
124
|
+
*
|
|
125
|
+
* What is Debouncing?
|
|
104
126
|
* Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.
|
|
105
127
|
* Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing
|
|
106
128
|
* or input changes where you only want to execute the handler after the events have stopped occurring.
|
|
@@ -108,10 +130,6 @@ export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {
|
|
|
108
130
|
* Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until
|
|
109
131
|
* the function stops being called for the specified delay period.
|
|
110
132
|
*
|
|
111
|
-
* Unlike the non-async Debouncer, this async version supports returning values from the debounced function,
|
|
112
|
-
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
113
|
-
* instead of setting the result on a state variable from within the debounced function.
|
|
114
|
-
*
|
|
115
133
|
* Error Handling:
|
|
116
134
|
* - If an `onError` handler is provided, it will be called with the error and debouncer instance
|
|
117
135
|
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
@@ -149,7 +167,12 @@ export declare class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
149
167
|
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>;
|
|
150
168
|
key: string;
|
|
151
169
|
options: AsyncDebouncerOptions<TFn>;
|
|
170
|
+
asyncRetryers: Map<number, AsyncRetryer<TFn>>;
|
|
152
171
|
constructor(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn>);
|
|
172
|
+
/**
|
|
173
|
+
* Emits a change event for the async debouncer instance. Mostly useful for devtools.
|
|
174
|
+
*/
|
|
175
|
+
_emit: () => void;
|
|
153
176
|
/**
|
|
154
177
|
* Updates the async debouncer options
|
|
155
178
|
*/
|
|
@@ -174,7 +197,34 @@ export declare class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
174
197
|
*/
|
|
175
198
|
flush: () => Promise<ReturnType<TFn> | undefined>;
|
|
176
199
|
/**
|
|
177
|
-
*
|
|
200
|
+
* Returns the AbortSignal for a specific execution.
|
|
201
|
+
* If no maybeExecuteCount is provided, returns the signal for the most recent execution.
|
|
202
|
+
* Returns null if no execution is found or not currently executing.
|
|
203
|
+
*
|
|
204
|
+
* @param maybeExecuteCount - Optional specific execution to get signal for
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const debouncer = new AsyncDebouncer(
|
|
208
|
+
* async (searchTerm: string) => {
|
|
209
|
+
* const signal = debouncer.getAbortSignal()
|
|
210
|
+
* if (signal) {
|
|
211
|
+
* const response = await fetch(`/api/search?q=${searchTerm}`, { signal })
|
|
212
|
+
* return response.json()
|
|
213
|
+
* }
|
|
214
|
+
* },
|
|
215
|
+
* { wait: 300 }
|
|
216
|
+
* )
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
getAbortSignal(maybeExecuteCount?: number): AbortSignal | null;
|
|
220
|
+
/**
|
|
221
|
+
* Aborts all ongoing executions with the internal abort controllers.
|
|
222
|
+
* Does NOT cancel any pending execution that have not started yet.
|
|
223
|
+
*/
|
|
224
|
+
abort: () => void;
|
|
225
|
+
/**
|
|
226
|
+
* Cancels any pending execution that have not started yet.
|
|
227
|
+
* Does NOT abort any execution already in progress.
|
|
178
228
|
*/
|
|
179
229
|
cancel: () => void;
|
|
180
230
|
/**
|
|
@@ -187,9 +237,29 @@ export declare class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
187
237
|
* The debounced function will only execute once the wait period has elapsed without any new calls.
|
|
188
238
|
* If called again during the wait period, the timer resets and a new wait period begins.
|
|
189
239
|
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
240
|
+
* Async vs Sync Versions:
|
|
241
|
+
* The async version provides advanced features over the sync debounce function:
|
|
242
|
+
* - Returns promises that can be awaited for debounced function results
|
|
243
|
+
* - Built-in retry support via AsyncRetryer integration
|
|
244
|
+
* - Abort support to cancel in-flight executions
|
|
245
|
+
* - Cancel support to prevent pending executions from starting
|
|
246
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
247
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
248
|
+
*
|
|
249
|
+
* The sync debounce function is lighter weight and simpler when you don't need async features,
|
|
250
|
+
* return values, or execution control.
|
|
251
|
+
*
|
|
252
|
+
* What is Debouncing?
|
|
253
|
+
* Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.
|
|
254
|
+
* Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing
|
|
255
|
+
* or input changes where you only want to execute the handler after the events have stopped occurring.
|
|
256
|
+
*
|
|
257
|
+
* Configuration Options:
|
|
258
|
+
* - `wait`: Delay in milliseconds to wait after the last call (required)
|
|
259
|
+
* - `leading`: Execute on the leading edge of the timeout (default: false)
|
|
260
|
+
* - `trailing`: Execute on the trailing edge of the timeout (default: true)
|
|
261
|
+
* - `enabled`: Whether the debouncer is enabled (default: true)
|
|
262
|
+
* - `asyncRetryerOptions`: Configure retry behavior for executions
|
|
193
263
|
*
|
|
194
264
|
* Error Handling:
|
|
195
265
|
* - If an `onError` handler is provided, it will be called with the error and debouncer instance
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from "@tanstack/store";
|
|
2
|
+
import { AsyncRetryer } from "./async-retryer.js";
|
|
2
3
|
import { parseFunctionOrValue, createKey } from "./utils.js";
|
|
3
4
|
import { emitChange, pacerEventClient } from "./event-client.js";
|
|
4
5
|
function getDefaultAsyncDebouncerState() {
|
|
@@ -15,7 +16,13 @@ function getDefaultAsyncDebouncerState() {
|
|
|
15
16
|
successCount: 0
|
|
16
17
|
};
|
|
17
18
|
}
|
|
19
|
+
function asyncDebouncerOptions(options) {
|
|
20
|
+
return options;
|
|
21
|
+
}
|
|
18
22
|
const defaultOptions = {
|
|
23
|
+
asyncRetryerOptions: {
|
|
24
|
+
maxAttempts: 1
|
|
25
|
+
},
|
|
19
26
|
enabled: true,
|
|
20
27
|
leading: false,
|
|
21
28
|
trailing: true,
|
|
@@ -25,9 +32,10 @@ class AsyncDebouncer {
|
|
|
25
32
|
constructor(fn, initialOptions) {
|
|
26
33
|
this.fn = fn;
|
|
27
34
|
this.store = new Store(getDefaultAsyncDebouncerState());
|
|
28
|
-
this
|
|
35
|
+
this.asyncRetryers = /* @__PURE__ */ new Map();
|
|
29
36
|
this.#timeoutId = null;
|
|
30
37
|
this.#resolvePreviousPromise = null;
|
|
38
|
+
this._emit = () => emitChange("AsyncDebouncer", this);
|
|
31
39
|
this.setOptions = (newOptions) => {
|
|
32
40
|
this.options = { ...this.options, ...newOptions };
|
|
33
41
|
if (!this.#getEnabled()) {
|
|
@@ -87,10 +95,15 @@ class AsyncDebouncer {
|
|
|
87
95
|
};
|
|
88
96
|
this.#execute = async (...args) => {
|
|
89
97
|
if (!this.#getEnabled()) return void 0;
|
|
90
|
-
|
|
98
|
+
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1;
|
|
91
99
|
try {
|
|
92
100
|
this.#setState({ isExecuting: true });
|
|
93
|
-
const
|
|
101
|
+
const currentAsyncRetryer = new AsyncRetryer(this.fn, {
|
|
102
|
+
...this.options.asyncRetryerOptions,
|
|
103
|
+
key: `${this.key}-retryer-${currentMaybeExecuteCount}`
|
|
104
|
+
});
|
|
105
|
+
this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer);
|
|
106
|
+
const result = await currentAsyncRetryer.execute(...args);
|
|
94
107
|
this.#setState({
|
|
95
108
|
lastResult: result,
|
|
96
109
|
successCount: this.store.state.successCount + 1
|
|
@@ -105,24 +118,22 @@ class AsyncDebouncer {
|
|
|
105
118
|
throw error;
|
|
106
119
|
}
|
|
107
120
|
} finally {
|
|
121
|
+
this.asyncRetryers.delete(currentMaybeExecuteCount);
|
|
108
122
|
this.#setState({
|
|
109
123
|
isExecuting: false,
|
|
110
124
|
isPending: false,
|
|
111
125
|
lastArgs: void 0,
|
|
112
126
|
settleCount: this.store.state.settleCount + 1
|
|
113
127
|
});
|
|
114
|
-
this.#abortController = null;
|
|
115
128
|
this.options.onSettled?.(args, this);
|
|
116
129
|
}
|
|
117
130
|
return this.store.state.lastResult;
|
|
118
131
|
};
|
|
119
132
|
this.flush = async () => {
|
|
120
133
|
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
121
|
-
this
|
|
122
|
-
this.#
|
|
123
|
-
|
|
124
|
-
this.#resolvePreviousPromiseInternal();
|
|
125
|
-
return result;
|
|
134
|
+
const { lastArgs } = this.store.state;
|
|
135
|
+
this.#cancelPendingExecution();
|
|
136
|
+
return await this.#execute(...lastArgs);
|
|
126
137
|
}
|
|
127
138
|
return void 0;
|
|
128
139
|
};
|
|
@@ -143,23 +154,23 @@ class AsyncDebouncer {
|
|
|
143
154
|
this.#resolvePreviousPromiseInternal();
|
|
144
155
|
this.#setState({
|
|
145
156
|
isPending: false,
|
|
146
|
-
isExecuting: false,
|
|
147
157
|
lastArgs: void 0
|
|
148
158
|
});
|
|
149
159
|
};
|
|
150
|
-
this
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
160
|
+
this.abort = () => {
|
|
161
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
162
|
+
this.asyncRetryers.clear();
|
|
163
|
+
this.#setState({
|
|
164
|
+
isExecuting: false
|
|
165
|
+
});
|
|
155
166
|
};
|
|
156
167
|
this.cancel = () => {
|
|
157
168
|
this.#cancelPendingExecution();
|
|
158
|
-
this.#abortExecution();
|
|
159
169
|
this.#setState({ canLeadingExecute: true });
|
|
160
170
|
};
|
|
161
171
|
this.reset = () => {
|
|
162
172
|
this.#setState(getDefaultAsyncDebouncerState());
|
|
173
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
163
174
|
};
|
|
164
175
|
this.key = createKey(initialOptions.key);
|
|
165
176
|
this.options = {
|
|
@@ -174,7 +185,6 @@ class AsyncDebouncer {
|
|
|
174
185
|
this.setOptions(event.payload.options);
|
|
175
186
|
});
|
|
176
187
|
}
|
|
177
|
-
#abortController;
|
|
178
188
|
#timeoutId;
|
|
179
189
|
#resolvePreviousPromise;
|
|
180
190
|
#setState;
|
|
@@ -184,7 +194,31 @@ class AsyncDebouncer {
|
|
|
184
194
|
#resolvePreviousPromiseInternal;
|
|
185
195
|
#clearTimeout;
|
|
186
196
|
#cancelPendingExecution;
|
|
187
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Returns the AbortSignal for a specific execution.
|
|
199
|
+
* If no maybeExecuteCount is provided, returns the signal for the most recent execution.
|
|
200
|
+
* Returns null if no execution is found or not currently executing.
|
|
201
|
+
*
|
|
202
|
+
* @param maybeExecuteCount - Optional specific execution to get signal for
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const debouncer = new AsyncDebouncer(
|
|
206
|
+
* async (searchTerm: string) => {
|
|
207
|
+
* const signal = debouncer.getAbortSignal()
|
|
208
|
+
* if (signal) {
|
|
209
|
+
* const response = await fetch(`/api/search?q=${searchTerm}`, { signal })
|
|
210
|
+
* return response.json()
|
|
211
|
+
* }
|
|
212
|
+
* },
|
|
213
|
+
* { wait: 300 }
|
|
214
|
+
* )
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
getAbortSignal(maybeExecuteCount) {
|
|
218
|
+
const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount;
|
|
219
|
+
const retryer = this.asyncRetryers.get(count);
|
|
220
|
+
return retryer?.getAbortSignal() ?? null;
|
|
221
|
+
}
|
|
188
222
|
}
|
|
189
223
|
function asyncDebounce(fn, initialOptions) {
|
|
190
224
|
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
@@ -192,6 +226,7 @@ function asyncDebounce(fn, initialOptions) {
|
|
|
192
226
|
}
|
|
193
227
|
export {
|
|
194
228
|
AsyncDebouncer,
|
|
195
|
-
asyncDebounce
|
|
229
|
+
asyncDebounce,
|
|
230
|
+
asyncDebouncerOptions
|
|
196
231
|
};
|
|
197
232
|
//# sourceMappingURL=async-debouncer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-debouncer.js","sources":["../../src/async-debouncer.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { createKey, parseFunctionOrValue } from './utils'\nimport { emitChange, pacerEventClient } from './event-client'\nimport type { AnyAsyncFunction, OptionalKeys } from './types'\n\nexport interface AsyncDebouncerState<TFn extends AnyAsyncFunction> {\n /**\n * Whether the debouncer can execute on the leading edge of the timeout\n */\n canLeadingExecute: boolean\n /**\n * Number of function executions that have resulted in errors\n */\n errorCount: number\n /**\n * Whether the debounced function is currently executing asynchronously\n */\n isExecuting: boolean\n /**\n * Whether the debouncer is waiting for the timeout to trigger execution\n */\n isPending: boolean\n /**\n * The arguments from the most recent call to maybeExecute\n */\n lastArgs: Parameters<TFn> | undefined\n /**\n * The result from the most recent successful function execution\n */\n lastResult: ReturnType<TFn> | undefined\n /**\n * Number of times maybeExecute has been called (for reduction calculations)\n */\n maybeExecuteCount: number\n /**\n * Number of function executions that have completed (either successfully or with errors)\n */\n settleCount: number\n /**\n * Current execution status - 'idle' when not active, 'pending' when waiting, 'executing' when running, 'settled' when completed\n */\n status: 'disabled' | 'idle' | 'pending' | 'executing' | 'settled'\n /**\n * Number of function executions that have completed successfully\n */\n successCount: number\n}\n\nfunction getDefaultAsyncDebouncerState<\n TFn extends AnyAsyncFunction,\n>(): AsyncDebouncerState<TFn> {\n return {\n canLeadingExecute: true,\n errorCount: 0,\n isExecuting: false,\n isPending: false,\n lastArgs: undefined,\n lastResult: undefined,\n maybeExecuteCount: 0,\n settleCount: 0,\n status: 'idle',\n successCount: 0,\n }\n}\n\n/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {\n /**\n * Whether the debouncer 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 | ((debouncer: AsyncDebouncer<TFn>) => boolean)\n /**\n * Initial state for the async debouncer\n */\n initialState?: Partial<AsyncDebouncerState<TFn>>\n /**\n * Optional key to identify this async debouncer instance.\n * If provided, the async debouncer will be identified by this key in the devtools and PacerProvider if applicable.\n */\n key?: string\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Optional error handler for when the debounced function throws.\n * If provided, the handler will be called with the error and debouncer instance.\n * This can be used alongside throwOnError - the handler will be called before any error is thrown.\n */\n onError?: (\n error: unknown,\n args: Parameters<TFn>,\n debouncer: AsyncDebouncer<TFn>,\n ) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSettled?: (args: Parameters<TFn>, debouncer: AsyncDebouncer<TFn>) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSuccess?: (\n result: ReturnType<TFn>,\n args: Parameters<TFn>,\n debouncer: AsyncDebouncer<TFn>,\n ) => void\n /**\n * Whether to throw errors when they occur.\n * Defaults to true if no onError handler is provided, false if an onError handler is provided.\n * Can be explicitly set to override these defaults.\n */\n throwOnError?: boolean\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((debouncer: AsyncDebouncer<TFn>) => number)\n}\n\ntype AsyncDebouncerOptionsWithOptionalCallbacks = OptionalKeys<\n AsyncDebouncerOptions<any>,\n 'initialState' | 'onError' | 'onSettled' | 'onSuccess' | 'key'\n>\n\nconst defaultOptions: AsyncDebouncerOptionsWithOptionalCallbacks = {\n enabled: true,\n leading: false,\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates an async debounced function.\n *\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * Unlike the non-async Debouncer, this async version supports returning values from the debounced function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the debounced function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying store\n *\n * State Management:\n * - The debouncer uses a reactive store for state management\n * - Use `initialState` to provide initial state values when creating the async debouncer\n * - The state includes canLeadingExecute, error count, execution status, and success/settle counts\n * - State can be accessed via the `store` property and its `state` getter\n * - The store is reactive and will notify subscribers of state changes\n *\n * @example\n * ```ts\n * const asyncDebouncer = new AsyncDebouncer(async (value: string) => {\n * const results = await searchAPI(value);\n * return results; // Return value is preserved\n * }, {\n * wait: 500,\n * onError: (error) => {\n * console.error('Search failed:', error);\n * }\n * });\n *\n * // Called on each keystroke but only executes after 500ms of no typing\n * // Returns the API response directly\n * const results = await asyncDebouncer.maybeExecute(inputElement.value);\n * ```\n */\nexport class AsyncDebouncer<TFn extends AnyAsyncFunction> {\n readonly store: Store<Readonly<AsyncDebouncerState<TFn>>> = new Store<\n AsyncDebouncerState<TFn>\n >(getDefaultAsyncDebouncerState<TFn>())\n key: string\n options: AsyncDebouncerOptions<TFn>\n #abortController: AbortController | null = null\n #timeoutId: NodeJS.Timeout | null = null\n #resolvePreviousPromise:\n | ((value?: ReturnType<TFn> | undefined) => void)\n | null = null\n\n constructor(\n public fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n ) {\n this.key = createKey(initialOptions.key)\n this.options = {\n ...defaultOptions,\n ...initialOptions,\n throwOnError: initialOptions.throwOnError ?? !initialOptions.onError,\n }\n this.#setState(this.options.initialState ?? {})\n\n pacerEventClient.on('d-AsyncDebouncer', (event) => {\n if (event.payload.key !== this.key) return\n this.#setState(event.payload.store.state as AsyncDebouncerState<TFn>)\n this.setOptions(event.payload.options)\n })\n }\n\n /**\n * Updates the async debouncer options\n */\n setOptions = (newOptions: Partial<AsyncDebouncerOptions<TFn>>): void => {\n this.options = { ...this.options, ...newOptions }\n\n // Cancel pending execution if the debouncer is disabled\n if (!this.#getEnabled()) {\n this.cancel()\n }\n }\n\n #setState = (newState: Partial<AsyncDebouncerState<TFn>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isPending, isExecuting, settleCount } = combinedState\n return {\n ...combinedState,\n status: !this.#getEnabled()\n ? 'disabled'\n : isPending\n ? 'pending'\n : isExecuting\n ? 'executing'\n : settleCount > 0\n ? 'settled'\n : 'idle',\n }\n })\n emitChange('AsyncDebouncer', this)\n }\n\n /**\n * Returns the current debouncer enabled state\n */\n #getEnabled = (): boolean => {\n return !!parseFunctionOrValue(this.options.enabled, this)\n }\n\n /**\n * Returns the current debouncer wait state\n */\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Attempts to execute the debounced function.\n * If a call is already in progress, it will be queued.\n *\n * Error Handling:\n * - If the debounced function throws and no `onError` handler is configured,\n * the error will be thrown from this method.\n * - If an `onError` handler is configured, errors will be caught and passed to the handler,\n * and this method will return undefined.\n * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.\n *\n * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError\n * @throws The error from the debounced function if no onError handler is configured\n */\n maybeExecute = async (\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> => {\n if (!this.#getEnabled()) return undefined\n this.#cancelPendingExecution()\n this.#setState({\n lastArgs: args,\n maybeExecuteCount: this.store.state.maybeExecuteCount + 1,\n })\n\n // Handle leading execution\n if (this.options.leading && this.store.state.canLeadingExecute) {\n this.#setState({ canLeadingExecute: false })\n await this.#execute(...args)\n return this.store.state.lastResult\n }\n\n // Handle trailing execution\n if (this.options.trailing && this.#getEnabled()) {\n this.#setState({ isPending: true })\n }\n\n return new Promise((resolve, reject) => {\n this.#resolvePreviousPromise = resolve\n // this.#rejectPreviousPromise = reject\n this.#timeoutId = setTimeout(async () => {\n // Execute trailing if enabled\n if (this.options.trailing && this.store.state.lastArgs) {\n try {\n await this.#execute(...this.store.state.lastArgs)\n } catch (error) {\n reject(error)\n }\n }\n\n // Reset state and resolve\n this.#setState({ canLeadingExecute: true })\n this.#resolvePreviousPromise = null\n resolve(this.store.state.lastResult)\n }, this.#getWait())\n })\n }\n\n #execute = async (\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> => {\n if (!this.#getEnabled()) return undefined\n this.#abortController = new AbortController()\n try {\n this.#setState({ isExecuting: true })\n const result = await this.fn(...args) // EXECUTE!\n this.#setState({\n lastResult: result,\n successCount: this.store.state.successCount + 1,\n })\n this.options.onSuccess?.(result, args, this)\n } catch (error) {\n this.#setState({\n errorCount: this.store.state.errorCount + 1,\n })\n this.options.onError?.(error, args, this)\n if (this.options.throwOnError) {\n throw error\n }\n } finally {\n this.#setState({\n isExecuting: false,\n isPending: false,\n lastArgs: undefined,\n settleCount: this.store.state.settleCount + 1,\n })\n this.#abortController = null\n this.options.onSettled?.(args, this)\n }\n return this.store.state.lastResult\n }\n\n /**\n * Processes the current pending execution immediately\n */\n flush = async (): Promise<ReturnType<TFn> | undefined> => {\n if (this.store.state.isPending && this.store.state.lastArgs) {\n this.#abortExecution() // abort any current execution\n this.#clearTimeout() // clear any existing timeout\n const result = await this.#execute(...this.store.state.lastArgs)\n\n // Resolve any pending promise from maybeExecute\n this.#resolvePreviousPromiseInternal()\n\n return result\n }\n return undefined\n }\n\n #resolvePreviousPromiseInternal = (): void => {\n if (this.#resolvePreviousPromise) {\n this.#resolvePreviousPromise(this.store.state.lastResult)\n this.#resolvePreviousPromise = null\n }\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = null\n }\n }\n\n #cancelPendingExecution = (): void => {\n this.#clearTimeout()\n this.#resolvePreviousPromiseInternal()\n this.#setState({\n isPending: false,\n isExecuting: false,\n lastArgs: undefined,\n })\n }\n\n #abortExecution = (): void => {\n if (this.#abortController) {\n this.#abortController.abort()\n this.#abortController = null\n }\n }\n\n /**\n * Cancels any pending execution or aborts any execution in progress\n */\n cancel = (): void => {\n this.#cancelPendingExecution()\n this.#abortExecution()\n this.#setState({ canLeadingExecute: true })\n }\n\n /**\n * Resets the debouncer state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultAsyncDebouncerState<TFn>())\n }\n}\n\n/**\n * Creates an async debounced function that delays execution until after a specified wait time.\n * The debounced function will only execute once the wait period has elapsed without any new calls.\n * If called again during the wait period, the timer resets and a new wait period begins.\n *\n * Unlike the non-async Debouncer, this async version supports returning values from the debounced function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the debounced function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - The error state can be checked using the underlying AsyncDebouncer instance\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async debouncer\n * - Use `onSuccess` callback to react to successful function execution and implement custom logic\n * - Use `onError` callback to react to function execution errors and implement custom error handling\n * - Use `onSettled` callback to react to function execution completion (success or error) and implement custom logic\n * - The state includes canLeadingExecute, error count, execution status, and success/settle counts\n * - State can be accessed via `asyncDebouncer.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `asyncDebouncer.state`\n *\n * @example\n * ```ts\n * const debounced = asyncDebounce(async (value: string) => {\n * const result = await saveToAPI(value);\n * return result; // Return value is preserved\n * }, {\n * wait: 1000,\n * onError: (error) => {\n * console.error('API call failed:', error);\n * },\n * throwOnError: true // Will both log the error and throw it\n * });\n *\n * // Will only execute once, 1 second after the last call\n * // Returns the API response directly\n * const result = await debounced(\"third\");\n * ```\n */\nexport function asyncDebounce<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute\n}\n"],"names":[],"mappings":";;;AAgDA,SAAS,gCAEqB;AAC5B,SAAO;AAAA,IACL,mBAAmB;AAAA,IACnB,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,mBAAmB;AAAA,IACnB,aAAa;AAAA,IACb,QAAQ;AAAA,IACR,cAAc;AAAA,EAAA;AAElB;AAwEA,MAAM,iBAA6D;AAAA,EACjE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,UAAU;AAAA,EACV,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ACH,cAAc,eAAe,gBAAgB,CAAC,eAAe;AAAA,IAAA;AAE/D,SAAK,UAAU,KAAK,QAAQ,gBAAgB,CAAA,CAAE;AAE9C,qBAAiB,GAAG,oBAAoB,CAAC,UAAU;AACjD,UAAI,MAAM,QAAQ,QAAQ,KAAK,IAAK;AACpC,WAAK,UAAU,MAAM,QAAQ,MAAM,KAAiC;AACpE,WAAK,WAAW,MAAM,QAAQ,OAAO;AAAA,IAAA,CACtC;AAAA,EAAA;AAAA,EAtBH;AAAA,EACA;AAAA,EACA;AAAA,EAmCA;AAAA,EA0BA;AAAA,EAOA;AAAA,EA6DA;AAAA,EAmDA;AAAA,EAOA;AAAA,EAOA;AAAA,EAUA;AAsBF;AA8CO,SAAS,cACd,IACA,gBACA;AACA,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AAC5D,SAAO,eAAe;AACxB;"}
|
|
1
|
+
{"version":3,"file":"async-debouncer.js","sources":["../../src/async-debouncer.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { AsyncRetryer } from './async-retryer'\nimport { createKey, parseFunctionOrValue } from './utils'\nimport { emitChange, pacerEventClient } from './event-client'\nimport type { AsyncRetryerOptions } from './async-retryer'\nimport type { AnyAsyncFunction, OptionalKeys } from './types'\n\nexport interface AsyncDebouncerState<TFn extends AnyAsyncFunction> {\n /**\n * Whether the debouncer can execute on the leading edge of the timeout\n */\n canLeadingExecute: boolean\n /**\n * Number of function executions that have resulted in errors\n */\n errorCount: number\n /**\n * Whether the debounced function is currently executing asynchronously\n */\n isExecuting: boolean\n /**\n * Whether the debouncer is waiting for the timeout to trigger execution\n */\n isPending: boolean\n /**\n * The arguments from the most recent call to maybeExecute\n */\n lastArgs: Parameters<TFn> | undefined\n /**\n * The result from the most recent successful function execution\n */\n lastResult: ReturnType<TFn> | undefined\n /**\n * Number of times maybeExecute has been called (for reduction calculations)\n */\n maybeExecuteCount: number\n /**\n * Number of function executions that have completed (either successfully or with errors)\n */\n settleCount: number\n /**\n * Current execution status - 'idle' when not active, 'pending' when waiting, 'executing' when running, 'settled' when completed\n */\n status: 'disabled' | 'idle' | 'pending' | 'executing' | 'settled'\n /**\n * Number of function executions that have completed successfully\n */\n successCount: number\n}\n\nfunction getDefaultAsyncDebouncerState<\n TFn extends AnyAsyncFunction,\n>(): AsyncDebouncerState<TFn> {\n return {\n canLeadingExecute: true,\n errorCount: 0,\n isExecuting: false,\n isPending: false,\n lastArgs: undefined,\n lastResult: undefined,\n maybeExecuteCount: 0,\n settleCount: 0,\n status: 'idle',\n successCount: 0,\n }\n}\n\n/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction> {\n /**\n * Options for configuring the underlying async retryer\n */\n asyncRetryerOptions?: AsyncRetryerOptions<TFn>\n /**\n * Whether the debouncer 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 | ((debouncer: AsyncDebouncer<TFn>) => boolean)\n /**\n * Initial state for the async debouncer\n */\n initialState?: Partial<AsyncDebouncerState<TFn>>\n /**\n * Optional key to identify this async debouncer instance.\n * If provided, the async debouncer will be identified by this key in the devtools and PacerProvider if applicable.\n */\n key?: string\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Optional error handler for when the debounced function throws.\n * If provided, the handler will be called with the error and debouncer instance.\n * This can be used alongside throwOnError - the handler will be called before any error is thrown.\n */\n onError?: (\n error: Error,\n args: Parameters<TFn>,\n debouncer: AsyncDebouncer<TFn>,\n ) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSettled?: (args: Parameters<TFn>, debouncer: AsyncDebouncer<TFn>) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSuccess?: (\n result: ReturnType<TFn>,\n args: Parameters<TFn>,\n debouncer: AsyncDebouncer<TFn>,\n ) => void\n /**\n * Whether to throw errors when they occur.\n * Defaults to true if no onError handler is provided, false if an onError handler is provided.\n * Can be explicitly set to override these defaults.\n */\n throwOnError?: boolean\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((debouncer: AsyncDebouncer<TFn>) => number)\n}\n\n/**\n * Utility function for sharing common `AsyncDebouncerOptions` options between different `AsyncDebouncer` instances.\n */\nexport function asyncDebouncerOptions<\n TFn extends AnyAsyncFunction = AnyAsyncFunction,\n TOptions extends Partial<AsyncDebouncerOptions<TFn>> = Partial<\n AsyncDebouncerOptions<TFn>\n >,\n>(options: TOptions): TOptions {\n return options\n}\n\ntype AsyncDebouncerOptionsWithOptionalCallbacks = OptionalKeys<\n AsyncDebouncerOptions<any>,\n 'initialState' | 'onError' | 'onSettled' | 'onSuccess' | 'key'\n>\n\nconst defaultOptions: AsyncDebouncerOptionsWithOptionalCallbacks = {\n asyncRetryerOptions: {\n maxAttempts: 1,\n },\n enabled: true,\n leading: false,\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates an async debounced function.\n *\n * Async vs Sync Versions:\n * The async version provides advanced features over the sync Debouncer:\n * - Returns promises that can be awaited for debounced function results\n * - Built-in retry support via AsyncRetryer integration\n * - Abort support to cancel in-flight executions\n * - Cancel support to prevent pending executions from starting\n * - Comprehensive error handling with onError callbacks and throwOnError control\n * - Detailed execution tracking (success/error/settle counts)\n *\n * The sync Debouncer is lighter weight and simpler when you don't need async features,\n * return values, or execution control.\n *\n * What is Debouncing?\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying store\n *\n * State Management:\n * - The debouncer uses a reactive store for state management\n * - Use `initialState` to provide initial state values when creating the async debouncer\n * - The state includes canLeadingExecute, error count, execution status, and success/settle counts\n * - State can be accessed via the `store` property and its `state` getter\n * - The store is reactive and will notify subscribers of state changes\n *\n * @example\n * ```ts\n * const asyncDebouncer = new AsyncDebouncer(async (value: string) => {\n * const results = await searchAPI(value);\n * return results; // Return value is preserved\n * }, {\n * wait: 500,\n * onError: (error) => {\n * console.error('Search failed:', error);\n * }\n * });\n *\n * // Called on each keystroke but only executes after 500ms of no typing\n * // Returns the API response directly\n * const results = await asyncDebouncer.maybeExecute(inputElement.value);\n * ```\n */\nexport class AsyncDebouncer<TFn extends AnyAsyncFunction> {\n readonly store: Store<Readonly<AsyncDebouncerState<TFn>>> = new Store<\n AsyncDebouncerState<TFn>\n >(getDefaultAsyncDebouncerState<TFn>())\n key: string\n options: AsyncDebouncerOptions<TFn>\n asyncRetryers = new Map<number, AsyncRetryer<TFn>>()\n #timeoutId: NodeJS.Timeout | null = null\n #resolvePreviousPromise:\n | ((value?: ReturnType<TFn> | undefined) => void)\n | null = null\n\n constructor(\n public fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n ) {\n this.key = createKey(initialOptions.key)\n this.options = {\n ...defaultOptions,\n ...initialOptions,\n throwOnError: initialOptions.throwOnError ?? !initialOptions.onError,\n }\n this.#setState(this.options.initialState ?? {})\n\n pacerEventClient.on('d-AsyncDebouncer', (event) => {\n if (event.payload.key !== this.key) return\n this.#setState(event.payload.store.state as AsyncDebouncerState<TFn>)\n this.setOptions(event.payload.options)\n })\n }\n\n /**\n * Emits a change event for the async debouncer instance. Mostly useful for devtools.\n */\n _emit = () => emitChange('AsyncDebouncer', this)\n\n /**\n * Updates the async debouncer options\n */\n setOptions = (newOptions: Partial<AsyncDebouncerOptions<TFn>>): void => {\n this.options = { ...this.options, ...newOptions }\n\n // Cancel pending execution if the debouncer is disabled\n if (!this.#getEnabled()) {\n this.cancel()\n }\n }\n\n #setState = (newState: Partial<AsyncDebouncerState<TFn>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isPending, isExecuting, settleCount } = combinedState\n return {\n ...combinedState,\n status: !this.#getEnabled()\n ? 'disabled'\n : isPending\n ? 'pending'\n : isExecuting\n ? 'executing'\n : settleCount > 0\n ? 'settled'\n : 'idle',\n }\n })\n emitChange('AsyncDebouncer', this)\n }\n\n /**\n * Returns the current debouncer enabled state\n */\n #getEnabled = (): boolean => {\n return !!parseFunctionOrValue(this.options.enabled, this)\n }\n\n /**\n * Returns the current debouncer wait state\n */\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Attempts to execute the debounced function.\n * If a call is already in progress, it will be queued.\n *\n * Error Handling:\n * - If the debounced function throws and no `onError` handler is configured,\n * the error will be thrown from this method.\n * - If an `onError` handler is configured, errors will be caught and passed to the handler,\n * and this method will return undefined.\n * - The error state can be checked using `getErrorCount()` and `getIsExecuting()`.\n *\n * @returns A promise that resolves with the function's return value, or undefined if an error occurred and was handled by onError\n * @throws The error from the debounced function if no onError handler is configured\n */\n maybeExecute = async (\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> => {\n if (!this.#getEnabled()) return undefined\n this.#cancelPendingExecution()\n this.#setState({\n lastArgs: args,\n maybeExecuteCount: this.store.state.maybeExecuteCount + 1,\n })\n\n // Handle leading execution\n if (this.options.leading && this.store.state.canLeadingExecute) {\n this.#setState({ canLeadingExecute: false })\n await this.#execute(...args)\n return this.store.state.lastResult\n }\n\n // Handle trailing execution\n if (this.options.trailing && this.#getEnabled()) {\n this.#setState({ isPending: true })\n }\n\n return new Promise((resolve, reject) => {\n this.#resolvePreviousPromise = resolve\n // this.#rejectPreviousPromise = reject\n this.#timeoutId = setTimeout(async () => {\n // Execute trailing if enabled\n if (this.options.trailing && this.store.state.lastArgs) {\n try {\n await this.#execute(...this.store.state.lastArgs)\n } catch (error) {\n reject(error)\n }\n }\n\n // Reset state and resolve\n this.#setState({ canLeadingExecute: true })\n this.#resolvePreviousPromise = null\n resolve(this.store.state.lastResult)\n }, this.#getWait())\n })\n }\n\n #execute = async (\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> => {\n if (!this.#getEnabled()) return undefined\n const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1\n\n try {\n this.#setState({ isExecuting: true })\n const currentAsyncRetryer = new AsyncRetryer(this.fn, {\n ...this.options.asyncRetryerOptions,\n key: `${this.key}-retryer-${currentMaybeExecuteCount}`,\n })\n this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer)\n const result = await currentAsyncRetryer.execute(...args) // EXECUTE!\n this.#setState({\n lastResult: result,\n successCount: this.store.state.successCount + 1,\n })\n this.options.onSuccess?.(result as ReturnType<TFn>, args, this)\n } catch (error) {\n this.#setState({\n errorCount: this.store.state.errorCount + 1,\n })\n this.options.onError?.(error as Error, args, this)\n if (this.options.throwOnError) {\n throw error\n }\n } finally {\n this.asyncRetryers.delete(currentMaybeExecuteCount) // dispose retryer\n this.#setState({\n isExecuting: false,\n isPending: false,\n lastArgs: undefined,\n settleCount: this.store.state.settleCount + 1,\n })\n this.options.onSettled?.(args, this)\n }\n return this.store.state.lastResult\n }\n\n /**\n * Processes the current pending execution immediately\n */\n flush = async (): Promise<ReturnType<TFn> | undefined> => {\n if (this.store.state.isPending && this.store.state.lastArgs) {\n const { lastArgs } = this.store.state\n this.#cancelPendingExecution()\n return await this.#execute(...lastArgs)\n }\n return undefined\n }\n\n #resolvePreviousPromiseInternal = (): void => {\n if (this.#resolvePreviousPromise) {\n this.#resolvePreviousPromise(this.store.state.lastResult)\n this.#resolvePreviousPromise = null\n }\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = null\n }\n }\n\n /**\n * Internal cancel without resetting the leading execute state\n */\n #cancelPendingExecution = (): void => {\n this.#clearTimeout()\n this.#resolvePreviousPromiseInternal()\n this.#setState({\n isPending: false,\n lastArgs: undefined,\n })\n }\n\n /**\n * Returns the AbortSignal for a specific execution.\n * If no maybeExecuteCount is provided, returns the signal for the most recent execution.\n * Returns null if no execution is found or not currently executing.\n *\n * @param maybeExecuteCount - Optional specific execution to get signal for\n * @example\n * ```typescript\n * const debouncer = new AsyncDebouncer(\n * async (searchTerm: string) => {\n * const signal = debouncer.getAbortSignal()\n * if (signal) {\n * const response = await fetch(`/api/search?q=${searchTerm}`, { signal })\n * return response.json()\n * }\n * },\n * { wait: 300 }\n * )\n * ```\n */\n getAbortSignal(maybeExecuteCount?: number): AbortSignal | null {\n const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount\n const retryer = this.asyncRetryers.get(count)\n return retryer?.getAbortSignal() ?? null\n }\n\n /**\n * Aborts all ongoing executions with the internal abort controllers.\n * Does NOT cancel any pending execution that have not started yet.\n */\n abort = (): void => {\n this.asyncRetryers.forEach((retryer) => retryer.abort())\n this.asyncRetryers.clear()\n this.#setState({\n isExecuting: false,\n })\n }\n\n /**\n * Cancels any pending execution that have not started yet.\n * Does NOT abort any execution already in progress.\n */\n cancel = (): void => {\n this.#cancelPendingExecution()\n this.#setState({ canLeadingExecute: true })\n }\n\n /**\n * Resets the debouncer state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultAsyncDebouncerState<TFn>())\n this.asyncRetryers.forEach((retryer) => retryer.reset())\n }\n}\n\n/**\n * Creates an async debounced function that delays execution until after a specified wait time.\n * The debounced function will only execute once the wait period has elapsed without any new calls.\n * If called again during the wait period, the timer resets and a new wait period begins.\n *\n * Async vs Sync Versions:\n * The async version provides advanced features over the sync debounce function:\n * - Returns promises that can be awaited for debounced function results\n * - Built-in retry support via AsyncRetryer integration\n * - Abort support to cancel in-flight executions\n * - Cancel support to prevent pending executions from starting\n * - Comprehensive error handling with onError callbacks and throwOnError control\n * - Detailed execution tracking (success/error/settle counts)\n *\n * The sync debounce function is lighter weight and simpler when you don't need async features,\n * return values, or execution control.\n *\n * What is Debouncing?\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Configuration Options:\n * - `wait`: Delay in milliseconds to wait after the last call (required)\n * - `leading`: Execute on the leading edge of the timeout (default: false)\n * - `trailing`: Execute on the trailing edge of the timeout (default: true)\n * - `enabled`: Whether the debouncer is enabled (default: true)\n * - `asyncRetryerOptions`: Configure retry behavior for executions\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - The error state can be checked using the underlying AsyncDebouncer instance\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async debouncer\n * - Use `onSuccess` callback to react to successful function execution and implement custom logic\n * - Use `onError` callback to react to function execution errors and implement custom error handling\n * - Use `onSettled` callback to react to function execution completion (success or error) and implement custom logic\n * - The state includes canLeadingExecute, error count, execution status, and success/settle counts\n * - State can be accessed via `asyncDebouncer.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `asyncDebouncer.state`\n *\n * @example\n * ```ts\n * const debounced = asyncDebounce(async (value: string) => {\n * const result = await saveToAPI(value);\n * return result; // Return value is preserved\n * }, {\n * wait: 1000,\n * onError: (error) => {\n * console.error('API call failed:', error);\n * },\n * throwOnError: true // Will both log the error and throw it\n * });\n *\n * // Will only execute once, 1 second after the last call\n * // Returns the API response directly\n * const result = await debounced(\"third\");\n * ```\n */\nexport function asyncDebounce<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute\n}\n"],"names":[],"mappings":";;;;AAkDA,SAAS,gCAEqB;AAC5B,SAAO;AAAA,IACL,mBAAmB;AAAA,IACnB,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,IACZ,mBAAmB;AAAA,IACnB,aAAa;AAAA,IACb,QAAQ;AAAA,IACR,cAAc;AAAA,EAAA;AAElB;AA0EO,SAAS,sBAKd,SAA6B;AAC7B,SAAO;AACT;AAOA,MAAM,iBAA6D;AAAA,EACjE,qBAAqB;AAAA,IACnB,aAAa;AAAA,EAAA;AAAA,EAEf,SAAS;AAAA,EACT,SAAS;AAAA,EACT,UAAU;AAAA,EACV,MAAM;AACR;AAwDO,MAAM,eAA6C;AAAA,EAYxD,YACS,IACP,gBACA;AAFO,SAAA,KAAA;AAZT,SAAS,QAAmD,IAAI,MAE9D,8BAAA,CAAoC;AAGtC,SAAA,oCAAoB,IAAA;AACpB,SAAA,aAAoC;AACpC,SAAA,0BAEW;AAwBX,SAAA,QAAQ,MAAM,WAAW,kBAAkB,IAAI;AAK/C,SAAA,aAAa,CAAC,eAA0D;AACtE,WAAK,UAAU,EAAE,GAAG,KAAK,SAAS,GAAG,WAAA;AAGrC,UAAI,CAAC,KAAK,eAAe;AACvB,aAAK,OAAA;AAAA,MACP;AAAA,IACF;AAEA,SAAA,YAAY,CAAC,aAAsD;AACjE,WAAK,MAAM,SAAS,CAAC,UAAU;AAC7B,cAAM,gBAAgB;AAAA,UACpB,GAAG;AAAA,UACH,GAAG;AAAA,QAAA;AAEL,cAAM,EAAE,WAAW,aAAa,YAAA,IAAgB;AAChD,eAAO;AAAA,UACL,GAAG;AAAA,UACH,QAAQ,CAAC,KAAK,YAAA,IACV,aACA,YACE,YACA,cACE,cACA,cAAc,IACZ,YACA;AAAA,QAAA;AAAA,MAEd,CAAC;A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AAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA6BA,eAAe,mBAAgD;AAC7D,UAAM,QAAQ,qBAAqB,KAAK,MAAM,MAAM;AACpD,UAAM,UAAU,KAAK,cAAc,IAAI,KAAK;AAC5C,WAAO,SAAS,oBAAoB;AAAA,EACtC;AA8BF;AAkEO,SAAS,cACd,IACA,gBACA;AACA,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AAC5D,SAAO,eAAe;AACxB;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from '@tanstack/store';
|
|
2
|
+
import { AsyncRetryer, AsyncRetryerOptions } from './async-retryer.js';
|
|
2
3
|
import { QueuePosition } from './queuer.js';
|
|
3
4
|
export interface AsyncQueuerState<TValue> {
|
|
4
5
|
/**
|
|
@@ -13,6 +14,10 @@ export interface AsyncQueuerState<TValue> {
|
|
|
13
14
|
* Number of task executions that have resulted in errors
|
|
14
15
|
*/
|
|
15
16
|
errorCount: number;
|
|
17
|
+
/**
|
|
18
|
+
* Number of times execute has been called
|
|
19
|
+
*/
|
|
20
|
+
executeCount: number;
|
|
16
21
|
/**
|
|
17
22
|
* Number of items that have been removed from the queue due to expiration
|
|
18
23
|
*/
|
|
@@ -21,6 +26,10 @@ export interface AsyncQueuerState<TValue> {
|
|
|
21
26
|
* Whether the queuer has no items to process (items array is empty)
|
|
22
27
|
*/
|
|
23
28
|
isEmpty: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the queuer is currently executing
|
|
31
|
+
*/
|
|
32
|
+
isExecuting: boolean;
|
|
24
33
|
/**
|
|
25
34
|
* Whether the queuer has reached its maximum capacity
|
|
26
35
|
*/
|
|
@@ -71,6 +80,10 @@ export interface AsyncQueuerState<TValue> {
|
|
|
71
80
|
successCount: number;
|
|
72
81
|
}
|
|
73
82
|
export interface AsyncQueuerOptions<TValue> {
|
|
83
|
+
/**
|
|
84
|
+
* Options for configuring the underlying async retryer
|
|
85
|
+
*/
|
|
86
|
+
asyncRetryerOptions?: AsyncRetryerOptions<(item: TValue) => Promise<any>>;
|
|
74
87
|
/**
|
|
75
88
|
* Default position to add items to the queuer
|
|
76
89
|
* @default 'back'
|
|
@@ -125,7 +138,7 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
125
138
|
* If provided, the handler will be called with the error and queuer instance.
|
|
126
139
|
* This can be used alongside throwOnError - the handler will be called before any error is thrown.
|
|
127
140
|
*/
|
|
128
|
-
onError?: (error:
|
|
141
|
+
onError?: (error: Error, item: TValue, queuer: AsyncQueuer<TValue>) => void;
|
|
129
142
|
/**
|
|
130
143
|
* Callback fired whenever an item expires in the queuer
|
|
131
144
|
*/
|
|
@@ -163,21 +176,38 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
163
176
|
*/
|
|
164
177
|
wait?: number | ((queuer: AsyncQueuer<TValue>) => number);
|
|
165
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Utility function for sharing common `AsyncQueuerOptions` options between different `AsyncQueuer` instances.
|
|
181
|
+
*/
|
|
182
|
+
export declare function asyncQueuerOptions<TValue = any, TOptions extends Partial<AsyncQueuerOptions<TValue>> = Partial<AsyncQueuerOptions<TValue>>>(options: TOptions): TOptions;
|
|
166
183
|
/**
|
|
167
184
|
* A flexible asynchronous queue for processing tasks with configurable concurrency, priority, and expiration.
|
|
168
185
|
*
|
|
169
|
-
*
|
|
186
|
+
* Async vs Sync Versions:
|
|
187
|
+
* The async version provides advanced features over the sync Queuer:
|
|
188
|
+
* - Returns promises that can be awaited for task results
|
|
189
|
+
* - Built-in retry support via AsyncRetryer integration for each queued task
|
|
190
|
+
* - Abort support to cancel in-flight task executions
|
|
191
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
192
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
193
|
+
* - Concurrent execution support (process multiple items simultaneously)
|
|
194
|
+
*
|
|
195
|
+
* The sync Queuer is lighter weight and simpler when you don't need async features,
|
|
196
|
+
* return values, or execution control.
|
|
197
|
+
*
|
|
198
|
+
* What is Queuing?
|
|
199
|
+
* Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
|
|
200
|
+
* Tasks are processed up to the configured concurrency limit. When a task completes,
|
|
201
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
202
|
+
*
|
|
203
|
+
* Key Features:
|
|
170
204
|
* - Priority queue support via the getPriority option
|
|
171
205
|
* - Configurable concurrency limit
|
|
172
206
|
* - Callbacks for task success, error, completion, and queue state changes
|
|
173
207
|
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
174
208
|
* - Pause and resume processing
|
|
175
|
-
* - Task cancellation
|
|
176
209
|
* - Item expiration to remove stale items from the queue
|
|
177
210
|
*
|
|
178
|
-
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
179
|
-
* the next pending task is processed if the concurrency limit allows.
|
|
180
|
-
*
|
|
181
211
|
* Error Handling:
|
|
182
212
|
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
183
213
|
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
@@ -220,7 +250,12 @@ export declare class AsyncQueuer<TValue> {
|
|
|
220
250
|
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
|
|
221
251
|
key: string;
|
|
222
252
|
options: AsyncQueuerOptions<TValue>;
|
|
253
|
+
asyncRetryers: Map<number, AsyncRetryer<(item: TValue) => Promise<any>>>;
|
|
223
254
|
constructor(fn: (item: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>);
|
|
255
|
+
/**
|
|
256
|
+
* Emits a change event for the async queuer instance. Mostly useful for devtools.
|
|
257
|
+
*/
|
|
258
|
+
_emit: () => void;
|
|
224
259
|
/**
|
|
225
260
|
* Updates the queuer options. New options are merged with existing options.
|
|
226
261
|
*/
|
|
@@ -301,9 +336,36 @@ export declare class AsyncQueuer<TValue> {
|
|
|
301
336
|
*/
|
|
302
337
|
stop: () => void;
|
|
303
338
|
/**
|
|
304
|
-
* Removes all pending items from the queue.
|
|
339
|
+
* Removes all pending items from the queue.
|
|
340
|
+
* Does NOT affect active tasks.
|
|
305
341
|
*/
|
|
306
342
|
clear: () => void;
|
|
343
|
+
/**
|
|
344
|
+
* Returns the AbortSignal for a specific execution.
|
|
345
|
+
* If no executeCount is provided, returns the signal for the most recent execution.
|
|
346
|
+
* Returns null if no execution is found or not currently executing.
|
|
347
|
+
*
|
|
348
|
+
* @param executeCount - Optional specific execution to get signal for
|
|
349
|
+
* @example
|
|
350
|
+
* ```typescript
|
|
351
|
+
* const queuer = new AsyncQueuer(
|
|
352
|
+
* async (item: string) => {
|
|
353
|
+
* const signal = queuer.getAbortSignal()
|
|
354
|
+
* if (signal) {
|
|
355
|
+
* const response = await fetch(`/api/process/${item}`, { signal })
|
|
356
|
+
* return response.json()
|
|
357
|
+
* }
|
|
358
|
+
* },
|
|
359
|
+
* { concurrency: 2 }
|
|
360
|
+
* )
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
getAbortSignal(executeCount?: number): AbortSignal | null;
|
|
364
|
+
/**
|
|
365
|
+
* Aborts all ongoing executions with the internal abort controllers.
|
|
366
|
+
* Does NOT clear out the items.
|
|
367
|
+
*/
|
|
368
|
+
abort: () => void;
|
|
307
369
|
/**
|
|
308
370
|
* Resets the queuer state to its default values
|
|
309
371
|
*/
|
|
@@ -313,6 +375,34 @@ export declare class AsyncQueuer<TValue> {
|
|
|
313
375
|
* Creates a new AsyncQueuer instance and returns a bound addItem function for adding tasks.
|
|
314
376
|
* The queuer is started automatically and ready to process items.
|
|
315
377
|
*
|
|
378
|
+
* Async vs Sync Versions:
|
|
379
|
+
* The async version provides advanced features over the sync queue function:
|
|
380
|
+
* - Returns promises that can be awaited for task results
|
|
381
|
+
* - Built-in retry support via AsyncRetryer integration for each queued task
|
|
382
|
+
* - Abort support to cancel in-flight task executions
|
|
383
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
384
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
385
|
+
* - Concurrent execution support (process multiple items simultaneously)
|
|
386
|
+
*
|
|
387
|
+
* The sync queue function is lighter weight and simpler when you don't need async features,
|
|
388
|
+
* return values, or execution control.
|
|
389
|
+
*
|
|
390
|
+
* What is Queuing?
|
|
391
|
+
* Queuing is a technique for managing and processing items sequentially or with controlled concurrency.
|
|
392
|
+
* Tasks are processed up to the configured concurrency limit. When a task completes,
|
|
393
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
394
|
+
*
|
|
395
|
+
* Configuration Options:
|
|
396
|
+
* - `concurrency`: Maximum number of concurrent tasks (default: 1)
|
|
397
|
+
* - `wait`: Time to wait between processing items (default: 0)
|
|
398
|
+
* - `maxSize`: Maximum number of items allowed in the queue (default: Infinity)
|
|
399
|
+
* - `getPriority`: Function to determine item priority
|
|
400
|
+
* - `addItemsTo`: Default position to add items ('back' or 'front', default: 'back')
|
|
401
|
+
* - `getItemsFrom`: Default position to get items ('front' or 'back', default: 'front')
|
|
402
|
+
* - `expirationDuration`: Maximum time items can stay in queue
|
|
403
|
+
* - `started`: Whether to start processing immediately (default: true)
|
|
404
|
+
* - `asyncRetryerOptions`: Configure retry behavior for task executions
|
|
405
|
+
*
|
|
316
406
|
* Error Handling:
|
|
317
407
|
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
318
408
|
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
@@ -333,11 +423,15 @@ export declare class AsyncQueuer<TValue> {
|
|
|
333
423
|
* - State can be accessed via the underlying AsyncQueuer instance's `store.state` property
|
|
334
424
|
* - When using framework adapters (React/Solid), state is accessed from the hook's state property
|
|
335
425
|
*
|
|
336
|
-
*
|
|
426
|
+
* @example
|
|
337
427
|
* ```ts
|
|
338
428
|
* const enqueue = asyncQueue<string>(async (item) => {
|
|
339
429
|
* return item.toUpperCase();
|
|
340
|
-
* }, {
|
|
430
|
+
* }, {
|
|
431
|
+
* concurrency: 2,
|
|
432
|
+
* wait: 100,
|
|
433
|
+
* onSuccess: (result) => console.log('Processed:', result)
|
|
434
|
+
* });
|
|
341
435
|
*
|
|
342
436
|
* enqueue('hello');
|
|
343
437
|
* ```
|