Skip to content

Commit

Permalink
Merge pull request #175 from michaelstonis/feature/async-reactive-com…
Browse files Browse the repository at this point in the history
…mand

adds support for asynchronous reactive command subscriptions
  • Loading branch information
neuecc authored Mar 22, 2024
2 parents 96c13c7 + 02df97e commit a8d433d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/R3/ReactiveCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Windows.Input; // for XAML binding

namespace R3;
Expand Down Expand Up @@ -26,6 +26,13 @@ public ReactiveCommand(Action<T> execute)
this.subscription = this.Subscribe(execute);
}

public ReactiveCommand(Func<T, CancellationToken, ValueTask> executeAsync, AwaitOperation awaitOperation = AwaitOperation.Sequential, bool configureAwait = true, bool cancelOnCompleted = true, int maxSequential = -1)
{
this.list = new FreeListCore<Subscription>(this);
this.canExecute = true;
this.subscription = this.SubscribeAwait(executeAsync, awaitOperation, configureAwait, cancelOnCompleted, maxSequential);
}

public ReactiveCommand(Observable<bool> canExecuteSource, bool initialCanExecute)
{
this.list = new FreeListCore<Subscription>(this);
Expand Down Expand Up @@ -186,4 +193,18 @@ public static ReactiveCommand<Unit> ToReactiveCommand(this Observable<bool> canE

return command;
}

public static ReactiveCommand<T> ToReactiveCommand<T>(
this Observable<bool> canExecuteSource, Func<T, CancellationToken, ValueTask> executeAsync,
bool initialCanExecute = true,
AwaitOperation awaitOperation = AwaitOperation.Sequential, bool configureAwait = true,
bool cancelOnCompleted = true, int maxSequential = -1)
{
var command = new ReactiveCommand<T>(canExecuteSource, initialCanExecute);

var subscription = command.SubscribeAwait(async (x, ct) => await executeAsync(x, ct), awaitOperation, configureAwait, cancelOnCompleted, maxSequential);
command.CombineSubscription(subscription);

return command;
}
}

0 comments on commit a8d433d

Please sign in to comment.