Fixed timer implementation
This commit is contained in:
		
							
								
								
									
										19
									
								
								examples/errors.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								examples/errors.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Shell\Shell;
 | 
			
		||||
use NoccyLabs\Shell\Context;
 | 
			
		||||
 | 
			
		||||
class CatchAllContext extends Context
 | 
			
		||||
{
 | 
			
		||||
    public function execute($cmd, ...$arg)
 | 
			
		||||
    {
 | 
			
		||||
        throw new \Exception("Uh-oh! Error!");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
$myShell = new Shell();
 | 
			
		||||
$myShell->setPrompt("test>");
 | 
			
		||||
$myShell->pushContext(new CatchAllContext());
 | 
			
		||||
$myShell->run();
 | 
			
		||||
@@ -30,9 +30,15 @@ class MyShell extends Shell
 | 
			
		||||
        $this->pushContext($context);
 | 
			
		||||
        $this->updatePrompt();
 | 
			
		||||
 | 
			
		||||
        $this->addTimer(5000, function () {
 | 
			
		||||
        $t1 = $this->addTimer(5000, function () {
 | 
			
		||||
            echo "5 seconds\n";
 | 
			
		||||
        });
 | 
			
		||||
        $app = $this;
 | 
			
		||||
        $t2 = $this->addTimer(15000, function () use ($t1, $app) {
 | 
			
		||||
            echo "Removing timers...\n";
 | 
			
		||||
            $app->removeTimer($t1);
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    protected function updatePrompt()
 | 
			
		||||
 
 | 
			
		||||
@@ -137,6 +137,7 @@ class Shell
 | 
			
		||||
            private $userdata;
 | 
			
		||||
            public function __construct($interval, callable $handler, array $userdata) {
 | 
			
		||||
                $this->interval = $interval / 1000;
 | 
			
		||||
                $this->next = microtime(true) + $this->interval;
 | 
			
		||||
                $this->handler = $handler;
 | 
			
		||||
                $this->userdata = $userdata;
 | 
			
		||||
            }
 | 
			
		||||
@@ -157,9 +158,11 @@ class Shell
 | 
			
		||||
     *
 | 
			
		||||
     * @param Timer $timer
 | 
			
		||||
     */
 | 
			
		||||
    public function removeTimer(Timer $timer)
 | 
			
		||||
    public function removeTimer($timer)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $this->timers = array_filter($this->timers, function ($v) use ($timer) {
 | 
			
		||||
            return ($v !== $timer);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setPrompt($text)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user