__TaskExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Tasks;
  8. namespace CommunityToolkit.Mvvm.ComponentModel.__Internals;
  9. /// <summary>
  10. /// An internal helper used to support <see cref="ObservableObject"/> and generated code from its template.
  11. /// This type is not intended to be used directly by user code.
  12. /// </summary>
  13. [EditorBrowsable(EditorBrowsableState.Never)]
  14. [Obsolete("This type is not intended to be used directly by user code")]
  15. public static class __TaskExtensions
  16. {
  17. /// <summary>
  18. /// Gets an awaitable object that skips end validation.
  19. /// </summary>
  20. /// <param name="task">The input <see cref="Task"/> to get the awaitable for.</param>
  21. /// <returns>A <see cref="TaskAwaitableWithoutEndValidation"/> object wrapping <paramref name="task"/>.</returns>
  22. [EditorBrowsable(EditorBrowsableState.Never)]
  23. [Obsolete("This method is not intended to be called directly by user code")]
  24. public static TaskAwaitableWithoutEndValidation GetAwaitableWithoutEndValidation(this Task task)
  25. {
  26. return new(task);
  27. }
  28. /// <summary>
  29. /// A custom task awaitable object that skips end validation.
  30. /// </summary>
  31. [EditorBrowsable(EditorBrowsableState.Never)]
  32. [Obsolete("This type is not intended to be called directly by user code")]
  33. public readonly struct TaskAwaitableWithoutEndValidation
  34. {
  35. /// <summary>
  36. /// The wrapped <see cref="Task"/> instance to create an awaiter for.
  37. /// </summary>
  38. private readonly Task task;
  39. /// <summary>
  40. /// Creates a new <see cref="TaskAwaitableWithoutEndValidation"/> instance with the specified parameters.
  41. /// </summary>
  42. /// <param name="task">The wrapped <see cref="Task"/> instance to create an awaiter for.</param>
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public TaskAwaitableWithoutEndValidation(Task task)
  45. {
  46. this.task = task;
  47. }
  48. /// <summary>
  49. /// Gets an <see cref="Awaiter"/> instance for the current underlying task.
  50. /// </summary>
  51. /// <returns>An <see cref="Awaiter"/> instance for the current underlying task.</returns>
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public Awaiter GetAwaiter()
  54. {
  55. return new(this.task);
  56. }
  57. /// <summary>
  58. /// An awaiter object for <see cref="TaskAwaitableWithoutEndValidation"/>.
  59. /// </summary>
  60. public readonly struct Awaiter : ICriticalNotifyCompletion
  61. {
  62. /// <summary>
  63. /// The underlying <see cref="TaskAwaiter"/> instance.
  64. /// </summary>
  65. private readonly TaskAwaiter taskAwaiter;
  66. /// <summary>
  67. /// Creates a new <see cref="Awaiter"/> instance with the specified parameters.
  68. /// </summary>
  69. /// <param name="task">The wrapped <see cref="Task"/> instance to create an awaiter for.</param>
  70. public Awaiter(Task task)
  71. {
  72. this.taskAwaiter = task.GetAwaiter();
  73. }
  74. /// <summary>
  75. /// Gets whether the operation has completed or not.
  76. /// </summary>
  77. /// <remarks>This property is intended for compiler user rather than use directly in code.</remarks>
  78. public bool IsCompleted
  79. {
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. get => this.taskAwaiter.IsCompleted;
  82. }
  83. /// <summary>
  84. /// Ends the await operation.
  85. /// </summary>
  86. /// <remarks>This method is intended for compiler user rather than use directly in code.</remarks>
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public void GetResult()
  89. {
  90. }
  91. /// <inheritdoc/>
  92. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  93. public void OnCompleted(Action continuation)
  94. {
  95. this.taskAwaiter.OnCompleted(continuation);
  96. }
  97. /// <inheritdoc/>
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. public void UnsafeOnCompleted(Action continuation)
  100. {
  101. this.taskAwaiter.UnsafeOnCompleted(continuation);
  102. }
  103. }
  104. }
  105. }