Skip to content
Merged
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
32 changes: 32 additions & 0 deletions src/SIL.Harmony.Tests/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SIL.Harmony.Sample.Changes;
using SIL.Harmony.Sample.Models;
using SIL.Harmony.Tests.Adapter;

namespace SIL.Harmony.Tests;

public class ConfigTests
{
[Fact]
public void CanGetEntityTypes()
{
var config = new CrdtConfig();
config.ObjectTypeListBuilder.DefaultAdapter()
.Add<Word>()
.Add<Definition>();
config.ObjectTypeListBuilder
.CustomAdapter<CustomObjectAdapterTests.IMyCustomInterface, CustomObjectAdapterTests.MyClassAdapter>()
.Add<CustomObjectAdapterTests.MyClass>();
var types = config.ObjectTypes.ToArray();
types.Should().BeEquivalentTo([typeof(Word), typeof(Definition), typeof(CustomObjectAdapterTests.MyClass)]);
}

[Fact]
public void CanGetChangeTypes()
{
var config = new CrdtConfig();
config.ChangeTypeListBuilder.Add<NewDefinitionChange>();
config.ChangeTypeListBuilder.Add<SetWordTextChange>();
var types = config.ChangeTypes.ToArray();
types.Should().BeEquivalentTo([typeof(NewDefinitionChange), typeof(SetWordTextChange)]);
}
}
4 changes: 4 additions & 0 deletions src/SIL.Harmony/Changes/ChangeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ internal ChangeContext(Commit commit, SnapshotWorker worker, CrdtConfig crdtConf

public Commit Commit { get; }
public async ValueTask<ObjectSnapshot?> GetSnapshot(Guid entityId) => await _worker.GetSnapshot(entityId);
public IAsyncEnumerable<object> GetObjectsReferencing(Guid entityId, bool includeDeleted = false)
{
return _worker.GetSnapshotsReferencing(entityId, includeDeleted).Select(s => s.Entity.DbObject);
}
public async ValueTask<T?> GetCurrent<T>(Guid entityId) where T : class
{
var snapshot = await GetSnapshot(entityId);
Expand Down
2 changes: 2 additions & 0 deletions src/SIL.Harmony/CrdtConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class CrdtConfig
/// </summary>
public bool AlwaysValidateCommits { get; set; } = true;
public ChangeTypeListBuilder ChangeTypeListBuilder { get; } = new();
public IEnumerable<Type> ChangeTypes => ChangeTypeListBuilder.Types.Select(t => t.DerivedType);
public ObjectTypeListBuilder ObjectTypeListBuilder { get; } = new();
public IEnumerable<Type> ObjectTypes => ObjectTypeListBuilder.AdapterProviders.SelectMany(p => p.GetRegistrations().Select(r => r.ObjectDbType));
public JsonSerializerOptions JsonSerializerOptions => _lazyJsonSerializerOptions.Value;
private readonly Lazy<JsonSerializerOptions> _lazyJsonSerializerOptions;

Expand Down
9 changes: 8 additions & 1 deletion src/SIL.Harmony/SnapshotWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private async ValueTask ApplyCommitChanges(IEnumerable<Commit> commits, bool upd
{
intermediateSnapshots[prevSnapshot.Entity.Id] = prevSnapshot;
}

await _crdtConfig.BeforeSaveObject.Invoke(entity.DbObject, newSnapshot);

AddSnapshot(newSnapshot);
Expand Down Expand Up @@ -202,6 +202,13 @@ private async ValueTask MarkDeleted(Guid deletedEntityId, Commit commit)
return snapshot;
}

internal IAsyncEnumerable<ObjectSnapshot> GetSnapshotsReferencing(Guid entityId, bool includeDeleted = false)
{
return _crdtRepository.CurrentSnapshots()
.Where(s => (includeDeleted || !s.EntityIsDeleted) && s.References.Contains(entityId))
.AsAsyncEnumerable();
}

private void AddSnapshot(ObjectSnapshot snapshot)
{
if (snapshot.IsRoot)
Expand Down