From 85f88fabc251441492c7792f52f10b7480161a5a Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 10 Nov 2022 21:54:34 +0100 Subject: [PATCH] fix: freezes on blocking operations very cheap fix: spawn a thread to send the message. This is very wasteful, and should instead be done by spawning a task on the tokio runtime. --- src/gui/mod.rs | 41 +++++++++++++++++++++++++++++------------ src/gui/scaffold.rs | 3 +-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 842e2b8..813d1fc 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -71,24 +71,41 @@ impl App { } } - pub fn save_all_panels(&self) { - if let Err(e) = self.view.op.blocking_send( - crate::worker::visualizer::BackgroundAction::UpdateAllPanels { panels: self.panels.clone() } - ) { - error!(target: "app", "Could not save panels: {:?}", e); - } + pub fn save_all_panels(&self) { // TODO can probably remove this and invoke op() directly + let msg = BackgroundAction::UpdateAllPanels { panels: self.panels.clone() }; + self.op(msg); } pub fn refresh_data(&self) { - if let Err(e) = self.view.flush.blocking_send(()) { - error!(target: "app", "Could not request flush: {:?}", e); - } + let flush_clone = self.view.flush.clone(); + std::thread::spawn(move || { + if let Err(e) = flush_clone.blocking_send(()) { + error!(target: "app-background", "Could not request flush: {:?}", e); + } + }); } pub fn op(&self, op: BackgroundAction) { - if let Err(e) = self.view.op.blocking_send(op) { - error!(target: "app", "Could not send operation: {:?}", e); - } + let op_clone = self.view.op.clone(); + std::thread::spawn(move || { + if let Err(e) = op_clone.blocking_send(op) { + error!(target: "app-background", "Could not send operation: {:?}", e); + } + }); + } + + fn update_db_uri(&self) { + let db_uri_clone = self.db_uri_tx.clone(); + let db_uri_str = self.db_uri.clone(); + let flush_clone = self.view.flush.clone(); + std::thread::spawn(move || { + if let Err(e) = db_uri_clone.blocking_send(db_uri_str) { + error!(target: "app-background", "Could not send new db uri : {:?}", e); + } + if let Err(e) = flush_clone.blocking_send(()) { + error!(target: "app-background", "Could not request data flush : {:?}", e); + } + }); } } diff --git a/src/gui/scaffold.rs b/src/gui/scaffold.rs index 97d9faa..de88c25 100644 --- a/src/gui/scaffold.rs +++ b/src/gui/scaffold.rs @@ -313,8 +313,7 @@ pub fn header(app: &mut App, ui: &mut Ui, frame: &mut Frame) { .hint_text("db uri") .show(ui); if ui.button("connect").clicked() { - app.db_uri_tx.blocking_send(app.db_uri.clone()).unwrap(); // TODO!!! - app.refresh_data(); + app.update_db_uri(); } ui.separator(); let last_edit = app.edit; // replace panels when going into edit mode