use crossterm::event::{self, Event};
use std::time::Duration;
use tokio::time::{Interval, MissedTickBehavior, interval};
pub struct EventHandler {
tick_interval: Interval,
}
impl EventHandler {
pub fn new(tick_rate: Duration) -> Self {
let mut tick_interval = interval(tick_rate);
tick_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
Self { tick_interval }
}
pub async fn next(&mut self) -> Option<Event> {
tokio::select! {
_ = self.tick_interval.tick() => {
None
}
event = read_event() => {
event.ok()
}
}
}
}
async fn read_event() -> anyhow::Result<Event> {
loop {
if event::poll(Duration::from_millis(0))? {
return Ok(event::read()?);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
}