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