/
AsyncPageable.cs
180 lines (166 loc) · 6.95 KB
/
AsyncPageable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
namespace Azure
{
/// <summary>
/// A collection of values that may take multiple service requests to
/// iterate over.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <example>
/// Example of enumerating an AsyncPageable using the <c> async foreach </c> loop:
/// <code snippet="Snippet:AsyncPageable" language="csharp">
/// // call a service method, which returns AsyncPageable<T>
/// AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
///
/// await foreach (SecretProperties secretProperties in allSecretProperties)
/// {
/// Console.WriteLine(secretProperties.Name);
/// }
/// </code>
/// or using a while loop:
/// <code snippet="Snippet:AsyncPageableLoop" language="csharp">
/// // call a service method, which returns AsyncPageable<T>
/// AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
///
/// IAsyncEnumerator<SecretProperties> enumerator = allSecretProperties.GetAsyncEnumerator();
/// try
/// {
/// while (await enumerator.MoveNextAsync())
/// {
/// SecretProperties secretProperties = enumerator.Current;
/// Console.WriteLine(secretProperties.Name);
/// }
/// }
/// finally
/// {
/// await enumerator.DisposeAsync();
/// }
/// </code>
/// </example>
public abstract class AsyncPageable<T> : IAsyncEnumerable<T> where T : notnull
{
/// <summary>
/// Gets a <see cref="CancellationToken"/> used for requests made while
/// enumerating asynchronously.
/// </summary>
protected virtual CancellationToken CancellationToken { get; }
/// <summary>
/// Initializes a new instance of the <see cref="AsyncPageable{T}"/>
/// class for mocking.
/// </summary>
protected AsyncPageable() =>
CancellationToken = CancellationToken.None;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncPageable{T}"/>
/// class.
/// </summary>
/// <param name="cancellationToken">
/// The <see cref="CancellationToken"/> used for requests made while
/// enumerating asynchronously.
/// </param>
protected AsyncPageable(CancellationToken cancellationToken) =>
CancellationToken = cancellationToken;
/// <summary>
/// Enumerate the values a <see cref="Page{T}"/> at a time. This may
/// make multiple service requests.
/// </summary>
/// <param name="continuationToken">
/// A continuation token indicating where to resume paging or null to
/// begin paging from the beginning.
/// </param>
/// <param name="pageSizeHint">
/// The number of items per <see cref="Page{T}"/> that should be requested (from
/// service operations that support it). It's not guaranteed that the value will be respected.
/// </param>
/// <returns>
/// An async sequence of <see cref="Page{T}"/>s.
/// </returns>
public abstract IAsyncEnumerable<Page<T>> AsPages(
string? continuationToken = default,
int? pageSizeHint = default);
/// <summary>
/// Enumerate the values in the collection asynchronously. This may
/// make multiple service requests.
/// </summary>
/// <param name="cancellationToken">
/// The <see cref="CancellationToken"/> used for requests made while
/// enumerating asynchronously.
/// </param>
/// <returns>An async sequence of values.</returns>
public virtual async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
await foreach (Page<T> page in AsPages().ConfigureAwait(false).WithCancellation(cancellationToken))
{
foreach (T value in page.Values)
{
yield return value;
}
}
}
/// <summary>
/// Creates an instance of <see cref="Pageable{T}"/> using the provided pages.
/// </summary>
/// <param name="pages">The pages of values to list as part of net new pageable instance.</param>
/// <returns>A new instance of <see cref="Pageable{T}"/></returns>
#pragma warning disable CA1000 // Do not declare static members on generic types
public static AsyncPageable<T> FromPages(IEnumerable<Page<T>> pages)
#pragma warning restore CA1000 // Do not declare static members on generic types
{
return new StaticPageable(pages);
}
/// <summary>
/// Creates a string representation of an <see cref="AsyncPageable{T}"/>.
/// </summary>
/// <returns>
/// A string representation of an <see cref="AsyncPageable{T}"/>.
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string? ToString() => base.ToString();
/// <summary>
/// Check if two <see cref="AsyncPageable{T}"/> instances are equal.
/// </summary>
/// <param name="obj">The instance to compare to.</param>
/// <returns>True if they're equal, false otherwise.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj) => base.Equals(obj);
/// <summary>
/// Get a hash code for the <see cref="AsyncPageable{T}"/>.
/// </summary>
/// <returns>Hash code for the <see cref="Page{T}"/>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
private class StaticPageable : AsyncPageable<T>
{
private readonly IEnumerable<Page<T>> _pages;
public StaticPageable(IEnumerable<Page<T>> pages)
{
_pages = pages;
}
#pragma warning disable 1998 // async function without await
public override async IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = default)
#pragma warning restore 1998
{
var shouldReturnPages = continuationToken == null;
foreach (var page in _pages)
{
if (shouldReturnPages)
{
yield return page;
}
else
{
if (continuationToken == page.ContinuationToken)
{
shouldReturnPages = true;
}
}
}
}
}
}
}