Переглянути джерело

Timer::add() should return integer

Timer::add() returned TimerInterface with reactPHP. But we need integer.
walkor 8 роки тому
батько
коміт
7f034dfd8a
1 змінених файлів з 21 додано та 4 видалено
  1. 21 4
      Events/React.php

+ 21 - 4
Events/React.php

@@ -26,6 +26,16 @@ class React implements LoopInterface
     protected $_loop = null;
 
     /**
+     * @var array
+     */
+    protected $_timerIdMap = array();
+
+    /**
+     * @var int
+     */
+    protected $_timerIdIndex = 0;
+
+    /**
      * React constructor.
      */
     public function __construct() {
@@ -58,13 +68,17 @@ class React implements LoopInterface
             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 @@ class React implements LoopInterface
                 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;