Implemented timers

This commit is contained in:
2016-11-02 14:22:53 +01:00
parent ea09a15963
commit 427cac578a
2 changed files with 79 additions and 1 deletions

View File

@ -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);
}