Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use Rc<Refcell<T>> instead of Arc<Mutex<T>> when sharing the DOM #1052

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ categories = ["gui", "asynchronous"]
features = ["freya-engine/mocked-engine"]

[features]
shared = []
rc-dom = []
skia-engine = ["freya-engine/skia-engine"]
fade-cached-incremental-areas = []

Expand Down
54 changes: 28 additions & 26 deletions crates/core/src/dom/doms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use std::sync::{
Arc,
Mutex,
MutexGuard,
use std::{
cell::{
Ref,
RefCell,
RefMut,
},
rc::Rc,
sync::{
Arc,
Mutex,
MutexGuard,
},
};

use dioxus_core::VirtualDom;
Expand Down Expand Up @@ -52,14 +60,14 @@ pub type DioxusNode<'a> = NodeRef<'a, CustomAttributeValues>;
/// Tiny wrapper over [FreyaDOM] to make it thread-safe if desired.
/// This is primarily used by the Devtools and Testing renderer.
pub struct SafeDOM {
#[cfg(not(feature = "shared"))]
#[cfg(not(feature = "rc-dom"))]
pub fdom: FreyaDOM,

#[cfg(feature = "shared")]
pub fdom: Arc<Mutex<FreyaDOM>>,
#[cfg(feature = "rc-dom")]
pub fdom: Rc<RefCell<FreyaDOM>>,
}

#[cfg(feature = "shared")]
#[cfg(feature = "rc-dom")]
impl Clone for SafeDOM {
fn clone(&self) -> Self {
Self {
Expand All @@ -69,52 +77,46 @@ impl Clone for SafeDOM {
}

impl SafeDOM {
#[cfg(not(feature = "shared"))]
#[cfg(not(feature = "rc-dom"))]
pub fn new(fdom: FreyaDOM) -> Self {
Self { fdom }
}

#[cfg(feature = "shared")]
#[cfg(feature = "rc-dom")]
pub fn new(fdom: FreyaDOM) -> Self {
Self {
fdom: Arc::new(Mutex::new(fdom)),
fdom: Rc::new(RefCell::new(fdom)),
}
}

/// Get a reference to the DOM.
#[cfg(not(feature = "shared"))]
#[cfg(not(feature = "rc-dom"))]
pub fn get(&self) -> &FreyaDOM {
&self.fdom
}

/// Get a reference to the DOM.
#[cfg(not(feature = "shared"))]
#[cfg(not(feature = "rc-dom"))]
pub fn try_get(&self) -> Option<&FreyaDOM> {
Some(&self.fdom)
}

/// Get a mutable reference to the DOM.
#[cfg(not(feature = "shared"))]
#[cfg(not(feature = "rc-dom"))]
pub fn get_mut(&mut self) -> &mut FreyaDOM {
&mut self.fdom
}

/// Get a reference to the DOM.
#[cfg(feature = "shared")]
pub fn get(&self) -> MutexGuard<FreyaDOM> {
return self.fdom.lock().unwrap();
}

/// Get a reference to the DOM.
#[cfg(feature = "shared")]
pub fn try_get(&self) -> Option<MutexGuard<FreyaDOM>> {
return self.fdom.try_lock().ok();
#[cfg(feature = "rc-dom")]
pub fn get(&self) -> Ref<FreyaDOM> {
return self.fdom.borrow();
}

/// Get a mutable reference to the dom.
#[cfg(feature = "shared")]
pub fn get_mut(&self) -> MutexGuard<FreyaDOM> {
return self.fdom.lock().unwrap();
#[cfg(feature = "rc-dom")]
pub fn get_mut(&self) -> RefMut<FreyaDOM> {
return self.fdom.borrow_mut();
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/devtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ freya-node-state = { workspace = true }
freya-renderer = { workspace = true }
freya-elements = { workspace = true }
freya-hooks = { workspace = true }
freya-core = { workspace = true, features = ["shared"] }
freya-core = { workspace = true }
freya-components = { workspace = true }
freya-engine = { workspace = true }
torin = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ skia-engine = ["freya-engine/skia-engine"]
freya-elements = { workspace = true }
freya-common = { workspace = true }
freya-node-state = { workspace = true }
freya-core = { workspace = true, features = ["shared"] }
freya-core = { workspace = true, features = ["rc-dom"] }
freya-hooks = { workspace = true }
freya-components = { workspace = true }
freya-engine = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/testing/src/test_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<T: 'static + Clone> TestingHandler<T> {
pub(crate) fn init_dom(&mut self) {
self.provide_vdom_contexts();
let sdom = self.utils.sdom();
let mut fdom = sdom.get();
let mut fdom = sdom.get_mut();
fdom.init_dom(&mut self.vdom, SCALE_FACTOR as f32);
}

Expand Down
Loading