switch to async event loop to not use busy waiting

This commit is contained in:
Michael Zhang 2021-03-22 18:15:23 -05:00
parent ed1a07c0d0
commit c9598b5adc
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 11 additions and 9 deletions

1
Cargo.lock generated
View file

@ -527,6 +527,7 @@ checksum = "7c36c10130df424b2f3552fcc2ddcd9b28a27b1e54b358b45874f88d1ca6888c"
dependencies = [ dependencies = [
"bitflags 1.2.1", "bitflags 1.2.1",
"crossterm_winapi 0.7.0", "crossterm_winapi 0.7.0",
"futures-core",
"lazy_static", "lazy_static",
"libc", "libc",
"mio", "mio",

View file

@ -20,7 +20,7 @@ async-trait = "0.1.48"
cfg-if = "1.0.0" cfg-if = "1.0.0"
chrono = "0.4.19" chrono = "0.4.19"
chrono-humanize = "0.1.2" chrono-humanize = "0.1.2"
crossterm = "0.19.0" crossterm = { version = "0.19.0", features = ["event-stream"] }
fern = { version = "0.6.0", features = ["colored"] } fern = { version = "0.6.0", features = ["colored"] }
format-bytes = "0.2.2" format-bytes = "0.2.2"
futures = "0.3.13" futures = "0.3.13"

View file

@ -22,7 +22,7 @@ use anyhow::Result;
use chrono::{Local, TimeZone}; use chrono::{Local, TimeZone};
use crossterm::{ use crossterm::{
cursor, cursor,
event::{self, Event, KeyCode, KeyEvent}, event::{self, Event, KeyCode, KeyEvent, EventStream},
style, terminal, style, terminal,
}; };
use downcast_rs::Downcast; use downcast_rs::Downcast;
@ -63,6 +63,7 @@ pub async fn run_ui2(
let backend = CrosstermBackend::new(&mut stdout); let backend = CrosstermBackend::new(&mut stdout);
let mut term = Terminal::new(backend)?; let mut term = Terminal::new(backend)?;
let mut ui_events = EventStream::new();
let should_exit = Arc::new(AtomicBool::new(false)); let should_exit = Arc::new(AtomicBool::new(false));
@ -85,20 +86,20 @@ pub async fn run_ui2(
ui.draw(f); ui.draw(f);
})?; })?;
// handle events coming from the UI
if event::poll(FRAME_DURATION)? {
let event = event::read()?;
ui.process_event(event)?;
}
select! { select! {
// got an event from the mail thread // got an event from the mail thread
evt = mail2ui_rx.recv().fuse() => if let Some(evt) = evt { evt = mail2ui_rx.recv().fuse() => if let Some(evt) = evt {
ui.process_mail_event(evt); ui.process_mail_event(evt);
}, },
// got an event from the ui thread
evt = ui_events.next().fuse() => if let Some(evt) = evt {
let evt = evt?;
ui.process_event(evt)?;
}
// wait for approx 60fps // wait for approx 60fps
_ = time::sleep(FRAME_DURATION).fuse() => {}, // _ = time::sleep(FRAME_DURATION).fuse() => {},
} }
} }