From 59b3cdd0dc9a9176da1d793c02bfee56f0473c7d Mon Sep 17 00:00:00 2001 From: Byson94 Date: Fri, 24 Oct 2025 21:00:03 +0530 Subject: [PATCH] feat: implement recv drain in plugin load func --- crates/ewwii/src/app.rs | 82 +++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/crates/ewwii/src/app.rs b/crates/ewwii/src/app.rs index 27dc223..e587dcd 100644 --- a/crates/ewwii/src/app.rs +++ b/crates/ewwii/src/app.rs @@ -844,50 +844,52 @@ impl App { let cp = self.config_parser.clone(); let wgs = self.widget_reg_store.clone(); - glib::MainContext::default().spawn_local(async move { - let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - while let Ok(req) = rx.recv() { - match req { - PluginRequest::RhaiEngineAct(func) => { - let mut cp = cp.borrow_mut(); - cp.action_with_engine(func); - } - PluginRequest::RegisterFunc((name, func)) => { - if let Err(e) = shared_utils::slib_store::register_functions(name, func) - { - log::error!("Error registering function: {}", e); - } - } - PluginRequest::ListWidgetIds(res_tx) => { - let wgs_guard = wgs.lock().unwrap(); - if let Some(wgs_brw) = wgs_guard.as_ref() { - let output: Vec = wgs_brw.widgets.keys().cloned().collect(); - if let Err(e) = res_tx.send(output) { - log::error!("Failed to send window list: {}", e); - } - } - } - PluginRequest::WidgetRegistryAct(func) => { - let mut wgs_guard = wgs.lock().unwrap(); - if let Some(ref mut registry) = *wgs_guard { - let repr_map: HashMap = registry - .widgets - .iter_mut() - .map(|(id, entry)| (*id, &mut entry.widget)) - .collect(); - - func(&mut ewwii_plugin_api::widget_backend::WidgetRegistryRepr { - widgets: repr_map, - }); - } - } + let handle_request = move |req: PluginRequest| { + match req { + PluginRequest::RhaiEngineAct(func) => { + cp.borrow_mut().action_with_engine(func); + } + PluginRequest::RegisterFunc((name, func)) => { + if let Err(e) = shared_utils::slib_store::register_functions(name, func) { + log::error!("Error registering function: {}", e); } } - })); + PluginRequest::ListWidgetIds(res_tx) => { + let wgs_guard = wgs.lock().unwrap(); + if let Some(wgs_brw) = wgs_guard.as_ref() { + let output: Vec = wgs_brw.widgets.keys().cloned().collect(); + let _ = res_tx.send(output); + } + } + PluginRequest::WidgetRegistryAct(func) => { + let mut wgs_guard = wgs.lock().unwrap(); + if let Some(ref mut registry) = *wgs_guard { + let repr_map: HashMap = registry + .widgets + .iter_mut() + .map(|(id, entry)| (*id, &mut entry.widget)) + .collect(); - if let Err(e) = result { - log::error!("Uncaught panic in GLib task: {:?}", e); + func(&mut ewwii_plugin_api::widget_backend::WidgetRegistryRepr { + widgets: repr_map, + }); + } + } } + }; + + // quick drain + while let Ok(req) = rx.try_recv() { + handle_request(req); + } + + // handling requests that arrive later + glib::MainContext::default().spawn_local(async move { + let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + while let Ok(req) = rx.recv() { + handle_request(req); + } + })); }); Ok(())