Skip to content

Commit

Permalink
Better code structure for RealmExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
aetherstrata committed Feb 17, 2024
1 parent 89c0d50 commit 2dff19f
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions Aosta.Ava/Aosta.Ava/Extensions/RealmExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,33 @@ public static class RealmExtensions
/// <param name="query">The Realm projection.</param>
/// <param name="token">The returned subscription token.</param>
/// <typeparam name="T">The type of the entities.</typeparam>
/// <returns>The <see cref="IChangeSet{TObject}"/> to observe on.</returns>
/// <returns>The <see cref="IChangeSet{TObject}">IChangeSet</see> to observe on.</returns>
public static IObservable<IChangeSet<T>> Connect<T>(this IQueryable<T> query, out IDisposable token)
where T : IRealmObjectBase
{
return Connect(query.AsRealmCollection(), out token);
}

/// <summary>
/// Convert the Realm list into an observable list and subscribe to the collection changes.
/// </summary>
/// <param name="list">The Realm list.</param>
/// <param name="token">The returned subscription token.</param>
/// <typeparam name="T">The type of the entities.</typeparam>
/// <returns>The <see cref="IChangeSet{TObject}">IChangeSet</see> to observe on.</returns>
public static IObservable<IChangeSet<T>> Connect<T>(this IList<T> list, out IDisposable token)
where T : IRealmObjectBase
{
return Connect(list.AsRealmCollection(), out token);
}

/// <summary>
/// Convert the Realm collection into an observable list and subscribe to the collection changes.
/// </summary>
/// <param name="collection">The Realm collection.</param>
/// <param name="token">The returned subscription token.</param>
/// <typeparam name="T">The type of the entities.</typeparam>
/// <returns>The <see cref="IChangeSet{TObject}">IChangeSet</see> to observe on.</returns>
public static IObservable<IChangeSet<T>> Connect<T>(this IRealmCollection<T> collection, out IDisposable token)
where T : IRealmObjectBase
{
Expand All @@ -56,20 +70,7 @@ public static IObservable<IChangeSet<T>> Connect<T>(this IRealmCollection<T> col
}
else
{
cache.Edit(update =>
{
// Handle deleted elements
foreach (int i in changes.DeletedIndices)
{
update.RemoveAt(i);
}

// Handle inserted elements
foreach (int i in changes.InsertedIndices)
{
update.Insert(i, sender[i]);
}
});
cache.Edit(update => updateCache(changes, update, sender));

logger.Debug("Processed {ChangesCount} changes for {Type} observable cache: [Removed: {Removed}, Added: {Added}, Moved: {Moved}]",
changes.DeletedIndices.Length + changes.InsertedIndices.Length,
Expand All @@ -82,4 +83,20 @@ public static IObservable<IChangeSet<T>> Connect<T>(this IRealmCollection<T> col

return cache.Connect();
}

private static void updateCache<T>(ChangeSet changes, IExtendedList<T> update, IRealmCollection<T> sender)
where T : IRealmObjectBase
{
// Handle deleted elements
foreach (int i in changes.DeletedIndices)
{
update.RemoveAt(i);
}

// Handle inserted elements
foreach (int i in changes.InsertedIndices)
{
update.Insert(i, sender[i]);
}
}
}

0 comments on commit 2dff19f

Please sign in to comment.