Skip to content

Commit

Permalink
feat: change Rc<RefCell> to Arc<RwLock>
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Feb 17, 2024
1 parent 606e12a commit d1c564f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
1 change: 1 addition & 0 deletions crates/html-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn process_node<MSG>(node: &rphtml::parser::Node) -> Result<Option<Node<MSG>>, P
vec![]
};

log::info!("accessing node_type here");
match node.node_type {
NodeType::Tag => {
let tag = &node.meta.as_ref().expect("must have a tag");
Expand Down
2 changes: 2 additions & 0 deletions crates/sauron-core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ pub(crate) fn find_node(node: &Node, path: &mut TreePath) -> Option<Node> {
} else {
let idx = path.remove_first();
let children = node.child_nodes();
assert!(children.is_object());
if let Some(child) = children.item(idx as u32) {
find_node(&child, path)
} else {
Expand Down Expand Up @@ -638,6 +639,7 @@ fn get_node_descendant_data_vdom_id(root_element: &Element) -> Vec<usize> {
let child_node_count = children.length();
for i in 0..child_node_count {
let child_node = children.item(i).expect("Expecting a child node");
log::info!("checking here is it is an element node..");
if child_node.node_type() == Node::ELEMENT_NODE {
let child_element = child_node.unchecked_ref::<Element>();
let child_data_vdom_id = get_node_descendant_data_vdom_id(child_element);
Expand Down
75 changes: 40 additions & 35 deletions crates/sauron-core/src/dom/dom_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,47 +308,51 @@ where
// before it is actully replaced in the DOM
PatchVariant::ReplaceNode { mut replacement } => {
let first_node = replacement.pop().expect("must have a first node");
if target_element.node_type() == Node::DOCUMENT_FRAGMENT_NODE {
// if we are patching a fragment mode in the top-level document
// it has no access to it's parent other than accessing the mount-node itself
if patch_path.is_empty() {
let mount_node = self.mount_node();
Self::clear_children(&mount_node);
mount_node
.append_child(&first_node)
.expect("must append child");
Self::dispatch_mount_event(&first_node);
log::info!("checking if it is a fragment node...");
if target_element.is_object(){
if target_element.node_type() == Node::DOCUMENT_FRAGMENT_NODE {
// if we are patching a fragment mode in the top-level document
// it has no access to it's parent other than accessing the mount-node itself
if patch_path.is_empty() {
let mount_node = self.mount_node();
Self::clear_children(&mount_node);
mount_node
.append_child(&first_node)
.expect("must append child");
Self::dispatch_mount_event(&first_node);

for node in replacement.into_iter() {
let node_elm: &web_sys::Element = node.unchecked_ref();
mount_node.append_child(node_elm).expect("append child");
Self::dispatch_mount_event(node_elm);
for node in replacement.into_iter() {
let node_elm: &web_sys::Element = node.unchecked_ref();
mount_node.append_child(node_elm).expect("append child");
Self::dispatch_mount_event(node_elm);
}
} else {
// the diffing algorithmn doesn't concern with fragment, instead it test the nodes contain in the fragment as if it where a list of nodes
unreachable!("patching a document fragment other than the root_node should not happen");
}
} else {
// the diffing algorithmn doesn't concern with fragment, instead it test the nodes contain in the fragment as if it where a list of nodes
unreachable!("patching a document fragment other than the root_node should not happen");
}
} else {
if target_element.node_type() == Node::ELEMENT_NODE {
self.remove_event_listeners(&target_element)?;
}
//let first_node = replacement.pop().expect("must have a first node");
target_element
.replace_with_with_node_1(&first_node)
.unwrap_or_else(|e| {
panic!("unable to replace node with {first_node:?}, {e:?}");
});
log::info!("checking if it is element node");
if target_element.node_type() == Node::ELEMENT_NODE {
self.remove_event_listeners(&target_element)?;
}
//let first_node = replacement.pop().expect("must have a first node");
target_element
.replace_with_with_node_1(&first_node)
.unwrap_or_else(|e| {
panic!("unable to replace node with {first_node:?}, {e:?}");
});

Self::dispatch_mount_event(&first_node);
Self::dispatch_mount_event(&first_node);

let first_node_elm: &web_sys::Element = first_node.unchecked_ref();
let first_node_elm: &web_sys::Element = first_node.unchecked_ref();

for node in replacement.into_iter() {
let node_elm: &web_sys::Element = node.unchecked_ref();
first_node_elm
.insert_adjacent_element(intern("beforebegin"), node_elm)
.expect("append child");
Self::dispatch_mount_event(node_elm);
for node in replacement.into_iter() {
let node_elm: &web_sys::Element = node.unchecked_ref();
first_node_elm
.insert_adjacent_element(intern("beforebegin"), node_elm)
.expect("append child");
Self::dispatch_mount_event(node_elm);
}
}
}

Expand All @@ -371,6 +375,7 @@ where
parent_target
.remove_child(&target_element)
.expect("must remove target node");
log::info!("checking if it is an element node");
if target_element.node_type() == Node::ELEMENT_NODE {
self.remove_event_listeners(&target_element)?;
}
Expand Down
6 changes: 4 additions & 2 deletions crates/sauron-core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ where

/// clone the app
pub fn app_clone(&self) -> APP {
/*
log::info!("BEFORE app_clone");
//TODO: This doesn't work all the time, maybe use Arc::RwLock as a replacement to
//Rc::RefCell
/*
unsafe{
let app: APP = std::mem::transmute_copy(&*self.app_context.app.borrow());
let app: APP = std::mem::transmute_copy(&*self.app_context.app.write().expect("poisoned"));
log::info!("AFTER transmute_copy..");
app
}
*/
Expand Down

0 comments on commit d1c564f

Please sign in to comment.