diff --git a/src/GameCult.Mesh/CultMeshPrimitives.cs b/src/GameCult.Mesh/CultMeshPrimitives.cs
index 789a0b24..b38abd05 100644
--- a/src/GameCult.Mesh/CultMeshPrimitives.cs
+++ b/src/GameCult.Mesh/CultMeshPrimitives.cs
@@ -1140,6 +1140,9 @@ namespace GameCult.Mesh
///
public TimeSpan FlushDelay { get; set; } = TimeSpan.FromMilliseconds(16);
+ /// Gets or sets whether direct edits to Current should be detected and flushed automatically.
+ public bool DetectLocalChanges { get; set; } = true;
+
/// Gets or sets whether canonical snapshots replace dirty local predictions immediately.
public bool ReplaceDirtyCurrentOnCanonicalSnapshot { get; set; }
}
@@ -1180,7 +1183,9 @@ namespace GameCult.Mesh
private readonly CultMeshReactiveDocumentOptions _options;
private readonly object _gate = new();
private IDisposable? _subscription;
- private Timer? _timer;
+ private Timer? _flushTimer;
+ private Timer? _changeDetectionTimer;
+ private byte[] _lastCleanSnapshot;
private bool _dirty;
private bool _flushQueued;
private bool _flushing;
@@ -1194,6 +1199,7 @@ namespace GameCult.Mesh
_document = document ?? throw new ArgumentNullException(nameof(document));
Current = current ?? throw new ArgumentNullException(nameof(current));
_options = options ?? new CultMeshReactiveDocumentOptions();
+ _lastCleanSnapshot = SerializeDocument(Current);
}
/// Gets the underlying document handle.
@@ -1218,6 +1224,14 @@ namespace GameCult.Mesh
internal void Start()
{
_subscription = _document.Watch(ApplyCanonicalSnapshot);
+ if (_options.DetectLocalChanges && _document.CanSet)
+ {
+ var interval = _options.FlushDelay <= TimeSpan.Zero
+ ? TimeSpan.FromMilliseconds(1)
+ : _options.FlushDelay;
+ _changeDetectionTimer = new Timer(_ => _ = Task.Run(DetectLocalChangesAsync));
+ _changeDetectionTimer.Change(interval, interval);
+ }
}
/// Marks the current value dirty and schedules a coalesced prediction or replacement.
@@ -1268,6 +1282,7 @@ namespace GameCult.Mesh
lock (_gate)
{
Current = latest;
+ _lastCleanSnapshot = SerializeDocument(Current);
_dirty = false;
Reconciliation = null;
return Current;
@@ -1281,6 +1296,8 @@ namespace GameCult.Mesh
lock (_gate)
{
ThrowIfDisposed();
+ if (!_dirty && _document.CanSet)
+ DetectLocalChangesLocked();
if (!_dirty)
return;
if (_flushing)
@@ -1289,10 +1306,11 @@ namespace GameCult.Mesh
return;
}
- _timer?.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
+ _flushTimer?.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
_flushing = true;
_dirty = false;
predicted = CultMeshDocumentHandle.CloneDocument(Current);
+ _lastCleanSnapshot = SerializeDocument(predicted);
}
try
@@ -1332,7 +1350,8 @@ namespace GameCult.Mesh
}
_subscription?.Dispose();
- _timer?.Dispose();
+ _flushTimer?.Dispose();
+ _changeDetectionTimer?.Dispose();
}
private void ApplyCanonicalSnapshot(TDocument canonical)
@@ -1357,10 +1376,33 @@ namespace GameCult.Mesh
}
Current = nextCanonical;
+ _lastCleanSnapshot = SerializeDocument(Current);
Reconciliation = null;
}
}
+ private Task DetectLocalChangesAsync()
+ {
+ lock (_gate)
+ {
+ if (_disposed || !_document.CanSet || _dirty || _flushing)
+ return Task.CompletedTask;
+ DetectLocalChangesLocked();
+ if (_dirty)
+ ScheduleFlushLocked();
+ }
+
+ return Task.CompletedTask;
+ }
+
+ private void DetectLocalChangesLocked()
+ {
+ var currentSnapshot = SerializeDocument(Current);
+ if (_lastCleanSnapshot.SequenceEqual(currentSnapshot))
+ return;
+ _dirty = true;
+ }
+
private void ScheduleFlushLocked()
{
if (_options.FlushDelay <= TimeSpan.Zero)
@@ -1369,8 +1411,13 @@ namespace GameCult.Mesh
return;
}
- _timer ??= new Timer(_ => _ = Task.Run(FlushAsync));
- _timer.Change(_options.FlushDelay, Timeout.InfiniteTimeSpan);
+ _flushTimer ??= new Timer(_ => _ = Task.Run(FlushAsync));
+ _flushTimer.Change(_options.FlushDelay, Timeout.InfiniteTimeSpan);
+ }
+
+ private static byte[] SerializeDocument(TDocument document)
+ {
+ return CultDocumentMessagePackSerialization.SerializeUntyped(document, typeof(TDocument));
}
private void ThrowIfDisposed()
diff --git a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs
index 647b6fda..7585ce8f 100644
--- a/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs
+++ b/tests/GameCult.Mesh.Tests/CultMeshStreamingTests.cs
@@ -1656,6 +1656,99 @@ public sealed class CultMeshStreamingTests
predictions[0].Revision.Should().Be(12);
}
+ [Test]
+ public async Task ReactiveDocument_DetectsDirectMemberEditsWithoutMarkDirty()
+ {
+ var subject = new Subject();
+ var current = new MeshNoteDocument
+ {
+ Schema = "tests.mesh_note.v1",
+ Text = "initial",
+ Revision = 1
+ };
+ var predictions = new List();
+ var handle = CultMesh.Document(
+ "mesh.note.reactive.direct-member-auto",
+ CultMesh.Verse("starbridge", "pilot-a"),
+ _ => Task.FromResult(current),
+ _ => subject,
+ value =>
+ {
+ current = value;
+ subject.OnNext(value);
+ return Task.CompletedTask;
+ },
+ value =>
+ {
+ predictions.Add(value);
+ current = value;
+ subject.OnNext(value);
+ return Task.CompletedTask;
+ });
+
+ using var reactive = await handle.ReactiveAsync(
+ new CultMeshReactiveDocumentOptions
+ {
+ FlushDelay = TimeSpan.FromMilliseconds(10)
+ });
+
+ reactive.Current.Text = "direct-member-edit";
+ reactive.Current.Revision = 2;
+ reactive.Current.Text = "direct-member-edit-final";
+ reactive.Current.Revision = 3;
+
+ await WaitForAsync(() => predictions.Count == 1);
+
+ predictions[0].Text.Should().Be("direct-member-edit-final");
+ predictions[0].Revision.Should().Be(3);
+ reactive.IsDirty.Should().BeFalse();
+ }
+
+ [Test]
+ public async Task ReactiveDocument_FlushAsyncCapturesDirectMemberEdit()
+ {
+ var subject = new Subject();
+ var current = new MeshNoteDocument
+ {
+ Schema = "tests.mesh_note.v1",
+ Text = "initial",
+ Revision = 1
+ };
+ var predictions = new List();
+ var handle = CultMesh.Document(
+ "mesh.note.reactive.direct-member-flush",
+ CultMesh.Verse("starbridge", "pilot-a"),
+ _ => Task.FromResult(current),
+ _ => subject,
+ value =>
+ {
+ current = value;
+ subject.OnNext(value);
+ return Task.CompletedTask;
+ },
+ value =>
+ {
+ predictions.Add(value);
+ current = value;
+ subject.OnNext(value);
+ return Task.CompletedTask;
+ });
+
+ using var reactive = await handle.ReactiveAsync(
+ new CultMeshReactiveDocumentOptions
+ {
+ FlushDelay = TimeSpan.FromMinutes(1)
+ });
+
+ reactive.Current.Text = "direct-member-flush";
+ reactive.Current.Revision = 4;
+ await reactive.FlushAsync();
+
+ predictions.Should().ContainSingle();
+ predictions[0].Text.Should().Be("direct-member-flush");
+ predictions[0].Revision.Should().Be(4);
+ }
+
[Test]
public async Task ReactiveDocument_CleanFlushDoesNotWrite()
{