Skip to content

Commit 238570f

Browse files
committed
Simplification of LibEvLoop.
1 parent ee1f786 commit 238570f

File tree

1 file changed

+55
-74
lines changed

1 file changed

+55
-74
lines changed

LibEvLoop.php

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,58 @@ class LibEvLoop implements LoopInterface
1818
{
1919
private $loop;
2020
private $nextTickQueue;
21-
private $timers;
22-
private $readEvents = array();
23-
private $writeEvents = array();
21+
private $timerEvents;
22+
private $readEvents = [];
23+
private $writeEvents = [];
2424
private $running;
2525

2626
public function __construct()
2727
{
2828
$this->loop = new EventLoop();
2929
$this->nextTickQueue = new NextTickQueue($this);
30-
$this->timers = new SplObjectStorage();
30+
$this->timerEvents = new SplObjectStorage();
3131
}
3232

3333
/**
3434
* {@inheritdoc}
3535
*/
3636
public function addReadStream($stream, callable $listener)
3737
{
38-
$this->addStream($stream, $listener, IOEvent::READ);
38+
$callback = function () use ($stream, $listener) {
39+
call_user_func($listener, $stream, $this);
40+
};
41+
42+
$event = new IOEvent($callback, $stream, IOEvent::READ);
43+
$this->loop->add($event);
44+
45+
$this->readEvents[(int) $stream] = $event;
3946
}
4047

4148
/**
4249
* {@inheritdoc}
4350
*/
4451
public function addWriteStream($stream, callable $listener)
4552
{
46-
$this->addStream($stream, $listener, IOEvent::WRITE);
53+
$callback = function () use ($stream, $listener) {
54+
call_user_func($listener, $stream, $this);
55+
};
56+
57+
$event = new IOEvent($callback, $stream, IOEvent::WRITE);
58+
$this->loop->add($event);
59+
60+
$this->writeEvents[(int) $stream] = $event;
4761
}
4862

4963
/**
5064
* {@inheritdoc}
5165
*/
5266
public function removeReadStream($stream)
5367
{
54-
if (isset($this->readEvents[(int) $stream])) {
55-
$this->readEvents[(int) $stream]->stop();
56-
unset($this->readEvents[(int) $stream]);
68+
$key = (int) $stream;
69+
70+
if (isset($this->readEvents[$key])) {
71+
$this->readEvents[$key]->stop();
72+
unset($this->readEvents[$key]);
5773
}
5874
}
5975

@@ -62,9 +78,11 @@ public function removeReadStream($stream)
6278
*/
6379
public function removeWriteStream($stream)
6480
{
65-
if (isset($this->writeEvents[(int) $stream])) {
66-
$this->writeEvents[(int) $stream]->stop();
67-
unset($this->writeEvents[(int) $stream]);
81+
$key = (int) $stream;
82+
83+
if (isset($this->writeEvents[$key])) {
84+
$this->writeEvents[$key]->stop();
85+
unset($this->writeEvents[$key]);
6886
}
6987
}
7088

@@ -83,7 +101,18 @@ public function removeStream($stream)
83101
public function addTimer($interval, callable $callback)
84102
{
85103
$timer = new Timer($this, $interval, $callback, false);
86-
$this->setupTimer($timer);
104+
105+
$callback = function () use ($timer) {
106+
call_user_func($timer->getCallback(), $timer);
107+
108+
if ($this->isTimerActive($timer)) {
109+
$this->cancelTimer($timer);
110+
}
111+
};
112+
113+
$event = new TimerEvent($callback, $timer->getInterval());
114+
$this->timerEvents->attach($timer, $event);
115+
$this->loop->add($event);
87116

88117
return $timer;
89118
}
@@ -94,7 +123,14 @@ public function addTimer($interval, callable $callback)
94123
public function addPeriodicTimer($interval, callable $callback)
95124
{
96125
$timer = new Timer($this, $interval, $callback, true);
97-
$this->setupTimer($timer);
126+
127+
$callback = function () use ($timer) {
128+
call_user_func($timer->getCallback(), $timer);
129+
};
130+
131+
$event = new TimerEvent($callback, $interval, $interval);
132+
$this->timerEvents->attach($timer, $event);
133+
$this->loop->add($event);
98134

99135
return $timer;
100136
}
@@ -104,9 +140,9 @@ public function addPeriodicTimer($interval, callable $callback)
104140
*/
105141
public function cancelTimer(TimerInterface $timer)
106142
{
107-
if (isset($this->timers[$timer])) {
108-
$this->loop->remove($this->timers[$timer]);
109-
$this->timers->detach($timer);
143+
if (isset($this->timerEvents[$timer])) {
144+
$this->loop->remove($this->timerEvents[$timer]);
145+
$this->timerEvents->detach($timer);
110146
}
111147
}
112148

@@ -115,7 +151,7 @@ public function cancelTimer(TimerInterface $timer)
115151
*/
116152
public function isTimerActive(TimerInterface $timer)
117153
{
118-
return $this->timers->contains($timer);
154+
return $this->timerEvents->contains($timer);
119155
}
120156

121157
/**
@@ -147,11 +183,7 @@ public function run()
147183

148184
$this->nextTickQueue->tick();
149185

150-
if (
151-
!$this->readEvents
152-
&& !$this->writeEvents
153-
&& !$this->timers->count()
154-
) {
186+
if (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
155187
break;
156188
}
157189

@@ -166,55 +198,4 @@ public function stop()
166198
{
167199
$this->running = false;
168200
}
169-
170-
private function addStream($stream, $listener, $flags)
171-
{
172-
$listener = $this->wrapStreamListener($stream, $listener, $flags);
173-
$event = new IOEvent($listener, $stream, $flags);
174-
$this->loop->add($event);
175-
176-
if (($flags & IOEvent::READ) === $flags) {
177-
$this->readEvents[(int) $stream] = $event;
178-
} elseif (($flags & IOEvent::WRITE) === $flags) {
179-
$this->writeEvents[(int) $stream] = $event;
180-
}
181-
}
182-
183-
private function wrapStreamListener($stream, $listener, $flags)
184-
{
185-
if (($flags & IOEvent::READ) === $flags) {
186-
$removeCallback = array($this, 'removeReadStream');
187-
} elseif (($flags & IOEvent::WRITE) === $flags) {
188-
$removeCallback = array($this, 'removeWriteStream');
189-
}
190-
191-
return function ($event) use ($stream, $listener, $removeCallback) {
192-
call_user_func($listener, $stream);
193-
};
194-
}
195-
196-
private function setupTimer(TimerInterface $timer)
197-
{
198-
$dummyCallback = function () {};
199-
$interval = $timer->getInterval();
200-
201-
if ($timer->isPeriodic()) {
202-
$libevTimer = new TimerEvent($dummyCallback, $interval, $interval);
203-
} else {
204-
$libevTimer = new TimerEvent($dummyCallback, $interval);
205-
}
206-
207-
$libevTimer->setCallback(function () use ($timer) {
208-
call_user_func($timer->getCallback(), $timer);
209-
210-
if (!$timer->isPeriodic()) {
211-
$timer->cancel();
212-
}
213-
});
214-
215-
$this->timers->attach($timer, $libevTimer);
216-
$this->loop->add($libevTimer);
217-
218-
return $timer;
219-
}
220201
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy