Skip to content

Commit

Permalink
feat: make find_node and find_all_nodes a method for DomNode
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 14, 2024
1 parent 8dd5531 commit f0d6707
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
38 changes: 1 addition & 37 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::dom::GroupedDomAttrValues;
use crate::dom::StatefulComponent;
use crate::dom::StatefulModel;
use crate::html::lookup;
use crate::vdom::TreePath;
use crate::{
dom::document,
dom::events::MountEvent,
Expand Down Expand Up @@ -153,7 +152,7 @@ impl PartialEq for DomNode {
}

impl DomNode {
fn children(&self) -> Option<Ref<'_, Vec<DomNode>>> {
pub(crate) fn children(&self) -> Option<Ref<'_, Vec<DomNode>>> {
match &self.inner {
DomInner::Element { children, .. } => Some(children.borrow()),
DomInner::Fragment { children, .. } => Some(children.borrow()),
Expand Down Expand Up @@ -837,38 +836,3 @@ where
}
}

pub(crate) fn find_node(target_node: &DomNode, path: &mut TreePath) -> Option<DomNode> {
if path.is_empty() {
Some(target_node.clone())
} else {
let idx = path.remove_first();
let children = target_node.children()?;
if let Some(child) = children.get(idx) {
find_node(child, path)
} else {
None
}
}
}

pub(crate) fn find_all_nodes(
target_node: &DomNode,
nodes_to_find: &[(&TreePath, Option<&&'static str>)],
) -> IndexMap<TreePath, DomNode> {
let mut nodes_to_patch: IndexMap<TreePath, DomNode> =
IndexMap::with_capacity(nodes_to_find.len());
for (path, tag) in nodes_to_find {
let mut traverse_path: TreePath = (*path).clone();
if let Some(found) = find_node(target_node, &mut traverse_path) {
nodes_to_patch.insert((*path).clone(), found);
} else {
log::warn!(
"can not find: {:?} {:?} target_node: {:?}",
path,
tag,
target_node
);
}
}
nodes_to_patch
}
43 changes: 41 additions & 2 deletions crates/core/src/dom/dom_patch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::dom;
use crate::dom::dom_node::find_all_nodes;
use crate::dom::dom_node::DomInner;
use crate::dom::DomAttr;
use crate::dom::DomAttrValue;
Expand Down Expand Up @@ -77,6 +76,46 @@ pub enum PatchVariant {
},
}

impl DomNode{

pub(crate) fn find_node(&self, path: &mut TreePath) -> Option<DomNode> {
if path.is_empty() {
Some(self.clone())
} else {
let idx = path.remove_first();
let children = self.children()?;
if let Some(child) = children.get(idx) {
child.find_node(path)
} else {
None
}
}
}

pub(crate) fn find_all_nodes(
&self,
nodes_to_find: &[(&TreePath, Option<&&'static str>)],
) -> IndexMap<TreePath, DomNode> {
let mut nodes_to_patch: IndexMap<TreePath, DomNode> =
IndexMap::with_capacity(nodes_to_find.len());
for (path, tag) in nodes_to_find {
let mut traverse_path: TreePath = (*path).clone();
if let Some(found) = self.find_node(&mut traverse_path) {
nodes_to_patch.insert((*path).clone(), found);
} else {
log::warn!(
"can not find: {:?} {:?} target_node: {:?}",
path,
tag,
&self
);
}
}
nodes_to_patch
}
}


impl<APP> Program<APP>
where
APP: Application + 'static,
Expand Down Expand Up @@ -150,7 +189,7 @@ where
)
.collect();

let nodes_lookup = find_all_nodes(target_node, &nodes_to_find);
let nodes_lookup = target_node.find_all_nodes(&nodes_to_find);

let dom_patches:Vec<DomPatch> = patches.iter().map(|patch|{
let patch_path = patch.path();
Expand Down

0 comments on commit f0d6707

Please sign in to comment.