use crate::actor::ActorId;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimerId(u64);
impl TimerId {
pub fn new() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
Self(COUNTER.fetch_add(1, Ordering::Relaxed))
}
}
impl Default for TimerId {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
pub struct TimerConfig {
pub id: TimerId,
pub actor_id: ActorId,
pub scheduled_at: Instant,
pub message: Box<dyn std::any::Any + Send>,
pub interval: Option<Duration>,
}
pub struct Timer {
id: TimerId,
actor_id: ActorId,
scheduled_at: Instant,
_message: Box<dyn std::any::Any + Send>,
interval: Option<Duration>,
}
impl Timer {
pub fn after(
duration: Duration,
actor_id: ActorId,
message: impl Into<Box<dyn std::any::Any + Send>>,
) -> Self {
Self {
id: TimerId::new(),
actor_id,
scheduled_at: Instant::now() + duration,
_message: message.into(),
interval: None,
}
}
pub fn every(
interval: Duration,
actor_id: ActorId,
message: impl Into<Box<dyn std::any::Any + Send>>,
) -> Self {
Self {
id: TimerId::new(),
actor_id,
scheduled_at: Instant::now() + interval,
_message: message.into(),
interval: Some(interval),
}
}
pub fn id(&self) -> TimerId {
self.id
}
pub fn actor_id(&self) -> ActorId {
self.actor_id
}
pub fn scheduled_at(&self) -> Instant {
self.scheduled_at
}
pub fn is_due(&self) -> bool {
Instant::now() >= self.scheduled_at
}
pub fn is_periodic(&self) -> bool {
self.interval.is_some()
}
pub fn reschedule(&mut self) {
if let Some(interval) = self.interval {
self.scheduled_at = Instant::now() + interval;
}
}
}