using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; namespace Octokit.Internal { public class ReadOnlyPagedCollection : ReadOnlyCollection, IReadOnlyPagedCollection { readonly ApiInfo _info; readonly Func>>> _nextPageFunc; public ReadOnlyPagedCollection(IApiResponse> response, Func>>> nextPageFunc) : base(response != null ? response.Body ?? new List() : new List()) { Ensure.ArgumentNotNull(response, nameof(response)); Ensure.ArgumentNotNull(nextPageFunc, nameof(nextPageFunc)); _nextPageFunc = nextPageFunc; if (response != null) { _info = response.HttpResponse.ApiInfo; } } public async Task> GetNextPage() { var nextPageUrl = _info?.GetNextPageUrl(); if (nextPageUrl == null) return null; var maybeTask = _nextPageFunc(nextPageUrl); if (maybeTask == null) { return null; } var response = await maybeTask.ConfigureAwait(false); return new ReadOnlyPagedCollection(response, _nextPageFunc); } } }