| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Azure |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A collection of values that may take multiple service requests to |
| | 13 | | /// iterate over. |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="T">The type of the values.</typeparam> |
| | 16 | | /// <example> |
| | 17 | | /// Example of enumerating an AsyncPageable using the <c> async foreach </c> loop: |
| | 18 | | /// <code snippet="Snippet:AsyncPageable"> |
| | 19 | | /// // call a service method, which returns AsyncPageable<T> |
| | 20 | | /// AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync(); |
| | 21 | | /// |
| | 22 | | /// await foreach (SecretProperties secretProperties in allSecretProperties) |
| | 23 | | /// { |
| | 24 | | /// Console.WriteLine(secretProperties.Name); |
| | 25 | | /// } |
| | 26 | | /// </code> |
| | 27 | | /// or using a while loop: |
| | 28 | | /// <code snippet="Snippet:AsyncPageableLoop"> |
| | 29 | | /// // call a service method, which returns AsyncPageable<T> |
| | 30 | | /// AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync(); |
| | 31 | | /// |
| | 32 | | /// IAsyncEnumerator<SecretProperties> enumerator = allSecretProperties.GetAsyncEnumerator(); |
| | 33 | | /// try |
| | 34 | | /// { |
| | 35 | | /// while (await enumerator.MoveNextAsync()) |
| | 36 | | /// { |
| | 37 | | /// SecretProperties secretProperties = enumerator.Current; |
| | 38 | | /// Console.WriteLine(secretProperties.Name); |
| | 39 | | /// } |
| | 40 | | /// } |
| | 41 | | /// finally |
| | 42 | | /// { |
| | 43 | | /// await enumerator.DisposeAsync(); |
| | 44 | | /// } |
| | 45 | | /// </code> |
| | 46 | | /// </example> |
| | 47 | | public abstract class AsyncPageable<T> : IAsyncEnumerable<T> where T : notnull |
| | 48 | | { |
| | 49 | | /// <summary> |
| | 50 | | /// Gets a <see cref="CancellationToken"/> used for requests made while |
| | 51 | | /// enumerating asynchronously. |
| | 52 | | /// </summary> |
| 0 | 53 | | protected virtual CancellationToken CancellationToken { get; } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Initializes a new instance of the <see cref="AsyncPageable{T}"/> |
| | 57 | | /// class for mocking. |
| | 58 | | /// </summary> |
| 0 | 59 | | protected AsyncPageable() => |
| 0 | 60 | | CancellationToken = CancellationToken.None; |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Initializes a new instance of the <see cref="AsyncPageable{T}"/> |
| | 64 | | /// class. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="cancellationToken"> |
| | 67 | | /// The <see cref="CancellationToken"/> used for requests made while |
| | 68 | | /// enumerating asynchronously. |
| | 69 | | /// </param> |
| 0 | 70 | | protected AsyncPageable(CancellationToken cancellationToken) => |
| 0 | 71 | | CancellationToken = cancellationToken; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Enumerate the values a <see cref="Page{T}"/> at a time. This may |
| | 75 | | /// make multiple service requests. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="continuationToken"> |
| | 78 | | /// A continuation token indicating where to resume paging or null to |
| | 79 | | /// begin paging from the beginning. |
| | 80 | | /// </param> |
| | 81 | | /// <param name="pageSizeHint"> |
| | 82 | | /// The size of <see cref="Page{T}"/>s that should be requested (from |
| | 83 | | /// service operations that support it). |
| | 84 | | /// </param> |
| | 85 | | /// <returns> |
| | 86 | | /// An async sequence of <see cref="Page{T}"/>s. |
| | 87 | | /// </returns> |
| | 88 | | public abstract IAsyncEnumerable<Page<T>> AsPages( |
| | 89 | | string? continuationToken = default, |
| | 90 | | int? pageSizeHint = default); |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Enumerate the values in the collection asynchronously. This may |
| | 94 | | /// make multiple service requests. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="cancellationToken"> |
| | 97 | | /// The <see cref="CancellationToken"/> used for requests made while |
| | 98 | | /// enumerating asynchronously. |
| | 99 | | /// </param> |
| | 100 | | /// <returns>An async sequence of values.</returns> |
| | 101 | | public virtual async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) |
| | 102 | | { |
| 0 | 103 | | await foreach (Page<T> page in AsPages().ConfigureAwait(false).WithCancellation(cancellationToken)) |
| | 104 | | { |
| 0 | 105 | | foreach (T value in page.Values) |
| | 106 | | { |
| 0 | 107 | | yield return value; |
| | 108 | | } |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Creates a string representation of an <see cref="AsyncPageable{T}"/>. |
| | 114 | | /// </summary> |
| | 115 | | /// <returns> |
| | 116 | | /// A string representation of an <see cref="AsyncPageable{T}"/>. |
| | 117 | | /// </returns> |
| | 118 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 119 | | public override string ToString() => base.ToString(); |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Check if two <see cref="AsyncPageable{T}"/> instances are equal. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="obj">The instance to compare to.</param> |
| | 125 | | /// <returns>True if they're equal, false otherwise.</returns> |
| | 126 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 127 | | public override bool Equals(object? obj) => base.Equals(obj); |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Get a hash code for the <see cref="AsyncPageable{T}"/>. |
| | 131 | | /// </summary> |
| | 132 | | /// <returns>Hash code for the <see cref="Page{T}"/>.</returns> |
| | 133 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 134 | | public override int GetHashCode() => base.GetHashCode(); |
| | 135 | | } |
| | 136 | | } |