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 14, 2024
1 parent 45f278f commit becc742
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/NexusMods.MnemonicDB.Abstractions/Query/ObservableDatoms.cs
Original file line number Diff line number Diff line change
@@ -1,24 +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>
/// Extensions for observing datoms in the database
/// </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 @@ -92,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 @@ -115,9 +119,10 @@ 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>();

var index = descriptor.Index;

for (var i = 0; i < updates.Count; i++)
for (var i = 0; i < updates.Count; i++)
{
var datom = updates[i].WithIndex(index);
if (!descriptor.Includes(datom))
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 @@ -1173,4 +1173,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 becc742

Please sign in to comment.