Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference

createBatcher

Function: createBatcher()

ts
function createBatcher<TValue, TSelected>(
   fn, 
   initialOptions, 
selector?): SolidBatcher<TValue, TSelected>
function createBatcher<TValue, TSelected>(
   fn, 
   initialOptions, 
selector?): SolidBatcher<TValue, TSelected>

Defined in: batcher/createBatcher.ts:63

Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.

Features:

  • Batch processing of items using the provided fn function
  • Configurable batch size and wait time
  • Custom batch processing logic via getShouldExecute
  • Event callbacks for monitoring batch operations
  • All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity

The batcher collects items and processes them in batches based on:

  • Maximum batch size
  • Time-based batching (process after X milliseconds)
  • Custom batch processing logic via getShouldExecute

Example usage:

tsx
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
  }
);

// Add items to batch
batcher.addItem('task1');
batcher.addItem('task2');

// Control the batcher
batcher.stop();  // Pause processing
batcher.start(); // Resume processing

// Access batcher state via signals
console.log('Items:', batcher.allItems());
console.log('Size:', batcher.size());
console.log('Is empty:', batcher.isEmpty());
console.log('Is running:', batcher.isRunning());
console.log('Batch count:', batcher.executionCount());
console.log('Item count:', batcher.totalItemsProcessed());
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
  }
);

// Add items to batch
batcher.addItem('task1');
batcher.addItem('task2');

// Control the batcher
batcher.stop();  // Pause processing
batcher.start(); // Resume processing

// Access batcher state via signals
console.log('Items:', batcher.allItems());
console.log('Size:', batcher.size());
console.log('Is empty:', batcher.isEmpty());
console.log('Is running:', batcher.isRunning());
console.log('Batch count:', batcher.executionCount());
console.log('Item count:', batcher.totalItemsProcessed());

Type Parameters

TValue

TSelected = BatcherState<TValue>

Parameters

fn

(items) => void

initialOptions

BatcherOptions<TValue> = {}

selector?

(state) => TSelected

Returns

SolidBatcher<TValue, TSelected>

Our Partners
Unkey
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.