Skip to content

Commit c61c806

Browse files
committed
[EventLoop] Reimplement timers for LibEvLoop.
1 parent 34f6c53 commit c61c806

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

LibEvLoop.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
namespace React\EventLoop;
44

5+
use SplObjectStorage;
6+
use libev\TimerEvent as LibEvTimer;
7+
use React\EventLoop\Timer\Timer;
8+
use React\EventLoop\Timer\TimerInterface;
9+
510
/**
611
* @see https://github.com/m4rw3r/php-libev
712
* @see https://gist.github.com/1688204
813
*/
914
class LibEvLoop implements LoopInterface
1015
{
1116
private $loop;
17+
private $timers;
1218
private $readEvents = array();
1319
private $writeEvents = array();
14-
private $timers = array();
1520

1621
public function __construct()
1722
{
1823
$this->loop = new \libev\EventLoop();
24+
$this->timers = new SplObjectStorage();
1925
}
2026

2127
public function addReadStream($stream, $listener)
@@ -85,49 +91,51 @@ private function wrapStreamListener($stream, $listener, $flags)
8591

8692
public function addTimer($interval, $callback)
8793
{
88-
$dummyCallback = function () {};
89-
$timer = new \libev\TimerEvent($dummyCallback, $interval);
94+
$timer = new Timer($this, $interval, $callback, false);
95+
$this->setupTimer($timer);
9096

91-
return $this->createTimer($timer, $callback, false);
97+
return $timer;
9298
}
9399

94100
public function addPeriodicTimer($interval, $callback)
95101
{
96-
$dummyCallback = function () {};
97-
$timer = new \libev\TimerEvent($dummyCallback, $interval, $interval);
102+
$timer = new Timer($this, $interval, $callback, true);
103+
$this->setupTimer($timer);
98104

99-
return $this->createTimer($timer, $callback, true);
105+
return $timer;
100106
}
101107

102-
public function cancelTimer($signature)
108+
public function cancelTimer($timer)
103109
{
104-
$this->loop->remove($this->timers[$signature]);
105-
unset($this->timers[$signature]);
110+
if (isset($this->timers[$timer])) {
111+
$this->loop->remove($this->timers[$timer]);
112+
$this->timers->detach($timer);
113+
}
106114
}
107115

108-
private function createTimer($timer, $callback, $periodic)
116+
private function setupTimer(TimerInterface $timer)
109117
{
110-
$signature = spl_object_hash($timer);
111-
$callback = $this->wrapTimerCallback($signature, $callback, $periodic);
112-
$timer->setCallback($callback);
118+
$dummyCallback = function () {};
119+
$interval = $timer->getInterval();
113120

114-
$this->timers[$signature] = $timer;
115-
$this->loop->add($timer);
121+
if ($timer->isPeriodic()) {
122+
$libevTimer = new \libev\TimerEvent($dummyCallback, $interval, $interval);
123+
} else {
124+
$libevTimer = new \libev\TimerEvent($dummyCallback, $interval);
125+
}
116126

117-
return $signature;
118-
}
127+
$libevTimer->setCallback(function () use ($timer) {
128+
call_user_func($timer->getCallback(), $timer);
119129

120-
private function wrapTimerCallback($signature, $callback, $periodic)
121-
{
122-
$loop = $this;
130+
if ($timer->isPeriodic() === false) {
131+
$timer->cancel();
132+
}
133+
});
123134

124-
return function ($event) use ($signature, $callback, $periodic, $loop) {
125-
call_user_func($callback, $signature, $loop);
135+
$this->timers->attach($timer, $libevTimer);
136+
$this->loop->add($libevTimer);
126137

127-
if (!$periodic) {
128-
$loop->cancelTimer($signature);
129-
}
130-
};
138+
return $timer;
131139
}
132140

133141
public function tick()

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