Skip to content

Commit

Permalink
WAF: Add AsyncDelegateCommand.ExecuteAsync method
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 20, 2023
1 parent 7b6b021 commit 37dae76
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Test.Waf.Applications
public class AsyncDelegateCommandTest
{
[TestMethod]
public void CanExecuteDuringAsyncExecute()
public async Task CanExecuteDuringAsyncExecute()
{
AssertHelper.ExpectedException<ArgumentNullException>(() => new AsyncDelegateCommand((Func<Task>)null!));

Expand All @@ -24,14 +24,16 @@ public void CanExecuteDuringAsyncExecute()
});

Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
var task = command.ExecuteAsync(null);

executeCalled = false;
Assert.IsFalse(command.CanExecute(null));
command.Execute(null);
command.Execute(null); // second call will be ignored
Assert.IsFalse(executeCalled);

Assert.IsFalse(task.IsCompleted);
tcs.SetResult(null);
await task;
Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
Assert.IsTrue(executeCalled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ private bool IsExecuting
public bool CanExecute(object? parameter) => !IsExecuting && (canExecute == null || canExecute(parameter));

/// <inheritdoc />
public async void Execute(object? parameter)
public void Execute(object? parameter) => _ = ExecuteAsync(parameter);

/// <summary>Defines the method to be called when the command is invoked.</summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
/// <returns>A task that represents an asynchronous operation.</returns>
public async Task ExecuteAsync(object? parameter)
{
if (!CanExecute(parameter)) return;

IsExecuting = true;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
[assembly: SuppressMessage("Security", "CA2109:Review visible event handlers", Justification = "<Pending>", Scope = "member", Target = "~M:System.Waf.Foundation.ObservableListViewBase`1.OnCollectionItemChanged(System.Object,System.ComponentModel.PropertyChangedEventArgs)")]
[assembly: SuppressMessage("Design", "CA1063:Implement IDisposable Correctly", Justification = "<Pending>", Scope = "member", Target = "~M:System.Waf.Presentation.Services.SettingsService.Dispose(System.Boolean)")]
[assembly: SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "<Pending>", Scope = "member", Target = "~M:System.Waf.Applications.IDelegateCommand.RaiseCanExecuteChanged")]
[assembly: SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "<Pending>", Scope = "member", Target = "~M:System.Waf.Applications.DelegateCommand.RaiseCanExecuteChanged(System.Waf.Applications.IDelegateCommand[])")]

0 comments on commit 37dae76

Please sign in to comment.