From ab994e3c827c65966ebc9cd2ac1d3a6048f042bc Mon Sep 17 00:00:00 2001 From: Frank Hamand Date: Mon, 13 Jun 2022 09:33:05 +0100 Subject: Batch key handling (#448) * Batch input events and only query once they are finished This simplifies the code a lot (no more bounded channel) and yields the same performance improvement with scroll wheel spam while fixing copy/paste * Clippy * fmt * Use blocking wait before emptying events channel This was causing a busy loop * Update query on filter mode change --- src/command/client/event.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/command/client/event.rs') diff --git a/src/command/client/event.rs b/src/command/client/event.rs index f818d728..8044e278 100644 --- a/src/command/client/event.rs +++ b/src/command/client/event.rs @@ -1,6 +1,6 @@ use std::{thread, time::Duration}; -use crossbeam_channel::{bounded, TrySendError}; +use crossbeam_channel::unbounded; use termion::{event::Event as TermEvent, event::Key, input::TermRead}; pub enum Event { @@ -35,22 +35,16 @@ impl Events { } pub fn with_config(config: Config) -> Events { - // Keep channel small so scroll events don't stack for ages. - let (tx, rx) = bounded(1); + let (tx, rx) = unbounded(); { let tx = tx.clone(); thread::spawn(move || { let tty = termion::get_tty().expect("Could not find tty"); for event in tty.events().flatten() { - if let Err(err) = tx.try_send(Event::Input(event)) { - if let TrySendError::Full(_) = err { - // Silently ignore send fails when buffer is full. - // This will most likely be scroll wheel spam and we can drop some events. - } else { - eprintln!("{}", err); - return; - } + if let Err(err) = tx.send(Event::Input(event)) { + eprintln!("{}", err); + return; } } }) @@ -69,4 +63,8 @@ impl Events { pub fn next(&self) -> Result, crossbeam_channel::RecvError> { self.rx.recv() } + + pub fn try_next(&self) -> Result, crossbeam_channel::TryRecvError> { + self.rx.try_recv() + } } -- cgit v1.3.1