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 {
  // When an ID is supplied the read-modify-write below must be serialized
  // against concurrent saves of the same snapshot, otherwise a later write
  // (e.g. `completed`) can clobber an earlier concurrent one (e.g.
  // `aborted`). New (UUID) snapshots have no contender, so skip the lock.
  if (snapshotId != null && snapshotId.isNotEmpty) {
    final file = await _fileFor(snapshotId, context);
    return _withFileLock(
      file.path,
      () => _saveSnapshotUnlocked(snapshotId, mutator, context),
    );
  }
  return _saveSnapshotUnlocked(snapshotId, mutator, context);
}