diff --git a/src/GameCult.Mesh/CultMeshSnapshots.cs b/src/GameCult.Mesh/CultMeshSnapshots.cs index 2515b4e9..0b17e6bb 100644 --- a/src/GameCult.Mesh/CultMeshSnapshots.cs +++ b/src/GameCult.Mesh/CultMeshSnapshots.cs @@ -168,6 +168,46 @@ namespace GameCult.Mesh public long ShardLogSequence => Snapshot.ShardLogSequence ?? 0L; } + /// + /// Snapshot endpoint view with a local node sync policy already bound. + /// + public sealed class CultMeshSyncedSnapshotEndpoint + { + internal CultMeshSyncedSnapshotEndpoint( + CultMeshSnapshotEndpoint endpoint, + CultMeshNode node, + bool flush) + { + Endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); + Node = node ?? throw new ArgumentNullException(nameof(node)); + Flush = flush; + } + + /// Gets the remote endpoint that provides snapshots. + public CultMeshSnapshotEndpoint Endpoint { get; } + + /// Gets the local node snapshots are synced into. + public CultMeshNode Node { get; } + + /// Gets whether each sync should flush the local node afterward. + public bool Flush { get; } + + /// Creates a typed document handle that syncs into the local node before returning values. + public CultMeshDocumentHandle Document( + string recordKey, + string? documentId = null) + where TDocument : class + { + return Endpoint.SyncedDocument(Node, recordKey, documentId, Flush); + } + + /// Creates a schema-aware catalog from typed endpoint bindings that sync into the local node before returning values. + public CultMeshDocumentCatalog Documents(params ICultMeshSnapshotDocumentBinding[] documents) + { + return Endpoint.SyncedDocuments(Node, Flush, documents); + } + } + /// /// Typed snapshot surface for one remote CultNet endpoint. /// @@ -209,6 +249,12 @@ namespace GameCult.Mesh /// Gets the polling interval for watch fallback. public TimeSpan PollInterval { get; } + /// Returns a view of this endpoint whose typed document handles sync into the supplied local node. + public CultMeshSyncedSnapshotEndpoint SyncTo(CultMeshNode node, bool flush = false) + { + return new CultMeshSyncedSnapshotEndpoint(this, node, flush); + } + /// Fetches one raw snapshot with this endpoint's configured request policy. public Task FetchSnapshotAsync( IReadOnlyList? schemaIds = null, diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 65ce4752..418d09f7 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -1230,7 +1230,8 @@ public sealed class CultMeshStreamingTests Text = "snapshot-endpoint-synced", Revision = 20 }, new CultRecordHandle(key)); - var syncedHandle = surface.SyncedDocument(node, key.Value); + var syncedEndpoint = surface.SyncTo(node); + var syncedHandle = syncedEndpoint.Document(key.Value); var syncedLatest = await syncedHandle.LatestAsync(); await sourceCache.UpsertAsync(new MeshNoteDocument { @@ -1238,8 +1239,7 @@ public sealed class CultMeshStreamingTests Text = "snapshot-endpoint-catalog-synced", Revision = 21 }, new CultRecordHandle(key)); - var syncedCatalog = surface.SyncedDocuments( - node, + var syncedCatalog = syncedEndpoint.Documents( CultMesh.SnapshotDocument(key.Value)); var syncedCatalogAlias = await syncedCatalog.LatestAsync();