From 7f034dfd8a2bf497c12e9796c394e4b35eae8af1 Mon Sep 17 00:00:00 2001 From: walkor Date: Thu, 9 Feb 2017 15:24:56 +0800 Subject: [PATCH] Timer::add() should return integer Timer::add() returned TimerInterface with reactPHP. But we need integer. --- Events/React.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Events/React.php b/Events/React.php index be8c87fb7..79e6f96e4 100644 --- a/Events/React.php +++ b/Events/React.php @@ -25,6 +25,16 @@ class React implements LoopInterface */ protected $_loop = null; + /** + * @var array + */ + protected $_timerIdMap = array(); + + /** + * @var int + */ + protected $_timerIdIndex = 0; + /** * React constructor. */ @@ -58,13 +68,17 @@ public function add($fd, $flag, $func, $args = array()) case EventInterface::EV_SIGNAL: return $this->_loop->addSignal($fd, $func); case EventInterface::EV_TIMER: - return $this->_loop->addPeriodicTimer($fd, function() use ($func, $args) { + $timer_obj = $this->_loop->addPeriodicTimer($fd, function() use ($func, $args) { call_user_func_array($func, $args); }); + $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj; + return $this->_timerIdIndex; case EventInterface::EV_TIMER_ONCE: - return $this->_loop->addTimer($fd, function() use ($func, $args) { + $timer_obj = $this->_loop->addTimer($fd, function() use ($func, $args) { call_user_func_array($func, $args); }); + $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj; + return $this->_timerIdIndex; } return false; } @@ -87,8 +101,11 @@ public function del($fd, $flag) return $this->_loop->removeSignal($fd); case EventInterface::EV_TIMER: case EventInterface::EV_TIMER_ONCE; - if ($fd !== null){ - return $this->_loop->cancelTimer($fd); + if (isset($this->_timerIdMap[$fd])){ + $timer_obj = $this->_timerIdMap[$fd]; + unset($this->_timerIdMap[$fd]); + $this->_loop->cancelTimer($timer_obj); + return true; } } return false;