Lines 100.00% 8 / 8
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DeferredWait 100.00% 8 / 8 100.00% 5 / 5 8 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 resolve 100.00% 2 / 2 100.00% 1 / 1 2
 fail 100.00% 2 / 2 100.00% 1 / 1 2
 expire 100.00% 2 / 2 100.00% 1 / 1 2
 awaitOutcome 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Support\Time;
6
7use Amp\DeferredFuture;
8use Stewart\Contracts\Time\PendingWait;
9use Throwable;
10
11/**
12 * @internal
13 * @template T
14 * @implements PendingWait<T>
15 */
16final readonly class DeferredWait implements PendingWait
17{
18    /** @var DeferredFuture<T|null> */
19    private DeferredFuture $outcome;
20
21    public function __construct()
22    {
23        $this->outcome = new DeferredFuture();
24    }
25
26    public function resolve(mixed $value): void
27    {
28        if (!$this->outcome->isComplete()) {
29            $this->outcome->complete($value);
30        }
31    }
32
33    public function fail(Throwable $reason): void
34    {
35        if (!$this->outcome->isComplete()) {
36            $this->outcome->error($reason);
37        }
38    }
39
40    public function expire(): void
41    {
42        if (!$this->outcome->isComplete()) {
43            $this->outcome->complete(null);
44        }
45    }
46
47    /** @return T|null */
48    public function awaitOutcome(): mixed
49    {
50        return $this->outcome->getFuture()->await();
51    }
52}