use crate::countdown::service::CountdownService; #[derive(Clone, Debug)] pub struct ClockAnchor { pub boot_instant: tokio::time::Instant, pub boot_epoch_ms: u128, } impl ClockAnchor { pub fn new() -> Self { Self { boot_instant: tokio::time::Instant::now(), boot_epoch_ms: std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_millis(), } } pub fn instant_to_epoch_ms(&self, instant: tokio::time::Instant) -> u128 { if let Some(delta) = instant.checked_duration_since(self.boot_instant) { self.boot_epoch_ms + delta.as_millis() } else { let delta = self.boot_instant.duration_since(instant).as_millis(); self.boot_epoch_ms.saturating_sub(delta) } } } //TODO: implement the handling of multiple countdowns #[derive(Debug)] pub struct AppState { pub clock_anchor: ClockAnchor, pub countdown_service: CountdownService, } impl AppState { pub fn new() -> Self { Self { clock_anchor: ClockAnchor::new(), countdown_service: CountdownService::new(), } } }