Skip to content

Commit

Permalink
update input algorithm to be similar to joshuto
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Nov 20, 2023
1 parent 4e07af5 commit 2f1fc1c
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/client/event/app_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Config {}
pub struct Events {
pub event_tx: mpsc::Sender<AppEvent>,
event_rx: mpsc::Receiver<AppEvent>,
pub input_tx: mpsc::SyncSender<()>,
pub input_tx: mpsc::Sender<()>,
}

impl Events {
Expand All @@ -39,9 +39,12 @@ impl Events {
}

pub fn with_config() -> Self {
let (input_tx, input_rx) = mpsc::sync_channel(1);
let (input_tx, input_rx) = mpsc::channel();
let (event_tx, event_rx) = mpsc::channel();

// edge case that starts off the input thread
let _ = input_tx.send(());

// signal thread
let event_tx2 = event_tx.clone();
let _ = thread::spawn(move || {
Expand All @@ -60,25 +63,11 @@ impl Events {
let _ = thread::spawn(move || {
let stdin = io::stdin();
let mut events = stdin.events();
match events.next() {
Some(event) => match event {
Ok(event) => {
if let Err(e) = event_tx2.send(AppEvent::Termion(event)) {
eprintln!("Input thread send err: {:#?}", e);
return;
}
}
Err(_) => return,
},
None => return,
}

while input_rx.recv().is_ok() {
loop {
let _ = input_rx.recv();
if let Some(Ok(event)) = events.next() {
if let Err(e) = event_tx2.send(AppEvent::Termion(event)) {
eprintln!("Input thread send err: {:#?}", e);
return;
}
let _ = event_tx2.send(AppEvent::Termion(event));
}
}
});
Expand All @@ -99,6 +88,10 @@ impl Events {
}

pub fn flush(&self) {
let _ = self.input_tx.send(());
loop {
if self.input_tx.send(()).is_ok() {
break;
}
}
}
}

0 comments on commit 2f1fc1c

Please sign in to comment.