Skip to content

Commit 55836c8

Browse files
authored
Merge pull request #8 from OwlyCode/wait-new-instances
Added fix to wait if new instances of dataloader were declared during the processing
2 parents 6cb2023 + 0fa6535 commit 55836c8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/DataLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ private static function awaitInstances()
263263
$dataLoader->process();
264264
}
265265
}
266+
267+
// If new dataloaders were instanciated in the meantime, wait again !
268+
if (count($dataLoaders) != count(self::$instances)) {
269+
self::awaitInstances();
270+
}
266271
}
267272

268273
private function getCacheKeyFromKey($key)

tests/DataLoadTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,29 @@ public function testCallingAwaitFunctionWhenNoInstanceOfDataLoaderShouldNotThrow
786786
DataLoader::await();
787787
}
788788

789+
public function testAwaitAlsoAwaitsNewlyCreatedDataloaders()
790+
{
791+
$firstComplete = false;
792+
$secondComplete = false;
793+
794+
$first = new DataLoader(function ($values) use (&$firstComplete, &$secondComplete) {
795+
$second = new DataLoader(function ($values) use (&$secondComplete) {
796+
$secondComplete = true;
797+
return self::$promiseFactory->createAll(['B']);
798+
}, self::$promiseFactory);
799+
800+
$second->load('B');
801+
802+
$firstComplete = true;
803+
return self::$promiseFactory->createAll(['A']);
804+
}, self::$promiseFactory);
805+
806+
DataLoader::await($first->load('A'));
807+
808+
$this->assertTrue($firstComplete);
809+
$this->assertTrue($secondComplete);
810+
}
811+
789812
public function cacheKey($key)
790813
{
791814
$cacheKey = [];

0 commit comments

Comments
 (0)