chore: switch to mutex

This commit is contained in:
Jonathan Kelley 2022-03-04 14:28:21 -05:00
parent c466425b82
commit dbcd91e32f

View file

@ -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<WindowId, WebView>,
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) is_ready: Arc<AtomicBool>,
}
@ -27,13 +27,13 @@ impl DesktopController {
props: P,
proxy: EventLoopProxy<UserWindowEvent>,
) -> 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::<SchedulerMsg>();
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() {