Add observe_immutable ext fn for ObservableCollection<T>
Converts sequence of CollectionChanged events to an IObservable<ImmutableList<T>>
This commit is contained in:
1 parent
6901fe07d7
commit
1c4e574512
2 files changed
+84
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using bts;
|
||||
using bts.collection;
|
||||
|
||||
using mbts;
|
||||
using mbts.utils;
|
||||
@@ -6,6 +7,7 @@ using mbts.utils;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
@@ -235,6 +237,54 @@ namespace System.Collections.Specialized {
|
||||
public static IObservable<NotifyCollectionChangedEventArgs> Observe(this INotifyCollectionChanged source) {
|
||||
return Observable.FromEvent<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(cb => (s, e) => cb(e), h => source.CollectionChanged += h, h => source.CollectionChanged -= h);
|
||||
}
|
||||
|
||||
public static IObservable<ImmutableList<T>> observe_immutable<T>(this ObservableCollection<T> source) {
|
||||
// TODO: unit tests
|
||||
return Observable.Create<ImmutableList<T>>(o => {
|
||||
var ilist = ImmutableList.CreateRange(source);
|
||||
o.OnNext(ilist);
|
||||
return source.Observe().Subscribe(e =>
|
||||
o.OnNext(ilist = e.Action switch {
|
||||
NotifyCollectionChangedAction.Add => add(e.NewStartingIndex, e.NewItems),
|
||||
NotifyCollectionChangedAction.Remove => remove(e.OldStartingIndex, e.OldItems),
|
||||
NotifyCollectionChangedAction.Move => move(e.OldStartingIndex, e.NewStartingIndex, e.NewItems),
|
||||
NotifyCollectionChangedAction.Replace => replace(e.NewStartingIndex, e.NewItems),
|
||||
NotifyCollectionChangedAction.Reset => ilist.Clear(),
|
||||
_ => throw new ArgumentException($"Invalid {nameof(NotifyCollectionChangedAction)} '{e.Action}'", nameof(e))
|
||||
}));
|
||||
|
||||
ImmutableList<T> add(int index, IList items) =>
|
||||
index < 0 ? ilist.add_range(items.Cast<T>()) : ilist.insert_range(index, items.Cast<T>());
|
||||
|
||||
ImmutableList<T> remove(int index, IList items) =>
|
||||
index < 0 ? ilist.remove_range(items.Cast<T>()) : ilist.remove_range(index, items.Count);
|
||||
|
||||
ImmutableList<T> move(int old_index, int new_index, IList items) {
|
||||
int i = 0;
|
||||
var bd = ilist.ToBuilder();
|
||||
for (; i < items.Count; i++) {
|
||||
bd.RemoveAt(old_index);
|
||||
}
|
||||
var dest = new_index;
|
||||
if (dest > old_index) {
|
||||
dest -= items.Count;
|
||||
}
|
||||
i = 0;
|
||||
foreach (T item in items) {
|
||||
bd.Insert(new_index + i, item);
|
||||
}
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
|
||||
ImmutableList<T> replace(int index, IList items) {
|
||||
var bd = ilist.ToBuilder();
|
||||
for (int i = 0; i < items.Count; i++) {
|
||||
bd[i + index] = (T)items[i];
|
||||
}
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace bts.collection {
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
static class ImmutableListExtensions {
|
||||
public static ImmutableList<T> add_range<T>(this ImmutableList<T> list, IEnumerable<T> items) {
|
||||
var bd = list.ToBuilder();
|
||||
bd.AddRange(items);
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
|
||||
public static ImmutableList<T> insert_range<T>(this ImmutableList<T> list, int index, IEnumerable<T> items) {
|
||||
var bd = list.ToBuilder();
|
||||
bd.InsertRange(index, items);
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
|
||||
public static ImmutableList<T> remove_range<T>(this ImmutableList<T> list, IEnumerable<T> items) {
|
||||
var bd = list.ToBuilder();
|
||||
foreach (var item in items) {
|
||||
bd.Remove(item);
|
||||
}
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
|
||||
public static ImmutableList<T> remove_range<T>(this ImmutableList<T> list, int index, int count) {
|
||||
var bd = list.ToBuilder();
|
||||
while (count-- > 0) {
|
||||
bd.RemoveAt(index + count);
|
||||
}
|
||||
return bd.ToImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user