Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit refreshing the UI to 60 times per second. #108

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
/contrib/man/*.roff

.vscode/
13 changes: 9 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crossterm::event::{Event as TermEvent, EventStream, KeyEvent};
use futures::{future::FutureExt, StreamExt};
use tokio::sync::mpsc;

const TICK_RATE: u64 = 250;
const TICK_RATE: u64 = 150;
const FRAME_RATE: f64 = 60.0;

pub enum Event {
Key(KeyEvent),
Render,
Tick,
}

Expand All @@ -21,10 +23,12 @@ impl Events {

tokio::task::spawn(async move {
let mut stream = EventStream::new();
let mut interval = tokio::time::interval(Duration::from_millis(TICK_RATE));
let mut render_interval = tokio::time::interval(Duration::from_secs_f64(1.0 / FRAME_RATE));
let mut tick_interval = tokio::time::interval(Duration::from_millis(TICK_RATE));

loop {
let delay = interval.tick();
let tick = tick_interval.tick();
let render = render_interval.tick();
let event = stream.next().fuse();

tokio::select! {
Expand All @@ -34,7 +38,8 @@ impl Events {
}
}

_ = delay => { let _ = tx.send(Event::Tick).await; },
_ = render => { let _ = tx.send(Event::Render).await; }
_ = tick => { let _ = tx.send(Event::Tick).await; },
}
}
});
Expand Down
Loading
Loading