use super::Timestamp;
#[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<Timestamp>,
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<Timestamp>;
fn schedule_replace(&mut self, schedule: Option<Timestamp>);
}
#[derive(candid::CandidType, candid::Deserialize, Debug, Clone, Default)]
pub struct Schedule(Option<Timestamp>);
impl Schedulable for Schedule {
fn schedule_find(&self) -> Option<Timestamp> {
self.0
}
fn schedule_replace(&mut self, schedule: Option<Timestamp>) {
self.0 = schedule
}
}