Skip to main content

zerolaunch_plugin_api/services/timer/
timer_manager.rs

1use crate::host::error::HostApiError;
2use crate::services::timer::types::{TimerCallback, TimerId, TimerMode};
3use async_trait::async_trait;
4use std::time::Duration;
5
6/// 定时器管理器 trait,定义平台无关的定时调度能力契约。
7/// 支持一次性定时和重复定时的创建与取消。
8/// 内部基于 tokio::sync::Notify(条件变量机制)实现触发。
9#[async_trait]
10pub trait TimerManager: Send + Sync {
11    /// 创建一个定时器。
12    ///
13    /// 参数:
14    /// - delay: 延迟时长(OneShot 为触发延迟,Interval 为重复间隔)
15    /// - mode: 定时模式(一次性或重复)
16    /// - callback: 触发时调用的回调函数,接收 TimerId 作为参数
17    ///
18    /// 返回:TimerId,可用于后续取消定时器。
19    async fn set_timer(
20        &self,
21        delay: Duration,
22        mode: TimerMode,
23        callback: TimerCallback,
24    ) -> Result<TimerId, HostApiError>;
25
26    /// 取消指定 ID 的定时器。
27    ///
28    /// 参数:id - 要取消的定时器 ID。
29    /// 返回:成功返回 Ok(()),定时器不存在时也返回 Ok(())。
30    async fn cancel_timer(&self, id: TimerId) -> Result<(), HostApiError>;
31
32    /// 取消所有定时器。
33    async fn cancel_all(&self) -> Result<(), HostApiError>;
34}