Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Oct 19, 2023
1 parent b4b5eda commit 661ca7e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions listing/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ func NewIterator[Req, Resp, T any](
}
}

func (i *Iterator[Req, Resp, T]) loadNextPageIfNeeded(ctx context.Context) (bool, error) {
func (i *Iterator[Req, Resp, T]) loadNextPageIfNeeded(ctx context.Context) error {
if i.currentPageIdx < len(i.currentPage) {
return true, nil
return nil
}

if i.isExhausted {
return false, ErrNoMoreItems
return ErrNoMoreItems
}
resp, err := i.getNextPage(ctx, i.nextReq)
if err != nil {
return false, err
return err
}
items := i.getItems(resp)
i.currentPage = items
Expand All @@ -71,16 +70,16 @@ func (i *Iterator[Req, Resp, T]) loadNextPageIfNeeded(ctx context.Context) (bool
if !i.isExhausted && (status == ListingStatusExhausted || (status == ListingStatusCheckResult && len(items) == 0)) {
i.isExhausted = true
}
return !i.isExhausted, err
return err
}

func (i *Iterator[Req, Resp, T]) Next(ctx context.Context) (T, error) {
var t T
ok, err := i.loadNextPageIfNeeded(ctx)
err := i.loadNextPageIfNeeded(ctx)
if err != nil {
return t, err
}
if !ok {
if i.currentPageIdx >= len(i.currentPage) {
return t, ErrNoMoreItems
}
item := i.currentPage[i.currentPageIdx]
Expand All @@ -89,5 +88,9 @@ func (i *Iterator[Req, Resp, T]) Next(ctx context.Context) (T, error) {
}

func (i *Iterator[Req, Resp, T]) HasNext(ctx context.Context) (bool, error) {
return i.loadNextPageIfNeeded(ctx)
err := i.loadNextPageIfNeeded(ctx)
if err != nil {
return false, err
}
return i.currentPageIdx < len(i.currentPage), nil
}

0 comments on commit 661ca7e

Please sign in to comment.