Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

createAsyncBatcher.md

113 lines4.0 KB

1---
2id: createAsyncBatcher
3title: createAsyncBatcher
4---
5​
6```ts
7function createAsyncBatcher<TValue, TSelected>(
8 host,
9 fn,
10 options?,
11selector?): LitAsyncBatcher<TValue, TSelected>;
12```
13​
14Defined in: [async-batcher/createAsyncBatcher.ts:99](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/async-batcher/createAsyncBatcher.ts#L99)
15​
16Creates and retains the AsyncBatcher for its Lit owner.
17​
18Collects items and processes them together when maxSize, wait, or getShouldExecute triggers a batch. Use addItem to accumulate work and flush to process a partial batch.
19​
20The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
21Use onSuccess, onError, and onSettled for execution outcomes.
22​
23## State and subscriptions
24​
25Pass a selector to track only the state consumed by the owner. The default selection is {},
26so utility state changes do not update the owner unless it opts in. Selection uses shallow
27comparison. The raw store remains available for additional subscriptions.
28Use utility.subscribe(childHost, selector) for a child subscription. It returns a getter
29and cleans up with the child without canceling the parent utility.
30​
31Available state fields:
32​
33- `errorCount`: Number of batch executions that have resulted in errors
34- `executionCount`: Number of batch executions that have been started
35- `failedItems`: Array of items that failed during batch processing
36- `isEmpty`: Whether the batcher has no items to process (items array is empty)
37- `isExecuting`: Whether a batch is currently being processed asynchronously
38- `isPending`: Whether the batcher is waiting for the timeout to trigger batch processing
39- `items`: Array of items currently queued for batch processing
40- `lastResult`: The result from the most recent batch execution
41- `settleCount`: Number of batch executions that have completed (either successfully or with errors)
42- `size`: Number of items currently in the batch queue
43- `status`: 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
44- `successCount`: Number of batch executions that have completed successfully
45- `totalItemsFailed`: Total number of items that have failed processing across all batches
46- `totalItemsProcessed`: Total number of items that have been processed across all batches
47​
48## Options and ownership
49​
50Pass an options object with property getters or a factory. Top-level properties are read
51reactively; function-valued core options remain callbacks. Local options override provider
52defaults. Updates retain the utility, its store, counters, and pending work.
53Disconnecting the host calls cancel() and abort().
54onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
55must perform all required cleanup. Use flush() where supported to finish pending work.
56Reconnecting the host refreshes options and restores subscriptions to the same instance.
57​
58## Type Parameters
59​
60### TValue
61​
62`TValue`
63​
64### TSelected
65​
66`TSelected` = \{
67\}
68​
69## Parameters
70​
71### host
72​
73`ReactiveControllerHost`
74​
75Owner of option updates, subscriptions, and cleanup.
76​
77### fn
78​
79(`items`) => `Promise`\<`any`\>
80​
81Function executed by the utility.
82​
83### options?
84​
85[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitAsyncBatcherOptions`](../interfaces/LitAsyncBatcherOptions.md)\<`TValue`, `TSelected`\>\> = `{}`
86​
87Core options or a reactive factory, plus an optional onUnmount callback.
88​
89### selector?
90​
91(`state`) => `TSelected`
92​
93Selects state that updates the owner. Omit to leave selected state empty.
94​
95## Returns
96​
97[`LitAsyncBatcher`](../interfaces/LitAsyncBatcher.md)\<`TValue`, `TSelected`\>
98​
99The retained utility instance with selected state and child subscriptions.
100​
101## Example
102​
103```ts
104import { createAsyncBatcher } from '@tanstack/lit-pacer'
105​
106const utility = createAsyncBatcher(
107 this, async (items: Array<string>) => { console.log(items) },
108 { maxSize: 5, wait: 1000 },
109 (state) => ({ size: state.size }),
111utility.addItem('item')
112// Selected state: utility.state.size
113```