Function: batch

index.batch

batch<T>(fn): T

Executes a function within a synchronous batch context, deferring subscriber notifications until completion.

Type parameters

Name Description
T The return type of the batch function.

Parameters

Name Type Description
fn () => T Synchronous callback function containing state mutations.

Returns

T

The return value of fn.

Remarks

Within a batch block, all mutations across signals, objects, and collections accumulate without triggering immediate downstream effects. Once the outermost batch function concludes, all unique affected subscribers are notified in a single consolidated pass. Nested calls to batch are automatically flattened.

Example

import { signal, effect, batch } from '@banksia/signals';

const a = signal(1);
const b = signal(2);

effect(() => console.log('Sum:', a.value + b.value)); // Logs: Sum: 3

batch(() => {
  a.value = 10;
  b.value = 20;
}); // Logs: Sum: 30 (only runs once after the batch finishes)