diff --git a/src/GameCult.Mesh/CultMeshPrimitives.cs b/src/GameCult.Mesh/CultMeshPrimitives.cs index a35f1c24..267b330a 100644 --- a/src/GameCult.Mesh/CultMeshPrimitives.cs +++ b/src/GameCult.Mesh/CultMeshPrimitives.cs @@ -903,6 +903,9 @@ namespace GameCult.Mesh /// Gets whether this handle can submit a client prediction for the underlying document value. bool CanSubmitPrediction { get; } + /// Gets whether this handle can accept a transparent document mutation. + bool CanSet { get; } + /// Creates a same-schema alias presentation for another CLR document type. CultMeshDocumentHandle AsSchemaAlias() where TAlias : class; } @@ -964,6 +967,9 @@ namespace GameCult.Mesh /// Gets whether this handle can submit a client prediction for the underlying document value. public bool CanSubmitPrediction => _submitPrediction != null; + /// Gets whether this handle can accept a transparent document mutation. + public bool CanSet => CanSubmitPrediction || CanReplace; + /// Reads one coherent document snapshot. public Task LatestAsync() { @@ -1013,6 +1019,39 @@ namespace GameCult.Mesh return _submitPrediction(value); } + /// + /// Sets the document value through the configured authority shape. + /// Prediction-backed documents publish a prediction; mutable documents replace authoritatively. + /// + public Task SetAsync(TDocument value) + { + if (value == null) throw new ArgumentNullException(nameof(value)); + if (_submitPrediction != null) + return _submitPrediction(value); + if (_replace != null) + return _replace(value); + + throw new NotSupportedException($"Document handle '{DocumentId}' does not accept mutations."); + } + + /// Reads, updates, and sets the document value through the configured authority shape. + public async Task UpdateAsync(Func update) + { + if (update == null) throw new ArgumentNullException(nameof(update)); + var next = update(await LatestAsync().ConfigureAwait(false)); + await SetAsync(next).ConfigureAwait(false); + return next; + } + + /// Reads, updates, and sets the document value through the configured authority shape. + public async Task UpdateAsync(Func> update) + { + if (update == null) throw new ArgumentNullException(nameof(update)); + var next = await update(await LatestAsync().ConfigureAwait(false)).ConfigureAwait(false); + await SetAsync(next).ConfigureAwait(false); + return next; + } + /// Creates a same-schema alias presentation for another CLR document type. public CultMeshDocumentHandle AsSchemaAlias() where TAlias : class { @@ -1198,6 +1237,13 @@ namespace GameCult.Mesh return TryGetDocument(out var document) && document.CanSubmitPrediction; } + /// Gets whether one typed document can accept transparent mutations by CLR type or same-schema alias. + public bool CanSet() + where TDocument : class + { + return TryGetDocument(out var document) && document.CanSet; + } + /// Submits a client prediction for one typed document by CLR type or same-schema alias. public Task SubmitPredictionAsync(TDocument value) where TDocument : class @@ -1205,6 +1251,27 @@ namespace GameCult.Mesh return Document().SubmitPredictionAsync(value); } + /// Sets one typed document through its configured authority shape by CLR type or same-schema alias. + public Task SetAsync(TDocument value) + where TDocument : class + { + return Document().SetAsync(value); + } + + /// Reads, updates, and sets one typed document through its configured authority shape. + public Task UpdateAsync(Func update) + where TDocument : class + { + return Document().UpdateAsync(update); + } + + /// Reads, updates, and sets one typed document through its configured authority shape. + public Task UpdateAsync(Func> update) + where TDocument : class + { + return Document().UpdateAsync(update); + } + /// Watches one typed document by CLR type or same-schema alias. public Observable Watch() where TDocument : class diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs index 418d09f7..d228788a 100644 --- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs +++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs @@ -863,12 +863,14 @@ public sealed class CultMeshStreamingTests handle.CanReplace.Should().BeTrue(); alias.CanSubmitPrediction.Should().BeTrue(); + alias.CanSet.Should().BeTrue(); var catalog = CultMesh.Documents(handle); catalog.CanSubmitPrediction().Should().BeTrue(); + catalog.CanSet().Should().BeTrue(); MeshNoteDocument observed = null!; using var subscription = handle.Watch(value => observed = value); - await catalog.SubmitPredictionAsync(new MeshNoteAliasDocument + await catalog.SetAsync(new MeshNoteAliasDocument { Schema = "tests.mesh_note.v1", Text = "predicted-thermal", @@ -879,6 +881,15 @@ public sealed class CultMeshStreamingTests observed.Text.Should().Be("predicted-thermal"); observed.Revision.Should().Be(5); (await handle.LatestAsync()).Text.Should().Be("predicted-thermal"); + + var updated = await alias.UpdateAsync(value => new MeshNoteAliasDocument + { + Schema = value.Schema, + Text = "updated-as-prediction", + Revision = value.Revision + 1 + }); + updated.Text.Should().Be("updated-as-prediction"); + cache.Get(key)!.Revision.Should().Be(6); } [Test]