Subtree memorization / reactive templates (#488)
This commit adds subtree memoization to Dioxus. Subtree memoization is basically a compile-time step that drastically reduces the amount of work the diffing engine needs to do at runtime by extracting non-changing nodes out into a static "template." Templates are then understood by the various renderers in the ecosystem as a faster way of rendering the same items. For example, in the web, templates are simply a set of DOM Nodes created once and then cloned later. This is the same pattern frameworks like Lithtml and SolidJS use to achieve near-perfect performance. Subtree memoization adds an additional level of complexity to Dioxus. The RSX macro needs to be much smarter to identify changing/nonchanging nodes and generate a mapping between the Template and its runtime counterparts. This commit represents a working starter point for this work, adding support for templates for the web, desktop, liveview, ssr, and native-core renderers. In the future we will try to shrink code generation, generally improve performance, and simplify our implementation.
This commit is contained in:
1 parent
b32fd2d2cd
commit
047ed1e553
80 files changed
+6774
-2552
No files matched your search
@@ -164,20 +164,20 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||
|
||||
let gen = quote! {
|
||||
impl State for #type_name {
|
||||
fn update<'a, T: dioxus_native_core::traversable::Traversable<Node = Self, Id = dioxus_core::ElementId>>(
|
||||
dirty: &[(dioxus_core::ElementId, dioxus_native_core::node_ref::NodeMask)],
|
||||
fn update<'a, T: dioxus_native_core::traversable::Traversable<Node = Self, Id = dioxus_core::GlobalNodeId>,T2: dioxus_native_core::traversable::Traversable<Node = dioxus_native_core::real_dom::NodeData, Id = dioxus_core::GlobalNodeId>>(
|
||||
dirty: &[(dioxus_core::GlobalNodeId, dioxus_native_core::node_ref::NodeMask)],
|
||||
state_tree: &'a mut T,
|
||||
vdom: &'a dioxus_core::VirtualDom,
|
||||
rdom: &'a T2,
|
||||
ctx: &anymap::AnyMap,
|
||||
) -> fxhash::FxHashSet<dioxus_core::ElementId>{
|
||||
) -> fxhash::FxHashSet<dioxus_core::GlobalNodeId>{
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct HeightOrdering {
|
||||
height: u16,
|
||||
id: dioxus_core::ElementId,
|
||||
id: dioxus_core::GlobalNodeId,
|
||||
}
|
||||
|
||||
impl HeightOrdering {
|
||||
fn new(height: u16, id: dioxus_core::ElementId) -> Self {
|
||||
fn new(height: u16, id: dioxus_core::GlobalNodeId) -> Self {
|
||||
HeightOrdering {
|
||||
height,
|
||||
id,
|
||||
@@ -185,9 +185,29 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||
}
|
||||
}
|
||||
|
||||
// not the ordering after height is just for deduplication it can be any ordering as long as it is consistent
|
||||
impl Ord for HeightOrdering {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.height.cmp(&other.height).then(self.id.0.cmp(&other.id.0))
|
||||
self.height.cmp(&other.height).then(match (self.id, other.id){
|
||||
(
|
||||
dioxus_core::GlobalNodeId::TemplateId {
|
||||
template_ref_id,
|
||||
template_node_id,
|
||||
},
|
||||
dioxus_core::GlobalNodeId::TemplateId {
|
||||
template_ref_id: o_template_ref_id,
|
||||
template_node_id: o_template_node_id,
|
||||
},
|
||||
) => template_ref_id
|
||||
.0
|
||||
.cmp(&o_template_ref_id.0)
|
||||
.then(template_node_id.0.cmp(&o_template_node_id.0)),
|
||||
(dioxus_core::GlobalNodeId::TemplateId { .. }, dioxus_core::GlobalNodeId::VNodeId(_)) => std::cmp::Ordering::Less,
|
||||
(dioxus_core::GlobalNodeId::VNodeId(_), dioxus_core::GlobalNodeId::TemplateId { .. }) => {
|
||||
std::cmp::Ordering::Greater
|
||||
}
|
||||
(dioxus_core::GlobalNodeId::VNodeId(s_id), dioxus_core::GlobalNodeId::VNodeId(o_id)) => s_id.0.cmp(&o_id.0),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +238,7 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||
|
||||
let mut dirty_elements = fxhash::FxHashSet::default();
|
||||
// the states of any elements that are dirty
|
||||
let mut states: fxhash::FxHashMap<dioxus_core::ElementId, MembersDirty> = fxhash::FxHashMap::default();
|
||||
let mut states: fxhash::FxHashMap<dioxus_core::GlobalNodeId, MembersDirty> = fxhash::FxHashMap::default();
|
||||
|
||||
for (id, mask) in dirty {
|
||||
let members_dirty = MembersDirty {
|
||||
@@ -244,7 +264,7 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||
<#child_types as dioxus_native_core::state::State>::update(
|
||||
dirty,
|
||||
&mut state_tree.map(|n| &n.#child_members, |n| &mut n.#child_members),
|
||||
vdom,
|
||||
rdom,
|
||||
ctx,
|
||||
)
|
||||
);
|
||||
@@ -493,7 +513,7 @@ impl<'a> StateStruct<'a> {
|
||||
let mut i = 0;
|
||||
while i < resolution_order.len(){
|
||||
let id = resolution_order[i].id;
|
||||
let vnode = vdom.get_element(id).unwrap();
|
||||
let node = rdom.get(id).unwrap();
|
||||
let members_dirty = states.get_mut(&id).unwrap();
|
||||
let (current_state, parent) = state_tree.get_node_parent_mut(id);
|
||||
let current_state = current_state.unwrap();
|
||||
@@ -515,7 +535,7 @@ impl<'a> StateStruct<'a> {
|
||||
let mut i = 0;
|
||||
while i < resolution_order.len(){
|
||||
let id = resolution_order[i].id;
|
||||
let vnode = vdom.get_element(id).unwrap();
|
||||
let node = rdom.get(id).unwrap();
|
||||
let members_dirty = states.get_mut(&id).unwrap();
|
||||
let (current_state, children) = state_tree.get_node_children_mut(id);
|
||||
let current_state = current_state.unwrap();
|
||||
@@ -534,7 +554,7 @@ impl<'a> StateStruct<'a> {
|
||||
let mut i = 0;
|
||||
while i < resolution_order.len(){
|
||||
let id = resolution_order[i];
|
||||
let vnode = vdom.get_element(id).unwrap();
|
||||
let node = rdom.get(id).unwrap();
|
||||
let members_dirty = states.get_mut(&id).unwrap();
|
||||
let current_state = state_tree.get_mut(id).unwrap();
|
||||
if members_dirty.#member && #reduce_member {
|
||||
@@ -679,8 +699,7 @@ impl<'a> StateMember<'a> {
|
||||
};
|
||||
|
||||
let ty = &self.mem.ty;
|
||||
let node_view =
|
||||
quote!(dioxus_native_core::node_ref::NodeView::new(vnode, #ty::NODE_MASK, vdom));
|
||||
let node_view = quote!(dioxus_native_core::node_ref::NodeView::new(node, #ty::NODE_MASK));
|
||||
let dep_idents = self.dep_mems.iter().map(|m| &m.0.ident);
|
||||
match self.dep_kind {
|
||||
DependencyKind::Node => {
|
||||
|
||||
@@ -109,7 +109,7 @@ macro_rules! test_state{
|
||||
let mut dom: RealDom<$s> = RealDom::new();
|
||||
|
||||
let nodes_updated = dom.apply_mutations(vec![mutations]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
|
||||
dom.traverse_depth_first(|n| {
|
||||
$(
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use dioxus::core as dioxus_core;
|
||||
use dioxus::core::{ElementId, VElement};
|
||||
use dioxus::core::{self as dioxus_core, GlobalNodeId};
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_native_core::real_dom::RealDom;
|
||||
use dioxus_native_core::state::State;
|
||||
use dioxus_native_core_macro::State;
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(State, Default, Clone)]
|
||||
struct Empty {}
|
||||
@@ -18,44 +16,8 @@ fn remove_node() {
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
|
||||
let mutations = vdom.create_vnodes(rsx! {
|
||||
div{
|
||||
div{}
|
||||
}
|
||||
});
|
||||
|
||||
let mut dom: RealDom<Empty> = RealDom::new();
|
||||
|
||||
let _to_update = dom.apply_mutations(vec![mutations]);
|
||||
let child_div = VElement {
|
||||
id: Cell::new(Some(ElementId(2))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(1))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[],
|
||||
};
|
||||
let child_div_el = VNode::Element(&child_div);
|
||||
let root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[child_div_el],
|
||||
};
|
||||
|
||||
assert_eq!(dom.size(), 2);
|
||||
assert!(&dom.contains_node(&VNode::Element(&root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
assert_eq!(dom[ElementId(2)].height, 2);
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
let mutations = vdom.diff_lazynodes(
|
||||
let (create, edit) = vdom.diff_lazynodes(
|
||||
rsx! {
|
||||
div{
|
||||
div{}
|
||||
@@ -65,22 +27,43 @@ fn remove_node() {
|
||||
div{}
|
||||
},
|
||||
);
|
||||
dom.apply_mutations(vec![mutations.1]);
|
||||
|
||||
let new_root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[],
|
||||
};
|
||||
println!("create: {:#?}", create);
|
||||
println!("edit: {:#?}", edit);
|
||||
|
||||
let _to_update = dom.apply_mutations(vec![create]);
|
||||
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(1),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
2
|
||||
);
|
||||
|
||||
dom.apply_mutations(vec![edit]);
|
||||
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(2),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -92,31 +75,7 @@ fn add_node() {
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
|
||||
let mutations = vdom.create_vnodes(rsx! {
|
||||
div{}
|
||||
});
|
||||
|
||||
let mut dom: RealDom<Empty> = RealDom::new();
|
||||
|
||||
let _to_update = dom.apply_mutations(vec![mutations]);
|
||||
|
||||
let root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[],
|
||||
};
|
||||
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert!(&dom.contains_node(&VNode::Element(&root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
let mutations = vdom.diff_lazynodes(
|
||||
let (create, update) = vdom.diff_lazynodes(
|
||||
rsx! {
|
||||
div{}
|
||||
},
|
||||
@@ -126,32 +85,41 @@ fn add_node() {
|
||||
}
|
||||
},
|
||||
);
|
||||
dom.apply_mutations(vec![mutations.1]);
|
||||
|
||||
let child_div = VElement {
|
||||
id: Cell::new(Some(ElementId(2))),
|
||||
key: None,
|
||||
tag: "p",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(1))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[],
|
||||
};
|
||||
let child_div_el = VNode::Element(&child_div);
|
||||
let new_root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[child_div_el],
|
||||
};
|
||||
let mut dom: RealDom<Empty> = RealDom::new();
|
||||
|
||||
assert_eq!(dom.size(), 2);
|
||||
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
assert_eq!(dom[ElementId(2)].height, 2);
|
||||
let _to_update = dom.apply_mutations(vec![create]);
|
||||
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
|
||||
dom.apply_mutations(vec![update]);
|
||||
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(2),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(2),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(1),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
2
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
use std::cell::Cell;
|
||||
|
||||
use dioxus::core as dioxus_core;
|
||||
use dioxus::core::{ElementId, VElement, VText};
|
||||
use dioxus::core::{self as dioxus_core, GlobalNodeId};
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_native_core::real_dom::RealDom;
|
||||
use dioxus_native_core::state::State;
|
||||
@@ -12,8 +9,6 @@ struct Empty {}
|
||||
|
||||
#[test]
|
||||
fn initial_build_simple() {
|
||||
use std::cell::Cell;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn Base(cx: Scope) -> Element {
|
||||
render!(div {})
|
||||
@@ -28,19 +23,17 @@ fn initial_build_simple() {
|
||||
let mut dom: RealDom<Empty> = RealDom::new();
|
||||
|
||||
let _to_update = dom.apply_mutations(vec![mutations]);
|
||||
let root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[],
|
||||
};
|
||||
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert!(&dom.contains_node(&VNode::Element(&root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -67,62 +60,59 @@ fn initial_build_with_children() {
|
||||
let mut dom: RealDom<Empty> = RealDom::new();
|
||||
|
||||
let _to_update = dom.apply_mutations(vec![mutations]);
|
||||
let first_text = VText {
|
||||
id: Cell::new(Some(ElementId(3))),
|
||||
text: "hello",
|
||||
is_static: true,
|
||||
};
|
||||
let first_text_node = VNode::Text(&first_text);
|
||||
let child_text = VText {
|
||||
id: Cell::new(Some(ElementId(5))),
|
||||
text: "world",
|
||||
is_static: true,
|
||||
};
|
||||
let child_text_node = VNode::Text(&child_text);
|
||||
let child_p_el = VElement {
|
||||
id: Cell::new(Some(ElementId(4))),
|
||||
key: None,
|
||||
tag: "p",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(2))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[child_text_node],
|
||||
};
|
||||
let child_p_node = VNode::Element(&child_p_el);
|
||||
let second_text = VText {
|
||||
id: Cell::new(Some(ElementId(6))),
|
||||
text: "hello world",
|
||||
is_static: true,
|
||||
};
|
||||
let second_text_node = VNode::Text(&second_text);
|
||||
let child_div_el = VElement {
|
||||
id: Cell::new(Some(ElementId(2))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(1))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[first_text_node, child_p_node, second_text_node],
|
||||
};
|
||||
let child_div_node = VNode::Element(&child_div_el);
|
||||
let root_div = VElement {
|
||||
id: Cell::new(Some(ElementId(1))),
|
||||
key: None,
|
||||
tag: "div",
|
||||
namespace: None,
|
||||
parent: Cell::new(Some(ElementId(0))),
|
||||
listeners: &[],
|
||||
attributes: &[],
|
||||
children: &[child_div_node],
|
||||
};
|
||||
assert_eq!(dom.size(), 6);
|
||||
assert!(&dom.contains_node(&VNode::Element(&root_div)));
|
||||
assert_eq!(dom[ElementId(1)].height, 1);
|
||||
assert_eq!(dom[ElementId(2)].height, 2);
|
||||
assert_eq!(dom[ElementId(3)].height, 3);
|
||||
assert_eq!(dom[ElementId(4)].height, 3);
|
||||
assert_eq!(dom[ElementId(5)].height, 4);
|
||||
assert_eq!(dom[ElementId(6)].height, 3);
|
||||
assert_eq!(dom.size(), 1);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(1),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(2),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(3),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(4),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
4
|
||||
);
|
||||
assert_eq!(
|
||||
dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(5),
|
||||
}]
|
||||
.node_data
|
||||
.height,
|
||||
3
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
use dioxus::core as dioxus_core;
|
||||
use dioxus::core_macro::rsx_without_templates;
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_native_core::{
|
||||
real_dom::{NodeType, RealDom},
|
||||
@@ -37,68 +38,68 @@ fn traverse() {
|
||||
let mut iter = PersistantElementIter::new();
|
||||
let div_tag = "div".to_string();
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
let text1 = "hello".to_string();
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text1, .. }
|
||||
));
|
||||
let p_tag = "p".to_string();
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: p_tag, .. }
|
||||
));
|
||||
let text2 = "world".to_string();
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text2, .. }
|
||||
));
|
||||
let text3 = "hello world".to_string();
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text3, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.next(&rdom).id()].node_type,
|
||||
&rdom[iter.next(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text3, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text2, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: p_tag, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text1, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
assert!(matches!(
|
||||
&rdom[iter.prev(&rdom).id()].node_type,
|
||||
&rdom[iter.prev(&rdom).id()].node_data.node_type,
|
||||
NodeType::Text { text: text3, .. }
|
||||
));
|
||||
}
|
||||
@@ -112,7 +113,7 @@ fn persist_removes() {
|
||||
}
|
||||
let vdom = VirtualDom::new(Base);
|
||||
let (build, update) = vdom.diff_lazynodes(
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -128,7 +129,7 @@ fn persist_removes() {
|
||||
}
|
||||
}
|
||||
},
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -177,16 +178,19 @@ fn persist_removes() {
|
||||
let p_tag = "p".to_string();
|
||||
let idx = iter1.next(&rdom).id();
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_type,
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Element { tag: p_tag, .. }
|
||||
));
|
||||
let text = "hello world".to_string();
|
||||
let idx = iter1.next(&rdom).id();
|
||||
assert!(matches!(&rdom[idx].node_type, NodeType::Text { text, .. }));
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Text { text, .. }
|
||||
));
|
||||
let div_tag = "div".to_string();
|
||||
let idx = iter2.next(&rdom).id();
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_type,
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Element { tag: div_tag, .. }
|
||||
));
|
||||
}
|
||||
@@ -200,7 +204,7 @@ fn persist_instertions_before() {
|
||||
}
|
||||
let vdom = VirtualDom::new(Base);
|
||||
let (build, update) = vdom.diff_lazynodes(
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -212,7 +216,7 @@ fn persist_instertions_before() {
|
||||
}
|
||||
}
|
||||
},
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -252,7 +256,7 @@ fn persist_instertions_before() {
|
||||
let p_tag = "div".to_string();
|
||||
let idx = iter.next(&rdom).id();
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_type,
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Element { tag: p_tag, .. }
|
||||
));
|
||||
}
|
||||
@@ -266,7 +270,7 @@ fn persist_instertions_after() {
|
||||
}
|
||||
let vdom = VirtualDom::new(Base);
|
||||
let (build, update) = vdom.diff_lazynodes(
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -278,7 +282,7 @@ fn persist_instertions_after() {
|
||||
}
|
||||
}
|
||||
},
|
||||
rsx! {
|
||||
rsx_without_templates! {
|
||||
div{
|
||||
p{
|
||||
key: "1",
|
||||
@@ -318,10 +322,13 @@ fn persist_instertions_after() {
|
||||
let p_tag = "p".to_string();
|
||||
let idx = iter.next(&rdom).id();
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_type,
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Element { tag: p_tag, .. }
|
||||
));
|
||||
let text = "hello world".to_string();
|
||||
let idx = iter.next(&rdom).id();
|
||||
assert!(matches!(&rdom[idx].node_type, NodeType::Text { text, .. }));
|
||||
assert!(matches!(
|
||||
&rdom[idx].node_data.node_type,
|
||||
NodeType::Text { text, .. }
|
||||
));
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
use anymap::AnyMap;
|
||||
use dioxus::core as dioxus_core;
|
||||
use dioxus::core::ElementId;
|
||||
use dioxus::core::{self as dioxus_core, GlobalNodeId};
|
||||
use dioxus::core::{AttributeValue, DomEdit, Mutations};
|
||||
use dioxus::core_macro::rsx_without_templates;
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_native_core::node_ref::*;
|
||||
use dioxus_native_core::real_dom::*;
|
||||
@@ -50,14 +51,13 @@ impl ChildDepState for ChildDepCallCounter {
|
||||
const NODE_MASK: NodeMask = NodeMask::ALL;
|
||||
fn reduce<'a>(
|
||||
&mut self,
|
||||
node: NodeView,
|
||||
_: NodeView,
|
||||
_: impl Iterator<Item = &'a Self::DepState>,
|
||||
_: &Self::Ctx,
|
||||
) -> bool
|
||||
where
|
||||
Self::DepState: 'a,
|
||||
{
|
||||
println!("{self:?} {:?}: {} {:?}", node.tag(), node.id(), node.text());
|
||||
self.0 += 1;
|
||||
true
|
||||
}
|
||||
@@ -142,8 +142,16 @@ impl NodeDepState<()> for NodeStateTester {
|
||||
*self = NodeStateTester(
|
||||
node.tag().map(|s| s.to_string()),
|
||||
node.attributes()
|
||||
.map(|a| (a.name.to_string(), a.value.to_string()))
|
||||
.collect(),
|
||||
.map(|iter| {
|
||||
iter.map(|a| {
|
||||
(
|
||||
a.attribute.name.to_string(),
|
||||
format!("{}", a.value.as_text().unwrap()),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
true
|
||||
}
|
||||
@@ -185,9 +193,12 @@ fn state_initial() {
|
||||
let nodes_updated = dom.apply_mutations(vec![mutations]);
|
||||
let mut ctx = AnyMap::new();
|
||||
ctx.insert(42u32);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, ctx);
|
||||
let _to_rerender = dom.update_state(nodes_updated, ctx);
|
||||
|
||||
let root_div = &dom[ElementId(1)];
|
||||
let root_div = &dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
||||
}];
|
||||
assert_eq!(root_div.state.bubbled.0, Some("div".to_string()));
|
||||
assert_eq!(
|
||||
root_div.state.bubbled.1,
|
||||
@@ -199,12 +210,18 @@ fn state_initial() {
|
||||
assert_eq!(root_div.state.pushed.0, Some("div".to_string()));
|
||||
assert_eq!(
|
||||
root_div.state.pushed.1,
|
||||
Some(Box::new(PushedDownStateTester(None, None)))
|
||||
Some(Box::new(PushedDownStateTester(
|
||||
Some("Root".to_string()),
|
||||
None
|
||||
)))
|
||||
);
|
||||
assert_eq!(root_div.state.node.0, Some("div".to_string()));
|
||||
assert_eq!(root_div.state.node.1, vec![]);
|
||||
|
||||
let child_p = &dom[ElementId(2)];
|
||||
let child_p = &dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(1),
|
||||
}];
|
||||
assert_eq!(child_p.state.bubbled.0, Some("p".to_string()));
|
||||
assert_eq!(child_p.state.bubbled.1, Vec::new());
|
||||
assert_eq!(child_p.state.pushed.0, Some("p".to_string()));
|
||||
@@ -212,7 +229,10 @@ fn state_initial() {
|
||||
child_p.state.pushed.1,
|
||||
Some(Box::new(PushedDownStateTester(
|
||||
Some("div".to_string()),
|
||||
Some(Box::new(PushedDownStateTester(None, None)))
|
||||
Some(Box::new(PushedDownStateTester(
|
||||
Some("Root".to_string()),
|
||||
None
|
||||
)))
|
||||
)))
|
||||
);
|
||||
assert_eq!(child_p.state.node.0, Some("p".to_string()));
|
||||
@@ -221,7 +241,10 @@ fn state_initial() {
|
||||
vec![("color".to_string(), "red".to_string())]
|
||||
);
|
||||
|
||||
let child_h1 = &dom[ElementId(3)];
|
||||
let child_h1 = &dom[GlobalNodeId::TemplateId {
|
||||
template_ref_id: dioxus_core::ElementId(1),
|
||||
template_node_id: dioxus::prelude::TemplateNodeId(2),
|
||||
}];
|
||||
assert_eq!(child_h1.state.bubbled.0, Some("h1".to_string()));
|
||||
assert_eq!(child_h1.state.bubbled.1, Vec::new());
|
||||
assert_eq!(child_h1.state.pushed.0, Some("h1".to_string()));
|
||||
@@ -229,7 +252,10 @@ fn state_initial() {
|
||||
child_h1.state.pushed.1,
|
||||
Some(Box::new(PushedDownStateTester(
|
||||
Some("div".to_string()),
|
||||
Some(Box::new(PushedDownStateTester(None, None)))
|
||||
Some(Box::new(PushedDownStateTester(
|
||||
Some("Root".to_string()),
|
||||
None
|
||||
)))
|
||||
)))
|
||||
);
|
||||
assert_eq!(child_h1.state.node.0, Some("h1".to_string()));
|
||||
@@ -261,7 +287,7 @@ fn state_reduce_parent_called_minimally_on_update() {
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
|
||||
let mutations = vdom.create_vnodes(rsx! {
|
||||
let mutations = vdom.create_vnodes(rsx_without_templates! {
|
||||
div {
|
||||
width: "100%",
|
||||
div{
|
||||
@@ -284,7 +310,7 @@ fn state_reduce_parent_called_minimally_on_update() {
|
||||
let mut dom: RealDom<CallCounterState> = RealDom::new();
|
||||
|
||||
let nodes_updated = dom.apply_mutations(vec![mutations]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
let nodes_updated = dom.apply_mutations(vec![Mutations {
|
||||
edits: vec![DomEdit::SetAttribute {
|
||||
root: 1,
|
||||
@@ -295,7 +321,7 @@ fn state_reduce_parent_called_minimally_on_update() {
|
||||
dirty_scopes: fxhash::FxHashSet::default(),
|
||||
refs: Vec::new(),
|
||||
}]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
|
||||
dom.traverse_depth_first(|n| {
|
||||
assert_eq!(n.state.part2.parent_counter.0, 2);
|
||||
@@ -307,7 +333,7 @@ fn state_reduce_parent_called_minimally_on_update() {
|
||||
fn state_reduce_child_called_minimally_on_update() {
|
||||
#[allow(non_snake_case)]
|
||||
fn Base(cx: Scope) -> Element {
|
||||
render!(div {
|
||||
cx.render(rsx_without_templates!(div {
|
||||
div{
|
||||
div{
|
||||
p{
|
||||
@@ -324,12 +350,12 @@ fn state_reduce_child_called_minimally_on_update() {
|
||||
"world"
|
||||
}
|
||||
}
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
let vdom = VirtualDom::new(Base);
|
||||
|
||||
let mutations = vdom.create_vnodes(rsx! {
|
||||
let mutations = vdom.create_vnodes(rsx_without_templates! {
|
||||
div {
|
||||
div{
|
||||
div{
|
||||
@@ -353,7 +379,7 @@ fn state_reduce_child_called_minimally_on_update() {
|
||||
let mut dom: RealDom<CallCounterState> = RealDom::new();
|
||||
|
||||
let nodes_updated = dom.apply_mutations(vec![mutations]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
let nodes_updated = dom.apply_mutations(vec![Mutations {
|
||||
edits: vec![DomEdit::SetAttribute {
|
||||
root: 4,
|
||||
@@ -364,15 +390,33 @@ fn state_reduce_child_called_minimally_on_update() {
|
||||
dirty_scopes: fxhash::FxHashSet::default(),
|
||||
refs: Vec::new(),
|
||||
}]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
|
||||
dom.traverse_depth_first(|n| {
|
||||
println!("{:?}", n);
|
||||
assert_eq!(
|
||||
n.state.part1.child_counter.0,
|
||||
if n.id.0 > 4 { 1 } else { 2 }
|
||||
if let GlobalNodeId::VNodeId(ElementId(id)) = n.node_data.id {
|
||||
if id > 4 {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
}
|
||||
} else {
|
||||
panic!()
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
n.state.child_counter.0,
|
||||
if let GlobalNodeId::VNodeId(ElementId(id)) = n.node_data.id {
|
||||
if id > 4 {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
}
|
||||
} else {
|
||||
panic!()
|
||||
}
|
||||
);
|
||||
assert_eq!(n.state.child_counter.0, if n.id.0 > 4 { 1 } else { 2 });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -457,7 +501,7 @@ fn dependancies_order_independant() {
|
||||
let mut dom: RealDom<UnorderedDependanciesState> = RealDom::new();
|
||||
|
||||
let nodes_updated = dom.apply_mutations(vec![mutations]);
|
||||
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
|
||||
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
|
||||
|
||||
let c = CDepCallCounter(1);
|
||||
let b = BDepCallCounter(1, c.clone());
|
||||
|
||||
Reference in new issue
Block a user