diff --git a/src/WpOrg/Api/Client.php b/src/WpOrg/Api/Client.php index 4f1cdac..8ad66cc 100644 --- a/src/WpOrg/Api/Client.php +++ b/src/WpOrg/Api/Client.php @@ -7,6 +7,7 @@ use Composer\Downloader\TransportException; use Composer\Util\HttpDownloader; use Composer\Util\Loop; +use React\Promise\Promise; use React\Promise\PromiseInterface; readonly class Client @@ -24,13 +25,8 @@ public function isClosed(string $slug): bool return false; } - $cached = $this->cache->read($slug); - if ($cached !== null) { - return $cached; - } - $result = null; - $promise = $this->fetchAndCacheAsync($slug) + $promise = $this->isClosedAsync($slug) ->then(function (bool $isClosed) use (&$result): void { $result = $isClosed; }); @@ -40,6 +36,19 @@ public function isClosed(string $slug): bool return $result; } + /** + * @return PromiseInterface + */ + private function isClosedAsync(string $slug): PromiseInterface + { + /** @var Promise */ + return new Promise(function (callable $resolve) use ($slug): void { + $cached = $this->cache->read($slug); + $next = $cached ?? $this->fetchAndCacheAsync($slug); + $resolve($next); + }); + } + /** * @return PromiseInterface */