function createBatcher<TValue, TSelected>(
fn,
options,
selector): SolidBatcher<TValue, TSelected>;
function createBatcher<TValue, TSelected>(
fn,
options,
selector): SolidBatcher<TValue, TSelected>;
Defined in: solid-pacer/src/batcher/createBatcher.ts:130
Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.
Features:
The batcher collects items and processes them in batches based on:
The hook uses TanStack Store for reactive state management. You can subscribe to state changes in two ways:
1. Using batcher.Subscribe component (Recommended for component tree subscriptions)
Use the Subscribe component to subscribe to state changes deep in your component tree without needing to pass a selector to the hook. This is ideal when you want to subscribe to state in child components.
2. Using the selector parameter (For hook-level subscriptions)
The selector parameter allows you to specify which state changes will trigger reactive updates at the hook level, optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
By default, there will be no reactive state subscriptions and you must opt-in to state tracking by providing a selector function or using the Subscribe component. This prevents unnecessary updates and gives you full control over when your component tracks state changes.
Available state properties:
Example usage:
// Default behavior - no reactive state subscriptions
const batcher = createBatcher(
(items) => {
// Process batch of items
console.log('Processing batch:', items);
},
{
maxSize: 5,
wait: 2000,
onExecute: (batcher) => console.log('Batch executed'),
getShouldExecute: (items) => items.length >= 3
}
);
// Opt-in to track items or isRunning changes (optimized for UI updates)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({ items: state.items, isRunning: state.isRunning })
);
// Opt-in to track execution metrics changes (optimized for tracking progress)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({
executionCount: state.executionCount,
totalItemsProcessed: state.totalItemsProcessed
})
);
// Add items to batch
batcher.addItem('task1');
batcher.addItem('task2');
// Control the batcher
batcher.stop(); // Pause processing
batcher.start(); // Resume processing
// Access the selected state (will be empty object {} unless selector provided)
const { items, isRunning } = batcher.state();
// Default behavior - no reactive state subscriptions
const batcher = createBatcher(
(items) => {
// Process batch of items
console.log('Processing batch:', items);
},
{
maxSize: 5,
wait: 2000,
onExecute: (batcher) => console.log('Batch executed'),
getShouldExecute: (items) => items.length >= 3
}
);
// Opt-in to track items or isRunning changes (optimized for UI updates)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({ items: state.items, isRunning: state.isRunning })
);
// Opt-in to track execution metrics changes (optimized for tracking progress)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({
executionCount: state.executionCount,
totalItemsProcessed: state.totalItemsProcessed
})
);
// Add items to batch
batcher.addItem('task1');
batcher.addItem('task2');
// Control the batcher
batcher.stop(); // Pause processing
batcher.start(); // Resume processing
// Access the selected state (will be empty object {} unless selector provided)
const { items, isRunning } = batcher.state();
TValue
TSelected = { }
(items) => void
BatcherOptions<TValue> = {}
(state) => TSelected
SolidBatcher<TValue, TSelected>