-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLoop.php
More file actions
264 lines (254 loc) · 7.28 KB
/
Loop.php
File metadata and controls
264 lines (254 loc) · 7.28 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php declare(strict_types=1);
/**
* Generic loop.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/MIT MIT
*/
namespace danog\Loop;
use Amp\DeferredFuture;
use AssertionError;
use Revolt\EventLoop;
use Stringable;
/**
* Generic loop, runs single callable.
*
* @api
*
* @author Daniil Gentili <daniil@daniil.it>
*/
abstract class Loop implements Stringable
{
/**
* Stop the loop.
*/
public const STOP = -1.0;
/**
* Pause the loop.
*/
public const PAUSE = null;
/**
* Rerun the loop.
*/
public const CONTINUE = 0.0;
/**
* Whether the loop is running.
*/
private bool $running = false;
/**
* Resume timer ID.
*/
private ?string $resumeTimer = null;
/**
* Resume deferred ID.
*/
private ?string $resumeImmediate = null;
/**
* Shutdown deferred.
*/
private ?DeferredFuture $shutdownDeferred = null;
/**
* Report pause, can be overriden for logging.
*
* @psalm-suppress PossiblyUnusedParam
*
* @param float $timeout Pause duration, 0 = forever
*/
protected function reportPause(float $timeout): void
{
}
/**
* Start the loop.
*
* Returns false if the loop is already running.
*/
public function start(): bool
{
while ($this->shutdownDeferred !== null) {
$this->shutdownDeferred->getFuture()->await();
}
if ($this->running) {
return false;
}
$this->running = true;
/** @infection-ignore-all */
if (!$this->resume()) {
// @codeCoverageIgnoreStart
throw new AssertionError("Could not resume!");
// @codeCoverageIgnoreEnd
}
$this->startedLoop();
return true;
}
/**
* Stops loop.
*
* Returns false if the loop is not running.
*/
public function stop(): bool
{
if (!$this->running) {
return false;
}
$this->running = false;
if ($this->resumeTimer !== null) {
$storedWatcherId = $this->resumeTimer;
EventLoop::cancel($storedWatcherId);
$this->resumeTimer = null;
}
if ($this->resumeImmediate !== null) {
$storedWatcherId = $this->resumeImmediate;
EventLoop::cancel($storedWatcherId);
$this->resumeImmediate = null;
}
if ($this->paused) {
$this->exitedLoop();
} else {
/** @infection-ignore-all */
if ($this->shutdownDeferred !== null) {
// @codeCoverageIgnoreStart
throw new AssertionError("Shutdown deferred is not null!");
// @codeCoverageIgnoreEnd
}
$this->shutdownDeferred = new DeferredFuture;
}
return true;
}
abstract protected function loop(): ?float;
private bool $paused = true;
private function loopInternal(): void
{
/** @infection-ignore-all */
if (!$this->running) {
// @codeCoverageIgnoreStart
throw new AssertionError("Already running!");
// @codeCoverageIgnoreEnd
}
/** @infection-ignore-all */
if (!$this->paused) {
// @codeCoverageIgnoreStart
throw new AssertionError("Already paused!");
// @codeCoverageIgnoreEnd
}
$this->paused = false;
try {
$timeout = $this->loop();
} catch (\Throwable $e) {
$this->exitedLoopInternal();
throw $e;
}
/** @var bool $this->running */
if (!$this->running) {
$this->exitedLoopInternal();
return;
}
if ($timeout === self::STOP) {
$this->exitedLoopInternal();
return;
}
$this->paused = true;
if ($timeout === self::PAUSE) {
$this->reportPause(0.0);
} else {
if ($this->resumeImmediate === null) {
/** @infection-ignore-all */
if ($this->resumeTimer !== null) {
// @codeCoverageIgnoreStart
throw new AssertionError("Already have a resume timer!");
// @codeCoverageIgnoreEnd
}
$this->resumeTimer = EventLoop::delay($timeout, function (): void {
$this->resumeTimer = null;
$this->loopInternal();
});
}
if ($timeout !== self::CONTINUE) {
$this->reportPause($timeout);
}
}
}
private function exitedLoopInternal(): void
{
$this->running = false;
$this->paused = true;
/** @infection-ignore-all */
if ($this->resumeTimer !== null) {
// @codeCoverageIgnoreStart
throw new AssertionError("Already have a resume timer!");
// @codeCoverageIgnoreEnd
}
/** @infection-ignore-all */
if ($this->resumeImmediate !== null) {
// @codeCoverageIgnoreStart
throw new AssertionError("Already have a resume immediate timer!");
// @codeCoverageIgnoreEnd
}
$this->exitedLoop();
if ($this->shutdownDeferred !== null) {
$d = $this->shutdownDeferred;
$this->shutdownDeferred = null;
EventLoop::queue($d->complete(...));
}
}
/**
* Signal that loop was started.
*/
protected function startedLoop(): void
{
}
/**
* Signal that loop has exited.
*/
protected function exitedLoop(): void
{
}
/**
* Check whether loop is running.
*/
public function isRunning(): bool
{
return $this->running;
}
/**
* Check whether loop is paused (different from isRunning, a loop may be running but paused).
*/
public function isPaused(): bool
{
return $this->paused;
}
/**
* Resume the loop.
*
* If resume is called multiple times, and the event loop hasn't resumed the loop yet,
* the loop will be resumed only once, not N times for every call.
*
* @param bool $postpone If true, multiple resumes will postpone the resuming to the end of the callback queue instead of leaving its position unchanged.
*
* @return bool Returns false if the loop is not paused.
*/
public function resume(bool $postpone = false): bool
{
if ($this->running && $this->paused) {
if ($this->resumeImmediate !== null) {
if (!$postpone) {
return true;
}
$resumeImmediate = $this->resumeImmediate;
$this->resumeImmediate = null;
EventLoop::cancel($resumeImmediate);
}
if ($this->resumeTimer !== null) {
$timer = $this->resumeTimer;
$this->resumeTimer = null;
EventLoop::cancel($timer);
}
$this->resumeImmediate = EventLoop::defer(function (): void {
$this->resumeImmediate = null;
$this->loopInternal();
});
return true;
}
return false;
}
}