batch method

void batch(
  1. String reason,
  2. void operations()
)

Wraps multiple operations in a batch.

Extensions will see BatchStarted before the operations and BatchEnded after. This allows extensions like undo/redo to group multiple operations into a single undoable action.

Batches can be nested. Only the outermost batch emits events.

Example:

controller.batch('delete-selection', () {
  for (final id in selectedNodeIds.toList()) {
    controller.removeNode(id);
  }
});

Implementation

void batch(String reason, void Function() operations) {
  if (_batchDepth == 0) {
    _emitEvent(BatchStarted(reason));
  }
  _batchDepth++;

  try {
    operations();
  } finally {
    _batchDepth--;
    if (_batchDepth == 0) {
      _emitEvent(BatchEnded());
    }
  }
}