getSnapshot method
Loads a snapshot either by its snapshotId or by sessionId.
Exactly one of snapshotId / sessionId must be provided. A sessionId
resolves to the session's latest leaf snapshot (the most recent snapshot
that no other snapshot points to as its parent). A branched history (more
than one leaf) resolves to the most-recently created leaf by default, or
is rejected with StatusCodes.FAILED_PRECONDITION when the store is
configured to reject branching.
context carries the ambient request/action context (e.g. the
authenticated user) so multi-tenant stores can route reads.
Implementation
@override
Future<SessionSnapshot?> getSnapshot({
String? snapshotId,
String? sessionId,
Map<String, dynamic>? context,
}) async {
final normalized = normalizeGetSnapshotOptions(
snapshotId: snapshotId,
sessionId: sessionId,
);
if (normalized.snapshotId != null) {
final snap = _snapshots[normalized.snapshotId];
if (snap == null) return null;
return _cloneSnapshot(snap);
}
// sessionId lookup: gather every snapshot belonging to this session and
// resolve the single leaf (latest) snapshot.
final owned = <SessionSnapshot>[];
for (final snap in _snapshots.values) {
if (snapshotSessionId(snap) == normalized.sessionId) {
owned.add(snap);
}
}
final leaf = selectLeafSnapshot(
owned,
normalized.sessionId!,
rejectBranching: rejectBranchingSessions,
);
return leaf != null ? _cloneSnapshot(leaf) : null;
}