From b6727bed80a81ff8561eddd71a76f8824ccbca34 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sun, 22 Jan 2017 23:22:13 +0100 Subject: [PATCH] Fixed timer implementation --- examples/errors.php | 19 +++++++++++++++++++ examples/timers.php | 8 +++++++- lib/Shell.php | 7 +++++-- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 examples/errors.php diff --git a/examples/errors.php b/examples/errors.php new file mode 100644 index 0000000..d6909c6 --- /dev/null +++ b/examples/errors.php @@ -0,0 +1,19 @@ +setPrompt("test>"); +$myShell->pushContext(new CatchAllContext()); +$myShell->run(); diff --git a/examples/timers.php b/examples/timers.php index 883b110..913ece0 100644 --- a/examples/timers.php +++ b/examples/timers.php @@ -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() diff --git a/lib/Shell.php b/lib/Shell.php index 99509c9..bdb001e 100644 --- a/lib/Shell.php +++ b/lib/Shell.php @@ -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)