batch<T> method

  1. @override
T batch<T>(
  1. T body()
)
override

Runs body as one change: the writes it makes (nested batches included) fire onChange once, with every key they touched, and bump version once, when the outermost batch returns or throws. The client batches each response, mutation, subscription event and optimistic callback, list-rule edits included; batch several manual writes with it.

body must be synchronous: a batch ends when body returns, so the writes after an await would each be their own change (asserted).

Implementation

@override
T batch<T>(T Function() body) {
  _batchDepth++;
  try {
    final result = body();
    assert(
      result is! Future,
      'Cache.batch: the body must be synchronous — the batch ends when it '
      'returns, not after its awaits',
    );
    return result;
  } finally {
    if (--_batchDepth == 0) {
      final held = _held;
      _held = null;
      _commit(held ?? const {});
    }
  }
}