2023-02-10 13:12:20 +08:00
|
|
|
<?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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 00:19:53 +08:00
|
|
|
public static function del(int $timer_id): void
|
|
|
|
|
{
|
|
|
|
|
Framework::getInstance()->getDriver()->getEventLoop()->clearTimer($timer_id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 13:12:20 +08:00
|
|
|
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 定时器回调函数错误');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|