AsyncRelayCommand{T}.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.ComponentModel;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using CommunityToolkit.Mvvm.ComponentModel.__Internals;
  10. using CommunityToolkit.Mvvm.Input.Internals;
  11. #pragma warning disable CS0618, CA1001
  12. namespace CommunityToolkit.Mvvm.Input;
  13. /// <summary>
  14. /// A generic command that provides a more specific version of <see cref="AsyncRelayCommand"/>.
  15. /// </summary>
  16. /// <typeparam name="T">The type of parameter being passed as input to the callbacks.</typeparam>
  17. public sealed partial class AsyncRelayCommand<T> : IAsyncRelayCommand<T>, ICancellationAwareCommand
  18. {
  19. /// <summary>
  20. /// The <see cref="Func{TResult}"/> to invoke when <see cref="Execute(T)"/> is used.
  21. /// </summary>
  22. private readonly Func<T?, Task>? execute;
  23. private readonly Func<object?, T?, Task>? execute1;
  24. /// <summary>
  25. /// The cancelable <see cref="Func{T1,T2,TResult}"/> to invoke when <see cref="Execute(object?)"/> is used.
  26. /// </summary>
  27. private readonly Func<T?, CancellationToken, Task>? cancelableExecute;
  28. private readonly Func<object?, T?, CancellationToken, Task>? cancelableExecute1;
  29. /// <summary>
  30. /// The optional action to invoke when <see cref="CanExecute(T)"/> is used.
  31. /// </summary>
  32. private readonly Predicate<T?>? canExecute;
  33. /// <summary>
  34. /// The options being set for the current command.
  35. /// </summary>
  36. private readonly AsyncRelayCommandOptions options;
  37. /// <summary>
  38. /// The <see cref="CancellationTokenSource"/> instance to use to cancel <see cref="cancelableExecute"/>.
  39. /// </summary>
  40. private CancellationTokenSource? cancellationTokenSource;
  41. /// <inheritdoc/>
  42. public event PropertyChangedEventHandler? PropertyChanged;
  43. /// <inheritdoc/>
  44. public event EventHandler? CanExecuteChanged;
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  47. /// </summary>
  48. /// <param name="execute">The execution logic.</param>
  49. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  50. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
  51. public AsyncRelayCommand(Func<T?, Task> execute)
  52. {
  53. ArgumentNullException.ThrowIfNull(execute);
  54. this.execute = execute;
  55. }
  56. public AsyncRelayCommand(Func<object?, T?, Task> execute)
  57. {
  58. ArgumentNullException.ThrowIfNull(execute);
  59. this.execute1 = execute;
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  63. /// </summary>
  64. /// <param name="execute">The execution logic.</param>
  65. /// <param name="options">The options to use to configure the async command.</param>
  66. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  67. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> is <see langword="null"/>.</exception>
  68. public AsyncRelayCommand(Func<T?, Task> execute, AsyncRelayCommandOptions options)
  69. {
  70. ArgumentNullException.ThrowIfNull(execute);
  71. this.execute = execute;
  72. this.options = options;
  73. }
  74. public AsyncRelayCommand(Func<object?, T?, Task> execute, AsyncRelayCommandOptions options)
  75. {
  76. ArgumentNullException.ThrowIfNull(execute);
  77. this.execute1 = execute;
  78. this.options = options;
  79. }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  82. /// </summary>
  83. /// <param name="cancelableExecute">The cancelable execution logic.</param>
  84. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  85. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> is <see langword="null"/>.</exception>
  86. public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute)
  87. {
  88. ArgumentNullException.ThrowIfNull(cancelableExecute);
  89. this.cancelableExecute = cancelableExecute;
  90. }
  91. public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute)
  92. {
  93. ArgumentNullException.ThrowIfNull(cancelableExecute);
  94. this.cancelableExecute1 = cancelableExecute;
  95. }
  96. /// <summary>
  97. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  98. /// </summary>
  99. /// <param name="cancelableExecute">The cancelable execution logic.</param>
  100. /// <param name="options">The options to use to configure the async command.</param>
  101. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  102. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> is <see langword="null"/>.</exception>
  103. public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)
  104. {
  105. ArgumentNullException.ThrowIfNull(cancelableExecute);
  106. this.cancelableExecute = cancelableExecute;
  107. this.options = options;
  108. }
  109. public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)
  110. {
  111. ArgumentNullException.ThrowIfNull(cancelableExecute);
  112. this.cancelableExecute1 = cancelableExecute;
  113. this.options = options;
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  117. /// </summary>
  118. /// <param name="execute">The execution logic.</param>
  119. /// <param name="canExecute">The execution status logic.</param>
  120. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  121. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
  122. public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute)
  123. {
  124. ArgumentNullException.ThrowIfNull(execute);
  125. ArgumentNullException.ThrowIfNull(canExecute);
  126. this.execute = execute;
  127. this.canExecute = canExecute;
  128. }
  129. public AsyncRelayCommand(Func<object?, T?, Task> execute, Predicate<T?> canExecute)
  130. {
  131. ArgumentNullException.ThrowIfNull(execute);
  132. ArgumentNullException.ThrowIfNull(canExecute);
  133. this.execute1 = execute;
  134. this.canExecute = canExecute;
  135. }
  136. /// <summary>
  137. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  138. /// </summary>
  139. /// <param name="execute">The execution logic.</param>
  140. /// <param name="canExecute">The execution status logic.</param>
  141. /// <param name="options">The options to use to configure the async command.</param>
  142. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  143. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="execute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
  144. public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
  145. {
  146. ArgumentNullException.ThrowIfNull(execute);
  147. ArgumentNullException.ThrowIfNull(canExecute);
  148. this.execute = execute;
  149. this.canExecute = canExecute;
  150. this.options = options;
  151. }
  152. public AsyncRelayCommand(Func<object?, T?, Task> execute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
  153. {
  154. ArgumentNullException.ThrowIfNull(execute);
  155. ArgumentNullException.ThrowIfNull(canExecute);
  156. this.execute1 = execute;
  157. this.canExecute = canExecute;
  158. this.options = options;
  159. }
  160. /// <summary>
  161. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  162. /// </summary>
  163. /// <param name="cancelableExecute">The cancelable execution logic.</param>
  164. /// <param name="canExecute">The execution status logic.</param>
  165. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  166. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
  167. public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute)
  168. {
  169. ArgumentNullException.ThrowIfNull(cancelableExecute);
  170. ArgumentNullException.ThrowIfNull(canExecute);
  171. this.cancelableExecute = cancelableExecute;
  172. this.canExecute = canExecute;
  173. }
  174. public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute)
  175. {
  176. ArgumentNullException.ThrowIfNull(cancelableExecute);
  177. ArgumentNullException.ThrowIfNull(canExecute);
  178. this.cancelableExecute1 = cancelableExecute;
  179. this.canExecute = canExecute;
  180. }
  181. /// <summary>
  182. /// Initializes a new instance of the <see cref="AsyncRelayCommand{T}"/> class.
  183. /// </summary>
  184. /// <param name="cancelableExecute">The cancelable execution logic.</param>
  185. /// <param name="canExecute">The execution status logic.</param>
  186. /// <param name="options">The options to use to configure the async command.</param>
  187. /// <remarks>See notes in <see cref="RelayCommand{T}(Action{T})"/>.</remarks>
  188. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="cancelableExecute"/> or <paramref name="canExecute"/> are <see langword="null"/>.</exception>
  189. public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
  190. {
  191. ArgumentNullException.ThrowIfNull(cancelableExecute);
  192. ArgumentNullException.ThrowIfNull(canExecute);
  193. this.cancelableExecute = cancelableExecute;
  194. this.canExecute = canExecute;
  195. this.options = options;
  196. }
  197. public AsyncRelayCommand(Func<object?, T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
  198. {
  199. ArgumentNullException.ThrowIfNull(cancelableExecute);
  200. ArgumentNullException.ThrowIfNull(canExecute);
  201. this.cancelableExecute1 = cancelableExecute;
  202. this.canExecute = canExecute;
  203. this.options = options;
  204. }
  205. private Task? executionTask;
  206. /// <inheritdoc/>
  207. public Task? ExecutionTask
  208. {
  209. get => this.executionTask;
  210. private set
  211. {
  212. if (ReferenceEquals(this.executionTask, value))
  213. {
  214. return;
  215. }
  216. this.executionTask = value;
  217. PropertyChanged?.Invoke(this, AsyncRelayCommand.ExecutionTaskChangedEventArgs);
  218. PropertyChanged?.Invoke(this, AsyncRelayCommand.IsRunningChangedEventArgs);
  219. bool isAlreadyCompletedOrNull = value?.IsCompleted ?? true;
  220. if (this.cancellationTokenSource is not null)
  221. {
  222. PropertyChanged?.Invoke(this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
  223. PropertyChanged?.Invoke(this, AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
  224. }
  225. if (isAlreadyCompletedOrNull)
  226. {
  227. return;
  228. }
  229. static async void MonitorTask(AsyncRelayCommand<T> @this, Task task)
  230. {
  231. await task.GetAwaitableWithoutEndValidation();
  232. if (ReferenceEquals(@this.executionTask, task))
  233. {
  234. @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.ExecutionTaskChangedEventArgs);
  235. @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.IsRunningChangedEventArgs);
  236. if (@this.cancellationTokenSource is not null)
  237. {
  238. @this.PropertyChanged?.Invoke(@this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
  239. }
  240. if ((@this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) == 0)
  241. {
  242. @this.CanExecuteChanged?.Invoke(@this, EventArgs.Empty);
  243. }
  244. }
  245. }
  246. MonitorTask(this, value!);
  247. }
  248. }
  249. /// <inheritdoc/>
  250. public bool CanBeCanceled => IsRunning && this.cancellationTokenSource is { IsCancellationRequested: false };
  251. /// <inheritdoc/>
  252. public bool IsCancellationRequested => this.cancellationTokenSource is { IsCancellationRequested: true };
  253. /// <inheritdoc/>
  254. public bool IsRunning => ExecutionTask is { IsCompleted: false };
  255. /// <inheritdoc/>
  256. bool ICancellationAwareCommand.IsCancellationSupported => this.execute is null;
  257. /// <inheritdoc/>
  258. public void NotifyCanExecuteChanged()
  259. {
  260. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  261. }
  262. /// <inheritdoc/>
  263. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  264. public bool CanExecute(T? parameter)
  265. {
  266. bool canExecute = this.canExecute?.Invoke(parameter) != false;
  267. return canExecute && ((this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) != 0 || ExecutionTask is not { IsCompleted: false });
  268. }
  269. /// <inheritdoc/>
  270. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  271. public bool CanExecute(object? parameter)
  272. {
  273. // Special case, see RelayCommand<T>.CanExecute(object?) for more info
  274. if (parameter is null && default(T) is not null)
  275. {
  276. return false;
  277. }
  278. if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
  279. {
  280. RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
  281. }
  282. return CanExecute(result);
  283. }
  284. /// <inheritdoc/>
  285. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  286. public void Execute(T? parameter)
  287. {
  288. Task executionTask = ExecuteAsync(null, parameter);
  289. if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
  290. {
  291. AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
  292. }
  293. }
  294. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  295. public void Execute(object? sender, T? parameter)
  296. {
  297. Task executionTask = ExecuteAsync(sender, parameter);
  298. if ((this.options & AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler) == 0)
  299. {
  300. AsyncRelayCommand.AwaitAndThrowIfFailed(executionTask);
  301. }
  302. }
  303. /// <inheritdoc/>
  304. public void Execute(object? parameter)
  305. {
  306. if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
  307. {
  308. RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
  309. }
  310. Execute(result);
  311. }
  312. public void Execute(object? sender, object? parameter)
  313. {
  314. if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
  315. {
  316. RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
  317. }
  318. Execute(sender, result);
  319. }
  320. public Task ExecuteAsync(T? parameter)
  321. {
  322. return ExecuteAsync(null, parameter);
  323. }
  324. /// <inheritdoc/>
  325. public Task ExecuteAsync(object? sender, T? parameter)
  326. {
  327. Task executionTask;
  328. if (this.execute is not null)
  329. {
  330. // Non cancelable command delegate
  331. executionTask = ExecutionTask = this.execute(parameter);
  332. }
  333. else if(this.execute1 is not null )
  334. {
  335. executionTask = ExecutionTask = this.execute1(sender, parameter);
  336. }
  337. else
  338. {
  339. // Cancel the previous operation, if one is pending
  340. this.cancellationTokenSource?.Cancel();
  341. CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
  342. // Invoke the cancelable command delegate with a new linked token
  343. executionTask = ExecutionTask =this.cancelableExecute1 !=null ? this.cancelableExecute1!(sender,parameter,cancellationTokenSource.Token): this.cancelableExecute!(parameter, cancellationTokenSource.Token);
  344. }
  345. // If concurrent executions are disabled, notify the can execute change as well
  346. if ((this.options & AsyncRelayCommandOptions.AllowConcurrentExecutions) == 0)
  347. {
  348. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
  349. }
  350. return executionTask;
  351. }
  352. /// <inheritdoc/>
  353. public Task ExecuteAsync(object? sender, object? parameter)
  354. {
  355. if (!RelayCommand<T>.TryGetCommandArgument(parameter, out T? result))
  356. {
  357. RelayCommand<T>.ThrowArgumentExceptionForInvalidCommandArgument(parameter);
  358. }
  359. return ExecuteAsync(sender, result);
  360. }
  361. public Task ExecuteAsync(object? parameter)
  362. {
  363. return ExecuteAsync(null,parameter);
  364. }
  365. /// <inheritdoc/>
  366. public void Cancel()
  367. {
  368. if (this.cancellationTokenSource is CancellationTokenSource { IsCancellationRequested: false } cancellationTokenSource)
  369. {
  370. cancellationTokenSource.Cancel();
  371. PropertyChanged?.Invoke(this, AsyncRelayCommand.CanBeCanceledChangedEventArgs);
  372. PropertyChanged?.Invoke(this, AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
  373. }
  374. }
  375. }