function createAsyncBatcher<TValue, TSelected>(
fn,
options,
selector): SolidAsyncBatcher<TValue, TSelected>;
function createAsyncBatcher<TValue, TSelected>(
fn,
options,
selector): SolidAsyncBatcher<TValue, TSelected>;
Defined in: solid-pacer/src/async-batcher/createAsyncBatcher.ts:158
Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.
This is the async version of the createBatcher hook. Unlike the sync version, this async batcher:
Features:
The batcher collects items and processes them in batches based on:
Error Handling:
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 asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{
maxSize: 10,
wait: 2000,
onSuccess: (result) => {
console.log('Batch processed successfully:', result);
},
onError: (error) => {
console.error('Batch processing failed:', error);
}
}
);
// Opt-in to track items or isExecuting changes (optimized for UI updates)
const asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{ maxSize: 10, wait: 2000 },
(state) => ({ items: state.items, isExecuting: state.isExecuting })
);
// Opt-in to track error state changes (optimized for error handling)
const asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{ maxSize: 10, wait: 2000 },
(state) => ({ hasError: state.hasError, lastError: state.lastError })
);
// Add items to batch
asyncBatcher.addItem(newItem);
// Manually execute batch
const result = await asyncBatcher.execute();
// Access the selected state (will be empty object {} unless selector provided)
const { items, isExecuting } = asyncBatcher.state();
// Default behavior - no reactive state subscriptions
const asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{
maxSize: 10,
wait: 2000,
onSuccess: (result) => {
console.log('Batch processed successfully:', result);
},
onError: (error) => {
console.error('Batch processing failed:', error);
}
}
);
// Opt-in to track items or isExecuting changes (optimized for UI updates)
const asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{ maxSize: 10, wait: 2000 },
(state) => ({ items: state.items, isExecuting: state.isExecuting })
);
// Opt-in to track error state changes (optimized for error handling)
const asyncBatcher = createAsyncBatcher(
async (items) => {
const results = await Promise.all(items.map(item => processItem(item)));
return results;
},
{ maxSize: 10, wait: 2000 },
(state) => ({ hasError: state.hasError, lastError: state.lastError })
);
// Add items to batch
asyncBatcher.addItem(newItem);
// Manually execute batch
const result = await asyncBatcher.execute();
// Access the selected state (will be empty object {} unless selector provided)
const { items, isExecuting } = asyncBatcher.state();
TValue
TSelected = { }
(items) => Promise<any>
AsyncBatcherOptions<TValue> = {}
(state) => TSelected
SolidAsyncBatcher<TValue, TSelected>