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

UseBatcher.md

155 lines4.1 KB

1---
2id: UseBatcher
3title: UseBatcher
4---
5​
6Defined in: [packages/ember-pacer/src/batcher/useBatcher.ts:84](https://github.com/TanStack/pacer/blob/main/packages/ember-pacer/src/batcher/useBatcher.ts#L84)
7​
8Creates and retains the Batcher for its Ember owner.
9​
10Collects 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.
11​
12## State and subscriptions
13​
14Pass a selector to track only the state consumed by the owner. The default selection is {},
15so utility state changes do not update the owner unless it opts in. Selection uses shallow
16comparison. The raw store remains available for additional subscriptions.
17The selector is the second positional argument. Read utility.state from the template.
18The contextual utility.Subscribe helper selects state for a child template.
19​
20Available state fields:
21​
22- `executionCount`: Number of batch executions that have been completed
23- `isEmpty`: Whether the batcher has no items to process (items array is empty)
24- `isPending`: Whether the batcher is waiting for the timeout to trigger batch processing
25- `items`: Array of items currently queued for batch processing
26- `size`: Number of items currently in the batch queue
27- `status`: Current processing status - 'idle' when not processing, 'pending' when waiting for timeout
28- `totalItemsProcessed`: Total number of items that have been processed across all batches
29​
30## Options and ownership
31​
32Tracked named arguments update options after rendering. createPacerScope supplies shared
33defaults through contextual helpers. Local named options override those defaults.
34Removing the helper invocation calls cancel().
35onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
36must perform all required cleanup. Use flush() where supported to finish pending work.
37​
38## Example
39​
40```gts
41import { on } from '@ember/modifier'
42import { fn } from '@ember/helper'
43import { useBatcher } from '@tanstack/ember-pacer'
44import type { BatcherState } from '@tanstack/ember-pacer'
45​
46const select = (state: BatcherState<string>) => ({ size: state.size })
47​
48<template>
49 {{#let (useBatcher @process select maxSize=5 wait=1000) as |utility|}}
50 <button {{on "click" (fn utility.addItem "item")}}>Schedule</button>
51 <span>{{utility.state.size}}</span>
52 {{/let}}
53</template>
54```
55​
56## Extends
57​
58- `default`\<\{
59 `Args`: \{
60 `Named`: [`EmberBatcherOptions`](../interfaces/EmberBatcherOptions.md)\<`TValue`, `TSelected`\>;
61 `Positional`: \| \[(`items`) => `void`\]
62 \| \[(`items`) => `void`, (`state`) => `TSelected`\];
63 \};
64 `Return`: [`EmberBatcher`](../interfaces/EmberBatcher.md)\<`TValue`, `TSelected`\>;
65\}\>
66​
67## Type Parameters
68​
69### TValue
70​
71`TValue`
72​
73### TSelected
74​
75`TSelected` = \{
76\}
77​
78## Constructors
79​
80### Constructor
81​
82```ts
83new UseBatcher<TValue, TSelected>(owner?): UseBatcher;
84```
85​
86Defined in: node\_modules/.pnpm/ember-source@7.3.0\_@glimmer+component@2.1.1\_supports-color@7.2.0\_\_supports-color@7.2.0/node\_modules/ember-source/types/stable/@ember/object/index.d.ts:28
87​
88#### Parameters
89​
90##### owner?
91​
92`Owner`
93​
94#### Returns
95​
96`UseBatcher`
97​
98#### Inherited from
99​
100```ts
101Helper<{
102 Args: {
103 Positional:
104 | [fn: (items: Array<TValue>) => void]
105 | [
106 fn: (items: Array<TValue>) => void,
107 selector: (state: BatcherState<TValue>) => TSelected,
108 ]
109 Named: EmberBatcherOptions<TValue, TSelected>
110 }
111 Return: EmberBatcher<TValue, TSelected>
112}>.constructor
113```
114​
115## Methods
116​
117### compute()
118​
119```ts
120compute(positional, options): EmberBatcher<TValue, TSelected>;
121```
122​
123Defined in: [packages/ember-pacer/src/batcher/useBatcher.ts:104](https://github.com/TanStack/pacer/blob/main/packages/ember-pacer/src/batcher/useBatcher.ts#L104)
124​
125Override this function when writing a class-based helper.
126​
127#### Parameters
128​
129##### positional
130​
131\[(`items`) => `void`, (`state`) => `TSelected`\]
132​
133The positional arguments to the helper
134​
135##### options
136​
137[`EmberBatcherOptions`](../interfaces/EmberBatcherOptions.md)\<`TValue`, `TSelected`\>
138​
139#### Returns
140​
141[`EmberBatcher`](../interfaces/EmberBatcher.md)\<`TValue`, `TSelected`\>
142​
143#### Method
144​
145compute
146​
147#### Since
148​
1491.13.0
150​
151#### Overrides
152​
153```ts
154Helper.compute
155```