Skip to content

Commit

Permalink
Timer::add() should return integer
Browse files Browse the repository at this point in the history
Timer::add() returned TimerInterface with reactPHP. But we need integer.
  • Loading branch information
walkor authored Feb 9, 2017
1 parent c92ae3c commit 7f034df
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Events/React.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class React implements LoopInterface
*/
protected $_loop = null;

/**
* @var array
*/
protected $_timerIdMap = array();

/**
* @var int
*/
protected $_timerIdIndex = 0;

/**
* React constructor.
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 7f034df

Please sign in to comment.