ic_timers/platform.rs
1//! Direct boundary to the Internet Computer timer provider.
2//!
3//! Recurrence, identity, arbitration, inventory, and measurement belong above
4//! this module.
5
6use ic_cdk_timers::{
7 TimerId as CdkTimerId, clear_timer as cdk_clear_timer, set_timer as cdk_set_timer,
8};
9use std::{future::Future, time::Duration};
10
11/// Opaque handle for one armed platform timer.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct TimerHandle(CdkTimerId);
14
15/// Arm one asynchronous one-shot callback.
16///
17/// Higher-level policies should use one-shots even for recurring work so they
18/// can control exactly when a successor becomes authoritative.
19pub fn set_timer(delay: Duration, task: impl Future<Output = ()> + 'static) -> TimerHandle {
20 TimerHandle(cdk_set_timer(delay, task))
21}
22
23/// Clear one still-armed platform callback.
24pub fn clear_timer(handle: TimerHandle) {
25 cdk_clear_timer(handle.0);
26}