AsyncRelayCommandOptions.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. namespace CommunityToolkit.Mvvm.Input;
  6. /// <summary>
  7. /// Options to customize the behavior of <see cref="AsyncRelayCommand"/> and <see cref="AsyncRelayCommand{T}"/> instances.
  8. /// </summary>
  9. [Flags]
  10. public enum AsyncRelayCommandOptions
  11. {
  12. /// <summary>
  13. /// No option is specified. The <see cref="AsyncRelayCommand"/> and <see cref="AsyncRelayCommand{T}"/> types will use their default behavior:
  14. /// <list type="bullet">
  15. /// <item>Concurrent execution is disallowed: a command is disabled if there is a pending asynchronous execution running.</item>
  16. /// <item>
  17. /// <para>
  18. /// Exceptions are thrown on the calling context: calling <see cref="AsyncRelayCommand.Execute(object?)"/> will await the
  19. /// returned <see cref="System.Threading.Tasks.Task"/> for the operation, and propagate the exception on the calling context.
  20. /// </para>
  21. /// <para>This behavior is consistent with synchronous commands, where exceptions in <see cref="RelayCommand.Execute(object?)"/> behave the same.</para>
  22. /// </item>
  23. /// </list>
  24. /// </summary>
  25. None = 0,
  26. /// <summary>
  27. /// <para>Concurrent executions are allowed. This option makes it so that the same command can be invoked concurrently multiple times.</para>
  28. /// <para>
  29. /// Note that additional considerations should be taken into account in this case:
  30. /// <list type="bullet">
  31. /// <item>If the command supports cancellation, previous invocations will automatically be canceled if a new one is started.</item>
  32. /// <item>The <see cref="AsyncRelayCommand.ExecutionTask"/> property will always represent the operation that was started last.</item>
  33. /// </list>
  34. /// </para>
  35. /// </summary>
  36. AllowConcurrentExecutions = 1 << 0,
  37. /// <summary>
  38. /// <para>Exceptions are not thrown on the calling context, and are propagated to <see cref="System.Threading.Tasks.TaskScheduler.UnobservedTaskException"/> instead.</para>
  39. /// <para>
  40. /// This affects how calls to <see cref="AsyncRelayCommand.Execute(object?)"/> behave. When this option is used, if an operation fails, that exception will not
  41. /// be rethrown on the calling context (as it is not awaited there). Instead, it will flow to <see cref="System.Threading.Tasks.TaskScheduler.UnobservedTaskException"/>.
  42. /// </para>
  43. /// <para>
  44. /// This option enables more advanced scenarios, where the <see cref="AsyncRelayCommand.ExecutionTask"/> property can be used to inspect the state of an operation
  45. /// that was queued. That is, even if the operation failed or was canceled, the details of that can be retrieved at a later time by accessing this property.
  46. /// </para>
  47. /// </summary>
  48. FlowExceptionsToTaskScheduler = 1 << 1
  49. }