Skip to content

Commit

Permalink
feat(feeds): Enable KeyTracking in fallback BindableListFeed collecti…
Browse files Browse the repository at this point in the history
…on tracking
  • Loading branch information
dr1rrb committed Sep 23, 2022
1 parent d8e6645 commit 8f534ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public BindableListFeed(string propertyName, IListFeed<T> source, SourceContext
PropertyName = propertyName;

_state = ctx.GetOrCreateListState(source);
_items = CreateBindableCollection(ctx);
_items = CreateBindableCollection(_state, ctx);
}

/// <inheritdoc />
Expand Down Expand Up @@ -78,15 +78,17 @@ ValueTask IState<IImmutableList<T>>.UpdateMessage(Action<MessageBuilder<IImmutab
=> _state.UpdateMessage(updater, ct);


private BindableCollection CreateBindableCollection(SourceContext ctx)
private static BindableCollection CreateBindableCollection(IListState<T> state, SourceContext ctx)
{
var currentCount = 0;
var pageTokens = new TokenSetAwaiter<PageToken>();

var requests = new RequestSource();
var pagination = new PaginationService(LoadMore);
var services = new SingletonServiceProvider(pagination);
var collection = BindableCollection.Create<T>(services: services);
var collection = BindableCollection.Create(
services: services,
itemComparer: ListFeed<T>.DefaultComparer);

if (ctx.Token.CanBeCanceled)
{
Expand All @@ -98,7 +100,7 @@ private BindableCollection CreateBindableCollection(SourceContext ctx)
// Note: we have to listen for collection changes on bindable _items to update the state.
// https://github.com/unoplatform/uno.extensions/issues/370

_state.GetSource(ctx.CreateChild(requests), ctx.Token).ForEachAsync(
state.GetSource(ctx.CreateChild(requests), ctx.Token).ForEachAsync(
msg =>
{
if (ctx.Token.IsCancellationRequested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Uno.Extensions.Reactive.Bindings.Collections._BindableCollection.Data;
using Uno.Extensions.Reactive.Bindings.Collections._BindableCollection.Facets;
using Uno.Extensions.Reactive.Bindings.Collections.Services;
using Uno.Extensions.Reactive.Collections;
using Uno.Extensions.Reactive.Dispatching;
using Uno.Extensions.Reactive.Utils;
using ISchedulersProvider = Uno.Extensions.Reactive.Dispatching.DispatcherHelper.FindDispatcher;
Expand All @@ -32,21 +33,7 @@ internal sealed partial class BindableCollection : ICollectionView, INotifyColle
/// Creates a new instance of a <see cref="BindableCollection"/>.
/// </summary>
/// <param name="initial">The initial items in the collection.</param>
/// <param name="itemComparer">
/// Comparer used to detect multiple versions of the **same entity (T)**, or null to use default.
/// <remarks>Usually this should only compare the ID of the entities in order to properly track the changes made on an entity.</remarks>
/// <remarks>For better performance, prefer provide null instead of <see cref="EqualityComparer{T}.Default"/> (cf. MapObservableCollection).</remarks>
/// </param>
/// <param name="itemVersionComparer">
/// Comparer used to detect multiple instance of the **same version** of the **same entity (T)**, or null to rely only on the <paramref name="itemComparer"/> (not recommanded).
/// <remarks>
/// This comparer will determine if two instances of the same entity (which was considered as equals by the <paramref name="itemComparer"/>),
/// are effectively equals or not (i.e. same version or not).
/// <br />
/// * If **Equals**: it's 2 **instances** of the **same version** of the **same entity** (all properties are equals), so we don't have to raise a <see cref="NotifyCollectionChangedAction.Replace"/>.<br />
/// * If **NOT Equals**: it's 2 **distinct versions** of the **same entity** (not all properties are equals) and we have to raise a 'Replace' to re-evaluate those properties.
/// </remarks>
/// </param>
/// <param name="itemComparer">Comparer used to track items.</param>
/// <param name="schedulersProvider">Schedulers provider to use to handle concurrency.</param>
/// <param name="services">A set of services that the collection can use (cf. Remarks)</param>
/// <param name="resetThreshold">Threshold on which the a single reset is raised instead of multiple collection changes.</param>
Expand All @@ -55,16 +42,12 @@ internal sealed partial class BindableCollection : ICollectionView, INotifyColle
/// </remarks>
internal static BindableCollection Create<T>(
IObservableCollection<T>? initial = null,
IEqualityComparer<T>? itemComparer = null,
IEqualityComparer<T>? itemVersionComparer = null,
ItemComparer<T> itemComparer = default,
ISchedulersProvider? schedulersProvider = null,
IServiceProvider? services = null,
int resetThreshold = DataStructure.DefaultResetThreshold)
{
var dataStructure = new DataStructure
(
(itemComparer?.ToEqualityComparer(), itemVersionComparer?.ToEqualityComparer())
)
var dataStructure = new DataStructure((ItemComparer)itemComparer)
{
ResetThreshold = resetThreshold
};
Expand All @@ -77,16 +60,12 @@ internal static BindableCollection Create<T>(
/// </summary>
internal static BindableCollection CreateUntyped(
IObservableCollection? initial = null,
IEqualityComparer? itemComparer = null,
IEqualityComparer? itemVersionComparer = null,
ItemComparer itemComparer = default,
ISchedulersProvider? schedulersProvider = null,
int resetThreshold = DataStructure.DefaultResetThreshold
)
{
var dataStructure = new DataStructure
(
(itemComparer, itemVersionComparer)
)
var dataStructure = new DataStructure(itemComparer)
{
ResetThreshold = resetThreshold
};
Expand All @@ -96,19 +75,13 @@ internal static BindableCollection CreateUntyped(

internal static BindableCollection CreateGrouped<TKey, T>(
IObservableCollection<TKey> initial,
IEqualityComparer<TKey>? keyComparer,
IEqualityComparer<TKey>? keyVersionComparer,
IEqualityComparer<T>? itemComparer = null,
IEqualityComparer<T>? itemVersionComparer = null,
ItemComparer<TKey> keyComparer,
ItemComparer<T> itemComparer = default,
ISchedulersProvider? schedulersProvider = null,
int resetThreshold = DataStructure.DefaultResetThreshold)
where TKey : IObservableGroup<T>
{
var dataStructure = new DataStructure
(
(keyComparer?.ToEqualityComparer(), keyVersionComparer?.ToEqualityComparer()),
(itemComparer?.ToEqualityComparer(), itemVersionComparer?.ToEqualityComparer())
)
var dataStructure = new DataStructure((ItemComparer)keyComparer, (ItemComparer)itemComparer)
{
ResetThreshold = resetThreshold
};
Expand All @@ -118,19 +91,13 @@ internal static BindableCollection CreateGrouped<TKey, T>(

internal static BindableCollection CreateGrouped<TGroup>(
IObservableCollection initial,
IEqualityComparer<TGroup>? groupComparer,
IEqualityComparer<TGroup>? groupVersionComparer,
IEqualityComparer? itemComparer,
IEqualityComparer? itemVersionComparer,
ItemComparer<TGroup> groupComparer,
ItemComparer itemComparer,
ISchedulersProvider? schedulersProvider = null,
int resetThreshold = DataStructure.DefaultResetThreshold)
where TGroup : IObservableGroup
{
var dataStructure = new DataStructure
(
(groupComparer?.ToEqualityComparer(), groupVersionComparer?.ToEqualityComparer()),
(itemComparer, itemVersionComparer)
)
var dataStructure = new DataStructure((ItemComparer)groupComparer, itemComparer)
{
ResetThreshold = resetThreshold
};
Expand All @@ -140,18 +107,12 @@ internal static BindableCollection CreateGrouped<TGroup>(

internal static BindableCollection CreateUntypedGrouped(
IObservableCollection initial,
IEqualityComparer? groupComparer,
IEqualityComparer? groupVersionComparer,
IEqualityComparer? itemComparer,
IEqualityComparer? itemVersionComparer,
ItemComparer groupComparer,
ItemComparer itemComparer,
ISchedulersProvider? schedulersProvider = null,
int resetThreshold = DataStructure.DefaultResetThreshold)
{
var dataStructure = new DataStructure
(
(groupComparer, groupVersionComparer),
(itemComparer, itemVersionComparer)
)
var dataStructure = new DataStructure(groupComparer, itemComparer)
{
ResetThreshold = resetThreshold
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal class DataStructure : IBindableCollectionDataStructure
{
public const int DefaultResetThreshold = 5;

private readonly (IEqualityComparer? itemComparer, IEqualityComparer? itemVersionComparer)[] _comparersStructure;
private readonly ItemComparer[] _comparersStructure;

public DataStructure(params (IEqualityComparer? itemComparer, IEqualityComparer? itemVersionComparer)[] comparersStructure)
public DataStructure(params ItemComparer[] comparersStructure)
{
_comparersStructure = comparersStructure;
}
Expand All @@ -33,7 +33,7 @@ public IBindableCollectionDataLayerStrategy GetLayer(uint level)
}

var comparers = _comparersStructure[level];
var tracker = new CollectionAnalyzer(new ItemComparer(comparers.itemComparer, comparers.itemVersionComparer));
var tracker = new CollectionAnalyzer(comparers);

if (level + 1 == _comparersStructure.Length)
{
Expand Down

0 comments on commit 8f534ed

Please sign in to comment.