| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal static class PageResponseEnumerator |
| | 13 | | { |
| | 14 | | public static FuncPageable<T> CreateEnumerable<T>(Func<string?, Page<T>> pageFunc) where T : notnull |
| | 15 | | { |
| 30 | 16 | | return new FuncPageable<T>((continuationToken, pageSizeHint) => pageFunc(continuationToken)); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public static FuncPageable<T> CreateEnumerable<T>(Func<string?, int?, Page<T>> pageFunc) where T : notnull |
| | 20 | | { |
| 0 | 21 | | return new FuncPageable<T>(pageFunc); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<string?, Task<Page<T>>> pageFunc) where T : notnull |
| | 25 | | { |
| 30 | 26 | | return new FuncAsyncPageable<T>((continuationToken, pageSizeHint) => pageFunc(continuationToken)); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<string?, int?, Task<Page<T>>> pageFunc) where T : n |
| | 30 | | { |
| 0 | 31 | | return new FuncAsyncPageable<T>(pageFunc); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | internal class FuncAsyncPageable<T> : AsyncPageable<T> where T : notnull |
| | 35 | | { |
| | 36 | | private readonly Func<string?, int?, Task<Page<T>>> _pageFunc; |
| | 37 | |
|
| 8 | 38 | | public FuncAsyncPageable(Func<string?, int?, Task<Page<T>>> pageFunc) |
| | 39 | | { |
| 8 | 40 | | _pageFunc = pageFunc; |
| 8 | 41 | | } |
| | 42 | |
|
| | 43 | | public override async IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHi |
| | 44 | | { |
| | 45 | | do |
| | 46 | | { |
| 22 | 47 | | Page<T> pageResponse = await _pageFunc(continuationToken, pageSizeHint).ConfigureAwait(false); |
| 22 | 48 | | yield return pageResponse; |
| 22 | 49 | | continuationToken = pageResponse.ContinuationToken; |
| 44 | 50 | | } while (continuationToken != null); |
| 8 | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| | 54 | | internal class FuncPageable<T> : Pageable<T> where T : notnull |
| | 55 | | { |
| | 56 | | private readonly Func<string?, int?, Page<T>> _pageFunc; |
| | 57 | |
|
| 8 | 58 | | public FuncPageable(Func<string?, int?, Page<T>> pageFunc) |
| | 59 | | { |
| 8 | 60 | | _pageFunc = pageFunc; |
| 8 | 61 | | } |
| | 62 | |
|
| | 63 | | public override IEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = defaul |
| | 64 | | { |
| | 65 | | do |
| | 66 | | { |
| 22 | 67 | | Page<T> pageResponse = _pageFunc(continuationToken, pageSizeHint); |
| 22 | 68 | | yield return pageResponse; |
| 22 | 69 | | continuationToken = pageResponse.ContinuationToken; |
| 44 | 70 | | } while (continuationToken != null); |
| 8 | 71 | | } |
| | 72 | | } |
| | 73 | | } |
| | 74 | | } |