Add snapshot endpoint document bindings

This commit is contained in:
Meta Vens 2026-06-27 18:30:17 +01:00
commit 3720a8d39d
2 changed files with 74 additions and 1 deletions

View file

@ -83,6 +83,55 @@ namespace GameCult.Mesh
public TimeSpan PollInterval { get; set; } = TimeSpan.FromMilliseconds(250);
}
/// <summary>
/// Describes one typed document handle to bind from a snapshot endpoint.
/// </summary>
public interface ICultMeshSnapshotDocumentBinding
{
/// <summary>Gets the CLR document type requested by this binding.</summary>
Type DocumentType { get; }
/// <summary>Gets the remote record key to read.</summary>
string RecordKey { get; }
/// <summary>Gets the optional semantic document id for the resulting handle.</summary>
string? DocumentId { get; }
/// <summary>Binds this document request to an endpoint-backed handle.</summary>
ICultMeshDocumentHandle Bind(CultMeshSnapshotEndpoint endpoint);
}
/// <summary>
/// Describes one typed document handle to bind from a snapshot endpoint.
/// </summary>
public sealed class CultMeshSnapshotDocumentBinding<TDocument> : ICultMeshSnapshotDocumentBinding
where TDocument : class
{
internal CultMeshSnapshotDocumentBinding(string recordKey, string? documentId)
{
RecordKey = string.IsNullOrWhiteSpace(recordKey)
? throw new ArgumentException("Value must be non-empty.", nameof(recordKey))
: recordKey;
DocumentId = documentId;
}
/// <inheritdoc />
public Type DocumentType => typeof(TDocument);
/// <inheritdoc />
public string RecordKey { get; }
/// <inheritdoc />
public string? DocumentId { get; }
/// <inheritdoc />
public ICultMeshDocumentHandle Bind(CultMeshSnapshotEndpoint endpoint)
{
if (endpoint == null) throw new ArgumentNullException(nameof(endpoint));
return endpoint.Document<TDocument>(RecordKey, DocumentId);
}
}
/// <summary>
/// Result from syncing a remote snapshot into a local CultMesh node.
/// </summary>
@ -266,6 +315,17 @@ namespace GameCult.Mesh
return CultMesh.Documents(documents);
}
/// <summary>Creates a schema-aware catalog from endpoint-backed typed document bindings.</summary>
public CultMeshDocumentCatalog Documents(params ICultMeshSnapshotDocumentBinding[] documents)
{
if (documents == null) throw new ArgumentNullException(nameof(documents));
return CultMesh.Documents(documents.Select(document =>
{
if (document == null) throw new ArgumentNullException(nameof(documents));
return document.Bind(this);
}));
}
private CultMeshSnapshotRequestOptions CreateRequest(
IReadOnlyList<string>? schemaIds,
IReadOnlyList<string>? recordKeys)
@ -318,6 +378,17 @@ namespace GameCult.Mesh
return new CultMeshSnapshotEndpoint(endpoint, options);
}
/// <summary>
/// Describes one typed document to bind from a CultMesh snapshot endpoint.
/// </summary>
public static CultMeshSnapshotDocumentBinding<TDocument> SnapshotDocument<TDocument>(
string recordKey,
string? documentId = null)
where TDocument : class
{
return new CultMeshSnapshotDocumentBinding<TDocument>(recordKey, documentId);
}
/// <summary>
/// Fetches one scoped raw CultNet snapshot from an endpoint.
/// </summary>

View file

@ -1221,13 +1221,15 @@ public sealed class CultMeshStreamingTests
var syncedAlias = await surface.SyncDocumentAsync<MeshNoteAliasDocument>(node, key.Value);
var aliasHandle = surface.Document<MeshNoteAliasDocument>(key.Value);
var aliasLatest = await aliasHandle.LatestAsync();
var catalog = surface.Documents(surface.Document<MeshNoteDocument>(key.Value));
var catalog = surface.Documents(
CultMesh.SnapshotDocument<MeshNoteDocument>(key.Value, "daemon:mesh-note:snapshot-endpoint"));
var catalogAlias = await catalog.LatestAsync<MeshNoteAliasDocument>();
fetchedAlias.Text.Should().Be("snapshot-endpoint");
syncedAlias.Text.Should().Be("snapshot-endpoint");
node.Cache.Get<MeshNoteDocument>(key)!.Revision.Should().Be(19);
aliasLatest.Revision.Should().Be(19);
catalog.Document<MeshNoteDocument>().DocumentId.Should().Be("daemon:mesh-note:snapshot-endpoint");
catalogAlias.Text.Should().Be("snapshot-endpoint");
requests.Should().HaveCount(4);
requests.Should().OnlyContain(request =>