1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Asynchronous\React; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Core\Loop\Contract\A\ALoopAdapter; |
8
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgument; |
9
|
|
|
use Closure; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\EventLoop\TimerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @codeCoverageIgnore |
15
|
|
|
*/ |
16
|
|
|
final class ReactLoopAdapter extends ALoopAdapter |
17
|
|
|
{ |
18
|
|
|
public function __construct( |
19
|
|
|
private readonly LoopInterface $loop, |
20
|
|
|
) { |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function autoStart(): void |
24
|
|
|
{ |
25
|
|
|
// ReactPHP event loop is started by its library code. |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function repeat(float $interval, Closure $closure): TimerInterface |
29
|
|
|
{ |
30
|
|
|
return $this->loop->addPeriodicTimer($interval, $closure); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function delay(float $delay, Closure $closure): void |
34
|
|
|
{ |
35
|
|
|
$this->loop->addTimer($delay, $closure); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function run(): void |
39
|
|
|
{ |
40
|
|
|
$this->loop->run(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function stop(): void |
44
|
|
|
{ |
45
|
|
|
$this->loop->stop(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function cancel(mixed $timer): void |
49
|
|
|
{ |
50
|
|
|
if (!$timer instanceof TimerInterface) { |
51
|
|
|
throw new InvalidArgument( |
52
|
|
|
sprintf( |
53
|
|
|
'Invalid timer type: %s, expected %s', |
54
|
|
|
gettype($timer), |
55
|
|
|
TimerInterface::class |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
$this->loop->cancelTimer($timer); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function onSignal(int $signal, Closure $closure): void |
63
|
|
|
{ |
64
|
|
|
$this->loop->addSignal($signal, $closure); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|