Skip to content

Commit

Permalink
Add nongeneric ReactiveCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Sep 27, 2024
1 parent f541cca commit 8fc498b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,13 @@ There is also `IReadOnlyBindableReactiveProperty<T>`, which is preferable when R

### ReactiveCommand

`ReactiveCommand<T>` is observable [ICommand](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.icommand) implementation. It can create from `Observable<bool> canExecuteSource`.
`ReactiveCommand<T>` and `ReactiveCommand` are observable [ICommand](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.icommand) implementation. It can create from `Observable<bool> canExecuteSource`.

```csharp
public class CommandViewModel : IDisposable
{
public BindableReactiveProperty<bool> OnCheck { get; } // bind to CheckBox
public ReactiveCommand<Unit> ShowMessageBox { get; } // bind to Button
public ReactiveCommand ShowMessageBox { get; } // bind to Button, non generics ReactiveCommand is ReactiveCommand<Unit>
public CommandViewModel()
{
Expand Down
45 changes: 32 additions & 13 deletions sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -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<bool>();
// Observable.Range(1,10).MinBy(
// Enumerable.Range(1,10).MinBy(

var a = new System.Reactive.Subjects.BehaviorSubject<int>(0);

//var d = Disposable.CreateBuilder();

var rp = new ReactiveCommand<int, string>(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);
}
}
12 changes: 6 additions & 6 deletions sandbox/WpfApp1/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:BasicUsagesViewModel />
<!--<local:BasicUsagesViewModel />-->
<!--<local:ValidationViewModel />-->
<!--<local:CommandViewModel />-->
<local:CommandViewModel />
</Window.DataContext>
<StackPanel>
<!--<StackPanel>
<TextBlock Text="Basic usages" FontSize="24" />
<Label Content="Input" />
<TextBox Text="{Binding Input.Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="Output" />
<TextBlock Text="{Binding Output.Value}" />
</StackPanel>
</StackPanel>-->

<!--<StackPanel Margin="10">
<Label Content="Validation" />
Expand All @@ -28,9 +28,9 @@
</StackPanel>-->


<!--<StackPanel Margin="10">
<StackPanel Margin="10">
<Label Content="Command" />
<CheckBox IsChecked="{Binding OnCheck.Value}" />
<Button Content="Btn" Command="{Binding ShowMessageBox}" />
</StackPanel>-->
</StackPanel>
</Window>
2 changes: 1 addition & 1 deletion sandbox/WpfApp1/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Dispose()
public class CommandViewModel : IDisposable
{
public BindableReactiveProperty<bool> OnCheck { get; }
public ReactiveCommand<Unit> ShowMessageBox { get; }
public ReactiveCommand ShowMessageBox { get; }

public CommandViewModel()
{
Expand Down
29 changes: 25 additions & 4 deletions src/R3/ReactiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,27 @@ public void Dispose()
}
}

public class ReactiveCommand : ReactiveCommand<Unit>
{
public ReactiveCommand() : base()
{
}

public ReactiveCommand(Action<Unit> execute) : base(execute)
{
}

public ReactiveCommand(Func<Unit, CancellationToken, ValueTask> executeAsync, AwaitOperation awaitOperation = AwaitOperation.Sequential, bool configureAwait = true, bool cancelOnCompleted = false, int maxSequential = -1)
: base(executeAsync, awaitOperation, configureAwait, cancelOnCompleted, maxSequential)
{
}

public ReactiveCommand(Observable<bool> canExecuteSource, bool initialCanExecute)
: base(canExecuteSource, initialCanExecute)
{
}
}

public static class ReactiveCommandExtensions
{
public static ReactiveCommand<T> ToReactiveCommand<T>(this Observable<bool> canExecuteSource, bool initialCanExecute = true)
Expand All @@ -375,15 +396,15 @@ public static ReactiveCommand<TInput, TOutput> ToReactiveCommand<TInput, TOutput
return command;
}

public static ReactiveCommand<Unit> ToReactiveCommand(this Observable<bool> canExecuteSource, bool initialCanExecute = true)
public static ReactiveCommand ToReactiveCommand(this Observable<bool> canExecuteSource, bool initialCanExecute = true)
{
var command = new ReactiveCommand<Unit>(canExecuteSource, initialCanExecute);
var command = new ReactiveCommand(canExecuteSource, initialCanExecute);
return command;
}

public static ReactiveCommand<Unit> ToReactiveCommand(this Observable<bool> canExecuteSource, Action<Unit> execute, bool initialCanExecute = true)
public static ReactiveCommand ToReactiveCommand(this Observable<bool> canExecuteSource, Action<Unit> execute, bool initialCanExecute = true)
{
var command = new ReactiveCommand<Unit>(canExecuteSource, initialCanExecute);
var command = new ReactiveCommand(canExecuteSource, initialCanExecute);

var subscription = command.Subscribe(execute);
command.CombineSubscription(subscription);
Expand Down

0 comments on commit 8fc498b

Please sign in to comment.