use super::{next_id, now};
use crate::{Cmd, Msg as TeaMsg, batch};
use std::fmt::Write as _;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StartStopMsg {
pub id: u64,
running: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TickMsg {
pub id: u64,
pub timeout: bool,
tag: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TimeoutMsg {
pub id: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Msg {
StartStop(StartStopMsg),
Tick(TickMsg),
Timeout(TimeoutMsg),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
pub timeout: Duration,
pub interval: Duration,
id: u64,
tag: u64,
running: bool,
}
impl Model {
#[must_use]
pub fn new(timeout: Duration) -> Self {
Self {
timeout,
interval: Duration::from_secs(1),
id: next_id(),
tag: 0,
running: true,
}
}
#[must_use]
pub const fn with_interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
#[must_use]
pub const fn running(&self) -> bool {
self.running && !self.timedout()
}
#[must_use]
pub const fn timedout(&self) -> bool {
self.timeout.is_zero()
}
#[must_use]
pub fn init(&self) -> Cmd<Msg> {
self.tick()
}
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
match msg {
TeaMsg::App(Msg::StartStop(m)) if self.is_mine(m.id) => self.start_or_stop(m.running),
TeaMsg::App(Msg::Tick(m)) if self.running() && self.is_mine(m.id) => {
self.on_tick(m.tag)
}
_ => Cmd::none(),
}
}
const fn is_mine(&self, id: u64) -> bool {
id == 0 || id == self.id
}
fn start_or_stop(&mut self, running: bool) -> Cmd<Msg> {
if self.running == running {
return Cmd::none();
}
self.running = running;
if running { self.tick() } else { Cmd::none() }
}
fn on_tick(&mut self, tag: u64) -> Cmd<Msg> {
if tag > 0 && tag != self.tag {
return Cmd::none();
}
self.timeout = self.timeout.saturating_sub(self.interval);
self.tag += 1;
batch([self.tick(), self.timed_out()])
}
#[must_use]
pub fn view(&self) -> String {
go_duration(self.timeout)
}
#[must_use]
pub fn start(&self) -> Cmd<Msg> {
self.start_stop(true)
}
#[must_use]
pub fn stop(&self) -> Cmd<Msg> {
self.start_stop(false)
}
#[must_use]
pub fn toggle(&self) -> Cmd<Msg> {
self.start_stop(!self.running())
}
fn tick(&self) -> Cmd<Msg> {
let msg = TickMsg {
id: self.id,
timeout: self.timedout(),
tag: self.tag,
};
crate::tick(self.interval, move |_| Msg::Tick(msg))
}
fn timed_out(&self) -> Cmd<Msg> {
if self.timedout() {
now(Msg::Timeout(TimeoutMsg { id: self.id }))
} else {
Cmd::none()
}
}
fn start_stop(&self, running: bool) -> Cmd<Msg> {
if running && self.timedout() {
return Cmd::none();
}
now(Msg::StartStop(StartStopMsg {
id: self.id,
running,
}))
}
}
pub(super) fn go_duration(d: Duration) -> String {
let nanos = d.as_nanos();
if nanos == 0 {
return "0s".into();
}
if nanos < 1_000_000_000 {
let (unit, scale) = match nanos {
0..1_000 => ("ns", 1),
1_000..1_000_000 => ("µs", 1_000),
_ => ("ms", 1_000_000),
};
return format!("{}{unit}", decimal(nanos, scale));
}
let secs = d.as_secs();
let (h, m) = (secs / 3600, secs / 60 % 60);
let mut out = String::new();
if h > 0 {
let _ = write!(out, "{h}h");
}
if h > 0 || m > 0 {
let _ = write!(out, "{m}m");
}
let rest = u128::from(secs % 60) * 1_000_000_000 + u128::from(d.subsec_nanos());
out + &decimal(rest, 1_000_000_000) + "s"
}
fn decimal(value: u128, scale: u128) -> String {
let (whole, frac) = (value / scale, value % scale);
if frac == 0 {
return whole.to_string();
}
let digits = scale.ilog10() as usize;
let frac = format!("{frac:0digits$}");
format!("{whole}.{}", frac.trim_end_matches('0'))
}
#[cfg(test)]
mod tests;