chore: switch to mutex

This commit is contained in:
Jonathan Kelley committed 2022-03-04 14:28:21 -05:00
1 parent c466425b82
commit dbcd91e32f
1 file changed
+11 -10
+11 -10
View File
@@ -2,8 +2,8 @@ use crate::desktop_context::{DesktopContext, UserWindowEvent};
use dioxus_core::*; use dioxus_core::*;
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::atomic::AtomicBool, sync::Arc,
sync::{Arc, RwLock}, sync::{atomic::AtomicBool, Mutex},
}; };
use wry::{ use wry::{
self, self,
@@ -14,7 +14,7 @@ use wry::{
pub(super) struct DesktopController { pub(super) struct DesktopController {
pub(super) webviews: HashMap<WindowId, WebView>, pub(super) webviews: HashMap<WindowId, WebView>,
pub(super) sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>, pub(super) sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
pub(super) pending_edits: Arc<RwLock<Vec<String>>>, pub(super) pending_edits: Arc<Mutex<Vec<String>>>,
pub(super) quit_app_on_close: bool, pub(super) quit_app_on_close: bool,
pub(super) is_ready: Arc<AtomicBool>, pub(super) is_ready: Arc<AtomicBool>,
} }
@@ -27,13 +27,13 @@ impl DesktopController {
props: P, props: P,
proxy: EventLoopProxy<UserWindowEvent>, proxy: EventLoopProxy<UserWindowEvent>,
) -> Self { ) -> Self {
let edit_queue = Arc::new(RwLock::new(Vec::new())); let edit_queue = Arc::new(Mutex::new(Vec::new()));
let pending_edits = edit_queue.clone();
let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>(); let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
let return_sender = sender.clone();
let pending_edits = edit_queue.clone();
let return_sender = sender.clone();
let desktop_context_proxy = proxy.clone(); let desktop_context_proxy = proxy.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
// We create the runtime as multithreaded, so you can still "spawn" onto multiple threads // We create the runtime as multithreaded, so you can still "spawn" onto multiple threads
let runtime = tokio::runtime::Builder::new_multi_thread() let runtime = tokio::runtime::Builder::new_multi_thread()
@@ -52,7 +52,7 @@ impl DesktopController {
let edits = dom.rebuild(); let edits = dom.rebuild();
edit_queue edit_queue
.write() .lock()
.unwrap() .unwrap()
.push(serde_json::to_string(&edits.edits).unwrap()); .push(serde_json::to_string(&edits.edits).unwrap());
@@ -62,9 +62,10 @@ impl DesktopController {
loop { loop {
dom.wait_for_work().await; dom.wait_for_work().await;
let mut muts = dom.work_with_deadline(|| false); let mut muts = dom.work_with_deadline(|| false);
while let Some(edit) = muts.pop() { while let Some(edit) = muts.pop() {
edit_queue edit_queue
.write() .lock()
.unwrap() .unwrap()
.push(serde_json::to_string(&edit.edits).unwrap()); .push(serde_json::to_string(&edit.edits).unwrap());
} }
@@ -93,7 +94,7 @@ impl DesktopController {
pub(super) fn try_load_ready_webviews(&mut self) { pub(super) fn try_load_ready_webviews(&mut self) {
if self.is_ready.load(std::sync::atomic::Ordering::Relaxed) { 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(); let (_id, view) = self.webviews.iter_mut().next().unwrap();
while let Some(edit) = queue.pop() { while let Some(edit) = queue.pop() {