-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Sequence.php
144 lines (125 loc) · 3.32 KB
/
Sequence.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace Psl\Async;
use Closure;
use Exception;
use Revolt\EventLoop;
use Revolt\EventLoop\Suspension;
use function array_slice;
/**
* Run an operation with a limit on number of ongoing asynchronous jobs of 1.
*
* Just like {@see Semaphore}, all operations must have the same input type (Tin) and output type (Tout), and be processed by the same function;
*
* @template Tin
* @template Tout
*
* @see Semaphore
*/
final class Sequence
{
private bool $ingoing = false;
/**
* @var list<Suspension>
*/
private array $pending = [];
/**
* @var list<Suspension>
*/
private array $waits = [];
/**
* @param (Closure(Tin): Tout) $operation
*/
public function __construct(
private readonly Closure $operation,
) {
}
/**
* Run the operation using the given `$input`, after all previous operations have completed.
*
* @param Tin $input
*
* @return Tout
*
* @see Sequence::cancel()
*/
public function waitFor(mixed $input): mixed
{
if ($this->ingoing) {
$this->pending[] = $suspension = EventLoop::getSuspension();
$suspension->suspend();
}
$this->ingoing = true;
try {
return ($this->operation)($input);
} finally {
$suspension = $this->pending[0] ?? null;
if ($suspension !== null) {
$this->pending = array_slice($this->pending, 1);
$suspension->resume();
} else {
foreach ($this->waits as $suspension) {
$suspension->resume();
}
$this->waits = [];
$this->ingoing = false;
}
}
}
/**
* Cancel all pending operations.
*
* Any pending operation will fail with the given exception.
*
* Future operations will continue execution as usual.
*/
public function cancel(Exception $exception): void
{
$suspensions = $this->pending;
$this->pending = [];
foreach ($suspensions as $suspension) {
$suspension->throw($exception);
}
}
/**
* Get the number of operations pending execution.
*
* @return int<0, max>
*/
public function getPendingOperations(): int
{
/** @var int<0, max> */
return count($this->pending);
}
/**
* Check if there's any operations pending execution.
*
* If this method returns `true`, it means future calls to `waitFor` will wait.
*/
public function hasPendingOperations(): bool
{
return $this->pending !== [];
}
/**
* Check if the sequence has any ingoing operations.
*
* If this method returns `true`, it means future calls to `waitFor` will wait.
* If this method returns `false`, it means future calls to `waitFor` will execute immediately.
*/
public function hasIngoingOperations(): bool
{
return $this->ingoing;
}
/**
* Wait for all pending operations to finish execution.
*/
public function waitForPending(): void
{
if (!$this->ingoing) {
return;
}
$suspension = EventLoop::getSuspension();
$this->waits[] = $suspension;
$suspension->suspend();
}
}