Lines
0.00%
0 / 8
Functions and Methods
0.00%
0 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Latch | 0.00% | 0 / 8 | 0.00% | 0 / 5 | 42 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| waitUntilOpen | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | |||||
| open | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | |||||
| isOpen | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| countWaiters | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Testing\Async; | |
| 6 | ||
| 7 | use Amp\Cancellation; | |
| 8 | use Amp\DeferredFuture; | |
| 9 | ||
| 10 | final class Latch | |
| 11 | { | |
| 12 | /** @var DeferredFuture<null> */ | |
| 13 | private readonly DeferredFuture $opened; | |
| 14 | ||
| 15 | private int $waiters = 0; | |
| 16 | ||
| 17 | public function __construct() | |
| 18 | { | |
| 19 | $this->opened = new DeferredFuture(); | |
| 20 | } | |
| 21 | ||
| 22 | public function waitUntilOpen(?Cancellation $cancellation = null): void | |
| 23 | { | |
| 24 | ++$this->waiters; | |
| 25 | ||
| 26 | try { | |
| 27 | $this->opened->getFuture()->await($cancellation); | |
| 28 | } finally { | |
| 29 | --$this->waiters; | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | public function open(): void | |
| 34 | { | |
| 35 | if (!$this->opened->isComplete()) { | |
| 36 | $this->opened->complete(); | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | public function isOpen(): bool | |
| 41 | { | |
| 42 | return $this->opened->isComplete(); | |
| 43 | } | |
| 44 | ||
| 45 | public function countWaiters(): int | |
| 46 | { | |
| 47 | return $this->waiters; | |
| 48 | } | |
| 49 | } |