@tanstack/pacer 0.15.4 → 0.16.1
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 -5
- package/dist/cjs/async-batcher.cjs.map +1 -1
- package/dist/cjs/async-batcher.d.cts +94 -17
- package/dist/cjs/async-debouncer.cjs +53 -19
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +76 -10
- package/dist/cjs/async-queuer.cjs +58 -2
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +100 -10
- package/dist/cjs/async-rate-limiter.cjs +51 -2
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +91 -28
- package/dist/cjs/async-retryer.cjs +285 -0
- package/dist/cjs/async-retryer.cjs.map +1 -0
- package/dist/cjs/async-retryer.d.cts +308 -0
- package/dist/cjs/async-throttler.cjs +101 -53
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +82 -10
- package/dist/cjs/batcher.cjs +5 -1
- package/dist/cjs/batcher.cjs.map +1 -1
- package/dist/cjs/batcher.d.cts +10 -2
- package/dist/cjs/debouncer.cjs +5 -1
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +7 -3
- package/dist/cjs/event-client.cjs +3 -1
- package/dist/cjs/event-client.cjs.map +1 -1
- package/dist/cjs/event-client.d.cts +3 -0
- package/dist/cjs/index.cjs +13 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +5 -1
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +8 -4
- package/dist/cjs/rate-limiter.cjs +5 -1
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +8 -1
- package/dist/cjs/throttler.cjs +5 -1
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +7 -0
- package/dist/cjs/utils.cjs +0 -10
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +0 -1
- package/dist/esm/async-batcher.d.ts +94 -17
- package/dist/esm/async-batcher.js +68 -7
- package/dist/esm/async-batcher.js.map +1 -1
- package/dist/esm/async-debouncer.d.ts +76 -10
- package/dist/esm/async-debouncer.js +55 -21
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +100 -10
- package/dist/esm/async-queuer.js +60 -4
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +91 -28
- package/dist/esm/async-rate-limiter.js +53 -4
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-retryer.d.ts +308 -0
- package/dist/esm/async-retryer.js +285 -0
- package/dist/esm/async-retryer.js.map +1 -0
- package/dist/esm/async-throttler.d.ts +82 -10
- package/dist/esm/async-throttler.js +103 -55
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +10 -2
- package/dist/esm/batcher.js +6 -2
- package/dist/esm/batcher.js.map +1 -1
- package/dist/esm/debouncer.d.ts +7 -3
- package/dist/esm/debouncer.js +7 -3
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/event-client.d.ts +3 -0
- package/dist/esm/event-client.js +3 -1
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +24 -12
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +8 -4
- package/dist/esm/queuer.js +7 -3
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +8 -1
- package/dist/esm/rate-limiter.js +7 -3
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +7 -0
- package/dist/esm/throttler.js +7 -3
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/utils.d.ts +0 -1
- package/dist/esm/utils.js +0 -10
- package/dist/esm/utils.js.map +1 -1
- package/package.json +13 -3
- package/src/async-batcher.ts +144 -22
- package/src/async-debouncer.ts +118 -33
- package/src/async-queuer.ts +147 -14
- package/src/async-rate-limiter.ts +129 -33
- package/src/async-retryer.ts +664 -0
- package/src/async-throttler.ts +196 -75
- package/src/batcher.ts +16 -4
- package/src/debouncer.ts +17 -5
- package/src/event-client.ts +6 -1
- package/src/index.ts +2 -0
- package/src/queuer.ts +19 -6
- package/src/rate-limiter.ts +18 -3
- package/src/throttler.ts +17 -2
- package/src/utils.ts +0 -15
|
@@ -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,6 +41,7 @@ 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;
|
|
37
46
|
this.setOptions = (newOptions) => {
|
|
38
47
|
this.options = { ...this.options, ...newOptions };
|
|
@@ -58,7 +67,7 @@ class AsyncBatcher {
|
|
|
58
67
|
this.#getWait = () => {
|
|
59
68
|
return utils.parseFunctionOrValue(this.options.wait, this);
|
|
60
69
|
};
|
|
61
|
-
this.addItem = (item) => {
|
|
70
|
+
this.addItem = async (item) => {
|
|
62
71
|
this.#setState({
|
|
63
72
|
items: [...this.store.state.items, item],
|
|
64
73
|
isPending: this.options.wait !== Infinity
|
|
@@ -66,22 +75,29 @@ class AsyncBatcher {
|
|
|
66
75
|
this.options.onItemsChange?.(this);
|
|
67
76
|
const shouldProcess = this.store.state.items.length >= this.options.maxSize || this.options.getShouldExecute(this.store.state.items, this);
|
|
68
77
|
if (shouldProcess) {
|
|
69
|
-
this.#execute();
|
|
78
|
+
return await this.#execute();
|
|
70
79
|
} else if (this.options.wait !== Infinity) {
|
|
71
80
|
this.#clearTimeout();
|
|
72
81
|
this.#timeoutId = setTimeout(() => this.#execute(), this.#getWait());
|
|
82
|
+
await new Promise((resolve) => setTimeout(resolve, this.#getWait()));
|
|
73
83
|
}
|
|
74
84
|
};
|
|
75
85
|
this.#execute = async () => {
|
|
76
86
|
if (this.store.state.items.length === 0) {
|
|
77
87
|
return void 0;
|
|
78
88
|
}
|
|
89
|
+
const currentExecuteCount = this.store.state.executeCount + 1;
|
|
79
90
|
const batch = this.peekAllItems();
|
|
80
91
|
this.clear();
|
|
81
92
|
this.options.onItemsChange?.(this);
|
|
82
|
-
this.#setState({ isExecuting: true });
|
|
93
|
+
this.#setState({ isExecuting: true, executeCount: currentExecuteCount });
|
|
83
94
|
try {
|
|
84
|
-
const
|
|
95
|
+
const currentAsyncRetryer = new asyncRetryer.AsyncRetryer(
|
|
96
|
+
this.fn,
|
|
97
|
+
this.options.asyncRetryerOptions
|
|
98
|
+
);
|
|
99
|
+
this.asyncRetryers.set(currentExecuteCount, currentAsyncRetryer);
|
|
100
|
+
const result = await currentAsyncRetryer.execute(batch);
|
|
85
101
|
this.#setState({
|
|
86
102
|
totalItemsProcessed: this.store.state.totalItemsProcessed + batch.length,
|
|
87
103
|
lastResult: result,
|
|
@@ -101,6 +117,7 @@ class AsyncBatcher {
|
|
|
101
117
|
}
|
|
102
118
|
return void 0;
|
|
103
119
|
} finally {
|
|
120
|
+
this.asyncRetryers.delete(currentExecuteCount);
|
|
104
121
|
this.#setState({
|
|
105
122
|
isExecuting: false,
|
|
106
123
|
settleCount: this.store.state.settleCount + 1
|
|
@@ -127,11 +144,25 @@ class AsyncBatcher {
|
|
|
127
144
|
this.clear = () => {
|
|
128
145
|
this.#setState({ items: [], failedItems: [], isPending: false });
|
|
129
146
|
};
|
|
147
|
+
this.abort = () => {
|
|
148
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
149
|
+
this.asyncRetryers.clear();
|
|
150
|
+
this.#setState({
|
|
151
|
+
isExecuting: false
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
this.cancel = () => {
|
|
155
|
+
this.#clearTimeout();
|
|
156
|
+
this.#setState({
|
|
157
|
+
isPending: false
|
|
158
|
+
});
|
|
159
|
+
};
|
|
130
160
|
this.reset = () => {
|
|
131
161
|
this.#setState(getDefaultAsyncBatcherState());
|
|
132
162
|
this.options.onItemsChange?.(this);
|
|
163
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
133
164
|
};
|
|
134
|
-
this.key =
|
|
165
|
+
this.key = initialOptions.key;
|
|
135
166
|
this.options = {
|
|
136
167
|
...defaultOptions,
|
|
137
168
|
...initialOptions,
|
|
@@ -149,6 +180,35 @@ class AsyncBatcher {
|
|
|
149
180
|
#getWait;
|
|
150
181
|
#execute;
|
|
151
182
|
#clearTimeout;
|
|
183
|
+
/**
|
|
184
|
+
* Returns the AbortSignal for a specific execution.
|
|
185
|
+
* If no executeCount is provided, returns the signal for the most recent execution.
|
|
186
|
+
* Returns null if no execution is found or not currently executing.
|
|
187
|
+
*
|
|
188
|
+
* @param executeCount - Optional specific execution to get signal for
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const batcher = new AsyncBatcher(
|
|
192
|
+
* async (items: string[]) => {
|
|
193
|
+
* const signal = batcher.getAbortSignal()
|
|
194
|
+
* if (signal) {
|
|
195
|
+
* const response = await fetch('/api/batch', {
|
|
196
|
+
* method: 'POST',
|
|
197
|
+
* body: JSON.stringify(items),
|
|
198
|
+
* signal
|
|
199
|
+
* })
|
|
200
|
+
* return response.json()
|
|
201
|
+
* }
|
|
202
|
+
* },
|
|
203
|
+
* { maxSize: 10, wait: 100 }
|
|
204
|
+
* )
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
getAbortSignal(executeCount) {
|
|
208
|
+
const count = executeCount ?? this.store.state.executeCount;
|
|
209
|
+
const retryer = this.asyncRetryers.get(count);
|
|
210
|
+
return retryer?.getAbortSignal() ?? null;
|
|
211
|
+
}
|
|
152
212
|
}
|
|
153
213
|
function asyncBatch(fn, options) {
|
|
154
214
|
const batcher = new AsyncBatcher(fn, options);
|
|
@@ -156,4 +216,5 @@ function asyncBatch(fn, options) {
|
|
|
156
216
|
}
|
|
157
217
|
exports.AsyncBatcher = AsyncBatcher;
|
|
158
218
|
exports.asyncBatch = asyncBatch;
|
|
219
|
+
exports.asyncBatcherOptions = asyncBatcherOptions;
|
|
159
220
|
//# 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,iB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,KAAK,MAAM,MAAM,KAAK;AAAA,IAAA;AAGnC,SAAA,kBAAkB,MAAqB;AACrC,aAAO,CAAC,GAAG,KAAK,MAAM,MAAM,WAAW;AAAA,IAAA;AAGzC,SAAA,gBAAgB,MAAY;AAC1B,UAAI,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 { 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 | undefined\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 = 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 * @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","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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CAAC;AAAA,EACH;AAAA,EAnBA;AAAA,EA4BA;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:
|
|
@@ -180,8 +200,9 @@ export declare class AsyncBatcher<TValue> {
|
|
|
180
200
|
#private;
|
|
181
201
|
fn: (items: Array<TValue>) => Promise<any>;
|
|
182
202
|
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
|
|
183
|
-
key: string;
|
|
203
|
+
key: string | undefined;
|
|
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>);
|
|
186
207
|
/**
|
|
187
208
|
* Updates the async batcher options
|
|
@@ -190,8 +211,12 @@ export declare class AsyncBatcher<TValue> {
|
|
|
190
211
|
/**
|
|
191
212
|
* Adds an item to the async batcher
|
|
192
213
|
* If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
|
|
214
|
+
*
|
|
215
|
+
* @returns The result from the batch function, or undefined if an error occurred and was handled by onError
|
|
216
|
+
*
|
|
217
|
+
* @throws The error from the batch function if no onError handler is configured or throwOnError is true
|
|
193
218
|
*/
|
|
194
|
-
addItem: (item: TValue) =>
|
|
219
|
+
addItem: (item: TValue) => Promise<any>;
|
|
195
220
|
/**
|
|
196
221
|
* Processes the current batch of items immediately
|
|
197
222
|
*/
|
|
@@ -205,19 +230,72 @@ export declare class AsyncBatcher<TValue> {
|
|
|
205
230
|
* Removes all items from the async batcher
|
|
206
231
|
*/
|
|
207
232
|
clear: () => void;
|
|
233
|
+
/**
|
|
234
|
+
* Returns the AbortSignal for a specific execution.
|
|
235
|
+
* If no executeCount is provided, returns the signal for the most recent execution.
|
|
236
|
+
* Returns null if no execution is found or not currently executing.
|
|
237
|
+
*
|
|
238
|
+
* @param executeCount - Optional specific execution to get signal for
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const batcher = new AsyncBatcher(
|
|
242
|
+
* async (items: string[]) => {
|
|
243
|
+
* const signal = batcher.getAbortSignal()
|
|
244
|
+
* if (signal) {
|
|
245
|
+
* const response = await fetch('/api/batch', {
|
|
246
|
+
* method: 'POST',
|
|
247
|
+
* body: JSON.stringify(items),
|
|
248
|
+
* signal
|
|
249
|
+
* })
|
|
250
|
+
* return response.json()
|
|
251
|
+
* }
|
|
252
|
+
* },
|
|
253
|
+
* { maxSize: 10, wait: 100 }
|
|
254
|
+
* )
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
getAbortSignal(executeCount?: number): AbortSignal | null;
|
|
258
|
+
/**
|
|
259
|
+
* Aborts all ongoing executions with the internal abort controllers.
|
|
260
|
+
* Does NOT cancel any pending execution that have not started yet.
|
|
261
|
+
* Does NOT clear out the items.
|
|
262
|
+
*/
|
|
263
|
+
abort: () => void;
|
|
264
|
+
/**
|
|
265
|
+
* Cancels any pending execution that have not started yet.
|
|
266
|
+
* Does NOT abort any execution already in progress.
|
|
267
|
+
* Does NOT clear out the items.
|
|
268
|
+
*/
|
|
269
|
+
cancel: () => void;
|
|
208
270
|
/**
|
|
209
271
|
* Resets the async batcher state to its default values
|
|
210
272
|
*/
|
|
211
273
|
reset: () => void;
|
|
212
274
|
}
|
|
213
275
|
/**
|
|
214
|
-
* Creates an async batcher that processes items in batches
|
|
276
|
+
* Creates an async batcher that processes items in batches.
|
|
277
|
+
*
|
|
278
|
+
* Async vs Sync Versions:
|
|
279
|
+
* The async version provides advanced features over the sync batch function:
|
|
280
|
+
* - Returns promises that can be awaited for batch results
|
|
281
|
+
* - Built-in retry support via AsyncRetryer integration
|
|
282
|
+
* - Abort support to cancel in-flight batch executions
|
|
283
|
+
* - Cancel support to prevent pending batches from starting
|
|
284
|
+
* - Comprehensive error handling with onError callbacks and throwOnError control
|
|
285
|
+
* - Detailed execution tracking (success/error/settle counts)
|
|
215
286
|
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
287
|
+
* The sync batch function is lighter weight and simpler when you don't need async features,
|
|
288
|
+
* return values, or execution control.
|
|
289
|
+
*
|
|
290
|
+
* What is Batching?
|
|
291
|
+
* Batching is a technique for grouping multiple operations together to be processed as a single unit.
|
|
292
|
+
*
|
|
293
|
+
* Configuration Options:
|
|
294
|
+
* - `maxSize`: Maximum number of items per batch (default: Infinity)
|
|
295
|
+
* - `wait`: Time to wait before processing batch (default: Infinity)
|
|
296
|
+
* - `getShouldExecute`: Custom logic to trigger batch processing
|
|
297
|
+
* - `asyncRetryerOptions`: Configure retry behavior for batch executions
|
|
298
|
+
* - `started`: Whether to start processing immediately (default: true)
|
|
221
299
|
*
|
|
222
300
|
* Error Handling:
|
|
223
301
|
* - If an `onError` handler is provided, it will be called with the error, the batch of items that failed, and batcher instance
|
|
@@ -232,7 +310,6 @@ export declare class AsyncBatcher<TValue> {
|
|
|
232
310
|
* - Use `onSuccess` callback to react to successful batch execution and implement custom logic
|
|
233
311
|
* - Use `onError` callback to react to batch execution errors and implement custom error handling
|
|
234
312
|
* - 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
313
|
* - Use `onItemsChange` callback to react to items being added or removed from the batcher
|
|
237
314
|
* - The state includes total items processed, success/error counts, and execution status
|
|
238
315
|
* - State can be accessed via the underlying AsyncBatcher instance's `store.state` property
|
|
@@ -259,5 +336,5 @@ export declare class AsyncBatcher<TValue> {
|
|
|
259
336
|
* batchItems(3); // Triggers batch processing
|
|
260
337
|
* ```
|
|
261
338
|
*/
|
|
262
|
-
export declare function asyncBatch<TValue>(fn: (items: Array<TValue>) => Promise<any>, options: AsyncBatcherOptions<TValue>): (item: TValue) =>
|
|
339
|
+
export declare function asyncBatch<TValue>(fn: (items: Array<TValue>) => Promise<any>, options: AsyncBatcherOptions<TValue>): (item: TValue) => Promise<any>;
|
|
263
340
|
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,7 +34,7 @@ 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;
|
|
33
40
|
this.setOptions = (newOptions) => {
|
|
@@ -89,10 +96,15 @@ class AsyncDebouncer {
|
|
|
89
96
|
};
|
|
90
97
|
this.#execute = async (...args) => {
|
|
91
98
|
if (!this.#getEnabled()) return void 0;
|
|
92
|
-
|
|
99
|
+
const currentMaybeExecuteCount = this.store.state.maybeExecuteCount + 1;
|
|
93
100
|
try {
|
|
94
101
|
this.#setState({ isExecuting: true });
|
|
95
|
-
const
|
|
102
|
+
const currentAsyncRetryer = new asyncRetryer.AsyncRetryer(this.fn, {
|
|
103
|
+
...this.options.asyncRetryerOptions,
|
|
104
|
+
key: `${this.key}-retryer-${currentMaybeExecuteCount}`
|
|
105
|
+
});
|
|
106
|
+
this.asyncRetryers.set(currentMaybeExecuteCount, currentAsyncRetryer);
|
|
107
|
+
const result = await currentAsyncRetryer.execute(...args);
|
|
96
108
|
this.#setState({
|
|
97
109
|
lastResult: result,
|
|
98
110
|
successCount: this.store.state.successCount + 1
|
|
@@ -107,24 +119,22 @@ class AsyncDebouncer {
|
|
|
107
119
|
throw error;
|
|
108
120
|
}
|
|
109
121
|
} finally {
|
|
122
|
+
this.asyncRetryers.delete(currentMaybeExecuteCount);
|
|
110
123
|
this.#setState({
|
|
111
124
|
isExecuting: false,
|
|
112
125
|
isPending: false,
|
|
113
126
|
lastArgs: void 0,
|
|
114
127
|
settleCount: this.store.state.settleCount + 1
|
|
115
128
|
});
|
|
116
|
-
this.#abortController = null;
|
|
117
129
|
this.options.onSettled?.(args, this);
|
|
118
130
|
}
|
|
119
131
|
return this.store.state.lastResult;
|
|
120
132
|
};
|
|
121
133
|
this.flush = async () => {
|
|
122
134
|
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
123
|
-
this
|
|
124
|
-
this.#
|
|
125
|
-
|
|
126
|
-
this.#resolvePreviousPromiseInternal();
|
|
127
|
-
return result;
|
|
135
|
+
const { lastArgs } = this.store.state;
|
|
136
|
+
this.#cancelPendingExecution();
|
|
137
|
+
return await this.#execute(...lastArgs);
|
|
128
138
|
}
|
|
129
139
|
return void 0;
|
|
130
140
|
};
|
|
@@ -145,25 +155,25 @@ class AsyncDebouncer {
|
|
|
145
155
|
this.#resolvePreviousPromiseInternal();
|
|
146
156
|
this.#setState({
|
|
147
157
|
isPending: false,
|
|
148
|
-
isExecuting: false,
|
|
149
158
|
lastArgs: void 0
|
|
150
159
|
});
|
|
151
160
|
};
|
|
152
|
-
this
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
161
|
+
this.abort = () => {
|
|
162
|
+
this.asyncRetryers.forEach((retryer) => retryer.abort());
|
|
163
|
+
this.asyncRetryers.clear();
|
|
164
|
+
this.#setState({
|
|
165
|
+
isExecuting: false
|
|
166
|
+
});
|
|
157
167
|
};
|
|
158
168
|
this.cancel = () => {
|
|
159
169
|
this.#cancelPendingExecution();
|
|
160
|
-
this.#abortExecution();
|
|
161
170
|
this.#setState({ canLeadingExecute: true });
|
|
162
171
|
};
|
|
163
172
|
this.reset = () => {
|
|
164
173
|
this.#setState(getDefaultAsyncDebouncerState());
|
|
174
|
+
this.asyncRetryers.forEach((retryer) => retryer.reset());
|
|
165
175
|
};
|
|
166
|
-
this.key =
|
|
176
|
+
this.key = initialOptions.key;
|
|
167
177
|
this.options = {
|
|
168
178
|
...defaultOptions,
|
|
169
179
|
...initialOptions,
|
|
@@ -176,7 +186,6 @@ class AsyncDebouncer {
|
|
|
176
186
|
this.setOptions(event.payload.options);
|
|
177
187
|
});
|
|
178
188
|
}
|
|
179
|
-
#abortController;
|
|
180
189
|
#timeoutId;
|
|
181
190
|
#resolvePreviousPromise;
|
|
182
191
|
#setState;
|
|
@@ -186,7 +195,31 @@ class AsyncDebouncer {
|
|
|
186
195
|
#resolvePreviousPromiseInternal;
|
|
187
196
|
#clearTimeout;
|
|
188
197
|
#cancelPendingExecution;
|
|
189
|
-
|
|
198
|
+
/**
|
|
199
|
+
* Returns the AbortSignal for a specific execution.
|
|
200
|
+
* If no maybeExecuteCount is provided, returns the signal for the most recent execution.
|
|
201
|
+
* Returns null if no execution is found or not currently executing.
|
|
202
|
+
*
|
|
203
|
+
* @param maybeExecuteCount - Optional specific execution to get signal for
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const debouncer = new AsyncDebouncer(
|
|
207
|
+
* async (searchTerm: string) => {
|
|
208
|
+
* const signal = debouncer.getAbortSignal()
|
|
209
|
+
* if (signal) {
|
|
210
|
+
* const response = await fetch(`/api/search?q=${searchTerm}`, { signal })
|
|
211
|
+
* return response.json()
|
|
212
|
+
* }
|
|
213
|
+
* },
|
|
214
|
+
* { wait: 300 }
|
|
215
|
+
* )
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
getAbortSignal(maybeExecuteCount) {
|
|
219
|
+
const count = maybeExecuteCount ?? this.store.state.maybeExecuteCount;
|
|
220
|
+
const retryer = this.asyncRetryers.get(count);
|
|
221
|
+
return retryer?.getAbortSignal() ?? null;
|
|
222
|
+
}
|
|
190
223
|
}
|
|
191
224
|
function asyncDebounce(fn, initialOptions) {
|
|
192
225
|
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
@@ -194,4 +227,5 @@ function asyncDebounce(fn, initialOptions) {
|
|
|
194
227
|
}
|
|
195
228
|
exports.AsyncDebouncer = AsyncDebouncer;
|
|
196
229
|
exports.asyncDebounce = asyncDebounce;
|
|
230
|
+
exports.asyncDebouncerOptions = asyncDebouncerOptions;
|
|
197
231
|
//# sourceMappingURL=async-debouncer.cjs.map
|