/// <summary> /// Tries to the move to the next page in the document producer. /// </summary> /// <param name="token">The cancellation token.</param> /// <returns>Whether the operation was successful.</returns> private async Task <bool> MoveNextPageAsync(CancellationToken token) { token.ThrowIfCancellationRequested(); if (this.itemsLeftInCurrentPage != 0) { throw new InvalidOperationException("Tried to move onto the next page before finishing the first page."); } // We need to buffer pages if empty, since that how we know there are no more pages left. await this.BufferMoreIfEmptyAsync(token); if (this.bufferedPages.Count == 0) { return(false); } // Pull a FeedResponse using TryMonad (we could have buffered an exception). TryMonad <DocumentFeedResponse <CosmosElement> > tryMonad = await this.bufferedPages.TakeAsync(token); DocumentFeedResponse <CosmosElement> feedResponse = tryMonad.Match <DocumentFeedResponse <CosmosElement> >( onSuccess: ((page) => { return(page); }), onError: (exceptionDispatchInfo) => { exceptionDispatchInfo.Throw(); return(null); }); // Update the state. this.PreviousContinuationToken = this.currentContinuationToken; this.currentContinuationToken = feedResponse.ResponseContinuation; this.currentPage = feedResponse.GetEnumerator(); this.IsAtBeginningOfPage = true; this.itemsLeftInCurrentPage = feedResponse.Count; // Prime the enumerator, // so that current is pointing to the first document instead of one before. if (this.MoveToFirstDocumentInPage()) { this.IsAtBeginningOfPage = true; return(true); } else { // We got an empty page if (this.currentContinuationToken != null) { return(await this.MoveNextPageAsync(token)); } return(false); } }
/// <summary> /// Tries to the move to the next page in the document producer. /// </summary> /// <param name="token">The cancellation token.</param> /// <returns>Whether the operation was successful.</returns> private async Task <bool> MoveNextPageAsync(CancellationToken token) { token.ThrowIfCancellationRequested(); if (this.bufferedPages.Count == 0) { return(false); } if (this.itemsLeftInCurrentPage != 0) { throw new InvalidOperationException("Tried to move onto the next page before finishing the first page."); } TryMonad <DocumentFeedResponse <CosmosElement> > tryMonad = await this.bufferedPages.TakeAsync(token); DocumentFeedResponse <CosmosElement> feedResponse = tryMonad.Match <DocumentFeedResponse <CosmosElement> >( onSuccess: ((page) => { return(page); }), onError: (exceptionDispatchInfo) => { exceptionDispatchInfo.Throw(); return(null); }); this.previousContinuationToken = this.currentContinuationToken; this.currentContinuationToken = feedResponse.ResponseContinuation; this.currentPage = feedResponse.GetEnumerator(); this.itemsLeftInCurrentPage = feedResponse.Count; if (this.MoveNextDocumentWithinCurrentPage()) { this.isAtBeginningOfPage = true; return(true); } else { return(false); } }