-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathDeferred.php
82 lines (72 loc) · 1.83 KB
/
Deferred.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Psl\Async;
use Psl;
use Throwable;
/**
* The following class was derived from code of Amphp.
*
* https://github.com/amphp/amp/blob/ac89b9e2ee04228e064e424056a08590b0cdc7b3/lib/Deferred.php
*
* Code subject to the MIT license (https://github.com/amphp/amp/blob/ac89b9e2ee04228e064e424056a08590b0cdc7b3/LICENSE).
*
* Copyright (c) 2015-2021 Amphp ( https://amphp.org )
*
* @template T
*/
final readonly class Deferred
{
/**
* @var Internal\State<T>
*/
private Internal\State $state;
/**
* @var Awaitable<T>
*/
private Awaitable $awaitable;
public function __construct()
{
$this->state = new Internal\State();
$this->awaitable = new Awaitable($this->state);
}
/**
* Completes the operation with a result value.
*
* @param T $result Result of the operation.
*
* @throws Psl\Exception\InvariantViolationException If the operation is no longer pending.
*/
public function complete(mixed $result): void
{
$this->state->complete($result);
}
/**
* Marks the operation as failed.
*
* @param Throwable $throwable Throwable to indicate the error.
*
* @throws Psl\Exception\InvariantViolationException If the operation is no longer pending.
*/
public function error(Throwable $throwable): void
{
$this->state->error($throwable);
}
/**
* @return bool True if the operation has completed.
*
* @psalm-mutation-free
*/
public function isComplete(): bool
{
return $this->state->isComplete();
}
/**
* @return Awaitable<T> The awaitable associated with this Deferred.
*
* @psalm-mutation-free
*/
public function getAwaitable(): Awaitable
{
return $this->awaitable;
}
}