Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObserveHasAnyDatoms #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading