From 461317070d3f69b4f23f63392b812227a922ec9c Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Tue, 26 Dec 2023 17:04:25 +0300 Subject: [PATCH] Renamed Where to WhereAsync --- .../Linq/ObservableView.SelectAsync.cs | 1 - ....Where.cs => ObservableView.WhereAsync.cs} | 68 ++++++++++++------- test/Kinetic.Tests/ObservableViewTests.cs | 4 +- 3 files changed, 45 insertions(+), 28 deletions(-) rename src/Kinetic/Linq/{ObservableView.Where.cs => ObservableView.WhereAsync.cs} (73%) diff --git a/src/Kinetic/Linq/ObservableView.SelectAsync.cs b/src/Kinetic/Linq/ObservableView.SelectAsync.cs index 342b1a2..05c1fbc 100644 --- a/src/Kinetic/Linq/ObservableView.SelectAsync.cs +++ b/src/Kinetic/Linq/ObservableView.SelectAsync.cs @@ -30,7 +30,6 @@ public static ObserverBuilder> SelectAsync public static ObserverBuilder> SelectAsync(this ReadOnlyObservableList source, Func> selector) => source.SelectAsync((item) => selector(item).ToBuilder()); - private struct SelectAsyncStateMachine : IObserverStateMachine>, IObserverStateMachine> diff --git a/src/Kinetic/Linq/ObservableView.Where.cs b/src/Kinetic/Linq/ObservableView.WhereAsync.cs similarity index 73% rename from src/Kinetic/Linq/ObservableView.Where.cs rename to src/Kinetic/Linq/ObservableView.WhereAsync.cs index 34ceb04..b73d1e1 100644 --- a/src/Kinetic/Linq/ObservableView.Where.cs +++ b/src/Kinetic/Linq/ObservableView.WhereAsync.cs @@ -6,13 +6,31 @@ namespace Kinetic.Linq; public static partial class ObservableView { - public static ObserverBuilder> Where(this ObserverBuilder> source, ObserverBuilderFactory predicate) => - source.ContinueWith, ListChange>(new(predicate)); + public static ObserverBuilder> WhereAsync(this ObserverBuilder> source, ObserverBuilderFactory predicate) => + source.ContinueWith, ListChange>(new(predicate)); - public static ObserverBuilder> Where(this ReadOnlyObservableList source, ObserverBuilderFactory predicate) => - source.Changed.ToBuilder().Where(predicate); + public static ObserverBuilder> WhereAsync(this ObserverBuilder> source, Func> predicate) => + source.WhereAsync((item) => predicate(item).Changed.ToBuilder()); - private struct WhereStateMachine : + public static ObserverBuilder> WhereAsync(this ObserverBuilder> source, Func> predicate) => + source.WhereAsync((item) => predicate(item).Changed.ToBuilder()); + + public static ObserverBuilder> WhereAsync(this ObserverBuilder> source, Func> predicate) => + source.WhereAsync((item) => predicate(item).ToBuilder()); + + public static ObserverBuilder> WhereAsync(this ReadOnlyObservableList source, ObserverBuilderFactory predicate) => + source.Changed.ToBuilder().WhereAsync(predicate); + + public static ObserverBuilder> WhereAsync(this ReadOnlyObservableList source, Func> predicate) => + source.WhereAsync((item) => predicate(item).Changed.ToBuilder()); + + public static ObserverBuilder> WhereAsync(this ReadOnlyObservableList source, Func> predicate) => + source.WhereAsync((item) => predicate(item).Changed.ToBuilder()); + + public static ObserverBuilder> WhereAsync(this ReadOnlyObservableList source, Func> predicate) => + source.WhereAsync((item) => predicate(item).ToBuilder()); + + private struct WhereAsyncStateMachine : IObserverStateMachine>, IObserverStateMachine> where TContinuation : struct, IObserverStateMachine> @@ -22,7 +40,7 @@ private struct WhereStateMachine : private List> _items = new(); private ObserverStateMachineBox? _box; - public WhereStateMachine(in TContinuation continuation, ObserverBuilderFactory predicate) + public WhereAsyncStateMachine(in TContinuation continuation, ObserverBuilderFactory predicate) { _continuation = continuation; _predicate = predicate; @@ -82,7 +100,7 @@ public void OnNext(ListChange value) { var item = new ObservableViewItem(value.NewIndex) { Item = value.NewItem }; var subscription = _predicate(item.Item) - .ContinueWith, ObservableViewItem>(new(item)) + .ContinueWith, ObservableViewItem>(new(item)) .Subscribe(ref this, _box!); _items.Insert(item.Index, item); @@ -107,7 +125,7 @@ public void OnNext(ListChange value) var newItem = new ObservableViewItem(value.NewIndex) { Item = value.NewItem }; var newSubscription = _predicate(newItem.Item) - .ContinueWith, ObservableViewItem>(new(newItem)) + .ContinueWith, ObservableViewItem>(new(newItem)) .Subscribe(ref this, _box!); _items[index] = newItem; @@ -204,37 +222,25 @@ private int CountBefore(int index) } } - private readonly struct WhereStateMachineFactory : IObserverStateMachineFactory, ListChange> + private readonly struct WhereAsyncStateMachineFactory : IObserverStateMachineFactory, ListChange> { private readonly ObserverBuilderFactory _predicate; - public WhereStateMachineFactory(ObserverBuilderFactory predicate) => + public WhereAsyncStateMachineFactory(ObserverBuilderFactory predicate) => _predicate = predicate; public void Create(in TContinuation continuation, ObserverStateMachine> source) where TContinuation : struct, IObserverStateMachine> => - source.ContinueWith(new WhereStateMachine(continuation, _predicate)); - } - - private readonly struct WherePredicateStateMachineFactory : IObserverStateMachineFactory> - { - private readonly ObservableViewItem _item; - - public WherePredicateStateMachineFactory(ObservableViewItem item) => - _item = item; - - public void Create(in TContinuation continuation, ObserverStateMachine source) - where TContinuation : struct, IObserverStateMachine> => - source.ContinueWith(new WherePredicateStateMachine(continuation, _item)); + source.ContinueWith(new WhereAsyncStateMachine(continuation, _predicate)); } - private struct WherePredicateStateMachine : IObserverStateMachine + private struct PredicateStateMachine : IObserverStateMachine where TContinuation : struct, IObserverStateMachine> { private TContinuation _continuation; private ObservableViewItem _item; - public WherePredicateStateMachine(in TContinuation continuation, ObservableViewItem item) + public PredicateStateMachine(in TContinuation continuation, ObservableViewItem item) { _continuation = continuation; _item = item; @@ -263,4 +269,16 @@ public void OnNext(bool value) _continuation.OnNext(_item); } } + + private readonly struct PredicateStateMachineFactory : IObserverStateMachineFactory> + { + private readonly ObservableViewItem _item; + + public PredicateStateMachineFactory(ObservableViewItem item) => + _item = item; + + public void Create(in TContinuation continuation, ObserverStateMachine source) + where TContinuation : struct, IObserverStateMachine> => + source.ContinueWith(new PredicateStateMachine(continuation, _item)); + } } \ No newline at end of file diff --git a/test/Kinetic.Tests/ObservableViewTests.cs b/test/Kinetic.Tests/ObservableViewTests.cs index 43707bc..5b7d60a 100644 --- a/test/Kinetic.Tests/ObservableViewTests.cs +++ b/test/Kinetic.Tests/ObservableViewTests.cs @@ -5,10 +5,10 @@ namespace Kinetic.Linq.Tests; public class ObservableViewTests { [Fact] - public void Where() + public void WhereAsync() { var list = new ObservableList(); - 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");