From 8fc498bf780ab79b46e37cec936d31e8d4b1b0c6 Mon Sep 17 00:00:00 2001 From: neuecc Date: Fri, 27 Sep 2024 16:37:05 +0900 Subject: [PATCH] Add nongeneric ReactiveCommand --- README.md | 4 +-- sandbox/ConsoleApp1/Program.cs | 45 +++++++++++++++++++++--------- sandbox/WpfApp1/MainWindow.xaml | 12 ++++---- sandbox/WpfApp1/MainWindow.xaml.cs | 2 +- src/R3/ReactiveCommand.cs | 29 ++++++++++++++++--- 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index f9a626dc..876267ca 100644 --- a/README.md +++ b/README.md @@ -917,13 +917,13 @@ There is also `IReadOnlyBindableReactiveProperty`, which is preferable when R ### ReactiveCommand -`ReactiveCommand` is observable [ICommand](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.icommand) implementation. It can create from `Observable canExecuteSource`. +`ReactiveCommand` and `ReactiveCommand` are observable [ICommand](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.icommand) implementation. It can create from `Observable canExecuteSource`. ```csharp public class CommandViewModel : IDisposable { public BindableReactiveProperty OnCheck { get; } // bind to CheckBox - public ReactiveCommand ShowMessageBox { get; } // bind to Button + public ReactiveCommand ShowMessageBox { get; } // bind to Button, non generics ReactiveCommand is ReactiveCommand public CommandViewModel() { diff --git a/sandbox/ConsoleApp1/Program.cs b/sandbox/ConsoleApp1/Program.cs index 20df5875..b30d3e53 100644 --- a/sandbox/ConsoleApp1/Program.cs +++ b/sandbox/ConsoleApp1/Program.cs @@ -1,26 +1,45 @@ -using R3; -//using System.Reactive.Linq; +//using R3; +using System.Reactive.Linq; using System.Diagnostics; using System.Threading.Channels; using System.Xml.Serialization; -var b = new Subject(); +// Observable.Range(1,10).MinBy( +// Enumerable.Range(1,10).MinBy( +var a = new System.Reactive.Subjects.BehaviorSubject(0); +//var d = Disposable.CreateBuilder(); -var rp = new ReactiveCommand(async (x, ct) => +// a.OnNext( + + + + +a.Do(v => Debug.Log($"a {v}")).Do(v => { - await Task.Delay(TimeSpan.FromSeconds(1)); - return x + "foo"; -}); + if (v < 20) + { + a.OnNext(v + 1); + } +}).Subscribe(); //.AddTo(ref d); + -rp.Subscribe(x => Console.WriteLine("a:" + x)); -rp.Subscribe(x => Console.WriteLine("b:" + x)); -rp.Execute(0); -rp.Execute(1); +Debug.Log($"a == 1: {a.Value == 1}"); // this should be 20, not 1! -Console.ReadLine(); +a.OnNext(0); // this -rp.Dispose(); +Debug.Log($"a == 20: {a.Value == 20}"); // this is correct + + + + +public static class Debug +{ + public static void Log(string msg) + { + Console.WriteLine(msg); + } +} diff --git a/sandbox/WpfApp1/MainWindow.xaml b/sandbox/WpfApp1/MainWindow.xaml index 7b92a815..e0563c71 100644 --- a/sandbox/WpfApp1/MainWindow.xaml +++ b/sandbox/WpfApp1/MainWindow.xaml @@ -7,17 +7,17 @@ mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> - + - + - + - + diff --git a/sandbox/WpfApp1/MainWindow.xaml.cs b/sandbox/WpfApp1/MainWindow.xaml.cs index ca8c6cac..149a1164 100644 --- a/sandbox/WpfApp1/MainWindow.xaml.cs +++ b/sandbox/WpfApp1/MainWindow.xaml.cs @@ -118,7 +118,7 @@ public void Dispose() public class CommandViewModel : IDisposable { public BindableReactiveProperty OnCheck { get; } - public ReactiveCommand ShowMessageBox { get; } + public ReactiveCommand ShowMessageBox { get; } public CommandViewModel() { diff --git a/src/R3/ReactiveCommand.cs b/src/R3/ReactiveCommand.cs index de99f8fc..89aeb8a4 100644 --- a/src/R3/ReactiveCommand.cs +++ b/src/R3/ReactiveCommand.cs @@ -351,6 +351,27 @@ public void Dispose() } } +public class ReactiveCommand : ReactiveCommand +{ + public ReactiveCommand() : base() + { + } + + public ReactiveCommand(Action execute) : base(execute) + { + } + + public ReactiveCommand(Func executeAsync, AwaitOperation awaitOperation = AwaitOperation.Sequential, bool configureAwait = true, bool cancelOnCompleted = false, int maxSequential = -1) + : base(executeAsync, awaitOperation, configureAwait, cancelOnCompleted, maxSequential) + { + } + + public ReactiveCommand(Observable canExecuteSource, bool initialCanExecute) + : base(canExecuteSource, initialCanExecute) + { + } +} + public static class ReactiveCommandExtensions { public static ReactiveCommand ToReactiveCommand(this Observable canExecuteSource, bool initialCanExecute = true) @@ -375,15 +396,15 @@ public static ReactiveCommand ToReactiveCommand ToReactiveCommand(this Observable canExecuteSource, bool initialCanExecute = true) + public static ReactiveCommand ToReactiveCommand(this Observable canExecuteSource, bool initialCanExecute = true) { - var command = new ReactiveCommand(canExecuteSource, initialCanExecute); + var command = new ReactiveCommand(canExecuteSource, initialCanExecute); return command; } - public static ReactiveCommand ToReactiveCommand(this Observable canExecuteSource, Action execute, bool initialCanExecute = true) + public static ReactiveCommand ToReactiveCommand(this Observable canExecuteSource, Action execute, bool initialCanExecute = true) { - var command = new ReactiveCommand(canExecuteSource, initialCanExecute); + var command = new ReactiveCommand(canExecuteSource, initialCanExecute); var subscription = command.Subscribe(execute); command.CombineSubscription(subscription);