@tanstack/pacer 0.15.4 → 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 +12 -2
- 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,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const store = require("@tanstack/store");
|
|
4
|
+
const asyncRetryer = require("./async-retryer.cjs");
|
|
4
5
|
const utils = require("./utils.cjs");
|
|
5
6
|
const eventClient = require("./event-client.cjs");
|
|
6
7
|
function getDefaultAsyncBatcherState() {
|
|
7
8
|
return {
|
|
8
9
|
errorCount: 0,
|
|
10
|
+
executeCount: 0,
|
|
9
11
|
failedItems: [],
|
|
10
12
|
isEmpty: true,
|
|
11
13
|
isExecuting: false,
|
|
@@ -20,7 +22,13 @@ function getDefaultAsyncBatcherState() {
|
|
|
20
22
|
totalItemsFailed: 0
|
|
21
23
|
};
|
|
22
24
|
}
|
|
25
|
+
function asyncBatcherOptions(options) {
|
|
26
|
+
return options;
|
|
27
|
+
}
|
|
23
28
|
const defaultOptions = {
|
|
29
|
+
asyncRetryerOptions: {
|
|
30
|
+
maxAttempts: 1
|
|
31
|
+
},
|
|
24
32
|
getShouldExecute: () => false,
|
|
25
33
|
maxSize: Infinity,
|
|
26
34
|
started: true,
|
|
@@ -33,7 +41,9 @@ class AsyncBatcher {
|
|
|
33
41
|
this.store = new store.Store(
|
|
34
42
|
getDefaultAsyncBatcherState()
|
|
35
43
|
);
|
|
44
|
+
this.asyncRetryers = /* @__PURE__ */ new Map();
|
|
36
45
|
this.#timeoutId = null;
|
|
46
|
+
this._emit = () => eventClient.emitChange("AsyncBatcher", this);
|
|
37
47
|
this.setOptions = (newOptions) => {
|
|
38
48
|
this.options = { ...this.options, ...newOptions };
|
|
39
49
|
};
|
|
@@ -58,7 +68,7 @@ class AsyncBatcher {
|
|
|
58
68
|
this.#getWait = () => {
|
|
59
69
|
return utils.parseFunctionOrValue(this.options.wait, this);
|
|
60
70
|
};
|
|
61
|
-
this.addItem = (item) => {
|
|
71
|
+
this.addItem = async (item) => {
|
|
62
72
|
this.#setState({
|
|
63
73
|
items: [...this.store.state.items, item],
|
|
64
74
|
isPending: this.options.wait !== Infinity
|
|
@@ -66,22 +76,29 @@ class AsyncBatcher {
|
|
|
66
76
|
this.options.onItemsChange?.(this);
|
|
67
77
|
const shouldProcess = this.store.state.items.length >= this.options.maxSize || this.options.getShouldExecute(this.store.state.items, this);
|
|
68
78
|
if (shouldProcess) {
|
|
69
|
-
this.#execute();
|
|
79
|
+
return await this.#execute();
|
|
70
80
|
} else if (this.options.wait !== Infinity) {
|
|
71
81
|
this.#clearTimeout();
|
|
72
82
|
this.#timeoutId = setTimeout(() => this.#execute(), this.#getWait());
|
|
83
|
+
await new Promise((resolve) => setTimeout(resolve, this.#getWait()));
|
|
73
84
|
}
|
|
74
85
|
};
|
|
75
86
|
this.#execute = async () => {
|
|
76
87
|
if (this.store.state.items.length === 0) {
|
|
77
88
|
return void 0;
|
|
78
89
|
}
|
|
90
|
+
const currentExecuteCount = this.store.state.executeCount + 1;
|
|
79
91
|
const batch = this.peekAllItems();
|
|
80
92
|
this.clear();
|
|
81
93
|
this.options.onItemsChange?.(this);
|
|
82
|
-
this.#setState({ isExecuting: true });
|
|
94
|
+
this.#setState({ isExecuting: true, executeCount: currentExecuteCount });
|
|
83
95
|
try {
|
|
84
|
-
const
|
|
96
|
+
const currentAsyncRetryer = new asyncRetryer.AsyncRetryer(
|
|
97
|
+
this.fn,
|
|
98
|
+
this.options.asyncRetryerOptions
|
|
99
|
+
);
|
|
100
|
+
this.asyncRetryers.set(currentExecuteCount, currentAsyncRetryer);
|
|
101
|
+
const result = await currentAsyncRetryer.execute(batch);
|
|
85
102
|
this.#setState({
|
|
86
103
|
totalItemsProcessed: this.store.state.totalItemsProcessed + batch.length,
|
|
87
104
|
lastResult: result,
|
|
@@ -101,6 +118,7 @@ class AsyncBatcher {
|
|
|
101
118
|
}
|
|
102
119
|
return void 0;
|
|
103
120
|
} finally {
|
|
121
|
+
this.asyncRetryers.delete(currentExecuteCount);
|
|
104
122
|
this.#setState({
|
|
105
123
|
isExecuting: false,
|
|
106
124
|
settleCount: this.store.state.settleCount + 1
|
|
@@ -127,9 +145,23 @@ class AsyncBatcher {
|
|
|
127
145
|
this.clear = () => {
|
|
128
146
|
this.#setState({ items: [], failedItems: [], isPending: false });
|
|
129
147
|
};
|
|
148
|
+
this.abort = () => {
|
|
149
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
150
|
+
this.asyncRetryers.clear();
|
|
151
|
+
this.#setState({
|
|
152
|
+
isExecuting: false
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
this.cancel = () => {
|
|
156
|
+
this.#clearTimeout();
|
|
157
|
+
this.#setState({
|
|
158
|
+
isPending: false
|
|
159
|
+
});
|
|
160
|
+
};
|
|
130
161
|
this.reset = () => {
|
|
131
162
|
this.#setState(getDefaultAsyncBatcherState());
|
|
132
163
|
this.options.onItemsChange?.(this);
|
|
164
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
133
165
|
};
|
|
134
166
|
this.key = utils.createKey(initialOptions.key);
|
|
135
167
|
this.options = {
|
|
@@ -149,6 +181,35 @@ class AsyncBatcher {
|
|
|
149
181
|
#getWait;
|
|
150
182
|
#execute;
|
|
151
183
|
#clearTimeout;
|
|
184
|
+
/**
|
|
185
|
+
* Returns the AbortSignal for a specific execution.
|
|
186
|
+
* If no executeCount is provided, returns the signal for the most recent execution.
|
|
187
|
+
* Returns null if no execution is found or not currently executing.
|
|
188
|
+
*
|
|
189
|
+
* @param executeCount - Optional specific execution to get signal for
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const batcher = new AsyncBatcher(
|
|
193
|
+
* async (items: string[]) => {
|
|
194
|
+
* const signal = batcher.getAbortSignal()
|
|
195
|
+
* if (signal) {
|
|
196
|
+
* const response = await fetch('/api/batch', {
|
|
197
|
+
* method: 'POST',
|
|
198
|
+
* body: JSON.stringify(items),
|
|
199
|
+
* signal
|
|
200
|
+
* })
|
|
201
|
+
* return response.json()
|
|
202
|
+
* }
|
|
203
|
+
* },
|
|
204
|
+
* { maxSize: 10, wait: 100 }
|
|
205
|
+
* )
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
getAbortSignal(executeCount) {
|
|
209
|
+
const count = executeCount ?? this.store.state.executeCount;
|
|
210
|
+
const retryer = this.asyncRetryers.get(count);
|
|
211
|
+
return retryer?.getAbortSignal() ?? null;
|
|
212
|
+
}
|
|
152
213
|
}
|
|
153
214
|
function asyncBatch(fn, options) {
|
|
154
215
|
const batcher = new AsyncBatcher(fn, options);
|
|
@@ -156,4 +217,5 @@ function asyncBatch(fn, options) {
|
|
|
156
217
|
}
|
|
157
218
|
exports.AsyncBatcher = AsyncBatcher;
|
|
158
219
|
exports.asyncBatch = asyncBatch;
|
|
220
|
+
exports.asyncBatcherOptions = asyncBatcherOptions;
|
|
159
221
|
//# sourceMappingURL=async-batcher.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-batcher.cjs","sources":["../../src/async-batcher.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { createKey, parseFunctionOrValue } from './utils'\nimport { emitChange, pacerEventClient } from './event-client'\nimport type { OptionalKeys } from './types'\n\nexport interface AsyncBatcherState<TValue> {\n /**\n * Number of batch executions that have resulted in errors\n */\n errorCount: number\n /**\n * Array of items that failed during batch processing\n */\n failedItems: Array<TValue>\n /**\n * Whether the batcher has no items to process (items array is empty)\n */\n isEmpty: boolean\n /**\n * Whether a batch is currently being processed asynchronously\n */\n isExecuting: boolean\n /**\n * Whether the batcher is waiting for the timeout to trigger batch processing\n */\n isPending: boolean\n /**\n * Array of items currently queued for batch processing\n */\n items: Array<TValue>\n /**\n * The result from the most recent batch execution\n */\n lastResult: any\n /**\n * Number of batch executions that have completed (either successfully or with errors)\n */\n settleCount: number\n /**\n * Number of items currently in the batch queue\n */\n size: number\n /**\n * Current processing status - 'idle' when not processing, 'pending' when waiting for timeout, 'executing' when processing, 'populated' when items are present, but no wait is configured\n */\n status: 'idle' | 'pending' | 'executing' | 'populated'\n /**\n * Number of batch executions that have completed successfully\n */\n successCount: number\n /**\n * Total number of items that have failed processing across all batches\n */\n totalItemsFailed: number\n /**\n * Total number of items that have been processed across all batches\n */\n totalItemsProcessed: number\n}\n\nfunction getDefaultAsyncBatcherState<TValue>(): AsyncBatcherState<TValue> {\n return {\n errorCount: 0,\n failedItems: [],\n isEmpty: true,\n isExecuting: false,\n isPending: false,\n items: [],\n lastResult: undefined,\n settleCount: 0,\n size: 0,\n status: 'idle',\n successCount: 0,\n totalItemsProcessed: 0,\n totalItemsFailed: 0,\n }\n}\n\n/**\n * Options for configuring an AsyncBatcher instance\n */\nexport interface AsyncBatcherOptions<TValue> {\n /**\n * Custom function to determine if a batch should be processed\n * Return true to process the batch immediately\n */\n getShouldExecute?: (\n items: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => boolean\n /**\n * Initial state for the async batcher\n */\n initialState?: Partial<AsyncBatcherState<TValue>>\n /**\n * Optional key to identify this async batcher instance.\n * If provided, the async batcher will be identified by this key in the devtools and PacerProvider if applicable.\n */\n key?: string\n /**\n * Maximum number of items in a batch\n * @default Infinity\n */\n maxSize?: number\n /**\n * Optional error handler for when the batch function throws.\n * If provided, the handler will be called with the error, the batch of items that failed, and batcher 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 batch: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => void\n /**\n * Callback fired after items are added to the batcher\n */\n onItemsChange?: (batcher: AsyncBatcher<TValue>) => void\n /**\n * Optional callback to call when a batch is settled (completed or failed)\n */\n onSettled?: (batch: Array<TValue>, batcher: AsyncBatcher<TValue>) => void\n /**\n * Optional callback to call when a batch succeeds\n */\n onSuccess?: (\n result: any,\n batch: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => void\n /**\n * Whether the batcher should start processing immediately\n * @default true\n */\n started?: boolean\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 * Maximum time in milliseconds to wait before processing a batch.\n * If the wait duration has elapsed, the batch will be processed.\n * If not provided, the batch will not be triggered by a timeout.\n * @default Infinity\n */\n wait?: number | ((asyncBatcher: AsyncBatcher<TValue>) => number)\n}\n\ntype AsyncBatcherOptionsWithOptionalCallbacks<TValue> = OptionalKeys<\n Required<AsyncBatcherOptions<TValue>>,\n | 'initialState'\n | 'onError'\n | 'onItemsChange'\n | 'onSettled'\n | 'onSuccess'\n | 'key'\n>\n\nconst defaultOptions: AsyncBatcherOptionsWithOptionalCallbacks<any> = {\n getShouldExecute: () => false,\n maxSize: Infinity,\n started: true,\n throwOnError: true,\n wait: Infinity,\n}\n\n/**\n * A class that collects items and processes them in batches asynchronously.\n *\n * This is the async version of the Batcher class. Unlike the sync version, this async batcher:\n * - Handles promises and returns results from batch executions\n * - Provides error handling with configurable error behavior\n * - Tracks success, error, and settle counts separately\n * - Has state tracking for when batches are executing\n * - Returns the result of the batch function execution\n *\n * Batching is a technique for grouping multiple operations together to be processed as a single unit.\n *\n * The AsyncBatcher provides a flexible way to implement async batching with configurable:\n * - Maximum batch size (number of items per batch)\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - Error handling for failed batch operations\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher 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 AsyncBatcher instance\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async batcher\n * - Use `onSuccess` callback to react to successful batch execution and implement custom logic\n * - Use `onError` callback to react to batch execution errors and implement custom error handling\n * - Use `onSettled` callback to react to batch execution completion (success or error) and implement custom logic\n * - Use `onExecute` callback to react to batch execution and implement custom logic\n * - Use `onItemsChange` callback to react to items being added or removed from the batcher\n * - The state includes total items processed, success/error counts, and execution status\n * - State can be accessed via `asyncBatcher.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `asyncBatcher.state`\n *\n * @example\n * ```ts\n * const batcher = new AsyncBatcher<number>(\n * async (items) => {\n * const result = await processItems(items);\n * console.log('Processing batch:', items);\n * return result;\n * },\n * {\n * maxSize: 5,\n * wait: 2000,\n * onSuccess: (result) => console.log('Batch succeeded:', result),\n * onError: (error) => console.error('Batch failed:', error)\n * }\n * );\n *\n * batcher.addItem(1);\n * batcher.addItem(2);\n * // After 2 seconds or when 5 items are added, whichever comes first,\n * // the batch will be processed and the result will be available\n * // batcher.execute() // manually trigger a batch\n * ```\n */\nexport class AsyncBatcher<TValue> {\n readonly store: Store<Readonly<AsyncBatcherState<TValue>>> = new Store(\n getDefaultAsyncBatcherState<TValue>(),\n )\n key: string\n options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>\n #timeoutId: NodeJS.Timeout | null = null\n\n constructor(\n public fn: (items: Array<TValue>) => Promise<any>,\n initialOptions: AsyncBatcherOptions<TValue>,\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-AsyncBatcher', (event) => {\n if (event.payload.key !== this.key) return\n this.#setState(event.payload.store.state)\n this.setOptions(event.payload.options)\n })\n }\n\n /**\n * Updates the async batcher options\n */\n setOptions = (newOptions: Partial<AsyncBatcherOptions<TValue>>): void => {\n this.options = { ...this.options, ...newOptions }\n }\n\n #setState = (newState: Partial<AsyncBatcherState<TValue>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isExecuting, isPending, items } = combinedState\n const size = items.length\n const isEmpty = size === 0\n return {\n ...combinedState,\n isEmpty,\n size,\n status: isExecuting\n ? 'executing'\n : isPending\n ? 'pending'\n : isEmpty\n ? 'idle'\n : 'populated',\n }\n })\n emitChange('AsyncBatcher', this)\n }\n\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Adds an item to the async batcher\n * If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed\n */\n addItem = (item: TValue): void => {\n this.#setState({\n items: [...this.store.state.items, item],\n isPending: this.options.wait !== Infinity,\n })\n this.options.onItemsChange?.(this)\n\n const shouldProcess =\n this.store.state.items.length >= this.options.maxSize ||\n this.options.getShouldExecute(this.store.state.items, this)\n\n if (shouldProcess) {\n this.#execute()\n } else if (this.options.wait !== Infinity) {\n this.#clearTimeout() // clear any pending timeout to replace it with a new one\n this.#timeoutId = setTimeout(() => this.#execute(), this.#getWait())\n }\n }\n\n /**\n * Processes the current batch of items asynchronously.\n * This method will automatically be triggered if the batcher is running and any of these conditions are met:\n * - The number of items reaches maxSize\n * - The wait duration has elapsed\n * - The getShouldExecute function returns true upon adding an item\n *\n * You can also call this method manually to process the current batch at any time.\n *\n * @returns A promise that resolves with the result of the batch function, or undefined if an error occurred and was handled by onError\n * @throws The error from the batch function if no onError handler is configured or throwOnError is true\n */\n #execute = async (): Promise<any> => {\n if (this.store.state.items.length === 0) {\n return undefined\n }\n\n const batch = this.peekAllItems() // copy of the items to be processed (to prevent race conditions)\n this.clear() // Clear items before processing to prevent race conditions\n this.options.onItemsChange?.(this)\n\n this.#setState({ isExecuting: true })\n\n try {\n const result = await this.fn(batch) // EXECUTE\n this.#setState({\n totalItemsProcessed:\n this.store.state.totalItemsProcessed + batch.length,\n lastResult: result,\n successCount: this.store.state.successCount + 1,\n })\n this.options.onSuccess?.(result, batch, this)\n return result\n } catch (error) {\n this.#setState({\n errorCount: this.store.state.errorCount + 1,\n failedItems: [...this.store.state.failedItems, ...batch],\n totalItemsFailed: this.store.state.totalItemsFailed + batch.length,\n })\n this.options.onError?.(error, batch, this)\n if (this.options.throwOnError) {\n throw error\n }\n return undefined\n } finally {\n this.#setState({\n isExecuting: false,\n settleCount: this.store.state.settleCount + 1,\n })\n this.options.onSettled?.(batch, this)\n }\n }\n\n /**\n * Processes the current batch of items immediately\n */\n flush = async (): Promise<any> => {\n this.#clearTimeout() // clear any pending timeout\n return await this.#execute()\n }\n\n /**\n * Returns a copy of all items in the async batcher\n */\n peekAllItems = (): Array<TValue> => {\n return [...this.store.state.items]\n }\n\n peekFailedItems = (): Array<TValue> => {\n return [...this.store.state.failedItems]\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = null\n }\n }\n\n /**\n * Removes all items from the async batcher\n */\n clear = (): void => {\n this.#setState({ items: [], failedItems: [], isPending: false })\n }\n\n /**\n * Resets the async batcher state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultAsyncBatcherState<TValue>())\n this.options.onItemsChange?.(this)\n }\n}\n\n/**\n * Creates an async batcher that processes items in batches\n *\n * Unlike the sync batcher, this async version:\n * - Handles promises and returns results from batch executions\n * - Provides error handling with configurable error behavior\n * - Tracks success, error, and settle counts separately\n * - Has state tracking for when batches are executing\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher 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 AsyncBatcher instance\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async batcher\n * - Use `onSuccess` callback to react to successful batch execution and implement custom logic\n * - Use `onError` callback to react to batch execution errors and implement custom error handling\n * - Use `onSettled` callback to react to batch execution completion (success or error) and implement custom logic\n * - Use `onExecute` callback to react to batch execution and implement custom logic\n * - Use `onItemsChange` callback to react to items being added or removed from the batcher\n * - The state includes total items processed, success/error counts, and execution status\n * - State can be accessed via the underlying AsyncBatcher instance's `store.state` property\n * - When using framework adapters (React/Solid), state is accessed from the hook's state property\n *\n * @example\n * ```ts\n * const batchItems = asyncBatch<number>(\n * async (items) => {\n * const result = await processApiCall(items);\n * console.log('Processing:', items);\n * return result;\n * },\n * {\n * maxSize: 3,\n * wait: 1000,\n * onSuccess: (result) => console.log('Batch succeeded:', result),\n * onError: (error) => console.error('Batch failed:', error)\n * }\n * );\n *\n * batchItems(1);\n * batchItems(2);\n * batchItems(3); // Triggers batch processing\n * ```\n */\nexport function asyncBatch<TValue>(\n fn: (items: Array<TValue>) => Promise<any>,\n options: AsyncBatcherOptions<TValue>,\n) {\n const batcher = new AsyncBatcher<TValue>(fn, options)\n return batcher.addItem\n}\n"],"names":["Store","emitChange","parseFunctionOrValue","createKey","pacerEventClient"],"mappings":";;;;;AA4DA,SAAS,8BAAiE;AACxE,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,aAAa,CAAA;AAAA,IACb,SAAS;AAAA,IACT,aAAa;AAAA,IACb,WAAW;AAAA,IACX,OAAO,CAAA;AAAA,IACP,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,cAAc;AAAA,IACd,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,EAAA;AAEtB;AAoFA,MAAM,iBAAgE;AAAA,EACpE,kBAAkB,MAAM;AAAA,EACxB,SAAS;AAAA,EACT,SAAS;AAAA,EACT,cAAc;AAAA,EACd,MAAM;AACR;AA+DO,MAAM,aAAqB;AAAA,EAQhC,YACS,IACP,gBACA;AAFO,SAAA,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KAAK,YAAY;AACnB,qBAAa,KAAK,UAAU;AAC5B,aAAK,aAAa;AAAA,MAAA;AAAA,IACpB;AAMF,SAAA,QAAQ,MAAY;AAClB,WAAK,UAAU,EAAE,OAAO,CAAA,GAAI,aAAa,CAAA,GAAI,WAAW,OAAO;AAAA,IAAA;AAMjE,SAAA,QAAQ,MAAY;AAClB,WAAK,UAAU,6BAAqC;AACpD,WAAK,QAAQ,gBAAgB,IAAI;AAAA,IAAA;AArKjC,SAAK,MAAMC,gBAAU,eAAe,GAAG;AACvC,SAAK,UAAU;AAAA,MACb,GAAG;AAAA,MACH,GAAG;AAAA,MACH,cAAc,eAAe,gBAAgB,CAAC,eAAe;AAAA,IAAA;AAE/D,SAAK,UAAU,KAAK,QAAQ,gBAAgB,CAAA,CAAE;AAE9CC,gBAAAA,iBAAiB,GAAG,kBAAkB,CAAC,UAAU;AAC/C,UAAI,MAAM,QAAQ,QAAQ,KAAK,IAAK;AACpC,WAAK,UAAU,MAAM,QAAQ,MAAM,KAAK;AACxC,WAAK,WAAW,MAAM,QAAQ,OAAO;AAAA,IAAA,CACtC;AAAA,EAAA;AAAA,EAlBH;AAAA,EA4BA;AAAA,EAyBA;AAAA,EAuCA;AAAA,EA4DA;AAqBF;AAmDO,SAAS,WACd,IACA,SACA;AACA,QAAM,UAAU,IAAI,aAAqB,IAAI,OAAO;AACpD,SAAO,QAAQ;AACjB;;;"}
|
|
1
|
+
{"version":3,"file":"async-batcher.cjs","sources":["../../src/async-batcher.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 { OptionalKeys } from './types'\n\nexport interface AsyncBatcherState<TValue> {\n /**\n * Number of batch executions that have resulted in errors\n */\n errorCount: number\n /**\n * Number of batch executions that have been executed\n */\n executeCount: number\n /**\n * Array of items that failed during batch processing\n */\n failedItems: Array<TValue>\n /**\n * Whether the batcher has no items to process (items array is empty)\n */\n isEmpty: boolean\n /**\n * Whether a batch is currently being processed asynchronously\n */\n isExecuting: boolean\n /**\n * Whether the batcher is waiting for the timeout to trigger batch processing\n */\n isPending: boolean\n /**\n * Array of items currently queued for batch processing\n */\n items: Array<TValue>\n /**\n * The result from the most recent batch execution\n */\n lastResult: any\n /**\n * Number of batch executions that have completed (either successfully or with errors)\n */\n settleCount: number\n /**\n * Number of items currently in the batch queue\n */\n size: number\n /**\n * Current processing status - 'idle' when not processing, 'pending' when waiting for timeout, 'executing' when processing, 'populated' when items are present, but no wait is configured\n */\n status: 'idle' | 'pending' | 'executing' | 'populated'\n /**\n * Number of batch executions that have completed successfully\n */\n successCount: number\n /**\n * Total number of items that have failed processing across all batches\n */\n totalItemsFailed: number\n /**\n * Total number of items that have been processed across all batches\n */\n totalItemsProcessed: number\n}\n\nfunction getDefaultAsyncBatcherState<TValue>(): AsyncBatcherState<TValue> {\n return {\n errorCount: 0,\n executeCount: 0,\n failedItems: [],\n isEmpty: true,\n isExecuting: false,\n isPending: false,\n items: [],\n lastResult: undefined,\n settleCount: 0,\n size: 0,\n status: 'idle',\n successCount: 0,\n totalItemsProcessed: 0,\n totalItemsFailed: 0,\n }\n}\n\n/**\n * Options for configuring an AsyncBatcher instance\n */\nexport interface AsyncBatcherOptions<TValue> {\n /**\n * Options for configuring the underlying async retryer\n */\n asyncRetryerOptions?: AsyncRetryerOptions<\n (items: Array<TValue>) => Promise<any>\n >\n /**\n * Custom function to determine if a batch should be processed\n * Return true to process the batch immediately\n */\n getShouldExecute?: (\n items: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => boolean\n /**\n * Initial state for the async batcher\n */\n initialState?: Partial<AsyncBatcherState<TValue>>\n /**\n * Optional key to identify this async batcher instance.\n * If provided, the async batcher will be identified by this key in the devtools and PacerProvider if applicable.\n */\n key?: string\n /**\n * Maximum number of items in a batch\n * @default Infinity\n */\n maxSize?: number\n /**\n * Optional error handler for when the batch function throws.\n * If provided, the handler will be called with the error, the batch of items that failed, and batcher 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 batch: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => void\n /**\n * Callback fired after items are added to the batcher\n */\n onItemsChange?: (batcher: AsyncBatcher<TValue>) => void\n /**\n * Optional callback to call when a batch is settled (completed or failed)\n */\n onSettled?: (batch: Array<TValue>, batcher: AsyncBatcher<TValue>) => void\n /**\n * Optional callback to call when a batch succeeds\n */\n onSuccess?: (\n result: any,\n batch: Array<TValue>,\n batcher: AsyncBatcher<TValue>,\n ) => void\n /**\n * Whether the batcher should start processing immediately\n * @default true\n */\n started?: boolean\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 * Maximum time in milliseconds to wait before processing a batch.\n * If the wait duration has elapsed, the batch will be processed.\n * If not provided, the batch will not be triggered by a timeout.\n * @default Infinity\n */\n wait?: number | ((asyncBatcher: AsyncBatcher<TValue>) => number)\n}\n\n/**\n * Utility function for sharing common `AsyncBatcherOptions` options between different `AsyncBatcher` instances.\n *\n */\nexport function asyncBatcherOptions<\n TValue = any,\n TOptions extends Partial<AsyncBatcherOptions<TValue>> = Partial<\n AsyncBatcherOptions<TValue>\n >,\n>(options: TOptions): TOptions {\n return options\n}\n\ntype AsyncBatcherOptionsWithOptionalCallbacks<TValue> = OptionalKeys<\n Required<AsyncBatcherOptions<TValue>>,\n | 'initialState'\n | 'onError'\n | 'onItemsChange'\n | 'onSettled'\n | 'onSuccess'\n | 'key'\n>\n\nconst defaultOptions: AsyncBatcherOptionsWithOptionalCallbacks<any> = {\n asyncRetryerOptions: {\n maxAttempts: 1,\n },\n getShouldExecute: () => false,\n maxSize: Infinity,\n started: true,\n throwOnError: true,\n wait: Infinity,\n}\n\n/**\n * A class that collects items and processes them in batches asynchronously.\n *\n * Async vs Sync Versions:\n * The async version provides advanced features over the sync Batcher:\n * - Returns promises that can be awaited for batch results\n * - Built-in retry support via AsyncRetryer integration\n * - Abort support to cancel in-flight batch executions\n * - Cancel support to prevent pending batches from starting\n * - Comprehensive error handling with onError callbacks and throwOnError control\n * - Detailed execution tracking (success/error/settle counts)\n *\n * The sync Batcher is lighter weight and simpler when you don't need async features,\n * return values, or execution control.\n *\n * What is Batching?\n * Batching is a technique for grouping multiple operations together to be processed as a single unit.\n *\n * The AsyncBatcher provides a flexible way to implement async batching with configurable:\n * - Maximum batch size (number of items per batch)\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - Error handling for failed batch operations\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher 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 AsyncBatcher instance\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async batcher\n * - Use `onSuccess` callback to react to successful batch execution and implement custom logic\n * - Use `onError` callback to react to batch execution errors and implement custom error handling\n * - Use `onSettled` callback to react to batch execution completion (success or error) and implement custom logic\n * - Use `onExecute` callback to react to batch execution and implement custom logic\n * - Use `onItemsChange` callback to react to items being added or removed from the batcher\n * - The state includes total items processed, success/error counts, and execution status\n * - State can be accessed via `asyncBatcher.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `asyncBatcher.state`\n *\n * @example\n * ```ts\n * const batcher = new AsyncBatcher<number>(\n * async (items) => {\n * const result = await processItems(items);\n * console.log('Processing batch:', items);\n * return result;\n * },\n * {\n * maxSize: 5,\n * wait: 2000,\n * onSuccess: (result) => console.log('Batch succeeded:', result),\n * onError: (error) => console.error('Batch failed:', error)\n * }\n * );\n *\n * batcher.addItem(1);\n * batcher.addItem(2);\n * // After 2 seconds or when 5 items are added, whichever comes first,\n * // the batch will be processed and the result will be available\n * // batcher.execute() // manually trigger a batch\n * ```\n */\nexport class AsyncBatcher<TValue> {\n readonly store: Store<Readonly<AsyncBatcherState<TValue>>> = new Store(\n getDefaultAsyncBatcherState<TValue>(),\n )\n key: string\n options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>\n asyncRetryers = new Map<\n number,\n AsyncRetryer<(items: Array<TValue>) => Promise<any>>\n >()\n #timeoutId: NodeJS.Timeout | null = null\n\n constructor(\n public fn: (items: Array<TValue>) => Promise<any>,\n initialOptions: AsyncBatcherOptions<TValue>,\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-AsyncBatcher', (event) => {\n if (event.payload.key !== this.key) return\n this.#setState(event.payload.store.state)\n this.setOptions(event.payload.options)\n })\n }\n\n /**\n * Emits a change event for the async batcher instance. Mostly useful for devtools.\n */\n _emit = () => emitChange('AsyncBatcher', this)\n\n /**\n * Updates the async batcher options\n */\n setOptions = (newOptions: Partial<AsyncBatcherOptions<TValue>>): void => {\n this.options = { ...this.options, ...newOptions }\n }\n\n #setState = (newState: Partial<AsyncBatcherState<TValue>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isExecuting, isPending, items } = combinedState\n const size = items.length\n const isEmpty = size === 0\n return {\n ...combinedState,\n isEmpty,\n size,\n status: isExecuting\n ? 'executing'\n : isPending\n ? 'pending'\n : isEmpty\n ? 'idle'\n : 'populated',\n }\n })\n emitChange('AsyncBatcher', this)\n }\n\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Adds an item to the async batcher\n * If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed\n *\n * @returns The result from the batch function, or undefined if an error occurred and was handled by onError\n *\n * @throws The error from the batch function if no onError handler is configured or throwOnError is true\n */\n addItem = async (item: TValue): Promise<any> => {\n this.#setState({\n items: [...this.store.state.items, item],\n isPending: this.options.wait !== Infinity,\n })\n this.options.onItemsChange?.(this)\n\n const shouldProcess =\n this.store.state.items.length >= this.options.maxSize ||\n this.options.getShouldExecute(this.store.state.items, this)\n\n if (shouldProcess) {\n return await this.#execute()\n } else if (this.options.wait !== Infinity) {\n this.#clearTimeout() // clear any pending timeout to replace it with a new one\n this.#timeoutId = setTimeout(() => this.#execute(), this.#getWait())\n await new Promise((resolve) => setTimeout(resolve, this.#getWait()))\n }\n }\n\n /**\n * Processes the current batch of items asynchronously.\n * This method will automatically be triggered if the batcher is running and any of these conditions are met:\n * - The number of items reaches maxSize\n * - The wait duration has elapsed\n * - The getShouldExecute function returns true upon adding an item\n *\n * You can also call this method manually to process the current batch at any time.\n *\n * @returns A promise that resolves with the result of the batch function, or undefined if an error occurred and was handled by onError\n * @throws The error from the batch function if no onError handler is configured or throwOnError is true\n */\n #execute = async (): Promise<any> => {\n if (this.store.state.items.length === 0) {\n return undefined\n }\n\n const currentExecuteCount = this.store.state.executeCount + 1\n const batch = this.peekAllItems() // copy of the items to be processed (to prevent race conditions)\n this.clear() // Clear items before processing to prevent race conditions\n this.options.onItemsChange?.(this)\n\n this.#setState({ isExecuting: true, executeCount: currentExecuteCount })\n\n try {\n const currentAsyncRetryer = new AsyncRetryer(\n this.fn,\n this.options.asyncRetryerOptions,\n )\n this.asyncRetryers.set(currentExecuteCount, currentAsyncRetryer)\n const result = await currentAsyncRetryer.execute(batch) // EXECUTE\n this.#setState({\n totalItemsProcessed:\n this.store.state.totalItemsProcessed + batch.length,\n lastResult: result,\n successCount: this.store.state.successCount + 1,\n })\n this.options.onSuccess?.(result, batch, this)\n return result\n } catch (error) {\n this.#setState({\n errorCount: this.store.state.errorCount + 1,\n failedItems: [...this.store.state.failedItems, ...batch],\n totalItemsFailed: this.store.state.totalItemsFailed + batch.length,\n })\n this.options.onError?.(error as Error, batch, this)\n if (this.options.throwOnError) {\n throw error\n }\n return undefined\n } finally {\n this.asyncRetryers.delete(currentExecuteCount) // dispose retryer\n this.#setState({\n isExecuting: false,\n settleCount: this.store.state.settleCount + 1,\n })\n this.options.onSettled?.(batch, this)\n }\n }\n\n /**\n * Processes the current batch of items immediately\n */\n flush = async (): Promise<any> => {\n this.#clearTimeout() // clear any pending timeout\n return await this.#execute()\n }\n\n /**\n * Returns a copy of all items in the async batcher\n */\n peekAllItems = (): Array<TValue> => {\n return [...this.store.state.items]\n }\n\n peekFailedItems = (): Array<TValue> => {\n return [...this.store.state.failedItems]\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = null\n }\n }\n\n /**\n * Removes all items from the async batcher\n */\n clear = (): void => {\n this.#setState({ items: [], failedItems: [], isPending: false })\n }\n\n /**\n * Returns the AbortSignal for a specific execution.\n * If no executeCount 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 executeCount - Optional specific execution to get signal for\n * @example\n * ```typescript\n * const batcher = new AsyncBatcher(\n * async (items: string[]) => {\n * const signal = batcher.getAbortSignal()\n * if (signal) {\n * const response = await fetch('/api/batch', {\n * method: 'POST',\n * body: JSON.stringify(items),\n * signal\n * })\n * return response.json()\n * }\n * },\n * { maxSize: 10, wait: 100 }\n * )\n * ```\n */\n getAbortSignal(executeCount?: number): AbortSignal | null {\n const count = executeCount ?? this.store.state.executeCount\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 * Does NOT clear out the items.\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 * Does NOT clear out the items.\n */\n cancel = (): void => {\n this.#clearTimeout()\n this.#setState({\n isPending: false,\n })\n }\n\n /**\n * Resets the async batcher state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultAsyncBatcherState<TValue>())\n this.options.onItemsChange?.(this)\n this.asyncRetryers.forEach((retryer) => retryer.reset())\n }\n}\n\n/**\n * Creates an async batcher that processes items in batches.\n *\n * Async vs Sync Versions:\n * The async version provides advanced features over the sync batch function:\n * - Returns promises that can be awaited for batch results\n * - Built-in retry support via AsyncRetryer integration\n * - Abort support to cancel in-flight batch executions\n * - Cancel support to prevent pending batches from starting\n * - Comprehensive error handling with onError callbacks and throwOnError control\n * - Detailed execution tracking (success/error/settle counts)\n *\n * The sync batch function is lighter weight and simpler when you don't need async features,\n * return values, or execution control.\n *\n * What is Batching?\n * Batching is a technique for grouping multiple operations together to be processed as a single unit.\n *\n * Configuration Options:\n * - `maxSize`: Maximum number of items per batch (default: Infinity)\n * - `wait`: Time to wait before processing batch (default: Infinity)\n * - `getShouldExecute`: Custom logic to trigger batch processing\n * - `asyncRetryerOptions`: Configure retry behavior for batch executions\n * - `started`: Whether to start processing immediately (default: true)\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher 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 AsyncBatcher instance\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the async batcher\n * - Use `onSuccess` callback to react to successful batch execution and implement custom logic\n * - Use `onError` callback to react to batch execution errors and implement custom error handling\n * - Use `onSettled` callback to react to batch execution completion (success or error) and implement custom logic\n * - Use `onItemsChange` callback to react to items being added or removed from the batcher\n * - The state includes total items processed, success/error counts, and execution status\n * - State can be accessed via the underlying AsyncBatcher instance's `store.state` property\n * - When using framework adapters (React/Solid), state is accessed from the hook's state property\n *\n * @example\n * ```ts\n * const batchItems = asyncBatch<number>(\n * async (items) => {\n * const result = await processApiCall(items);\n * console.log('Processing:', items);\n * return result;\n * },\n * {\n * maxSize: 3,\n * wait: 1000,\n * onSuccess: (result) => console.log('Batch succeeded:', result),\n * onError: (error) => console.error('Batch failed:', error)\n * }\n * );\n *\n * batchItems(1);\n * batchItems(2);\n * batchItems(3); // Triggers batch processing\n * ```\n */\nexport function asyncBatch<TValue>(\n fn: (items: Array<TValue>) => Promise<any>,\n options: AsyncBatcherOptions<TValue>,\n) {\n const batcher = new AsyncBatcher<TValue>(fn, options)\n return batcher.addItem\n}\n"],"names":["Store","emitChange","parseFunctionOrValue","AsyncRetryer","createKey","pacerEventClient"],"mappings":";;;;;;AAkEA,SAAS,8BAAiE;AACxE,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,aAAa,CAAA;AAAA,IACb,SAAS;AAAA,IACT,aAAa;AAAA,IACb,WAAW;AAAA,IACX,OAAO,CAAA;AAAA,IACP,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,cAAc;AAAA,IACd,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,EAAA;AAEtB;AAoFO,SAAS,oBAKd,SAA6B;AAC7B,SAAO;AACT;AAYA,MAAM,iBAAgE;AAAA,EACpE,qBAAqB;AAAA,IACnB,aAAa;AAAA,EAAA;AAAA,EAEf,kBAAkB,MAAM;AAAA,EACxB,SAAS;AAAA,EACT,SAAS;AAAA,EACT,cAAc;AAAA,EACd,MAAM;AACR;AAqEO,MAAM,aAAqB;AAAA,EAYhC,YACS,IACP,gBACA;AAFO,SAAA,KAAA;AAZT,SAAS,QAAoD,IAAIA,MAAAA;AAAAA,MAC/D,4BAAA;AAAA,IAAoC;AAItC,SAAA,oCAAoB,IAAA;AAIpB,SAAA,aAAoC;AAwBpC,SAAA,QAAQ,MAAMC,uBAAW,gBAAgB,I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AA,EAnBA;AAAA,EAiCA;AAAA,EAyBA;AAAA,EA4CA;AAAA,EAmEA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsCA,eAAe,cAA2C;AACxD,UAAM,QAAQ,gBAAgB,KAAK,MAAM,MAAM;AAC/C,UAAM,UAAU,KAAK,cAAc,IAAI,KAAK;AAC5C,WAAO,SAAS,oBAAoB;AAAA,EACtC;AAmCF;AAkEO,SAAS,WACd,IACA,SACA;AACA,QAAM,UAAU,IAAI,aAAqB,IAAI,OAAO;AACpD,SAAO,QAAQ;AACjB;;;;"}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { Store } from '@tanstack/store';
|
|
2
|
+
import { AsyncRetryer, AsyncRetryerOptions } from './async-retryer.cjs';
|
|
2
3
|
import { OptionalKeys } from './types.cjs';
|
|
3
4
|
export interface AsyncBatcherState<TValue> {
|
|
4
5
|
/**
|
|
5
6
|
* Number of batch executions that have resulted in errors
|
|
6
7
|
*/
|
|
7
8
|
errorCount: number;
|
|
9
|
+
/**
|
|
10
|
+
* Number of batch executions that have been executed
|
|
11
|
+
*/
|
|
12
|
+
executeCount: number;
|
|
8
13
|
/**
|
|
9
14
|
* Array of items that failed during batch processing
|
|
10
15
|
*/
|
|
@@ -58,6 +63,10 @@ export interface AsyncBatcherState<TValue> {
|
|
|
58
63
|
* Options for configuring an AsyncBatcher instance
|
|
59
64
|
*/
|
|
60
65
|
export interface AsyncBatcherOptions<TValue> {
|
|
66
|
+
/**
|
|
67
|
+
* Options for configuring the underlying async retryer
|
|
68
|
+
*/
|
|
69
|
+
asyncRetryerOptions?: AsyncRetryerOptions<(items: Array<TValue>) => Promise<any>>;
|
|
61
70
|
/**
|
|
62
71
|
* Custom function to determine if a batch should be processed
|
|
63
72
|
* Return true to process the batch immediately
|
|
@@ -82,7 +91,7 @@ export interface AsyncBatcherOptions<TValue> {
|
|
|
82
91
|
* If provided, the handler will be called with the error, the batch of items that failed, and batcher instance.
|
|
83
92
|
* This can be used alongside throwOnError - the handler will be called before any error is thrown.
|
|
84
93
|
*/
|
|
85
|
-
onError?: (error:
|
|
94
|
+
onError?: (error: Error, batch: Array<TValue>, batcher: AsyncBatcher<TValue>) => void;
|
|
86
95
|
/**
|
|
87
96
|
* Callback fired after items are added to the batcher
|
|
88
97
|
*/
|
|
@@ -114,17 +123,28 @@ export interface AsyncBatcherOptions<TValue> {
|
|
|
114
123
|
*/
|
|
115
124
|
wait?: number | ((asyncBatcher: AsyncBatcher<TValue>) => number);
|
|
116
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Utility function for sharing common `AsyncBatcherOptions` options between different `AsyncBatcher` instances.
|
|
128
|
+
*
|
|
129
|
+
*/
|
|
130
|
+
export declare function asyncBatcherOptions<TValue = any, TOptions extends Partial<AsyncBatcherOptions<TValue>> = Partial<AsyncBatcherOptions<TValue>>>(options: TOptions): TOptions;
|
|
117
131
|
type AsyncBatcherOptionsWithOptionalCallbacks<TValue> = OptionalKeys<Required<AsyncBatcherOptions<TValue>>, 'initialState' | 'onError' | 'onItemsChange' | 'onSettled' | 'onSuccess' | 'key'>;
|
|
118
132
|
/**
|
|
119
133
|
* A class that collects items and processes them in batches asynchronously.
|
|
120
134
|
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* -
|
|
124
|
-
* -
|
|
125
|
-
* -
|
|
126
|
-
* -
|
|
135
|
+
* Async vs Sync Versions:
|
|
136
|
+
* The async version provides advanced features over the sync Batcher:
|
|
137
|
+
* - Returns promises that can be awaited for batch results
|
|
138
|
+
* - Built-in retry support via AsyncRetryer integration
|
|
139
|
+
* - Abort support to cancel in-flight batch executions
|
|
140
|
+
* - Cancel support to prevent pending batches from starting
|
|
141
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
142
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
143
|
+
*
|
|
144
|
+
* The sync Batcher is lighter weight and simpler when you don't need async features,
|
|
145
|
+
* return values, or execution control.
|
|
127
146
|
*
|
|
147
|
+
* What is Batching?
|
|
128
148
|
* Batching is a technique for grouping multiple operations together to be processed as a single unit.
|
|
129
149
|
*
|
|
130
150
|
* The AsyncBatcher provides a flexible way to implement async batching with configurable:
|
|
@@ -182,7 +202,12 @@ export declare class AsyncBatcher<TValue> {
|
|
|
182
202
|
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
|
|
183
203
|
key: string;
|
|
184
204
|
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
|
|
205
|
+
asyncRetryers: Map<number, AsyncRetryer<(items: Array<TValue>) => Promise<any>>>;
|
|
185
206
|
constructor(fn: (items: Array<TValue>) => Promise<any>, initialOptions: AsyncBatcherOptions<TValue>);
|
|
207
|
+
/**
|
|
208
|
+
* Emits a change event for the async batcher instance. Mostly useful for devtools.
|
|
209
|
+
*/
|
|
210
|
+
_emit: () => void;
|
|
186
211
|
/**
|
|
187
212
|
* Updates the async batcher options
|
|
188
213
|
*/
|
|
@@ -190,8 +215,12 @@ export declare class AsyncBatcher<TValue> {
|
|
|
190
215
|
/**
|
|
191
216
|
* Adds an item to the async batcher
|
|
192
217
|
* If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
|
|
218
|
+
*
|
|
219
|
+
* @returns The result from the batch function, or undefined if an error occurred and was handled by onError
|
|
220
|
+
*
|
|
221
|
+
* @throws The error from the batch function if no onError handler is configured or throwOnError is true
|
|
193
222
|
*/
|
|
194
|
-
addItem: (item: TValue) =>
|
|
223
|
+
addItem: (item: TValue) => Promise<any>;
|
|
195
224
|
/**
|
|
196
225
|
* Processes the current batch of items immediately
|
|
197
226
|
*/
|
|
@@ -205,19 +234,72 @@ export declare class AsyncBatcher<TValue> {
|
|
|
205
234
|
* Removes all items from the async batcher
|
|
206
235
|
*/
|
|
207
236
|
clear: () => void;
|
|
237
|
+
/**
|
|
238
|
+
* Returns the AbortSignal for a specific execution.
|
|
239
|
+
* If no executeCount is provided, returns the signal for the most recent execution.
|
|
240
|
+
* Returns null if no execution is found or not currently executing.
|
|
241
|
+
*
|
|
242
|
+
* @param executeCount - Optional specific execution to get signal for
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const batcher = new AsyncBatcher(
|
|
246
|
+
* async (items: string[]) => {
|
|
247
|
+
* const signal = batcher.getAbortSignal()
|
|
248
|
+
* if (signal) {
|
|
249
|
+
* const response = await fetch('/api/batch', {
|
|
250
|
+
* method: 'POST',
|
|
251
|
+
* body: JSON.stringify(items),
|
|
252
|
+
* signal
|
|
253
|
+
* })
|
|
254
|
+
* return response.json()
|
|
255
|
+
* }
|
|
256
|
+
* },
|
|
257
|
+
* { maxSize: 10, wait: 100 }
|
|
258
|
+
* )
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
getAbortSignal(executeCount?: number): AbortSignal | null;
|
|
262
|
+
/**
|
|
263
|
+
* Aborts all ongoing executions with the internal abort controllers.
|
|
264
|
+
* Does NOT cancel any pending execution that have not started yet.
|
|
265
|
+
* Does NOT clear out the items.
|
|
266
|
+
*/
|
|
267
|
+
abort: () => void;
|
|
268
|
+
/**
|
|
269
|
+
* Cancels any pending execution that have not started yet.
|
|
270
|
+
* Does NOT abort any execution already in progress.
|
|
271
|
+
* Does NOT clear out the items.
|
|
272
|
+
*/
|
|
273
|
+
cancel: () => void;
|
|
208
274
|
/**
|
|
209
275
|
* Resets the async batcher state to its default values
|
|
210
276
|
*/
|
|
211
277
|
reset: () => void;
|
|
212
278
|
}
|
|
213
279
|
/**
|
|
214
|
-
* Creates an async batcher that processes items in batches
|
|
280
|
+
* Creates an async batcher that processes items in batches.
|
|
215
281
|
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* -
|
|
219
|
-
* -
|
|
220
|
-
* -
|
|
282
|
+
* Async vs Sync Versions:
|
|
283
|
+
* The async version provides advanced features over the sync batch function:
|
|
284
|
+
* - Returns promises that can be awaited for batch results
|
|
285
|
+
* - Built-in retry support via AsyncRetryer integration
|
|
286
|
+
* - Abort support to cancel in-flight batch executions
|
|
287
|
+
* - Cancel support to prevent pending batches from starting
|
|
288
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
289
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
290
|
+
*
|
|
291
|
+
* The sync batch function is lighter weight and simpler when you don't need async features,
|
|
292
|
+
* return values, or execution control.
|
|
293
|
+
*
|
|
294
|
+
* What is Batching?
|
|
295
|
+
* Batching is a technique for grouping multiple operations together to be processed as a single unit.
|
|
296
|
+
*
|
|
297
|
+
* Configuration Options:
|
|
298
|
+
* - `maxSize`: Maximum number of items per batch (default: Infinity)
|
|
299
|
+
* - `wait`: Time to wait before processing batch (default: Infinity)
|
|
300
|
+
* - `getShouldExecute`: Custom logic to trigger batch processing
|
|
301
|
+
* - `asyncRetryerOptions`: Configure retry behavior for batch executions
|
|
302
|
+
* - `started`: Whether to start processing immediately (default: true)
|
|
221
303
|
*
|
|
222
304
|
* Error Handling:
|
|
223
305
|
* - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher instance
|
|
@@ -232,7 +314,6 @@ export declare class AsyncBatcher<TValue> {
|
|
|
232
314
|
* - Use `onSuccess` callback to react to successful batch execution and implement custom logic
|
|
233
315
|
* - Use `onError` callback to react to batch execution errors and implement custom error handling
|
|
234
316
|
* - Use `onSettled` callback to react to batch execution completion (success or error) and implement custom logic
|
|
235
|
-
* - Use `onExecute` callback to react to batch execution and implement custom logic
|
|
236
317
|
* - Use `onItemsChange` callback to react to items being added or removed from the batcher
|
|
237
318
|
* - The state includes total items processed, success/error counts, and execution status
|
|
238
319
|
* - State can be accessed via the underlying AsyncBatcher instance's `store.state` property
|
|
@@ -259,5 +340,5 @@ export declare class AsyncBatcher<TValue> {
|
|
|
259
340
|
* batchItems(3); // Triggers batch processing
|
|
260
341
|
* ```
|
|
261
342
|
*/
|
|
262
|
-
export declare function asyncBatch<TValue>(fn: (items: Array<TValue>) => Promise<any>, options: AsyncBatcherOptions<TValue>): (item: TValue) =>
|
|
343
|
+
export declare function asyncBatch<TValue>(fn: (items: Array<TValue>) => Promise<any>, options: AsyncBatcherOptions<TValue>): (item: TValue) => Promise<any>;
|
|
263
344
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const store = require("@tanstack/store");
|
|
4
|
+
const asyncRetryer = require("./async-retryer.cjs");
|
|
4
5
|
const utils = require("./utils.cjs");
|
|
5
6
|
const eventClient = require("./event-client.cjs");
|
|
6
7
|
function getDefaultAsyncDebouncerState() {
|
|
@@ -17,7 +18,13 @@ function getDefaultAsyncDebouncerState() {
|
|
|
17
18
|
successCount: 0
|
|
18
19
|
};
|
|
19
20
|
}
|
|
21
|
+
function asyncDebouncerOptions(options) {
|
|
22
|
+
return options;
|
|
23
|
+
}
|
|
20
24
|
const defaultOptions = {
|
|
25
|
+
asyncRetryerOptions: {
|
|
26
|
+
maxAttempts: 1
|
|
27
|
+
},
|
|
21
28
|
enabled: true,
|
|
22
29
|
leading: false,
|
|
23
30
|
trailing: true,
|
|
@@ -27,9 +34,10 @@ class AsyncDebouncer {
|
|
|
27
34
|
constructor(fn, initialOptions) {
|
|
28
35
|
this.fn = fn;
|
|
29
36
|
this.store = new store.Store(getDefaultAsyncDebouncerState());
|
|
30
|
-
this
|
|
37
|
+
this.asyncRetryers = /* @__PURE__ */ new Map();
|
|
31
38
|
this.#timeoutId = null;
|
|
32
39
|
this.#resolvePreviousPromise = null;
|
|
40
|
+
this._emit = () => eventClient.emitChange("AsyncDebouncer", this);
|
|
33
41
|
this.setOptions = (newOptions) => {
|
|
34
42
|
this.options = { ...this.options, ...newOptions };
|
|
35
43
|
if (!this.#getEnabled()) {
|
|
@@ -89,10 +97,15 @@ class AsyncDebouncer {
|
|
|
89
97
|
};
|
|
90
98
|
this.#execute = async (...args) => {
|
|
91
99
|
if (!this.#getEnabled()) return void 0;
|
|
92
|
-
|
|
100
|
+
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1;
|
|
93
101
|
try {
|
|
94
102
|
this.#setState({ isExecuting: true });
|
|
95
|
-
const
|
|
103
|
+
const currentAsyncRetryer = new asyncRetryer.AsyncRetryer(this.fn, {
|
|
104
|
+
...this.options.asyncRetryerOptions,
|
|
105
|
+
key: `${this.key}-retryer-${currentMaybeExecuteCount}`
|
|
106
|
+
});
|
|
107
|
+
this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer);
|
|
108
|
+
const result = await currentAsyncRetryer.execute(...args);
|
|
96
109
|
this.#setState({
|
|
97
110
|
lastResult: result,
|
|
98
111
|
successCount: this.store.state.successCount + 1
|
|
@@ -107,24 +120,22 @@ class AsyncDebouncer {
|
|
|
107
120
|
throw error;
|
|
108
121
|
}
|
|
109
122
|
} finally {
|
|
123
|
+
this.asyncRetryers.delete(currentMaybeExecuteCount);
|
|
110
124
|
this.#setState({
|
|
111
125
|
isExecuting: false,
|
|
112
126
|
isPending: false,
|
|
113
127
|
lastArgs: void 0,
|
|
114
128
|
settleCount: this.store.state.settleCount + 1
|
|
115
129
|
});
|
|
116
|
-
this.#abortController = null;
|
|
117
130
|
this.options.onSettled?.(args, this);
|
|
118
131
|
}
|
|
119
132
|
return this.store.state.lastResult;
|
|
120
133
|
};
|
|
121
134
|
this.flush = async () => {
|
|
122
135
|
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
123
|
-
this
|
|
124
|
-
this.#
|
|
125
|
-
|
|
126
|
-
this.#resolvePreviousPromiseInternal();
|
|
127
|
-
return result;
|
|
136
|
+
const { lastArgs } = this.store.state;
|
|
137
|
+
this.#cancelPendingExecution();
|
|
138
|
+
return await this.#execute(...lastArgs);
|
|
128
139
|
}
|
|
129
140
|
return void 0;
|
|
130
141
|
};
|
|
@@ -145,23 +156,23 @@ class AsyncDebouncer {
|
|
|
145
156
|
this.#resolvePreviousPromiseInternal();
|
|
146
157
|
this.#setState({
|
|
147
158
|
isPending: false,
|
|
148
|
-
isExecuting: false,
|
|
149
159
|
lastArgs: void 0
|
|
150
160
|
});
|
|
151
161
|
};
|
|
152
|
-
this
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
162
|
+
this.abort = () => {
|
|
163
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
164
|
+
this.asyncRetryers.clear();
|
|
165
|
+
this.#setState({
|
|
166
|
+
isExecuting: false
|
|
167
|
+
});
|
|
157
168
|
};
|
|
158
169
|
this.cancel = () => {
|
|
159
170
|
this.#cancelPendingExecution();
|
|
160
|
-
this.#abortExecution();
|
|
161
171
|
this.#setState({ canLeadingExecute: true });
|
|
162
172
|
};
|
|
163
173
|
this.reset = () => {
|
|
164
174
|
this.#setState(getDefaultAsyncDebouncerState());
|
|
175
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
165
176
|
};
|
|
166
177
|
this.key = utils.createKey(initialOptions.key);
|
|
167
178
|
this.options = {
|
|
@@ -176,7 +187,6 @@ class AsyncDebouncer {
|
|
|
176
187
|
this.setOptions(event.payload.options);
|
|
177
188
|
});
|
|
178
189
|
}
|
|
179
|
-
#abortController;
|
|
180
190
|
#timeoutId;
|
|
181
191
|
#resolvePreviousPromise;
|
|
182
192
|
#setState;
|
|
@@ -186,7 +196,31 @@ class AsyncDebouncer {
|
|
|
186
196
|
#resolvePreviousPromiseInternal;
|
|
187
197
|
#clearTimeout;
|
|
188
198
|
#cancelPendingExecution;
|
|
189
|
-
|
|
199
|
+
/**
|
|
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) {
|
|
220
|
+
const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount;
|
|
221
|
+
const retryer = this.asyncRetryers.get(count);
|
|
222
|
+
return retryer?.getAbortSignal() ?? null;
|
|
223
|
+
}
|
|
190
224
|
}
|
|
191
225
|
function asyncDebounce(fn, initialOptions) {
|
|
192
226
|
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
@@ -194,4 +228,5 @@ function asyncDebounce(fn, initialOptions) {
|
|
|
194
228
|
}
|
|
195
229
|
exports.AsyncDebouncer = AsyncDebouncer;
|
|
196
230
|
exports.asyncDebounce = asyncDebounce;
|
|
231
|
+
exports.asyncDebouncerOptions = asyncDebouncerOptions;
|
|
197
232
|
//# sourceMappingURL=async-debouncer.cjs.map
|