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>(fn, initialOptions): SolidBatcher<TValue>
function createBatcher<TValue>(fn, initialOptions): SolidBatcher<TValue>

Defined in: batcher/createBatcher.ts:90

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.batchExecutionCount());
console.log('Item count:', batcher.itemExecutionCount());
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.batchExecutionCount());
console.log('Item count:', batcher.itemExecutionCount());

Type Parameters

TValue

Parameters

fn

(items) => void

initialOptions

BatcherOptions<TValue> = {}

Returns

SolidBatcher<TValue>

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.