Skip to content

Commit

Permalink
Iterator / PubSub Updates (#598)
Browse files Browse the repository at this point in the history
* always attempt to request the next page as long as a token exists

* fix flaky pubsub tests
  • Loading branch information
dwsupplee authored Jul 24, 2017
1 parent c2c6b16 commit 1d45b2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
17 changes: 15 additions & 2 deletions Iterator/ItemIteratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function next()
$this->pageIndex++;
$this->position++;

if (count($this->pageIterator->current()) <= $this->pageIndex && $this->pageIterator->nextResultToken()) {
if (count($this->pageIterator->current()) <= $this->pageIndex && $this->nextResultToken()) {
$this->pageIterator->next();
$this->pageIndex = 0;
}
Expand All @@ -126,10 +126,23 @@ public function next()
*/
public function valid()
{
if (isset($this->pageIterator->current()[$this->pageIndex])) {
$page = $this->pageIterator->current();

if (isset($page[$this->pageIndex])) {
return true;
}

// If there are no results, but a token for the next page
// exists let's continue paging until there are results.
while ($this->nextResultToken()) {
$this->pageIterator->next();
$page = $this->pageIterator->current();

if (isset($page[$this->pageIndex])) {
return true;
}
}

return false;
}
}
19 changes: 11 additions & 8 deletions Iterator/PageIteratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ public function rewind()
*/
public function current()
{
if (!$this->page) {
if ($this->page === null) {
$this->page = $this->executeCall();
}

return $this->get($this->itemsPath, $this->page);
$page = $this->get($this->itemsPath, $this->page);

if ($this->nextResultToken()) {
return $page ?: [];
}

return $page;
}

/**
Expand All @@ -196,12 +202,9 @@ public function key()
public function next()
{
$this->position++;

if ($this->nextResultToken()) {
$this->page = $this->executeCall();
} else {
$this->page = null;
}
$this->page = $this->nextResultToken()
? $this->executeCall()
: null;
}

/**
Expand Down

0 comments on commit 1d45b2e

Please sign in to comment.