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