diff --git a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs index f0f3aa5..0ae96b4 100644 --- a/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs +++ b/surfaces/unity/Packages/com.gamecult.brokkr/Editor/BrokkrCultMeshMirror.cs @@ -17,6 +17,8 @@ namespace GameCult.Brokkr.Editor { private readonly Queue commandQueue = new(); private readonly Queue syncReceiptQueue = new(); + private readonly HashSet queuedCommandIds = new(StringComparer.Ordinal); + private readonly HashSet completedCommandIds = new(StringComparer.Ordinal); private CultMeshNode node; private IDisposable commandSubscription; private IDisposable syncReceiptSubscription; @@ -48,14 +50,17 @@ namespace GameCult.Brokkr.Editor } }); + commandQueue.Clear(); + queuedCommandIds.Clear(); + completedCommandIds.Clear(); + RefreshCommandState(); + commandSubscription = node.Database .Watch() .Subscribe(change => { if (change.Document != null) - { - commandQueue.Enqueue(change.Document); - } + TryQueueCommand(change.Document); }); syncReceiptSubscription = node.Database .Watch() @@ -75,10 +80,11 @@ namespace GameCult.Brokkr.Editor await node.FlushAsync(soft: true); } - internal Task PullExternalUpdatesAsync() + internal async Task PullExternalUpdatesAsync() { RequireRunning(); - return node.Cache.PullAllBackingStoresAsync(); + await node.Cache.PullAllBackingStoresAsync(); + RefreshCommandState(); } internal async Task PublishPrefabMirrorSnapshotAsync(BrokkrUnityPrefabMirrorSnapshot snapshot) @@ -99,6 +105,11 @@ namespace GameCult.Brokkr.Editor : $"unity/receipts/{receipt.commandId}"; await node.Database.PutAsync(new CultRecordKey(key), receipt); await node.FlushAsync(soft: true); + if (!string.IsNullOrWhiteSpace(receipt.commandId)) + { + completedCommandIds.Add(receipt.commandId); + queuedCommandIds.Remove(receipt.commandId); + } } internal async Task PublishCommandAsync(BrokkrUnityCommand command) @@ -149,9 +160,16 @@ namespace GameCult.Brokkr.Editor internal bool TryDequeueCommand(out BrokkrUnityCommand command) { - if (commandQueue.Count > 0) + while (commandQueue.Count > 0) { - command = commandQueue.Dequeue(); + var candidate = commandQueue.Dequeue(); + queuedCommandIds.Remove(candidate.commandId); + if (HasReceipt(candidate.commandId)) + { + continue; + } + + command = candidate; return true; } @@ -159,6 +177,44 @@ namespace GameCult.Brokkr.Editor return false; } + private void RefreshCommandState() + { + var documents = node.Database.Cache.AllStoredDocuments.ToArray(); + foreach (var receipt in documents + .Select(stored => stored.Document) + .OfType()) + { + if (!string.IsNullOrWhiteSpace(receipt.commandId)) + completedCommandIds.Add(receipt.commandId); + } + + foreach (var command in documents + .Where(stored => stored.Document is BrokkrUnityCommand) + .OrderBy(stored => stored.StoredAt, StringComparer.Ordinal) + .ThenBy(stored => stored.Key.Value, StringComparer.Ordinal) + .Select(stored => (BrokkrUnityCommand)stored.Document)) + TryQueueCommand(command); + } + + private void TryQueueCommand(BrokkrUnityCommand command) + { + if (string.IsNullOrWhiteSpace(command.commandId) || + HasReceipt(command.commandId) || + !queuedCommandIds.Add(command.commandId)) + return; + commandQueue.Enqueue(command); + } + + private bool HasReceipt(string commandId) + { + if (node == null || string.IsNullOrWhiteSpace(commandId)) + { + return false; + } + + return completedCommandIds.Contains(commandId); + } + internal bool TryDequeueSyncReceipt(out BrokkrSyncReceipt receipt) { if (syncReceiptQueue.Count > 0)