-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathSuspension.php
More file actions
178 lines (149 loc) · 4.93 KB
/
Suspension.php
File metadata and controls
178 lines (149 loc) · 4.93 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Revolt\EventLoop;
use Revolt\EventLoop;
/**
* Should be used to run and suspend the event loop instead of directly interacting with fibers.
*
* **Example**
*
* ```php
* $suspension = EventLoop::createSuspension();
*
* $promise->then(fn ($value) => $suspension->resume($value), fn ($throwable) => $suspension->throw($throwable));
*
* $suspension->suspend();
* ```
*/
final class Suspension
{
/** @var string Next listener ID. */
private static string $nextId = 'a';
/** @var Listener[] */
private static array $listeners = [];
private static bool $invokingListeners = false;
private ?\Fiber $fiber;
private \Fiber $scheduler;
private Driver $driver;
private bool $pending = false;
/**
* Use {@see EventLoop::createSuspension()} to create Suspensions.
*
* @param Driver $driver
* @param \Fiber $scheduler
*
* @internal
*/
public function __construct(Driver $driver, \Fiber $scheduler)
{
$this->driver = $driver;
$this->fiber = \Fiber::getCurrent();
if ($this->fiber === $scheduler) {
throw new \Error(\sprintf(
'Cannot call %s() within a scheduler microtask (%s::queue() callback)',
__METHOD__,
EventLoop::class,
));
}
$this->scheduler = $scheduler;
}
public function throw(\Throwable $throwable): void
{
if (!$this->pending) {
throw new \Error('Must call throw() before calling resume()');
}
if (self::$invokingListeners) {
throw new \Error('Cannot call throw() within a suspension listener');
}
$this->pending = false;
if ($this->fiber) {
$this->driver->queue([$this->fiber, 'throw'], $throwable);
} else {
// Suspend event loop fiber to {main}.
$this->driver->queue([\Fiber::class, 'suspend'], static fn () => throw $throwable);
}
}
public function resume(mixed $value): void
{
if (!$this->pending) {
throw new \Error('Must call suspend() before calling resume()');
}
if (self::$invokingListeners) {
throw new \Error('Cannot call throw() within a suspension listener');
}
$this->pending = false;
if ($this->fiber) {
$this->driver->queue([$this->fiber, 'resume'], $value);
} else {
// Suspend event loop fiber to {main}.
$this->driver->queue([\Fiber::class, 'suspend'], static fn () => $value);
}
}
public function suspend(): mixed
{
if ($this->pending) {
throw new \Error('Must call resume() or throw() before calling suspend() again');
}
if ($this->fiber !== \Fiber::getCurrent()) {
throw new \Error('Must not call suspend() from another fiber');
}
if (self::$invokingListeners) {
throw new \Error('Cannot call suspend() within a suspension listener');
}
$this->pending = true;
if (!empty(self::$listeners)) {
$this->invokeListeners('onSuspend');
}
try {
// Awaiting from within a fiber.
if ($this->fiber) {
return \Fiber::suspend();
}
// Awaiting from {main}.
$lambda = $this->scheduler->isStarted() ? $this->scheduler->resume() : $this->scheduler->start();
/** @psalm-suppress RedundantCondition $this->pending should be changed when resumed. */
if ($this->pending) {
// Should only be true if the event loop exited without resolving the promise.
throw new \Error('Event loop suspended or exited unexpectedly');
}
return $lambda();
} finally {
if (!empty(self::$listeners)) {
$this->invokeListeners('onResume');
}
}
}
private function invokeListeners(string $method): void
{
$id = \spl_object_id($this);
self::$invokingListeners = true;
foreach (self::$listeners as $listener) {
try {
$listener->{$method}($id);
} catch (\Throwable $exception) {
$this->driver->queue(static fn () => throw $exception);
}
}
self::$invokingListeners = false;
}
/**
* Add a listener that is invoked when any Suspension is suspended, resumed, or destroyed.
*
* @param Listener $listener
* @return string ID that can be used to remove the listener using {@see unlisten()}.
*/
public static function listen(Listener $listener): string
{
$id = self::$nextId++;
self::$listeners[$id] = $listener;
return $id;
}
/**
* Remove the suspension listener.
*
* @param string $id
*/
public static function unlisten(string $id): void
{
unset(self::$listeners[$id]);
}
}