update dependancy to stretch2 master and remove refrences to tree

This commit is contained in:
Evan Almloff committed 2022-04-04 13:37:04 -05:00
1 parent 06e8785938
commit 351722d136
10 files changed
+125 -191

No files matched your search

+1 -2
View File
@@ -11,8 +11,7 @@ dioxus-core = { path = "../core", version = "^0.2.0" }
dioxus-html = { path = "../html", version = "^0.2.0" }
dioxus-core-macro = { path = "../core-macro", version = "^0.2.0" }
# stretch2 = "0.4.1"
stretch2 = { git = "https://github.com/Demonthos/stretch.git" }
stretch2 = { git = "https://github.com/DioxusLabs/stretch" }
smallvec = "1.6"
fxhash = "0.2"
+23 -23
View File
@@ -6,9 +6,9 @@ use std::{
use dioxus_core::{ElementId, Mutations, VNode, VirtualDom};
/// A tree that can sync with the VirtualDom mutations intended for use in lazy renderers.
/// A Dom that can sync with the VirtualDom mutations intended for use in lazy renderers.
/// The render state passes from parent to children and or accumulates state from children to parents.
/// To get started implement [PushedDownState] and or [BubbledUpState] and call [Tree::apply_mutations] to update the tree and [Tree::update_state] to update the state of the nodes.
/// To get started implement [PushedDownState] and or [BubbledUpState] and call [RealDom::apply_mutations] to update the dom and [RealDom::update_state] to update the state of the nodes.
#[derive(Debug)]
pub struct RealDom<US: BubbledUpState = (), DS: PushedDownState = ()> {
root: usize,
@@ -43,7 +43,7 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
}
/// Updates the tree, up and down state and return a set of nodes that were updated pass this to update_state.
/// Updates the dom, up and down state and return a set of nodes that were updated pass this to update_state.
pub fn apply_mutations(&mut self, mutations_vec: Vec<Mutations>) -> Vec<usize> {
let mut nodes_updated = Vec::new();
for mutations in mutations_vec {
@@ -299,17 +299,17 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
}
// remove a node and it's children from the tree.
// remove a node and it's children from the dom.
fn remove(&mut self, id: usize) -> Option<Node<US, DS>> {
// We do not need to remove the node from the parent's children list for children.
fn inner<US: BubbledUpState, DS: PushedDownState>(
tree: &mut RealDom<US, DS>,
dom: &mut RealDom<US, DS>,
id: usize,
) -> Option<Node<US, DS>> {
let mut node = tree.nodes[id as usize].take()?;
let mut node = dom.nodes[id as usize].take()?;
if let NodeType::Element { children, .. } = &mut node.node_type {
for c in children {
inner(tree, c.0)?;
inner(dom, c.0)?;
}
}
Some(node)
@@ -355,7 +355,7 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
}
/// Check if the tree contains a node and its children.
/// Check if the dom contains a node and its children.
pub fn contains_node(&self, node: &VNode) -> bool {
match node {
VNode::Component(_) => {
@@ -363,8 +363,8 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
VNode::Element(e) => {
if let Some(id) = e.id.get() {
let tree_node = &self[id];
match &tree_node.node_type {
let dom_node = &self[id];
match &dom_node.node_type {
NodeType::Element {
tag,
namespace,
@@ -392,8 +392,8 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
VNode::Placeholder(_) => true,
VNode::Text(t) => {
if let Some(id) = t.id.get() {
let tree_node = &self[id];
match &tree_node.node_type {
let dom_node = &self[id];
match &dom_node.node_type {
NodeType::Text { text } => t.text == text,
_ => false,
}
@@ -404,9 +404,9 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
}
/// Return the number of nodes in the tree.
/// Return the number of nodes in the dom.
pub fn size(&self) -> usize {
// The tree has a root node, ignore it.
// The dom has a root node, ignore it.
self.nodes.iter().filter(|n| n.is_some()).count() - 1
}
@@ -415,18 +415,18 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
self.root
}
/// Call a function for each node in the tree, depth first.
/// Call a function for each node in the dom, depth first.
pub fn traverse_depth_first(&self, mut f: impl FnMut(&Node<US, DS>)) {
fn inner<US: BubbledUpState, DS: PushedDownState>(
tree: &RealDom<US, DS>,
dom: &RealDom<US, DS>,
id: ElementId,
f: &mut impl FnMut(&Node<US, DS>),
) {
let node = &tree[id];
let node = &dom[id];
f(node);
if let NodeType::Element { children, .. } = &node.node_type {
for c in children {
inner(tree, *c, f);
inner(dom, *c, f);
}
}
}
@@ -437,18 +437,18 @@ impl<US: BubbledUpState, DS: PushedDownState> RealDom<US, DS> {
}
}
/// Call a function for each node in the tree, depth first.
/// Call a function for each node in the dom, depth first.
pub fn traverse_depth_first_mut(&mut self, mut f: impl FnMut(&mut Node<US, DS>)) {
fn inner<US: BubbledUpState, DS: PushedDownState>(
tree: &mut RealDom<US, DS>,
dom: &mut RealDom<US, DS>,
id: ElementId,
f: &mut impl FnMut(&mut Node<US, DS>),
) {
let node = &mut tree[id];
let node = &mut dom[id];
f(node);
if let NodeType::Element { children, .. } = &mut node.node_type {
for c in children.clone() {
inner(tree, c, f);
inner(dom, c, f);
}
}
}
@@ -488,7 +488,7 @@ impl<US: BubbledUpState, DS: PushedDownState> IndexMut<ElementId> for RealDom<US
}
}
/// The node is stored client side and stores only basic data about the node. For more complete information about the node see [`TreeNode::element`].
/// The node is stored client side and stores only basic data about the node. For more complete information about the node see [`domNode::element`].
#[derive(Debug, Clone)]
pub struct Node<US: BubbledUpState, DS: PushedDownState> {
/// The id of the node this node was created from.
+20 -20
View File
@@ -20,9 +20,9 @@ fn remove_node() {
}
});
let mut tree: RealDom<(), ()> = RealDom::new();
let mut dom: RealDom<(), ()> = RealDom::new();
let _to_update = tree.apply_mutations(vec![mutations]);
let _to_update = dom.apply_mutations(vec![mutations]);
let child_div = VElement {
id: Cell::new(Some(ElementId(2))),
key: None,
@@ -45,10 +45,10 @@ fn remove_node() {
children: &[child_div_el],
};
assert_eq!(tree.size(), 2);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
assert_eq!(dom.size(), 2);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[1].height, 1);
assert_eq!(dom[2].height, 2);
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
@@ -61,7 +61,7 @@ fn remove_node() {
div{}
},
);
tree.apply_mutations(vec![mutations.1]);
dom.apply_mutations(vec![mutations.1]);
let new_root_div = VElement {
id: Cell::new(Some(ElementId(1))),
@@ -74,9 +74,9 @@ fn remove_node() {
children: &[],
};
assert_eq!(tree.size(), 1);
assert!(&tree.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(dom.size(), 1);
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(dom[1].height, 1);
}
#[test]
@@ -92,9 +92,9 @@ fn add_node() {
div{}
});
let mut tree: RealDom<(), ()> = RealDom::new();
let mut dom: RealDom<(), ()> = RealDom::new();
let _to_update = tree.apply_mutations(vec![mutations]);
let _to_update = dom.apply_mutations(vec![mutations]);
let root_div = VElement {
id: Cell::new(Some(ElementId(1))),
@@ -107,9 +107,9 @@ fn add_node() {
children: &[],
};
assert_eq!(tree.size(), 1);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(dom.size(), 1);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[1].height, 1);
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
@@ -122,7 +122,7 @@ fn add_node() {
}
},
);
tree.apply_mutations(vec![mutations.1]);
dom.apply_mutations(vec![mutations.1]);
let child_div = VElement {
id: Cell::new(Some(ElementId(2))),
@@ -146,8 +146,8 @@ fn add_node() {
children: &[child_div_el],
};
assert_eq!(tree.size(), 2);
assert!(&tree.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
assert_eq!(dom.size(), 2);
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(dom[1].height, 1);
assert_eq!(dom[2].height, 2);
}
+15 -15
View File
@@ -21,9 +21,9 @@ fn initial_build_simple() {
div{}
});
let mut tree: RealDom<(), ()> = RealDom::new();
let mut dom: RealDom<(), ()> = RealDom::new();
let _to_update = tree.apply_mutations(vec![mutations]);
let _to_update = dom.apply_mutations(vec![mutations]);
let root_div = VElement {
id: Cell::new(Some(ElementId(1))),
key: None,
@@ -34,9 +34,9 @@ fn initial_build_simple() {
attributes: &[],
children: &[],
};
assert_eq!(tree.size(), 1);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(dom.size(), 1);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[1].height, 1);
}
#[test]
@@ -60,9 +60,9 @@ fn initial_build_with_children() {
}
});
let mut tree: RealDom<(), ()> = RealDom::new();
let mut dom: RealDom<(), ()> = RealDom::new();
let _to_update = tree.apply_mutations(vec![mutations]);
let _to_update = dom.apply_mutations(vec![mutations]);
let first_text = VText {
id: Cell::new(Some(ElementId(3))),
text: "hello",
@@ -113,12 +113,12 @@ fn initial_build_with_children() {
attributes: &[],
children: &[child_div_node],
};
assert_eq!(tree.size(), 6);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
assert_eq!(tree[3].height, 3);
assert_eq!(tree[4].height, 3);
assert_eq!(tree[5].height, 4);
assert_eq!(tree[6].height, 3);
assert_eq!(dom.size(), 6);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[1].height, 1);
assert_eq!(dom[2].height, 2);
assert_eq!(dom[3].height, 3);
assert_eq!(dom[4].height, 3);
assert_eq!(dom[5].height, 4);
assert_eq!(dom[6].height, 3);
}
+22 -22
View File
@@ -77,12 +77,12 @@ fn state_initial() {
}
});
let mut tree: RealDom<BubbledUpStateTester, PushedDownStateTester> = RealDom::new();
let mut dom: RealDom<BubbledUpStateTester, PushedDownStateTester> = RealDom::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut 42, &mut 42);
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut 42, &mut 42);
let root_div = &tree[1];
let root_div = &dom[1];
assert_eq!(root_div.up_state.0, "1");
assert_eq!(
root_div.up_state.1,
@@ -94,7 +94,7 @@ fn state_initial() {
assert_eq!(root_div.down_state.0, "1");
assert_eq!(root_div.down_state.1, None);
let child_p = &tree[2];
let child_p = &dom[2];
assert_eq!(child_p.up_state.0, "2");
assert_eq!(child_p.up_state.1, Vec::new());
assert_eq!(child_p.down_state.0, "2");
@@ -103,7 +103,7 @@ fn state_initial() {
Some(Box::new(PushedDownStateTester("1".to_string(), None)))
);
let child_h1 = &tree[3];
let child_h1 = &dom[3];
assert_eq!(child_h1.up_state.0, "3");
assert_eq!(child_h1.up_state.1, Vec::new());
assert_eq!(child_h1.down_state.0, "3");
@@ -178,12 +178,12 @@ fn state_reduce_initally_called_minimally() {
}
});
let mut tree: RealDom<CallCounter, CallCounter> = RealDom::new();
let mut dom: RealDom<CallCounter, CallCounter> = RealDom::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse_depth_first(|n| {
dom.traverse_depth_first(|n| {
assert_eq!(n.up_state.0, 1);
assert_eq!(n.down_state.0, 1);
});
@@ -233,11 +233,11 @@ fn state_reduce_down_called_minimally_on_update() {
}
});
let mut tree: RealDom<CallCounter, CallCounter> = RealDom::new();
let mut dom: RealDom<CallCounter, CallCounter> = RealDom::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = tree.apply_mutations(vec![Mutations {
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = dom.apply_mutations(vec![Mutations {
edits: vec![DomEdit::SetAttribute {
root: 1,
field: "width",
@@ -247,9 +247,9 @@ fn state_reduce_down_called_minimally_on_update() {
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse_depth_first(|n| {
dom.traverse_depth_first(|n| {
assert_eq!(n.down_state.0, 2);
});
}
@@ -298,11 +298,11 @@ fn state_reduce_up_called_minimally_on_update() {
}
});
let mut tree: RealDom<CallCounter, CallCounter> = RealDom::new();
let mut dom: RealDom<CallCounter, CallCounter> = RealDom::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = tree.apply_mutations(vec![Mutations {
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = dom.apply_mutations(vec![Mutations {
edits: vec![DomEdit::SetAttribute {
root: 4,
field: "width",
@@ -312,9 +312,9 @@ fn state_reduce_up_called_minimally_on_update() {
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let _to_rerender = dom.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse_depth_first(|n| {
dom.traverse_depth_first(|n| {
assert_eq!(n.up_state.0, if n.id.0 > 4 { 1 } else { 2 });
});
}