Add blocking_iter to block but return a try_iter chain

This commit is contained in:
Austen Adler 2021-05-14 20:00:21 -04:00
parent 8d6446d886
commit 2ec4e2ea1a
2 changed files with 7 additions and 17 deletions

View File

@ -280,17 +280,8 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
})?; })?;
if let Event::Input(key) = events.next()? {
app.error_msg = match handle_key(&mut app, key) {
// Exit the program
Ok(CalculatorResponse::Quit) => break 'outer,
Ok(CalculatorResponse::Continue) => None,
Err(e) => Some(format!("{}", e)),
};
}
// Slurp events without a redraw // Slurp events without a redraw
for e in events.try_iter() { for e in events.blocking_iter()? {
match e { match e {
Event::Input(key) => { Event::Input(key) => {
app.error_msg = match handle_key(&mut app, key) { app.error_msg = match handle_key(&mut app, key) {

View File

@ -1,6 +1,7 @@
use std::io; use std::io;
use std::iter;
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::mpsc::TryIter; use std::sync::mpsc::{Iter, TryIter};
use std::sync::{ use std::sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
@ -54,11 +55,9 @@ impl Events {
} }
} }
pub fn next(&self) -> Result<Event<KeyEvent>, mpsc::RecvError> { /// Returns a chain of the next element (blocking) and a TryIter of everything after it.
self.rx.recv() /// Useful for reading a single Iterator of all elements once after blocking, so there are not too many UI redraws.
} pub fn blocking_iter(&self) -> Result<iter::Chain<iter::Once<Event<KeyEvent>>,TryIter<Event<KeyEvent>>>, mpsc::RecvError> {
Ok(iter::once(self.rx.recv()?).chain(self.rx.try_iter()).into_iter())
pub fn try_iter(&self) -> TryIter<Event<KeyEvent>> {
self.rx.try_iter()
} }
} }