Implemented timers
This commit is contained in:
		@@ -16,6 +16,8 @@ class Shell
 | 
			
		||||
    protected $contextStack = [];
 | 
			
		||||
 | 
			
		||||
    protected $listeners = [];
 | 
			
		||||
    
 | 
			
		||||
    protected $timers = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
@@ -126,7 +128,26 @@ class Shell
 | 
			
		||||
     */
 | 
			
		||||
    public function addTimer($interval, callable $handler, array $userdata=[])
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $timer = new class($interval, $handler, $userdata) {
 | 
			
		||||
            private $next;
 | 
			
		||||
            private $interval;
 | 
			
		||||
            private $handler;
 | 
			
		||||
            private $userdata;
 | 
			
		||||
            public function __construct($interval, callable $handler, array $userdata) {
 | 
			
		||||
                $this->interval = $interval / 1000;
 | 
			
		||||
                $this->handler = $handler;
 | 
			
		||||
                $this->userdata = $userdata;
 | 
			
		||||
            }
 | 
			
		||||
            public function update() {
 | 
			
		||||
                $now = microtime(true);
 | 
			
		||||
                if ($now > $this->next) {
 | 
			
		||||
                    $this->next = $now + $this->interval;
 | 
			
		||||
                    call_user_func($this->handler, $this->userdata);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
        $this->timers[] = $timer;
 | 
			
		||||
        return $timer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -287,6 +308,9 @@ class Shell
 | 
			
		||||
            // Execute the buffer
 | 
			
		||||
            ob_start();
 | 
			
		||||
            $this->dispatchEvent("update");
 | 
			
		||||
            foreach ($this->timers as $timer) {
 | 
			
		||||
                $timer->update();
 | 
			
		||||
            }
 | 
			
		||||
            if ($buffer) {
 | 
			
		||||
                $this->executeBuffer($buffer);
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user