batch<T> method

FutureOr<T> batch<T>(
  1. FutureOr<T> fn(), {
  2. String? label,
})
inherited

Runs fn inside a store-scoped batch.

Only notifications for this store are deferred while the batch is open. Other stores mutated inside notify immediately as normal (unless a global Orbit.batch is also active).

Implementation

FutureOr<T> batch<T>(
  FutureOr<T> Function() fn, {
  String? label,
}) {
  final effectiveLabel = label ?? '${runtimeType}.batch';
  _openScopedBatch(effectiveLabel);
  Orbit._openUndoBatch(effectiveLabel);

  try {
    final result = fn();
    if (result is Future) {
      return (result as Future).whenComplete(() {
        Orbit._closeUndoBatch();
        _closeScopedBatch();
      }) as FutureOr<T>;
    }
    Orbit._closeUndoBatch();
    _closeScopedBatch();
    return result;
  } catch (error) {
    Orbit._closeUndoBatch();
    _closeScopedBatch();
    rethrow;
  }
}