Add transparent CultMesh document mutation
This commit is contained in:
parent
6f610a2908
commit
1758a9c52f
2 changed files with 79 additions and 1 deletions
|
|
@ -903,6 +903,9 @@ namespace GameCult.Mesh
|
|||
/// <summary>Gets whether this handle can submit a client prediction for the underlying document value.</summary>
|
||||
bool CanSubmitPrediction { get; }
|
||||
|
||||
/// <summary>Gets whether this handle can accept a transparent document mutation.</summary>
|
||||
bool CanSet { get; }
|
||||
|
||||
/// <summary>Creates a same-schema alias presentation for another CLR document type.</summary>
|
||||
CultMeshDocumentHandle<TAlias> AsSchemaAlias<TAlias>() where TAlias : class;
|
||||
}
|
||||
|
|
@ -964,6 +967,9 @@ namespace GameCult.Mesh
|
|||
/// <summary>Gets whether this handle can submit a client prediction for the underlying document value.</summary>
|
||||
public bool CanSubmitPrediction => _submitPrediction != null;
|
||||
|
||||
/// <summary>Gets whether this handle can accept a transparent document mutation.</summary>
|
||||
public bool CanSet => CanSubmitPrediction || CanReplace;
|
||||
|
||||
/// <summary>Reads one coherent document snapshot.</summary>
|
||||
public Task<TDocument> LatestAsync()
|
||||
{
|
||||
|
|
@ -1013,6 +1019,39 @@ namespace GameCult.Mesh
|
|||
return _submitPrediction(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the document value through the configured authority shape.
|
||||
/// Prediction-backed documents publish a prediction; mutable documents replace authoritatively.
|
||||
/// </summary>
|
||||
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.");
|
||||
}
|
||||
|
||||
/// <summary>Reads, updates, and sets the document value through the configured authority shape.</summary>
|
||||
public async Task<TDocument> UpdateAsync(Func<TDocument, TDocument> update)
|
||||
{
|
||||
if (update == null) throw new ArgumentNullException(nameof(update));
|
||||
var next = update(await LatestAsync().ConfigureAwait(false));
|
||||
await SetAsync(next).ConfigureAwait(false);
|
||||
return next;
|
||||
}
|
||||
|
||||
/// <summary>Reads, updates, and sets the document value through the configured authority shape.</summary>
|
||||
public async Task<TDocument> UpdateAsync(Func<TDocument, Task<TDocument>> 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;
|
||||
}
|
||||
|
||||
/// <summary>Creates a same-schema alias presentation for another CLR document type.</summary>
|
||||
public CultMeshDocumentHandle<TAlias> AsSchemaAlias<TAlias>() where TAlias : class
|
||||
{
|
||||
|
|
@ -1198,6 +1237,13 @@ namespace GameCult.Mesh
|
|||
return TryGetDocument<TDocument>(out var document) && document.CanSubmitPrediction;
|
||||
}
|
||||
|
||||
/// <summary>Gets whether one typed document can accept transparent mutations by CLR type or same-schema alias.</summary>
|
||||
public bool CanSet<TDocument>()
|
||||
where TDocument : class
|
||||
{
|
||||
return TryGetDocument<TDocument>(out var document) && document.CanSet;
|
||||
}
|
||||
|
||||
/// <summary>Submits a client prediction for one typed document by CLR type or same-schema alias.</summary>
|
||||
public Task SubmitPredictionAsync<TDocument>(TDocument value)
|
||||
where TDocument : class
|
||||
|
|
@ -1205,6 +1251,27 @@ namespace GameCult.Mesh
|
|||
return Document<TDocument>().SubmitPredictionAsync(value);
|
||||
}
|
||||
|
||||
/// <summary>Sets one typed document through its configured authority shape by CLR type or same-schema alias.</summary>
|
||||
public Task SetAsync<TDocument>(TDocument value)
|
||||
where TDocument : class
|
||||
{
|
||||
return Document<TDocument>().SetAsync(value);
|
||||
}
|
||||
|
||||
/// <summary>Reads, updates, and sets one typed document through its configured authority shape.</summary>
|
||||
public Task<TDocument> UpdateAsync<TDocument>(Func<TDocument, TDocument> update)
|
||||
where TDocument : class
|
||||
{
|
||||
return Document<TDocument>().UpdateAsync(update);
|
||||
}
|
||||
|
||||
/// <summary>Reads, updates, and sets one typed document through its configured authority shape.</summary>
|
||||
public Task<TDocument> UpdateAsync<TDocument>(Func<TDocument, Task<TDocument>> update)
|
||||
where TDocument : class
|
||||
{
|
||||
return Document<TDocument>().UpdateAsync(update);
|
||||
}
|
||||
|
||||
/// <summary>Watches one typed document by CLR type or same-schema alias.</summary>
|
||||
public Observable<TDocument> Watch<TDocument>()
|
||||
where TDocument : class
|
||||
|
|
|
|||
|
|
@ -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<MeshNoteAliasDocument>().Should().BeTrue();
|
||||
catalog.CanSet<MeshNoteAliasDocument>().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<MeshNoteDocument>(key)!.Revision.Should().Be(6);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue