Skip to content

Commit

Permalink
[Code] ToStream utils
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Nov 10, 2021
1 parent 63f3c9d commit 62b0ee3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 12 additions & 7 deletions WDE.MVVM/Observable/Functional/ObservableCollectionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ public class ObservableCollectionStream<T> : IObservable<CollectionEvent<T>>
private readonly IEnumerable<T> initial;
private readonly INotifyCollectionChanged notifyCollectionChanged;
private readonly bool onRemoveOnDispose;
private readonly bool includeInitial;

public ObservableCollectionStream(IEnumerable<T> initial, INotifyCollectionChanged notifyCollectionChanged, bool onRemoveOnDispose)
public ObservableCollectionStream(IEnumerable<T> initial, INotifyCollectionChanged notifyCollectionChanged, bool onRemoveOnDispose, bool includeInitial)
{
this.initial = initial;
this.notifyCollectionChanged = notifyCollectionChanged;
this.onRemoveOnDispose = onRemoveOnDispose;
this.includeInitial = includeInitial;
}

public ObservableCollectionStream(ObservableCollection<T> collection, bool onRemoveOnDispose) : this(collection, collection, onRemoveOnDispose)
public ObservableCollectionStream(ObservableCollection<T> collection, bool onRemoveOnDispose, bool includeInitial) : this(collection, collection, onRemoveOnDispose, includeInitial)
{
}

public IDisposable Subscribe(IObserver<CollectionEvent<T>> observer)
{
return new Subscription(initial, notifyCollectionChanged, observer, onRemoveOnDispose);
return new Subscription(initial, notifyCollectionChanged, observer, onRemoveOnDispose, includeInitial);
}

private class Subscription : IDisposable
Expand All @@ -35,7 +37,7 @@ private class Subscription : IDisposable
private readonly bool removeOnDispose;

public Subscription(IEnumerable<T> initial, INotifyCollectionChanged changed,
IObserver<CollectionEvent<T>> observer, bool removeOnDispose)
IObserver<CollectionEvent<T>> observer, bool removeOnDispose, bool includeInitial)
{
this.initial = initial;
notifyCollectionChanged = changed;
Expand All @@ -44,10 +46,13 @@ public Subscription(IEnumerable<T> initial, INotifyCollectionChanged changed,
this.removeOnDispose = removeOnDispose;
changed.CollectionChanged += CollectionOnCollectionChanged;

int index = 0;
foreach (var t in initial)
if (includeInitial)
{
observer.OnNext(new CollectionEvent<T>(CollectionEventType.Add, t, index++));
int index = 0;
foreach (var t in initial)
{
observer.OnNext(new CollectionEvent<T>(CollectionEventType.Add, t, index++));
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions WDE.MVVM/Observable/FunctionalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ public static IObservable<int> ToCountChangedObservable<T>(this ObservableCollec
return collection.ToObservable(o => o.Count);
}

public static IObservable<CollectionEvent<T>> ToStreamForChanges<T>(this ObservableCollection<T> collection)
{
return new ObservableCollectionStream<T>(collection, false, false);
}

public static IObservable<CollectionEvent<T>> ToStream<T>(this ObservableCollection<T> collection, bool onRemoveOnDispose)
{
return new ObservableCollectionStream<T>(collection, onRemoveOnDispose);
return new ObservableCollectionStream<T>(collection, onRemoveOnDispose, true);
}

public static System.IDisposable DisposeOnRemove<T>(this ObservableCollection<T> collection, Func<bool> shouldDispose) where T : System.IDisposable
Expand Down Expand Up @@ -97,7 +102,7 @@ public static System.IDisposable ToStreamForEachValue<T>(this ObservableCollecti

public static IObservable<CollectionEvent<T>> ToStream<T>(this INotifyCollectionChanged collection, IEnumerable<T> initial)
{
return new ObservableCollectionStream<T>(initial, collection, false);
return new ObservableCollectionStream<T>(initial, collection, false, true);
}

public static void RemoveAll<T>(this ObservableCollection<T> collection)
Expand Down

0 comments on commit 62b0ee3

Please sign in to comment.