mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-06-01 20:34:06 +02:00
Implement FlowExceptionsToTaskScheduler option
This commit is contained in:
parent
79204ab489
commit
dba9c4163e
CommunityToolkit.Mvvm/Input
@ -279,7 +279,14 @@ public bool CanExecute(object? parameter)
|
||||
/// <inheritdoc/>
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
_ = ExecuteAsync(parameter);
|
||||
Task executionTask = ExecuteAsync(parameter);
|
||||
|
||||
// If exceptions shouldn't flow to the task scheduler, await the resulting task. This is
|
||||
// delegated to a separate method to keep this one more compact in case the option is set.
|
||||
if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
|
||||
{
|
||||
AwaitAndThrowIfFailed(executionTask);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -323,4 +330,13 @@ public void Cancel()
|
||||
PropertyChanged?.Invoke(this, IsCancellationRequestedChangedEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Awaits an input <see cref="Task"/> and throws an exception on the calling context, if the task fails.
|
||||
/// </summary>
|
||||
/// <param name="executionTask">The input <see cref="Task"/> instance to await.</param>
|
||||
internal static async void AwaitAndThrowIfFailed(Task executionTask)
|
||||
{
|
||||
await executionTask;
|
||||
}
|
||||
}
|
||||
|
@ -275,13 +275,18 @@ public bool CanExecute(object? parameter)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Execute(T? parameter)
|
||||
{
|
||||
_ = ExecuteAsync(parameter);
|
||||
Task executionTask = ExecuteAsync(parameter);
|
||||
|
||||
if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
|
||||
{
|
||||
AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
_ = ExecuteAsync((T?)parameter);
|
||||
Execute((T?)parameter);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
Loading…
Reference in New Issue
Block a user