Skip to content

Commit

Permalink
Add ObserveHasAnyDatoms
Browse files Browse the repository at this point in the history
Resolves #91
  • Loading branch information
erri120 committed Nov 6, 2024
1 parent 87522de commit 95df996
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/NexusMods.MnemonicDB.Abstractions/Query/ObservableDatoms.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
using DynamicData;
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.DatomComparators;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.MnemonicDB.Abstractions.IndexSegments;
using NexusMods.MnemonicDB.Abstractions.Internals;

namespace NexusMods.MnemonicDB.Abstractions.Query;

/// <summary>
/// Extension methods to observe changes in the DB.
/// </summary>
[PublicAPI]
public static class ObservableDatoms
{

/// <summary>
/// Delays the creation of the IObservable until after the first value from the src is received. Once the first
/// value arrives the ctor function will be called to create the observable. This is useful for situations where
Expand Down Expand Up @@ -89,7 +88,15 @@ public static IObservable<IChangeSet<Datom, DatomKey>> ObserveDatoms(this IConne
{
return conn.Revisions.DelayUntilFirstValue(() => conn.ObserveDatoms(SliceDescriptor.Create(attribute, conn.AttributeCache)));
}


/// <summary>
/// Observe whether datoms for the descriptor exist at all.
/// </summary>
public static IObservable<bool> ObserveHasAnyDatoms(this IConnection connection, SliceDescriptor descriptor)
{
return connection.Revisions.Select(revision => revision.Datoms(descriptor).Count != 0);
}

/// <summary>
/// Converts a set of observed datoms to a set of observed entity ids, assumes that there will be no datoms with
/// duplicate entity ids.
Expand All @@ -112,8 +119,6 @@ public static IObservable<IChangeSet<Datom, EntityId>> AsEntityIds(this IObserva
private static IChangeSet<Datom, DatomKey> Diff(AttributeCache cache, IndexSegment updates, SliceDescriptor descriptor)
{
var changes = new ChangeSet<Datom, DatomKey>();

AttributeId previousAid = default;

var index = descriptor.Index;

Expand Down
34 changes: 34 additions & 0 deletions tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,4 +1172,38 @@ public async Task CanExciseEntities()
.NotBeEmpty();

}

[Fact]
public async Task Test_ObserveHasAnyDatoms()
{
var hasAnyDatoms = false;

using var disposable = Connection
.ObserveHasAnyDatoms(SliceDescriptor.Create(Loadout.Name, Connection.AttributeCache))
.Subscribe(value => hasAnyDatoms = value);

hasAnyDatoms.Should().BeFalse();

EntityId entityId;
using (var tx = Connection.BeginTransaction())
{
var loadout = new Loadout.New(tx)
{
Name = "Test Loadout"
};

var result = await tx.Commit();
entityId = result.Remap(loadout).Id;
}

hasAnyDatoms.Should().BeTrue();

using (var tx = Connection.BeginTransaction())
{
tx.Delete(entityId, recursive: false);
await tx.Commit();
}

hasAnyDatoms.Should().BeFalse();
}
}

0 comments on commit 95df996

Please sign in to comment.