use super::TimestampNanos;
#[inline]
pub fn async_execute(task: impl FnOnce() + 'static) -> ic_cdk_timers::TimerId {
ic_cdk_timers::set_timer(std::time::Duration::ZERO, task)
}
pub use ic_cdk_timers::TimerId;
pub fn schedule_start(
schedule: Option<TimestampNanos>,
task: impl FnMut() + 'static,
) -> Option<TimerId> {
schedule.and_then(|interval| {
Some(ic_cdk_timers::set_timer_interval(
std::time::Duration::from_nanos(interval),
task,
))
})
}
pub fn schedule_stop(timer_id: Option<TimerId>) {
if let Some(timer_id) = timer_id {
ic_cdk_timers::clear_timer(timer_id)
}
}
pub trait Schedulable {
fn schedule_find(&self) -> Option<TimestampNanos>;
fn schedule_replace(&mut self, schedule: Option<TimestampNanos>);
}
#[derive(candid::CandidType, candid::Deserialize, Debug, Clone, Default)]
pub struct Schedule(Option<TimestampNanos>);
impl Schedulable for Schedule {
fn schedule_find(&self) -> Option<TimestampNanos> {
self.0
}
fn schedule_replace(&mut self, schedule: Option<TimestampNanos>) {
self.0 = schedule
}
}