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

createBatcher.md

103 lines3.1 KB

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