本页还没有译文
本页 32 段还没译成简体中文,下面显示的是英文原文。createBatcher
还没翻译译自英文(tanstack.com)本页 32 段都还没译
function createBatcher<TValue, TSelected>(
scope,
fn,
options?,
selector?): AlpineBatcher<TValue, TSelected>;Defined in: batcher/createBatcher.ts:83
Creates and retains the Batcher for its Alpine owner.
Collects 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.
State and subscriptions
Pass a selector to track only the state consumed by the owner. The default selection is {}, so utility state changes do not update the owner unless it opts in. Selection uses shallow comparison. The raw store remains available for additional subscriptions. Use utility.subscribe(childScope, selector) for a child subscription. It returns a getter and cleans up with the child without canceling the parent utility.
Available state fields:
executionCount: Number of batch executions that have been completedisEmpty: Whether the batcher has no items to process (items array is empty)isPending: Whether the batcher is waiting for the timeout to trigger batch processingitems: Array of items currently queued for batch processingsize: Number of items currently in the batch queuestatus: Current processing status - 'idle' when not processing, 'pending' when waiting for timeouttotalItemsProcessed: Total number of items that have been processed across all batches
Options and ownership
Pass an options object with property getters or a factory. Top-level properties are read reactively; function-valued core options remain callbacks. Local options override provider defaults. Updates retain the utility, its store, counters, and pending work. Destroying the owning scope calls cancel(). onUnmount replaces default cleanup and receives the same adapter instance. A custom callback must perform all required cleanup. Use flush() where supported to finish pending work.
Type Parameters
TValue
TValue
TSelected
TSelected = {
}
Parameters
scope
Owner of option updates, subscriptions, and cleanup.
fn
(items) => void
Function executed by the utility.
options?
AlpinePacerOptions<AlpineBatcherOptions<TValue, TSelected>> = {}
Core options or a reactive factory, plus an optional onUnmount callback.
selector?
(state) => TSelected
Selects state that updates the owner. Omit to leave selected state empty.
Returns
AlpineBatcher<TValue, TSelected>
The retained utility instance with selected state and child subscriptions.
Example
import { createBatcher } from '@tanstack/alpine-pacer'
const utility = createBatcher(
scope, (items: Array<string>) => { console.log(items) },
{ maxSize: 5, wait: 1000 },
(state) => ({ size: state.size }),
)
utility.addItem('item')
// Selected state: utility.state.size