saveSnapshot method

  1. @override
Future<String?> saveSnapshot(
  1. String? snapshotId,
  2. SnapshotMutator mutator, {
  3. Map<String, dynamic>? context,
})
override

Atomically reads the current snapshot (if snapshotId is provided), passes it to mutator, and persists the result.

context carries the ambient request/action context (e.g. the authenticated user) so multi-tenant stores can route writes.

Returns the snapshotId that was used, or null when the mutator returned null.

Implementation

@override
Future<String?> saveSnapshot(
  String? snapshotId,
  SnapshotMutator mutator, {
  Map<String, dynamic>? context,
}) async {
  final current = (snapshotId != null && snapshotId.isNotEmpty)
      ? _snapshots[snapshotId]
      : null;

  final result = mutator(current != null ? _cloneSnapshot(current) : null);
  if (result == null) return null;

  final id = resolveSnapshotId(snapshotId, result);
  result.snapshotId = id;
  _snapshots[id] = _cloneSnapshot(result);

  final snapshotListeners = _listeners[id];
  if (snapshotListeners != null) {
    for (final listener in [...snapshotListeners]) {
      listener(_cloneSnapshot(result));
    }
  }
  return id;
}