use crate::application::{BoxFuture, Recipe, Subscription};
use alloc::boxed::Box;
use core::hash::{Hash, Hasher};
use embassy_time::{Duration, Timer};
pub struct Tick<M>
where
M: Clone + 'static,
{
pub duration: Duration,
pub message: M,
}
impl<M> Hash for Tick<M>
where
M: Clone + Hash + 'static,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.duration.as_ticks().hash(state);
self.message.hash(state);
}
}
impl<M> Recipe for Tick<M>
where
M: Clone + Hash + 'static,
{
type Message = M;
fn next(&mut self) -> Option<BoxFuture<M>> {
let duration = self.duration;
let message = self.message.clone();
Some(Box::pin(async move {
Timer::after(duration).await;
message
}))
}
}
#[must_use]
pub fn every<M>(duration: Duration, message: M) -> Subscription<M>
where
M: Clone + Hash + 'static,
{
Subscription::from_recipe(Tick { duration, message })
}