|
1 | 1 | <?php |
2 | 2 | namespace Kafka; |
3 | 3 |
|
4 | | -use Amp as AmpLoop; |
5 | | - |
6 | 4 | class Loop |
7 | 5 | { |
8 | | - use \Kafka\SingletonTrait; |
| 6 | + use \Kafka\SingletonTrait; |
9 | 7 |
|
10 | 8 | private $watchers = []; |
11 | 9 |
|
12 | | - public function repeat(int $interval, callable $callback) |
| 10 | + public function repeat($interval, callable $callback) |
13 | 11 | { |
14 | | - $watcherId = AmpLoop::repeat($interval, $callback); |
| 12 | + $watcherId = \Amp\repeat($callback, $interval); |
15 | 13 | $this->addWatcher($watcherId); |
16 | 14 | return $watcherId; |
17 | 15 | } |
18 | 16 |
|
19 | | - public function defer(callable $callback) |
| 17 | + public function defer(callable $callback) |
20 | 18 | { |
21 | | - $watcherId = AmpLoop::defer($callback); |
| 19 | + $watcherId = \Amp\immediately($callback); |
22 | 20 | $this->addWatcher($watcherId); |
23 | 21 | return $watcherId; |
24 | 22 | } |
25 | 23 |
|
26 | | - public function cancel(string $watcherId) |
| 24 | + public function cancel($watcherId) |
27 | 25 | { |
28 | | - AmpLoop::cancel($watcherId); |
| 26 | + \Amp\cancel($watcherId); |
29 | 27 | $this->delWatcher($watcherId); |
30 | 28 | } |
31 | 29 |
|
32 | | - public function disable(string $watcherId) |
| 30 | + public function disable($watcherId) |
33 | 31 | { |
34 | | - AmpLoop::disable($watcherId); |
| 32 | + \Amp\disable($watcherId); |
35 | 33 | } |
36 | 34 |
|
37 | | - public function enable(string $watcherId) |
| 35 | + public function enable($watcherId) |
38 | 36 | { |
39 | | - AmpLoop::enable($watcherId); |
| 37 | + \Amp\enable($watcherId); |
40 | 38 | } |
41 | 39 |
|
42 | | - public function delay(int $delay, callable $callback, $data = null) |
| 40 | + public function delay($delay, callable $callback, $data = []) |
43 | 41 | { |
44 | | - $watcherId = AmpLoop::delay($delay, $callback, $data); |
| 42 | + $watcherId = \Amp\once($callback, $delay, $data); |
45 | 43 | $this->addWatcher($watcherId); |
46 | 44 | return $watcherId; |
47 | 45 | } |
48 | 46 |
|
49 | | - public function onReadable($stream, callable $callback, $data = null) |
| 47 | + public function onReadable($stream, callable $callback, $data = []) |
50 | 48 | { |
51 | | - $watcherId = AmpLoop::onReadable($stream, $callback, $data); |
| 49 | + $watcherId = \Amp\onReadable($stream, $callback, $data); |
52 | 50 | $this->addWatcher($watcherId); |
53 | 51 | return $watcherId; |
54 | 52 | } |
55 | 53 |
|
56 | | - public function onWritable($stream, callable $callback, $data = null) |
| 54 | + public function onWritable($stream, callable $callback, $data = []) |
57 | 55 | { |
58 | | - $watcherId = AmpLoop::onWritable($stream, $callback, $data); |
| 56 | + $watcherId = \Amp\onWritable($stream, $callback, $data); |
59 | 57 | $this->addWatcher($watcherId); |
60 | 58 | return $watcherId; |
61 | 59 | } |
62 | 60 |
|
63 | | - public function run() |
| 61 | + public function run() |
64 | 62 | { |
65 | | - $info = AmpLoop::getInfo(); |
| 63 | + $info = \Amp\info(); |
66 | 64 | if (isset($info['running']) && $info['running'] === true) { |
67 | 65 | return; |
68 | 66 | } |
69 | 67 |
|
70 | | - AmpLoop::run(); |
| 68 | + \Amp\run(); |
71 | 69 | } |
72 | 70 |
|
73 | | - public function stop() |
| 71 | + public function stop() |
74 | 72 | { |
75 | | - AmpLoop::stop(); |
| 73 | + \Amp\stop(); |
76 | 74 | foreach ($this->watchers as $watcherId => $unused) { |
77 | 75 | $this->cancel($watcherId); |
78 | 76 | } |
79 | 77 | } |
80 | 78 |
|
81 | | - public function getInfo() |
| 79 | + public function getInfo() |
82 | 80 | { |
83 | | - return AmpLoop::getInfo(); |
| 81 | + return \Amp\info(); |
84 | 82 | } |
85 | 83 |
|
86 | | - private function addWatcher(string $watcherId) |
| 84 | + private function addWatcher($watcherId) |
87 | 85 | { |
88 | 86 | $this->watchers[$watcherId] = true; |
89 | 87 | } |
90 | 88 |
|
91 | | - private function delWatcher(string $watcherId) |
| 89 | + private function delWatcher($watcherId) |
92 | 90 | { |
93 | 91 | if (! isset($this->watchers[$watcherId])) { |
94 | 92 | return; |
|
0 commit comments