From 8248ad6362c23331a1b642648b83e63350088507 Mon Sep 17 00:00:00 2001 From: halgari Date: Mon, 29 Jul 2024 17:41:23 -0600 Subject: [PATCH] Fix a bug with empty `RecentlyAdded` on the first DB created after bootstrapping. --- src/NexusMods.MnemonicDB/Db.cs | 2 +- tests/NexusMods.MnemonicDB.Tests/DbTests.cs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/NexusMods.MnemonicDB/Db.cs b/src/NexusMods.MnemonicDB/Db.cs index d492261..a54d94a 100644 --- a/src/NexusMods.MnemonicDB/Db.cs +++ b/src/NexusMods.MnemonicDB/Db.cs @@ -39,7 +39,7 @@ public Db(ISnapshot snapshot, TxId txId, AttributeRegistry registry) _cache = new IndexSegmentCache(); Snapshot = snapshot; BasisTxId = txId; - RecentlyAdded = new IndexSegment(); + RecentlyAdded = snapshot.Datoms(SliceDescriptor.Create(txId, registry)); } private Db(ISnapshot snapshot, TxId txId, AttributeRegistry registry, RegistryId registryId, IConnection connection, IndexSegmentCache newCache, IndexSegment recentlyAdded) diff --git a/tests/NexusMods.MnemonicDB.Tests/DbTests.cs b/tests/NexusMods.MnemonicDB.Tests/DbTests.cs index e13cade..0ba7a38 100644 --- a/tests/NexusMods.MnemonicDB.Tests/DbTests.cs +++ b/tests/NexusMods.MnemonicDB.Tests/DbTests.cs @@ -2,6 +2,7 @@ using DynamicData; using NexusMods.MnemonicDB.Abstractions; using NexusMods.Hashing.xxHash64; +using NexusMods.MnemonicDB.Abstractions.BuiltInEntities; using NexusMods.MnemonicDB.Abstractions.DatomIterators; using NexusMods.MnemonicDB.Abstractions.Models; using NexusMods.MnemonicDB.Abstractions.Query; @@ -601,7 +602,7 @@ public async Task CanObserveIndexChanges() var slice = SliceDescriptor.Create(Mod.Name, Connection.Db.Registry); // Setup the subscription - using var _ = ObservableDatoms.ObserveDatoms(Connection, slice) + using var _ = Connection.ObserveDatoms(slice) // Snapshot the values each time .QueryWhenChanged(datoms => datoms.Select(d => d.Resolved.ObjectValue.ToString()!).ToArray()) // Add the changes to the list @@ -625,6 +626,18 @@ public async Task CanObserveIndexChanges() await Verify(changes); } + [Fact] + public async Task CanGetInitialDbStateFromObservable() + { + var attrs = await Connection.ObserveDatoms(AttributeDefinition.UniqueId).FirstAsync(); + attrs.TotalChanges.Should().BeGreaterThan(0); + + + attrs = await Connection.ObserveDatoms(AttributeDefinition.UniqueId).FirstAsync(); + attrs.TotalChanges.Should().BeGreaterThan(0); + + } + /// /// Test for a flickering bug in UI users, where datoms that are changed result in a `add` and a `remove` operation /// causing the UI to flicker, instead we want to issue changes on a ScalarAttribute as a refresh/change