use std::fmt::Debug;
use tokio::sync::mpsc::Sender;
use crate::{throws, CommonResult, TerminalWindowMainThreadSignal};
#[derive(Debug, Default)]
pub struct Animator {
pub animator_kill_channel: Option<Sender<()>>,
}
impl Animator {
pub fn start<AS>(
&mut self,
channel_sender: Sender<TerminalWindowMainThreadSignal<AS>>,
start_animator_task: fn(Sender<TerminalWindowMainThreadSignal<AS>>) -> Sender<()>,
) where
AS: Debug + Default + Clone + Sync + Send,
{
if self.is_animation_started() {
return;
}
self.animator_kill_channel = Some(start_animator_task(channel_sender));
}
#[must_use]
pub fn is_animation_started(&self) -> bool {
matches!(&self.animator_kill_channel, Some(_handle))
}
#[must_use]
pub fn is_animation_not_started(&self) -> bool { !self.is_animation_started() }
pub fn stop(&mut self) -> CommonResult<()> {
throws!({
if let Some(kill_channel) = &self.animator_kill_channel {
let kill_channel_clone = kill_channel.clone();
tokio::spawn(async move {
kill_channel_clone.send(()).await.ok();
});
self.animator_kill_channel = None;
}
});
}
}