| | 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 PageableHelpers |
| | 13 | | { |
| | 14 | | public static Pageable<T> CreateEnumerable<T>(Func<int?, Page<T>> firstPageFunc, Func<string?, int?, Page<T>>? n |
| | 15 | | { |
| 32 | 16 | | PageFunc<T> first = (continuationToken, pageSizeHint) => firstPageFunc(pageSizeHint); |
| 16 | 17 | | PageFunc<T>? next = nextPageFunc != null ? new PageFunc<T>(nextPageFunc) : null; |
| 16 | 18 | | return new FuncPageable<T>(first, next, pageSize); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<int?, Task<Page<T>>> firstPageFunc, Func<string?, i |
| | 22 | | { |
| 32 | 23 | | AsyncPageFunc<T> first = (continuationToken, pageSizeHint) => firstPageFunc(pageSizeHint); |
| 16 | 24 | | AsyncPageFunc<T>? next = nextPageFunc != null ? new AsyncPageFunc<T>(nextPageFunc) : null; |
| 16 | 25 | | return new FuncAsyncPageable<T>(first, next, pageSize); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | internal delegate Task<Page<T>> AsyncPageFunc<T>(string? continuationToken = default, int? pageSizeHint = defaul |
| | 29 | | internal delegate Page<T> PageFunc<T>(string? continuationToken = default, int? pageSizeHint = default); |
| | 30 | |
|
| | 31 | | internal class FuncAsyncPageable<T> : AsyncPageable<T> where T : notnull |
| | 32 | | { |
| | 33 | | private readonly AsyncPageFunc<T> _firstPageFunc; |
| | 34 | | private readonly AsyncPageFunc<T>? _nextPageFunc; |
| | 35 | | private readonly int? _defaultPageSize; |
| | 36 | |
|
| 16 | 37 | | public FuncAsyncPageable(AsyncPageFunc<T> firstPageFunc, AsyncPageFunc<T>? nextPageFunc, int? defaultPageSiz |
| | 38 | | { |
| 16 | 39 | | _firstPageFunc = firstPageFunc; |
| 16 | 40 | | _nextPageFunc = nextPageFunc; |
| 16 | 41 | | _defaultPageSize = defaultPageSize; |
| 16 | 42 | | } |
| | 43 | |
|
| | 44 | | public override async IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHi |
| | 45 | | { |
| 16 | 46 | | AsyncPageFunc<T>? pageFunc = _firstPageFunc; |
| 16 | 47 | | int? pageSize = pageSizeHint ?? _defaultPageSize; |
| | 48 | | do |
| | 49 | | { |
| 20 | 50 | | Page<T> pageResponse = await pageFunc(continuationToken, pageSize).ConfigureAwait(false); |
| 20 | 51 | | yield return pageResponse; |
| 20 | 52 | | continuationToken = pageResponse.ContinuationToken; |
| 20 | 53 | | pageFunc = _nextPageFunc; |
| 40 | 54 | | } while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null); |
| 16 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| | 58 | | internal class FuncPageable<T> : Pageable<T> where T : notnull |
| | 59 | | { |
| | 60 | | private readonly PageFunc<T> _firstPageFunc; |
| | 61 | | private readonly PageFunc<T>? _nextPageFunc; |
| | 62 | | private readonly int? _defaultPageSize; |
| | 63 | |
|
| 16 | 64 | | public FuncPageable(PageFunc<T> firstPageFunc, PageFunc<T>? nextPageFunc, int? defaultPageSize = default) |
| | 65 | | { |
| 16 | 66 | | _firstPageFunc = firstPageFunc; |
| 16 | 67 | | _nextPageFunc = nextPageFunc; |
| 16 | 68 | | _defaultPageSize = defaultPageSize; |
| 16 | 69 | | } |
| | 70 | |
|
| | 71 | | public override IEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = defaul |
| | 72 | | { |
| 16 | 73 | | PageFunc<T>? pageFunc = _firstPageFunc; |
| 16 | 74 | | int? pageSize = pageSizeHint ?? _defaultPageSize; |
| | 75 | | do |
| | 76 | | { |
| 20 | 77 | | Page<T> pageResponse = pageFunc(continuationToken, pageSize); |
| 20 | 78 | | yield return pageResponse; |
| 20 | 79 | | continuationToken = pageResponse.ContinuationToken; |
| 20 | 80 | | pageFunc = _nextPageFunc; |
| 40 | 81 | | } while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null); |
| 16 | 82 | | } |
| | 83 | | } |
| | 84 | | } |
| | 85 | | } |