Browse Source

offDealy offRepeat

walkor 2 years ago
parent
commit
c72cda85ce

+ 9 - 1
src/Events/Ev.php

@@ -73,7 +73,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             $this->eventTimer[$timerId]->stop();
@@ -86,6 +86,14 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function repeat(float $interval, $func, $args)
     {
         $event = new \EvTimer($interval, $interval, function () use ($func, $args) {

+ 10 - 2
src/Events/Event.php

@@ -93,7 +93,7 @@ class Event implements EventInterface
         $timerId = $this->timerId++;
         $event = new $className($this->eventBase, -1, $className::TIMEOUT, function () use ($func, $args, $timerId) {
             try {
-                $this->deleteTimer($timerId);
+                $this->offDelay($timerId);
                 $func(...$args);
             } catch (\Throwable $e) {
                 Worker::stopAll(250, $e);
@@ -109,7 +109,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             $this->eventTimer[$timerId]->del();
@@ -122,6 +122,14 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function repeat(float $interval, $func, $args)
     {
         $className = $this->eventClassName;

+ 10 - 3
src/Events/EventInterface.php

@@ -34,11 +34,18 @@ interface EventInterface
     public function repeat(float $interval, $func, $args);
 
     /**
-     * Delete a timer.
+     * Delete a delay timer.
      * @param $timerId
      * @return bool
      */
-    public function deleteTimer($timerId);
+    public function offDelay($timerId);
+
+    /**
+     * Delete a repeat timer.
+     * @param $timerId
+     * @return bool
+     */
+    public function offRepeat($timerId);
 
     /**
      * Execute a callback when a stream resource becomes readable or is closed for reading.
@@ -66,7 +73,7 @@ interface EventInterface
     /**
      * Cancel a callback of stream writable.
      * @param $stream
-     * @return mixed
+     * @return void
      */
     public function offWritable($stream);
 

+ 9 - 1
src/Events/Revolt.php

@@ -211,7 +211,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             $this->driver->cancel($this->eventTimer[$timerId]);
@@ -224,6 +224,14 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function deleteAllTimer()
     {
         foreach ($this->eventTimer as $cbId) {

+ 9 - 1
src/Events/Select.php

@@ -146,7 +146,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             unset($this->eventTimer[$timerId]);
@@ -158,6 +158,14 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function onReadable($stream, $func)
     {
         $count = \count($this->readFds);

+ 9 - 1
src/Events/Swoole.php

@@ -65,7 +65,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             $res = Timer::clear($timerId);
@@ -78,6 +78,14 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function repeat(float $interval, $func, $args)
     {
         if ($this->mapId > \PHP_INT_MAX) {

+ 19 - 2
src/Events/Swow.php

@@ -99,7 +99,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteTimer($timerId)
+    public function offDelay($timerId)
     {
         if (isset($this->eventTimer[$timerId])) {
             try {
@@ -115,10 +115,18 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
+    public function offRepeat($timerId)
+    {
+        return $this->offDelay($timerId);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function deleteAllTimer()
     {
         foreach ($this->eventTimer as $timerId) {
-            $this->deleteTimer($timerId);
+            $this->offDelay($timerId);
         }
     }
 
@@ -254,16 +262,25 @@ class Swow implements EventInterface
         Coroutine::getMain()->kill();
     }
 
+    /**
+     * @return void
+     */
     public function destroy()
     {
         $this->stop();
     }
 
+    /**
+     * @return void
+     */
     public function clearAllTimer()
     {
         $this->deleteAllTimer();
     }
 
+    /**
+     * @return void
+     */
     public function loop()
     {
         waitAll();

+ 13 - 11
src/Timer.php

@@ -106,8 +106,7 @@ class Timer
     public static function add(float $timeInterval, callable $func, $args = [], bool $persistent = true)
     {
         if ($timeInterval < 0) {
-            Worker::safeEcho(new Exception("bad time_interval"));
-            return false;
+            throw new \RuntimeException('$timeInterval can not less than 0');
         }
 
         if ($args === null) {
@@ -165,10 +164,12 @@ class Timer
             case Swoole::class:
                 System::sleep($delay);
                 return null;
+            // Swow
+            case Swow::class:
+                usleep($delay * 1000 * 1000);
+                return null;
         }
-        // Swow or non coroutine environment
-        usleep($delay * 1000 * 1000);
-        return null;
+        throw new \RuntimeException('Timer::sleep() require revolt/event-loop. Please run command "composer install revolt/event-loop" and restart workerman');
     }
 
     /**
@@ -215,16 +216,17 @@ class Timer
     public static function del($timerId)
     {
         if (self::$event) {
-            return self::$event->deleteTimer($timerId);
+            return self::$event->offDelay($timerId);
         }
-
         foreach(self::$tasks as $runTime => $taskData) 
         {
-            if(array_key_exists($timerId, $taskData)) unset(self::$tasks[$runTime][$timerId]);
+            if(array_key_exists($timerId, $taskData)) {
+                unset(self::$tasks[$runTime][$timerId]);
+            }
+        }
+        if(array_key_exists($timerId, self::$status)) {
+            unset(self::$status[$timerId]);
         }
-
-        if(array_key_exists($timerId, self::$status)) unset(self::$status[$timerId]);
-
         return true;
     }