add timer tick

This commit is contained in:
Jerry
2023-02-10 13:12:20 +08:00
parent f03c0940da
commit df25aebd60
11 changed files with 132 additions and 6 deletions

32
src/ZM/Schedule/Timer.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace ZM\Schedule;
use ZM\Annotation\Framework\Tick;
use ZM\Framework;
class Timer
{
public static function tick(int $ms, callable $callback, int $times = 0): int
{
return Framework::getInstance()->getDriver()->getEventLoop()->addTimer($ms, $callback, $times);
}
public static function after(int $ms, callable $callback): int
{
return Framework::getInstance()->getDriver()->getEventLoop()->addTimer($ms, $callback);
}
public static function registerTick(Tick $v): void
{
if ($v->class !== '' && $v->method !== '') {
self::tick($v->tick_ms, [resolve($v->class), $v->method]);
} elseif (is_callable($v->method)) {
self::tick($v->tick_ms, $v->method);
} else {
logger()->warning('注册的 Tick 定时器回调函数错误');
}
}
}