| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | |
|
| | 8 | | #nullable enable |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal static class OperationHelpers |
| | 13 | | { |
| 0 | 14 | | public static TimeSpan DefaultPollingInterval { get; } = TimeSpan.FromSeconds(1); |
| | 15 | |
|
| | 16 | | public static T GetValue<T>(ref T? value) where T: class |
| | 17 | | { |
| 0 | 18 | | if (value is null) |
| | 19 | | { |
| 0 | 20 | | throw new InvalidOperationException("The operation has not completed yet."); |
| | 21 | | } |
| | 22 | |
|
| 0 | 23 | | return value; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static T GetValue<T>(ref T? value) where T: struct |
| | 27 | | { |
| 0 | 28 | | if (value == null) |
| | 29 | | { |
| 0 | 30 | | throw new InvalidOperationException("The operation has not completed yet."); |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | return value.Value; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public static ValueTask<Response<TResult>> DefaultWaitForCompletionAsync<TResult>(this Operation<TResult> operat |
| | 37 | | where TResult : notnull |
| | 38 | | { |
| 0 | 39 | | return operation.WaitForCompletionAsync(DefaultPollingInterval, cancellationToken); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public static async ValueTask<Response<TResult>> DefaultWaitForCompletionAsync<TResult>(this Operation<TResult> |
| | 43 | | where TResult : notnull |
| | 44 | | { |
| 154 | 45 | | while (true) |
| | 46 | | { |
| 186 | 47 | | await operation.UpdateStatusAsync(cancellationToken).ConfigureAwait(false); |
| 186 | 48 | | if (operation.HasCompleted) |
| | 49 | | { |
| 32 | 50 | | return Response.FromValue(operation.Value, operation.GetRawResponse()); |
| | 51 | | } |
| | 52 | |
|
| 154 | 53 | | await Task.Delay(pollingInterval, cancellationToken).ConfigureAwait(false); |
| | 54 | | } |
| 32 | 55 | | } |
| | 56 | | } |
| | 57 | | } |