mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 00:25:35 +08:00
add timer tick
This commit is contained in:
32
src/ZM/Schedule/Timer.php
Normal file
32
src/ZM/Schedule/Timer.php
Normal 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 定时器回调函数错误');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user