Only recreate TViews when necessary in RXCollection
This commit is contained in:
1 parent
2689ed806d
commit
9cb7b84f57
2 files changed
+144
-3
No files matched your search
@@ -20,12 +20,105 @@ namespace mbts.ui.utility {
|
||||
}
|
||||
|
||||
static class RXCollection {
|
||||
public static IDisposable connect<TView, TModel, TKey>(IList<TView> target,
|
||||
public static IDisposable connect<TView, TModel, TKey>(
|
||||
IList<TView> target,
|
||||
IObservable<TableUpdate<TModel, TKey>> table_update_source,
|
||||
IObservable<IComparer<TModel>> comparer_source,
|
||||
Func<TModel, TView> view_factory,
|
||||
Func<TView, TModel> get_model) where TKey : IEquatable<TKey> =>
|
||||
connect(target.WrapSelector(get_model, view_factory), table_update_source, comparer_source);
|
||||
Func<TView, TModel> get_model,
|
||||
Func<TView, TModel, TView> update_view_factory = null,
|
||||
bool regen_views_on_comp_change = true
|
||||
) where TKey : IEquatable<TKey> {
|
||||
if (update_view_factory is null) {
|
||||
var wrapped_target = target.WrapSelector(get_model, view_factory);
|
||||
return connect(wrapped_target, table_update_source, comparer_source);
|
||||
}
|
||||
|
||||
IComparer<TModel> comp = null;
|
||||
TableUpdate<TModel, TKey> table = null;
|
||||
|
||||
return Disposable.Create(
|
||||
comparer_source.Subscribe(on_comp),
|
||||
table_update_source.Subscribe(on_table));
|
||||
|
||||
void on_comp(IComparer<TModel> new_comp) {
|
||||
comp = new_comp;
|
||||
if (table is null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!regen_views_on_comp_change) {
|
||||
target.sync_with(target.Ordered(comp.wrap(get_model)).ToList());
|
||||
return;
|
||||
}
|
||||
reset();
|
||||
}
|
||||
|
||||
void on_table(TableUpdate<TModel, TKey> new_table) {
|
||||
bool is_first_upd = table is null;
|
||||
table = new_table;
|
||||
|
||||
if (comp is null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_first_upd) {
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
var view_comp = comp.wrap(get_model);
|
||||
var model_list = target.WrapSelector(get_model, view_factory);
|
||||
|
||||
// removed
|
||||
model_list.RemoveSorted(table.removed, comp, out var n_rem, out var n_rem_expected);
|
||||
Log.assert(n_rem == n_rem_expected);
|
||||
|
||||
// changes
|
||||
foreach (var ch in table.changes) {
|
||||
var old_idx = model_list.BinarySearch(ch.Old, comp);
|
||||
Log.assert(old_idx >= 0 && old_idx < target.Count);
|
||||
var new_idx = model_list.BinarySearch(ch.New, comp);
|
||||
if (new_idx < 0) {
|
||||
new_idx = ~new_idx;
|
||||
}
|
||||
if (old_idx < new_idx) {
|
||||
// decrement new_idx bc the oldview, at old_idx, will be removed before inserting
|
||||
// the newview, at new_idx
|
||||
new_idx--;
|
||||
}
|
||||
|
||||
var old_view = target[old_idx];
|
||||
var new_view = update_view_factory(old_view, ch.New);
|
||||
if (new_idx == old_idx) {
|
||||
// the index hasn't actually changed
|
||||
if (!ReferenceEquals(new_view, old_view)) {
|
||||
target[old_idx] = new_view;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove old_view and insert new_view
|
||||
target.RemoveAt(old_idx);
|
||||
target.Insert(new_idx, new_view);
|
||||
}
|
||||
|
||||
// added
|
||||
model_list.InsertSortedDistinct(
|
||||
table.added, comp, out var n_ins, out var n_ins_expected);
|
||||
Log.assert(n_ins == n_ins_expected);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
if (comp is null || table is null) {
|
||||
return;
|
||||
}
|
||||
|
||||
target.Clear();
|
||||
target.InsertSorted(
|
||||
table.state.Values.Select(view_factory), comp.wrap(get_model));
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable connect<T, TKey>(IList<T> target,
|
||||
IObservable<TableUpdate<T, TKey>> table_update_source,
|
||||
|
||||
@@ -32,6 +32,12 @@ namespace bts.collections {
|
||||
public static List<T> empty_if_null<T>(this List<T> source) =>
|
||||
source ?? new(0);
|
||||
|
||||
public static int BinarySearch<T>(this IList<T> source, T item) where T : IComparable<T> =>
|
||||
BinarySearch(source, item, Comparer<T>.Default);
|
||||
|
||||
public static int BinarySearch<T>(this IList<T> source, T item, IComparer<T> comparer) =>
|
||||
CollectionHelper.binary_search(source, item, comparer);
|
||||
|
||||
public static void InsertSorted<T>(this IList<T> list, T item) =>
|
||||
list.InsertSorted(item, Comparer<T>.Default);
|
||||
|
||||
@@ -66,6 +72,29 @@ namespace bts.collections {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts items which are comparatively distinct (according to <paramref name="comparer"/>)
|
||||
/// in <paramref name="list"/>
|
||||
/// </summary>
|
||||
public static void InsertSortedDistinct<T>(
|
||||
this IList<T> list,
|
||||
IEnumerable<T> items,
|
||||
IComparer<T> comparer,
|
||||
out int n_inserted,
|
||||
out int n_attempted
|
||||
) {
|
||||
n_attempted = 0;
|
||||
int count_before = list.Count;
|
||||
foreach (var item in items) {
|
||||
n_attempted++;
|
||||
int i = list.BinarySearch(item, comparer);
|
||||
if (i < 0) {
|
||||
list.Insert(~i, item);
|
||||
}
|
||||
}
|
||||
n_inserted = list.Count - count_before;
|
||||
}
|
||||
|
||||
public static void InsertOrReplaceSorted<T>(this IList<T> list, T item, IComparer<T> comparer) {
|
||||
int i = CollectionHelper.binary_search(list, item, comparer);
|
||||
if (i < 0) {
|
||||
@@ -130,6 +159,25 @@ namespace bts.collections {
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveSorted<T>(
|
||||
this IList<T> list,
|
||||
IEnumerable<T> items,
|
||||
IComparer<T> comparer,
|
||||
out int num_attempted,
|
||||
out int num_removed
|
||||
) {
|
||||
num_attempted = 0;
|
||||
var count_before = list.Count;
|
||||
foreach (var item in items) {
|
||||
num_attempted++;
|
||||
int i = CollectionHelper.binary_search(list, item, comparer);
|
||||
if (i >= 0) {
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
num_removed = count_before - list.Count;
|
||||
}
|
||||
|
||||
public static T Pop<T>(this IList<T> list, int index) {
|
||||
T rv = list.ElementAt(index);
|
||||
list.RemoveAt(index);
|
||||
|
||||
Reference in new issue
Block a user