From dbcd91e32f13b6d95de926528765c60932809cdd Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 4 Mar 2022 14:28:21 -0500 Subject: [PATCH] chore: switch to mutex --- packages/desktop/src/controller.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/desktop/src/controller.rs b/packages/desktop/src/controller.rs index 83246b14e..8a1494e52 100644 --- a/packages/desktop/src/controller.rs +++ b/packages/desktop/src/controller.rs @@ -2,8 +2,8 @@ use crate::desktop_context::{DesktopContext, UserWindowEvent}; use dioxus_core::*; use std::{ collections::HashMap, - sync::atomic::AtomicBool, - sync::{Arc, RwLock}, + sync::Arc, + sync::{atomic::AtomicBool, Mutex}, }; use wry::{ self, @@ -14,7 +14,7 @@ use wry::{ pub(super) struct DesktopController { pub(super) webviews: HashMap, pub(super) sender: futures_channel::mpsc::UnboundedSender, - pub(super) pending_edits: Arc>>, + pub(super) pending_edits: Arc>>, pub(super) quit_app_on_close: bool, pub(super) is_ready: Arc, } @@ -27,13 +27,13 @@ impl DesktopController { props: P, proxy: EventLoopProxy, ) -> Self { - let edit_queue = Arc::new(RwLock::new(Vec::new())); - let pending_edits = edit_queue.clone(); - + let edit_queue = Arc::new(Mutex::new(Vec::new())); let (sender, receiver) = futures_channel::mpsc::unbounded::(); - let return_sender = sender.clone(); + let pending_edits = edit_queue.clone(); + let return_sender = sender.clone(); let desktop_context_proxy = proxy.clone(); + std::thread::spawn(move || { // We create the runtime as multithreaded, so you can still "spawn" onto multiple threads let runtime = tokio::runtime::Builder::new_multi_thread() @@ -52,7 +52,7 @@ impl DesktopController { let edits = dom.rebuild(); edit_queue - .write() + .lock() .unwrap() .push(serde_json::to_string(&edits.edits).unwrap()); @@ -62,9 +62,10 @@ impl DesktopController { loop { dom.wait_for_work().await; let mut muts = dom.work_with_deadline(|| false); + while let Some(edit) = muts.pop() { edit_queue - .write() + .lock() .unwrap() .push(serde_json::to_string(&edit.edits).unwrap()); } @@ -93,7 +94,7 @@ impl DesktopController { pub(super) fn try_load_ready_webviews(&mut self) { if self.is_ready.load(std::sync::atomic::Ordering::Relaxed) { - let mut queue = self.pending_edits.write().unwrap(); + let mut queue = self.pending_edits.lock().unwrap(); let (_id, view) = self.webviews.iter_mut().next().unwrap(); while let Some(edit) = queue.pop() {