Skip to content

Commit

Permalink
Renamed Where to WhereAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed Dec 26, 2023
1 parent 6217af6 commit 4613170
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/Kinetic/Linq/ObservableView.SelectAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static ObserverBuilder<ListChange<TResult>> SelectAsync<TSource, TResult>
public static ObserverBuilder<ListChange<TResult>> SelectAsync<TSource, TResult>(this ReadOnlyObservableList<TSource> source, Func<TSource, IObservable<TResult>> selector) =>
source.SelectAsync((item) => selector(item).ToBuilder());


private struct SelectAsyncStateMachine<TSource, TResult, TContinuation> :
IObserverStateMachine<ListChange<TSource>>,
IObserverStateMachine<ObservableViewItem<TResult>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@ namespace Kinetic.Linq;

public static partial class ObservableView
{
public static ObserverBuilder<ListChange<T>> Where<T>(this ObserverBuilder<ListChange<T>> source, ObserverBuilderFactory<T, bool> predicate) =>
source.ContinueWith<WhereStateMachineFactory<T>, ListChange<T>>(new(predicate));
public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ObserverBuilder<ListChange<T>> source, ObserverBuilderFactory<T, bool> predicate) =>
source.ContinueWith<WhereAsyncStateMachineFactory<T>, ListChange<T>>(new(predicate));

public static ObserverBuilder<ListChange<T>> Where<T>(this ReadOnlyObservableList<T> source, ObserverBuilderFactory<T, bool> predicate) =>
source.Changed.ToBuilder().Where(predicate);
public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ObserverBuilder<ListChange<T>> source, Func<T, Property<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).Changed.ToBuilder());

private struct WhereStateMachine<T, TContinuation> :
public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ObserverBuilder<ListChange<T>> source, Func<T, ReadOnlyProperty<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).Changed.ToBuilder());

public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ObserverBuilder<ListChange<T>> source, Func<T, IObservable<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).ToBuilder());

public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ReadOnlyObservableList<T> source, ObserverBuilderFactory<T, bool> predicate) =>
source.Changed.ToBuilder().WhereAsync(predicate);

public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ReadOnlyObservableList<T> source, Func<T, Property<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).Changed.ToBuilder());

public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ReadOnlyObservableList<T> source, Func<T, ReadOnlyProperty<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).Changed.ToBuilder());

public static ObserverBuilder<ListChange<T>> WhereAsync<T>(this ReadOnlyObservableList<T> source, Func<T, IObservable<bool>> predicate) =>
source.WhereAsync((item) => predicate(item).ToBuilder());

private struct WhereAsyncStateMachine<T, TContinuation> :
IObserverStateMachine<ListChange<T>>,
IObserverStateMachine<ObservableViewItem<T>>
where TContinuation : struct, IObserverStateMachine<ListChange<T>>
Expand All @@ -22,7 +40,7 @@ private struct WhereStateMachine<T, TContinuation> :
private List<ObservableViewItem<T>> _items = new();
private ObserverStateMachineBox? _box;

public WhereStateMachine(in TContinuation continuation, ObserverBuilderFactory<T, bool> predicate)
public WhereAsyncStateMachine(in TContinuation continuation, ObserverBuilderFactory<T, bool> predicate)
{
_continuation = continuation;
_predicate = predicate;
Expand Down Expand Up @@ -82,7 +100,7 @@ public void OnNext(ListChange<T> value)
{
var item = new ObservableViewItem<T>(value.NewIndex) { Item = value.NewItem };
var subscription = _predicate(item.Item)
.ContinueWith<WherePredicateStateMachineFactory<T>, ObservableViewItem<T>>(new(item))
.ContinueWith<PredicateStateMachineFactory<T>, ObservableViewItem<T>>(new(item))
.Subscribe(ref this, _box!);

_items.Insert(item.Index, item);
Expand All @@ -107,7 +125,7 @@ public void OnNext(ListChange<T> value)

var newItem = new ObservableViewItem<T>(value.NewIndex) { Item = value.NewItem };
var newSubscription = _predicate(newItem.Item)
.ContinueWith<WherePredicateStateMachineFactory<T>, ObservableViewItem<T>>(new(newItem))
.ContinueWith<PredicateStateMachineFactory<T>, ObservableViewItem<T>>(new(newItem))
.Subscribe(ref this, _box!);

_items[index] = newItem;
Expand Down Expand Up @@ -204,37 +222,25 @@ private int CountBefore(int index)
}
}

private readonly struct WhereStateMachineFactory<T> : IObserverStateMachineFactory<ListChange<T>, ListChange<T>>
private readonly struct WhereAsyncStateMachineFactory<T> : IObserverStateMachineFactory<ListChange<T>, ListChange<T>>
{
private readonly ObserverBuilderFactory<T, bool> _predicate;

public WhereStateMachineFactory(ObserverBuilderFactory<T, bool> predicate) =>
public WhereAsyncStateMachineFactory(ObserverBuilderFactory<T, bool> predicate) =>
_predicate = predicate;

public void Create<TContinuation>(in TContinuation continuation, ObserverStateMachine<ListChange<T>> source)
where TContinuation : struct, IObserverStateMachine<ListChange<T>> =>
source.ContinueWith(new WhereStateMachine<T, TContinuation>(continuation, _predicate));
}

private readonly struct WherePredicateStateMachineFactory<T> : IObserverStateMachineFactory<bool, ObservableViewItem<T>>
{
private readonly ObservableViewItem<T> _item;

public WherePredicateStateMachineFactory(ObservableViewItem<T> item) =>
_item = item;

public void Create<TContinuation>(in TContinuation continuation, ObserverStateMachine<bool> source)
where TContinuation : struct, IObserverStateMachine<ObservableViewItem<T>> =>
source.ContinueWith(new WherePredicateStateMachine<T, TContinuation>(continuation, _item));
source.ContinueWith(new WhereAsyncStateMachine<T, TContinuation>(continuation, _predicate));
}

private struct WherePredicateStateMachine<T, TContinuation> : IObserverStateMachine<bool>
private struct PredicateStateMachine<T, TContinuation> : IObserverStateMachine<bool>
where TContinuation : struct, IObserverStateMachine<ObservableViewItem<T>>
{
private TContinuation _continuation;
private ObservableViewItem<T> _item;

public WherePredicateStateMachine(in TContinuation continuation, ObservableViewItem<T> item)
public PredicateStateMachine(in TContinuation continuation, ObservableViewItem<T> item)
{
_continuation = continuation;
_item = item;
Expand Down Expand Up @@ -263,4 +269,16 @@ public void OnNext(bool value)
_continuation.OnNext(_item);
}
}

private readonly struct PredicateStateMachineFactory<T> : IObserverStateMachineFactory<bool, ObservableViewItem<T>>
{
private readonly ObservableViewItem<T> _item;

public PredicateStateMachineFactory(ObservableViewItem<T> item) =>
_item = item;

public void Create<TContinuation>(in TContinuation continuation, ObserverStateMachine<bool> source)
where TContinuation : struct, IObserverStateMachine<ObservableViewItem<T>> =>
source.ContinueWith(new PredicateStateMachine<T, TContinuation>(continuation, _item));
}
}
4 changes: 2 additions & 2 deletions test/Kinetic.Tests/ObservableViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Kinetic.Linq.Tests;
public class ObservableViewTests
{
[Fact]
public void Where()
public void WhereAsync()
{
var list = new ObservableList<Item>();
var view = list.Where(item => item.Number.Changed.Select(static number => number % 2 == 0)).ToView();
var view = list.WhereAsync(item => item.Number.Changed.Select(static number => number % 2 == 0)).ToView();

var itemA = new Item(0, "A");
var itemB = new Item(1, "B");
Expand Down

0 comments on commit 4613170

Please sign in to comment.