-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Pageable.cs
148 lines (133 loc) · 5.39 KB
/
Pageable.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
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>
#pragma warning disable AZC0012 // Avoid single word type names
public abstract class Pageable<T> : IEnumerable<T> where T : notnull
#pragma warning restore AZC0012 // Avoid single word type names
{
/// <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="Pageable{T}"/>
/// class for mocking.
/// </summary>
protected Pageable() =>
CancellationToken = CancellationToken.None;
/// <summary>
/// Initializes a new instance of the <see cref="Pageable{T}"/>
/// class.
/// </summary>
/// <param name="cancellationToken">
/// The <see cref="CancellationToken"/> used for requests made while
/// enumerating asynchronously.
/// </param>
protected Pageable(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 IEnumerable<Page<T>> AsPages(
string? continuationToken = default,
int? pageSizeHint = default);
/// <summary>
/// Creates a string representation of an <see cref="Pageable{T}"/>.
/// </summary>
/// <returns>
/// A string representation of an <see cref="Pageable{T}"/>.
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string? ToString() => base.ToString();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Enumerate the values in the collection. This may make multiple service requests.
/// </summary>
public virtual IEnumerator<T> GetEnumerator()
{
foreach (Page<T> page in AsPages())
{
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 Pageable<T> FromPages(IEnumerable<Page<T>> pages)
#pragma warning restore CA1000 // Do not declare static members on generic types
{
return new StaticPageable(pages);
}
/// <summary>
/// Check if two <see cref="Pageable{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="Pageable{T}"/>.
/// </summary>
/// <returns>Hash code for the <see cref="Pageable{T}"/>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
private class StaticPageable : Pageable<T>
{
private readonly IEnumerable<Page<T>> _pages;
public StaticPageable(IEnumerable<Page<T>> pages)
{
_pages = pages;
}
public override IEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = default)
{
var shouldReturnPages = continuationToken == null;
foreach (var page in _pages)
{
if (shouldReturnPages)
{
yield return page;
}
else
{
if (continuationToken == page.ContinuationToken)
{
shouldReturnPages = true;
}
}
}
}
}
}
}