From 66666d488299c61e6bdb171cdd0f4dedb2c3ab99 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 12 Jul 2026 21:27:01 +0100 Subject: [PATCH] Read immutable typed records through managed sessions --- src/GameCult.Mesh/CultMeshClient.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/GameCult.Mesh/CultMeshClient.cs b/src/GameCult.Mesh/CultMeshClient.cs index 804dc0d0..aeef39b3 100644 --- a/src/GameCult.Mesh/CultMeshClient.cs +++ b/src/GameCult.Mesh/CultMeshClient.cs @@ -140,6 +140,34 @@ namespace GameCult.Mesh } } + /// Reads one typed record once through the reusable document session. + public async Task ReadAsync( + string endpointId, + string recordKey, + CancellationToken cancellationToken = default) + where TDocument : class + { + ThrowIfDisposed(); + if (string.IsNullOrWhiteSpace(endpointId)) throw new ArgumentException("Endpoint identity is required.", nameof(endpointId)); + if (string.IsNullOrWhiteSpace(recordKey)) throw new ArgumentException("Record key is required.", nameof(recordKey)); + var cacheRegistry = CultMesh.CreateCultCacheDocumentRegistry(typeof(TDocument)); + var networkRegistry = CultMesh.CreateCultNetDocumentRegistry(new[] { typeof(TDocument) }, cacheRegistry); + using var snapshot = await CultMeshSnapshotSession.ConnectAsync( + _sessions, + CultMeshEndpointId.Parse(endpointId), + new CultMeshSnapshotRequestOptions(), + networkRegistry, + cancellationToken) + .ConfigureAwait(false); + var documents = await snapshot.FetchDocumentsAsync( + recordKeys: new[] { recordKey }, + schemaIds: new[] { cacheRegistry.GetRequired().SchemaId }) + .ConfigureAwait(false); + return documents.FirstOrDefault() + ?? throw new InvalidOperationException( + $"CultMesh endpoint '{endpointId}' did not publish {typeof(TDocument).FullName} record '{recordKey}'."); + } + public void Dispose() { if (_disposed) return;