Skip to content

Commit

Permalink
Merge pull request #8 from OwlyCode/wait-new-instances
Browse files Browse the repository at this point in the history
Added fix to wait if new instances of dataloader were declared during the processing
  • Loading branch information
mcg-web authored Jan 20, 2017
2 parents 6cb2023 + 0fa6535 commit 55836c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/DataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ private static function awaitInstances()
$dataLoader->process();
}
}

// If new dataloaders were instanciated in the meantime, wait again !
if (count($dataLoaders) != count(self::$instances)) {
self::awaitInstances();
}
}

private function getCacheKeyFromKey($key)
Expand Down
23 changes: 23 additions & 0 deletions tests/DataLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,29 @@ public function testCallingAwaitFunctionWhenNoInstanceOfDataLoaderShouldNotThrow
DataLoader::await();
}

public function testAwaitAlsoAwaitsNewlyCreatedDataloaders()
{
$firstComplete = false;
$secondComplete = false;

$first = new DataLoader(function ($values) use (&$firstComplete, &$secondComplete) {
$second = new DataLoader(function ($values) use (&$secondComplete) {
$secondComplete = true;
return self::$promiseFactory->createAll(['B']);
}, self::$promiseFactory);

$second->load('B');

$firstComplete = true;
return self::$promiseFactory->createAll(['A']);
}, self::$promiseFactory);

DataLoader::await($first->load('A'));

$this->assertTrue($firstComplete);
$this->assertTrue($secondComplete);
}

public function cacheKey($key)
{
$cacheKey = [];
Expand Down

0 comments on commit 55836c8

Please sign in to comment.