123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Threading.Tasks;
- using CommunityToolkit.Mvvm.ComponentModel.__Internals;
- using CommunityToolkit.Mvvm.Input.Internals;
- #pragma warning disable CS0618, CA1001
- namespace CommunityToolkit.Mvvm.Input;
- /// <summary>
- /// A generic command that provides a more specific version of <see cref="AsyncRelayCommand"/>.
- /// </summary>
- /// <typeparam name="T">The type of parameter being passed as input to the callbacks.</typeparam>
- public sealed partial class AsyncRelayCommand<T> : IAsyncRelayCommand<T>, ICancellationAwareCommand
- {
- /// <summary>
- /// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute(T)"/> is used.
- /// </summary>
- private readonly Func<T?, Task>? execute;
- private readonly Func<object?, T?, Task>? execute1;
- /// <summary>
- /// The cancelable <see cref="Func{T1,T2,TResult}"/> to invoke when <see cref="Execute(object?)"/> is used.
- /// </summary>
- private readonly Func<T?, CancellationToken, Task>? cancelableExecute;
- private readonly Func<object?, T?, CancellationToken, Task>? cancelableExecute1;
- /// <summary>
- /// The optional action to invoke when <see cref="CanExecute(T)"/> is used.
- /// </summary>
- private readonly Predicate<T?>? canExecute;
- /// <summary>
- /// The options being set for the current command.
- /// </summary>
- private readonly AsyncRelayCommandOptions options;
- /// <summary>
- /// The <see cref="CancellationTokenSource"/> instance to use to cancel <see cref="cancelableExecute"/>.
- /// </summary>
- private CancellationTokenSource? cancellationTokenSource;
- /// <inheritdoc/>
- public event PropertyChangedEventHandler? PropertyChanged;
- /// <inheritdoc/>
- public event EventHandler? CanExecuteChanged;
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, Task> execute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute = execute;
- }
- public AsyncRelayCommand(Func<object?, T?, Task> execute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute1 = execute;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <param name="options">The options to use to configure the async command.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, Task> execute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute = execute;
- this.options = options;
- }
- public AsyncRelayCommand(Func<object?, T?, Task> execute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(execute);
- this.execute1 = execute;
- this.options = options;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="cancelableExecute">The cancelable execution logic.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> is <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- this.cancelableExecute = cancelableExecute;
- }
- public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- this.cancelableExecute1 = cancelableExecute;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="cancelableExecute">The cancelable execution logic.</param>
- /// <param name="options">The options to use to configure the async command.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> is <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- this.cancelableExecute = cancelableExecute;
- this.options = options;
- }
- public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- this.cancelableExecute1 = cancelableExecute;
- this.options = options;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <param name="canExecute">The execution status logic.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute = execute;
- this.canExecute = canExecute;
- }
- public AsyncRelayCommand(Func<object?, T?, Task> execute, Predicate<T?> canExecute)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute1 = execute;
- this.canExecute = canExecute;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="execute">The execution logic.</param>
- /// <param name="canExecute">The execution status logic.</param>
- /// <param name="options">The options to use to configure the async command.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute = execute;
- this.canExecute = canExecute;
- this.options = options;
- }
- public AsyncRelayCommand(Func<object?, T?, Task> execute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(execute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.execute1 = execute;
- this.canExecute = canExecute;
- this.options = options;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="cancelableExecute">The cancelable execution logic.</param>
- /// <param name="canExecute">The execution status logic.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.cancelableExecute = cancelableExecute;
- this.canExecute = canExecute;
- }
- public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.cancelableExecute1 = cancelableExecute;
- this.canExecute = canExecute;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
- /// </summary>
- /// <param name="cancelableExecute">The cancelable execution logic.</param>
- /// <param name="canExecute">The execution status logic.</param>
- /// <param name="options">The options to use to configure the async command.</param>
- /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
- /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
- public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.cancelableExecute = cancelableExecute;
- this.canExecute = canExecute;
- this.options = options;
- }
- public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
- {
- ArgumentNullException.ThrowIfNull(cancelableExecute);
- ArgumentNullException.ThrowIfNull(canExecute);
- this.cancelableExecute1 = cancelableExecute;
- this.canExecute = canExecute;
- this.options = options;
- }
- private Task? executionTask;
- /// <inheritdoc/>
- public Task? ExecutionTask
- {
- get => this.executionTask;
- private set
- {
- if (ReferenceEquals(this.executionTask, value))
- {
- return;
- }
- this.executionTask = value;
- PropertyChanged?.Invoke(this, AsyncRelayCommand.ExecutionTaskChangedEventArgs);
- PropertyChanged?.Invoke(this, AsyncRelayCommand.IsRunningChangedEventArgs);
- bool isAlreadyCompletedOrNull = value?.IsCompleted ?? true;
- if (this.cancellationTokenSource is not null)
- {
- PropertyChanged?.Invoke(this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
- PropertyChanged?.Invoke(this, AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
- }
- if (isAlreadyCompletedOrNull)
- {
- return;
- }
- static async void MonitorTask(AsyncRelayCommand<T> @this, Task task)
- {
- await task.GetAwaitableWithoutEndValidation();
- if (ReferenceEquals(@this.executionTask, task))
- {
- @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.ExecutionTaskChangedEventArgs);
- @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.IsRunningChangedEventArgs);
-
- if (@this.cancellationTokenSource is not null)
- {
- @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
- }
- if ((@this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) == 0)
- {
- @this.CanExecuteChanged?.Invoke(@this, EventArgs.Empty);
- }
- }
- }
- MonitorTask(this, value!);
- }
- }
- /// <inheritdoc/>
- public bool CanBeCanceled => IsRunning && this.cancellationTokenSource is { IsCancellationRequested: false };
- /// <inheritdoc/>
- public bool IsCancellationRequested => this.cancellationTokenSource is { IsCancellationRequested: true };
- /// <inheritdoc/>
- public bool IsRunning => ExecutionTask is { IsCompleted: false };
- /// <inheritdoc/>
- bool ICancellationAwareCommand.IsCancellationSupported => this.execute is null;
- /// <inheritdoc/>
- public void NotifyCanExecuteChanged()
- {
- CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- /// <inheritdoc/>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool CanExecute(T? parameter)
- {
- bool canExecute = this.canExecute?.Invoke(parameter) != false;
- return canExecute && ((this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) != 0 || ExecutionTask is not { IsCompleted: false });
- }
- /// <inheritdoc/>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool CanExecute(object? parameter)
- {
- // Special case, see RelayCommand<T>.CanExecute(object?) for more info
- if (parameter is null && default(T) is not null)
- {
- return false;
- }
- if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
- {
- RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
- }
- return CanExecute(result);
- }
- /// <inheritdoc/>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Execute(T? parameter)
- {
- Task executionTask = ExecuteAsync(null, parameter);
- if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
- {
- AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Execute(object? sender, T? parameter)
- {
- Task executionTask = ExecuteAsync(sender, parameter);
- if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
- {
- AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
- }
- }
- /// <inheritdoc/>
- public void Execute(object? parameter)
- {
- if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
- {
- RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
- }
- Execute(result);
- }
- public void Execute(object? sender, object? parameter)
- {
- if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
- {
- RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
- }
- Execute(sender, result);
- }
- public Task ExecuteAsync(T? parameter)
- {
- return ExecuteAsync(null, parameter);
- }
- /// <inheritdoc/>
- public Task ExecuteAsync(object? sender, T? parameter)
- {
- Task executionTask;
- if (this.execute is not null)
- {
- // Non cancelable command delegate
- executionTask = ExecutionTask = this.execute(parameter);
- }
- else if(this.execute1 is not null )
- {
- executionTask = ExecutionTask = this.execute1(sender, parameter);
- }
- else
- {
- // Cancel the previous operation, if one is pending
- this.cancellationTokenSource?.Cancel();
- CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
- // Invoke the cancelable command delegate with a new linked token
- executionTask = ExecutionTask =this.cancelableExecute1 !=null ? this.cancelableExecute1!(sender,parameter,cancellationTokenSource.Token): this.cancelableExecute!(parameter, cancellationTokenSource.Token);
- }
- // If concurrent executions are disabled, notify the can execute change as well
- if ((this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) == 0)
- {
- CanExecuteChanged?.Invoke(this, EventArgs.Empty);
- }
- return executionTask;
- }
- /// <inheritdoc/>
- public Task ExecuteAsync(object? sender, object? parameter)
- {
- if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
- {
- RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
- }
- return ExecuteAsync(sender, result);
- }
- public Task ExecuteAsync(object? parameter)
- {
- return ExecuteAsync(null,parameter);
- }
- /// <inheritdoc/>
- public void Cancel()
- {
- if (this.cancellationTokenSource is CancellationTokenSource { IsCancellationRequested: false } cancellationTokenSource)
- {
- cancellationTokenSource.Cancel();
- PropertyChanged?.Invoke(this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
- PropertyChanged?.Invoke(this, AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
- }
- }
- }
|