From 8c0ddfb990057c5ae8d6d15ab0e0ec3f83b07ee4 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Mon, 27 May 2024 22:11:57 -0700 Subject: Simplify the threading code. --- src/main.rs | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f4291e..e505ee6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ fn main() -> ExitCode { let output = Entry::builder().editable(false).build(); grid.attach(&output, 0, 1, 1, 1); - let (ping_send, ping_recv) = std::sync::mpsc::sync_channel(1); + let (ping_send, ping_recv) = async_channel::bounded(1); let mailbox = Arc::new(Mutex::new(None)); let key_controller = EventControllerKey::new(); @@ -58,29 +58,27 @@ fn main() -> ExitCode { ); window.add_controller(key_controller); - let (output_send, output_recv) = async_channel::bounded(1); - let (confirm_send, confirm_recv) = async_channel::bounded(1); - - gtk4::gio::spawn_blocking(move || loop { - let Ok(_) = ping_recv.recv() else { - return; - }; - let Some(input) = get_mail(mailbox.as_ref()) else { - continue; - }; - let Some(result) = parser::parse(&input) else { - continue; - }; - output_send.send_blocking(result).unwrap(); - confirm_recv.recv_blocking().unwrap(); - }); - - glib::spawn_future_local(clone!(@weak output => async move { - while let Ok(result) = output_recv.recv().await { - output.set_text(&format!("{}", result)); - confirm_send.send(()).await.unwrap(); + glib::spawn_future_local(async move { + loop { + if ping_recv.recv().await.is_err() { + break; + } + let Some(input) = get_mail(mailbox.as_ref()) else { + continue; + }; + let (output_send, output_recv) = async_channel::bounded(1); + let thread_handle = gtk4::gio::spawn_blocking(move || { + if let Some(result) = parser::parse(&input) { + output_send.send_blocking(result).unwrap(); + } + }); + let _ = thread_handle.await; + let Ok(result) = output_recv.try_recv() else { + continue; + }; + output.set_text(&format!("{result}")); } - })); + }); window.present(); }); -- cgit v1.3.1