diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-27 22:11:57 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-05-27 22:11:57 -0700 |
| commit | 8c0ddfb990057c5ae8d6d15ab0e0ec3f83b07ee4 (patch) | |
| tree | 68e525a8fad0acaada2d9d4993377222d7f7b8c0 | |
| parent | fbe36c4248ad9863de5cdd47956327b991e4e64b (diff) | |
| download | qc-8c0ddfb990057c5ae8d6d15ab0e0ec3f83b07ee4.tar.zst | |
Simplify the threading code.
| -rw-r--r-- | src/main.rs | 44 |
1 files 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(); }); |
