Kraken\Loop\Timer\TimerBox::tick PHP Method

tick() public method

public tick ( )
    public function tick()
    {
        $time = $this->updateTime();
        $timers = $this->timers;
        $scheduler = $this->scheduler;
        while (!$scheduler->isEmpty()) {
            $timer = $scheduler->top();
            if (!isset($timers[$timer])) {
                $scheduler->extract();
                $timers->detach($timer);
                continue;
            }
            if ($timers[$timer] >= $time) {
                break;
            }
            $scheduler->extract();
            $callback = $timer->getCallback();
            $callback($timer);
            if ($timer->isPeriodic() && isset($timers[$timer])) {
                $timers[$timer] = $scheduledAt = $timer->getInterval() + $time;
                $scheduler->insert($timer, -$scheduledAt);
            } else {
                $timers->detach($timer);
            }
        }
    }

Usage Example

Example #1
0
 /**
  * @override
  * @inheritDoc
  */
 public function start()
 {
     if ($this->flowController->isRunning) {
         return;
     }
     // TODO KRF-107
     $this->addPeriodicTimer(1, function () {
         usleep(1);
     });
     $this->flowController->isRunning = true;
     $this->startTickQueue->tick();
     while ($this->flowController->isRunning) {
         $this->nextTickQueue->tick();
         $this->futureTickQueue->tick();
         $this->timers->tick();
         // Next-tick or future-tick queues have pending callbacks ...
         if (!$this->flowController->isRunning || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) {
             $timeout = 0;
         } else {
             if ($scheduledAt = $this->timers->getFirst()) {
                 $timeout = $scheduledAt - $this->timers->getTime();
                 $timeout = $timeout < 0 ? 0 : $timeout * self::MICROSECONDS_PER_SECOND;
             } else {
                 if ($this->readStreams || $this->writeStreams) {
                     $timeout = null;
                 } else {
                     break;
                 }
             }
         }
         $this->waitForStreamActivity($timeout);
     }
 }