1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React; |
4
|
|
|
|
5
|
|
|
use React\EventLoop\LoopInterface; |
6
|
|
|
use React\EventLoop\TimerInterface; |
7
|
|
|
|
8
|
|
|
final class HighPrecisionScheduler implements SchedulerInterface |
9
|
|
|
{ |
10
|
|
|
/** @var LoopInterface */ |
11
|
|
|
private $loop; |
12
|
|
|
|
13
|
|
|
/** @var bool */ |
14
|
|
|
private $useHighResolution = false; |
15
|
|
|
|
16
|
|
|
/** @var callable[] */ |
17
|
|
|
private $ticks = []; |
18
|
|
|
|
19
|
|
|
/** @var TimerInterface */ |
20
|
|
|
private $timer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param LoopInterface $loop |
24
|
|
|
*/ |
25
|
2 |
|
public function __construct(LoopInterface $loop) |
26
|
|
|
{ |
27
|
2 |
|
$this->loop = $loop; |
28
|
2 |
|
$this->useHighResolution = \function_exists('hrtime'); |
29
|
|
|
|
30
|
2 |
|
$this->align(); |
31
|
2 |
|
} |
32
|
|
|
|
33
|
2 |
|
public function schedule(callable $tick): void |
34
|
|
|
{ |
35
|
|
|
// Push this new tick on the stack with the rest, running it in the next minute |
36
|
2 |
|
$this->ticks[] = $tick; |
37
|
|
|
|
38
|
|
|
// Initial tick because some actions might want to run in this minute |
39
|
2 |
|
$tick(); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
2 |
|
private function time(): float |
43
|
|
|
{ |
44
|
2 |
|
return $this->useHighResolution ? \hrtime(true) * 1e-9 : \microtime(true); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
private function hasDrifted(float $time): bool |
48
|
|
|
{ |
49
|
2 |
|
return (int)\date('s', (int)$time) > 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
private function tick(): void |
53
|
|
|
{ |
54
|
2 |
|
$startOfTick = $this->time(); |
55
|
|
|
|
56
|
2 |
|
foreach ($this->ticks as $tick) { |
57
|
1 |
|
$tick(); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
if ($this->hasDrifted($startOfTick)) { |
61
|
|
|
$this->align(); |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
2 |
|
private function align(): void |
66
|
|
|
{ |
67
|
2 |
|
if ($this->timer instanceof TimerInterface) { |
68
|
|
|
$this->loop->cancelTimer($this->timer); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$currentSecond = (int)\date('s', (int)$this->time()); |
72
|
|
|
|
73
|
2 |
View Code Duplication |
if ($currentSecond >= 1 && $currentSecond <= 55) { |
|
|
|
|
74
|
|
|
$this->loop->addTimer(55 - $currentSecond, function (): void { |
75
|
1 |
|
$this->align(); |
76
|
1 |
|
}); |
77
|
|
|
|
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
View Code Duplication |
if ($currentSecond > 55 && $currentSecond <= 58) { |
|
|
|
|
82
|
|
|
$this->loop->addTimer(1, function (): void { |
83
|
1 |
|
$this->align(); |
84
|
1 |
|
}); |
85
|
|
|
|
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
if ($currentSecond === 59) { |
90
|
|
|
$this->loop->addTimer(0.001, function (): void { |
91
|
1 |
|
$this->align(); |
92
|
1 |
|
}); |
93
|
|
|
|
94
|
1 |
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
$this->tick(); |
98
|
|
|
|
99
|
|
|
$this->timer = $this->loop->addPeriodicTimer(60, function (): void { |
100
|
|
|
$this->tick(); |
101
|
2 |
|
}); |
102
|
2 |
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.