use super::{Model, Msg, StartStopMsg, TickMsg, TimeoutMsg, now};
use crate::Cmd;
impl Model {
pub fn start(&self) -> Cmd<Msg> {
self.start_stop(true)
}
pub fn stop(&self) -> Cmd<Msg> {
self.start_stop(false)
}
pub fn toggle(&self) -> Cmd<Msg> {
self.start_stop(!self.running())
}
pub(super) 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))
}
pub(super) 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,
}))
}
}