-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Deferred.php
70 lines (60 loc) 路 1.42 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
<?php
declare(strict_types=1);
namespace Psl\Async;
use Psl;
use Throwable;
/**
* @template T
*/
final 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.
*/
public function isComplete(): bool
{
return $this->state->isComplete();
}
/**
* @return Awaitable<T> The awaitable associated with this Deferred.
*/
public function getAwaitable(): Awaitable
{
return $this->awaitable;
}
}