Skip to content

Commit 270eaad

Browse files
authored
Client: Extract cache read into async promise (#7)
1 parent 3677ed1 commit 270eaad

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/WpOrg/Api/Client.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Composer\Downloader\TransportException;
88
use Composer\Util\HttpDownloader;
99
use Composer\Util\Loop;
10+
use React\Promise\Promise;
1011
use React\Promise\PromiseInterface;
1112

1213
readonly class Client
@@ -24,13 +25,8 @@ public function isClosed(string $slug): bool
2425
return false;
2526
}
2627

27-
$cached = $this->cache->read($slug);
28-
if ($cached !== null) {
29-
return $cached;
30-
}
31-
3228
$result = null;
33-
$promise = $this->fetchAndCacheAsync($slug)
29+
$promise = $this->isClosedAsync($slug)
3430
->then(function (bool $isClosed) use (&$result): void {
3531
$result = $isClosed;
3632
});
@@ -40,6 +36,19 @@ public function isClosed(string $slug): bool
4036
return $result;
4137
}
4238

39+
/**
40+
* @return PromiseInterface<bool>
41+
*/
42+
private function isClosedAsync(string $slug): PromiseInterface
43+
{
44+
/** @var Promise<bool> */
45+
return new Promise(function (callable $resolve) use ($slug): void {
46+
$cached = $this->cache->read($slug);
47+
$next = $cached ?? $this->fetchAndCacheAsync($slug);
48+
$resolve($next);
49+
});
50+
}
51+
4352
/**
4453
* @return PromiseInterface<bool>
4554
*/

0 commit comments

Comments
 (0)