Skip to content

Commit

Permalink
Merge pull request #88 from Nexus-Mods/refcount-observables
Browse files Browse the repository at this point in the history
Fix bug with RefCount and ObservableDatoms
  • Loading branch information
halgari authored Aug 8, 2024
2 parents d6207d6 + b8520dd commit d35b2c0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static IObservable<IChangeSet<Datom>> ObserveDatoms(this IConnection conn
{
lock (set)
{
if (rev.BasisTxId <= lastTxId)
if (rev.BasisTxId <= lastTxId && idx != 0)
return ChangeSet<Datom>.Empty;

lastTxId = rev.BasisTxId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
Test Mod,
Test Mod 2,
Test Mod 3
]
46 changes: 46 additions & 0 deletions tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,50 @@ public async Task CollectionAttributesAreSupportedOnModels()

modRO.Tags.Should().BeEquivalentTo(["A", "B", "C"]);
}

/// <summary>
/// Tests a bug experienced in the app, when RefCount is used with ObserveDatoms, if the last subscription
/// is disposed, and then another subscriber attaches, an exception would be thrown. This test ensures that
/// this behavior works correctly.
/// </summary>
[Fact]
public async Task RefCountWorksWithObservables()
{
var tx = Connection.BeginTransaction();
var mod = new Mod.New(tx)
{
Name = "Test Mod",
Source = new Uri("http://test.com"),
LoadoutId = EntityId.From(0)
};
var result = await tx.Commit();

var modRO = result.Remap(mod);

var refObs = Mod.Observe(Connection, modRO.Id)
.Replay(1)
.RefCount();

List<Mod.ReadOnly> mods = [];

var firstDisp = refObs.Subscribe(mods.Add);

{
var tx2 = Connection.BeginTransaction();
tx2.Add(modRO.Id, Mod.Name, "Test Mod 2");
await tx2.Commit();
}

firstDisp.Dispose();

var secondDisp = refObs.Subscribe(mods.Add);

{
var tx2 = Connection.BeginTransaction();
tx2.Add(modRO.Id, Mod.Name, "Test Mod 3");
await tx2.Commit();
}

await Verify(mods.Distinct().Select(m => m.Name));
}
}

0 comments on commit d35b2c0

Please sign in to comment.