2022-06-25 13:27:10 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-03-31 01:45:41 +00:00
|
|
|
use dioxus_native_core::real_dom::*;
|
2022-04-16 17:23:31 +00:00
|
|
|
use dioxus_native_core::state::{ChildDepState, NodeDepState, ParentDepState, State};
|
2022-12-06 23:38:04 +00:00
|
|
|
use dioxus_native_core::tree::TreeView;
|
2022-12-07 22:51:14 +00:00
|
|
|
use dioxus_native_core::{node_ref::*, NodeId, SendAnyMap};
|
2022-04-12 12:51:57 +00:00
|
|
|
use dioxus_native_core_macro::State;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, State)]
|
|
|
|
struct CallCounterState {
|
2022-04-16 17:23:31 +00:00
|
|
|
#[child_dep_state(child_counter)]
|
2022-04-12 12:51:57 +00:00
|
|
|
child_counter: ChildDepCallCounter,
|
2022-04-16 17:23:31 +00:00
|
|
|
#[parent_dep_state(parent_counter)]
|
2022-04-12 12:51:57 +00:00
|
|
|
parent_counter: ParentDepCallCounter,
|
|
|
|
#[node_dep_state()]
|
|
|
|
node_counter: NodeDepCallCounter,
|
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
struct ChildDepCallCounter(u32);
|
|
|
|
impl ChildDepState for ChildDepCallCounter {
|
2022-03-27 01:10:15 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (Self,);
|
2022-04-12 12:51:57 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::ALL;
|
2022-04-12 23:46:16 +00:00
|
|
|
fn reduce<'a>(
|
2022-04-12 12:51:57 +00:00
|
|
|
&mut self,
|
2022-09-30 19:03:06 +00:00
|
|
|
_: NodeView,
|
2022-12-06 23:38:04 +00:00
|
|
|
_: impl Iterator<Item = (&'a Self,)>,
|
2022-04-12 23:46:16 +00:00
|
|
|
_: &Self::Ctx,
|
|
|
|
) -> bool
|
|
|
|
where
|
|
|
|
Self::DepState: 'a,
|
|
|
|
{
|
2022-03-27 01:10:15 +00:00
|
|
|
self.0 += 1;
|
2022-04-12 12:51:57 +00:00
|
|
|
true
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
struct ParentDepCallCounter(u32);
|
|
|
|
impl ParentDepState for ParentDepCallCounter {
|
2022-03-27 01:10:15 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (Self,);
|
2022-04-12 12:51:57 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::ALL;
|
2022-12-06 23:38:04 +00:00
|
|
|
fn reduce(&mut self, _node: NodeView, _parent: Option<(&Self,)>, _ctx: &Self::Ctx) -> bool {
|
2022-04-12 12:51:57 +00:00
|
|
|
self.0 += 1;
|
2022-12-06 23:38:04 +00:00
|
|
|
println!("ParentDepCallCounter::reduce on {:?}\n{}", _node, self.0);
|
2022-04-12 12:51:57 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
struct NodeDepCallCounter(u32);
|
2022-12-06 23:38:04 +00:00
|
|
|
impl NodeDepState for NodeDepCallCounter {
|
2022-04-12 12:51:57 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = ();
|
2022-04-12 12:51:57 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::ALL;
|
2022-07-07 04:50:22 +00:00
|
|
|
fn reduce(&mut self, _node: NodeView, _sibling: (), _ctx: &Self::Ctx) -> bool {
|
2022-03-27 01:10:15 +00:00
|
|
|
self.0 += 1;
|
2022-04-12 12:51:57 +00:00
|
|
|
true
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:24:39 +00:00
|
|
|
#[allow(clippy::vec_box)]
|
2022-03-27 01:10:15 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
2022-04-12 12:51:57 +00:00
|
|
|
struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
|
|
|
|
impl ChildDepState for BubbledUpStateTester {
|
2022-03-27 01:10:15 +00:00
|
|
|
type Ctx = u32;
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (Self,);
|
2022-04-16 22:22:03 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
|
2022-04-12 23:46:16 +00:00
|
|
|
fn reduce<'a>(
|
|
|
|
&mut self,
|
|
|
|
node: NodeView,
|
2022-12-06 23:38:04 +00:00
|
|
|
children: impl Iterator<Item = (&'a Self,)>,
|
2022-04-12 23:46:16 +00:00
|
|
|
ctx: &Self::Ctx,
|
|
|
|
) -> bool
|
|
|
|
where
|
|
|
|
Self::DepState: 'a,
|
|
|
|
{
|
2022-03-27 01:10:15 +00:00
|
|
|
assert_eq!(*ctx, 42);
|
|
|
|
*self = BubbledUpStateTester(
|
2022-04-12 12:51:57 +00:00
|
|
|
node.tag().map(|s| s.to_string()),
|
2022-12-06 23:38:04 +00:00
|
|
|
children
|
|
|
|
.into_iter()
|
|
|
|
.map(|(c,)| Box::new(c.clone()))
|
|
|
|
.collect(),
|
2022-03-27 01:10:15 +00:00
|
|
|
);
|
2022-04-12 12:51:57 +00:00
|
|
|
true
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
2022-04-12 12:51:57 +00:00
|
|
|
struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>);
|
|
|
|
impl ParentDepState for PushedDownStateTester {
|
2022-03-27 01:10:15 +00:00
|
|
|
type Ctx = u32;
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (Self,);
|
2022-04-16 22:22:03 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
|
2022-12-06 23:38:04 +00:00
|
|
|
fn reduce(&mut self, node: NodeView, parent: Option<(&Self,)>, ctx: &Self::Ctx) -> bool {
|
2022-03-27 01:10:15 +00:00
|
|
|
assert_eq!(*ctx, 42);
|
|
|
|
*self = PushedDownStateTester(
|
2022-04-12 12:51:57 +00:00
|
|
|
node.tag().map(|s| s.to_string()),
|
2022-12-06 23:38:04 +00:00
|
|
|
parent.map(|(c,)| Box::new(c.clone())),
|
2022-03-27 01:10:15 +00:00
|
|
|
);
|
2022-04-12 12:51:57 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
|
|
|
struct NodeStateTester(Option<String>, Vec<(String, String)>);
|
2022-12-06 23:38:04 +00:00
|
|
|
impl NodeDepState for NodeStateTester {
|
2022-04-12 12:51:57 +00:00
|
|
|
type Ctx = u32;
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = ();
|
2022-04-16 22:22:03 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::new_with_attrs(AttributeMask::All).with_tag();
|
2022-07-07 04:50:22 +00:00
|
|
|
fn reduce(&mut self, node: NodeView, _sibling: (), ctx: &Self::Ctx) -> bool {
|
2022-04-12 12:51:57 +00:00
|
|
|
assert_eq!(*ctx, 42);
|
|
|
|
*self = NodeStateTester(
|
|
|
|
node.tag().map(|s| s.to_string()),
|
|
|
|
node.attributes()
|
2022-09-30 19:03:06 +00:00
|
|
|
.map(|iter| {
|
|
|
|
iter.map(|a| {
|
|
|
|
(
|
|
|
|
a.attribute.name.to_string(),
|
2022-10-02 21:12:24 +00:00
|
|
|
a.value.as_text().unwrap().to_string(),
|
2022-09-30 19:03:06 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default(),
|
2022-04-12 12:51:57 +00:00
|
|
|
);
|
|
|
|
true
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:23:31 +00:00
|
|
|
#[derive(State, Clone, Default, Debug)]
|
|
|
|
struct StateTester {
|
|
|
|
#[child_dep_state(bubbled, u32)]
|
|
|
|
bubbled: BubbledUpStateTester,
|
|
|
|
#[parent_dep_state(pushed, u32)]
|
|
|
|
pushed: PushedDownStateTester,
|
|
|
|
#[node_dep_state(NONE, u32)]
|
|
|
|
node: NodeStateTester,
|
|
|
|
}
|
|
|
|
|
2022-03-27 01:10:15 +00:00
|
|
|
#[test]
|
2022-04-04 17:19:21 +00:00
|
|
|
fn state_initial() {
|
2022-03-27 01:10:15 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn Base(cx: Scope) -> Element {
|
2022-12-06 23:38:04 +00:00
|
|
|
render! {
|
|
|
|
div {
|
|
|
|
p{
|
|
|
|
color: "red"
|
|
|
|
}
|
|
|
|
h1{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mut vdom = VirtualDom::new(Base);
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mutations = vdom.rebuild();
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
let mut dom: RealDom<StateTester> = RealDom::new();
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let (nodes_updated, _) = dom.apply_mutations(mutations);
|
|
|
|
let mut ctx = SendAnyMap::new();
|
2022-04-12 12:51:57 +00:00
|
|
|
ctx.insert(42u32);
|
2022-09-30 19:03:06 +00:00
|
|
|
let _to_rerender = dom.update_state(nodes_updated, ctx);
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let root_div_id = dom.children_ids(NodeId(0)).unwrap()[0];
|
|
|
|
let root_div = &dom.get(root_div_id).unwrap();
|
2022-04-12 12:51:57 +00:00
|
|
|
assert_eq!(root_div.state.bubbled.0, Some("div".to_string()));
|
2022-03-27 01:10:15 +00:00
|
|
|
assert_eq!(
|
2022-04-12 12:51:57 +00:00
|
|
|
root_div.state.bubbled.1,
|
2022-03-27 01:10:15 +00:00
|
|
|
vec![
|
2022-04-12 12:51:57 +00:00
|
|
|
Box::new(BubbledUpStateTester(Some("p".to_string()), Vec::new())),
|
|
|
|
Box::new(BubbledUpStateTester(Some("h1".to_string()), Vec::new()))
|
2022-03-27 01:10:15 +00:00
|
|
|
]
|
|
|
|
);
|
2022-04-12 12:51:57 +00:00
|
|
|
assert_eq!(root_div.state.pushed.0, Some("div".to_string()));
|
2022-04-16 17:23:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
root_div.state.pushed.1,
|
2022-09-30 19:03:06 +00:00
|
|
|
Some(Box::new(PushedDownStateTester(
|
|
|
|
Some("Root".to_string()),
|
|
|
|
None
|
|
|
|
)))
|
2022-04-16 17:23:31 +00:00
|
|
|
);
|
2022-04-12 12:51:57 +00:00
|
|
|
assert_eq!(root_div.state.node.0, Some("div".to_string()));
|
|
|
|
assert_eq!(root_div.state.node.1, vec![]);
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let child_p_id = dom.children_ids(root_div_id).unwrap()[0];
|
|
|
|
let child_p = &dom[child_p_id];
|
2022-04-12 12:51:57 +00:00
|
|
|
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()));
|
2022-03-27 01:10:15 +00:00
|
|
|
assert_eq!(
|
2022-04-12 12:51:57 +00:00
|
|
|
child_p.state.pushed.1,
|
|
|
|
Some(Box::new(PushedDownStateTester(
|
|
|
|
Some("div".to_string()),
|
2022-09-30 19:03:06 +00:00
|
|
|
Some(Box::new(PushedDownStateTester(
|
|
|
|
Some("Root".to_string()),
|
|
|
|
None
|
|
|
|
)))
|
2022-04-12 12:51:57 +00:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
assert_eq!(child_p.state.node.0, Some("p".to_string()));
|
|
|
|
assert_eq!(
|
|
|
|
child_p.state.node.1,
|
|
|
|
vec![("color".to_string(), "red".to_string())]
|
2022-03-27 01:10:15 +00:00
|
|
|
);
|
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let child_h1_id = dom.children_ids(root_div_id).unwrap()[1];
|
|
|
|
let child_h1 = &dom[child_h1_id];
|
2022-04-12 12:51:57 +00:00
|
|
|
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()));
|
2022-03-27 01:10:15 +00:00
|
|
|
assert_eq!(
|
2022-04-12 12:51:57 +00:00
|
|
|
child_h1.state.pushed.1,
|
|
|
|
Some(Box::new(PushedDownStateTester(
|
|
|
|
Some("div".to_string()),
|
2022-09-30 19:03:06 +00:00
|
|
|
Some(Box::new(PushedDownStateTester(
|
|
|
|
Some("Root".to_string()),
|
|
|
|
None
|
|
|
|
)))
|
2022-04-12 12:51:57 +00:00
|
|
|
)))
|
2022-03-27 01:10:15 +00:00
|
|
|
);
|
2022-04-12 12:51:57 +00:00
|
|
|
assert_eq!(child_h1.state.node.0, Some("h1".to_string()));
|
|
|
|
assert_eq!(child_h1.state.node.1, vec![]);
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-16 17:23:31 +00:00
|
|
|
fn state_reduce_parent_called_minimally_on_update() {
|
2022-03-27 01:10:15 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn Base(cx: Scope) -> Element {
|
2022-12-06 23:38:04 +00:00
|
|
|
let width = if cx.generation() == 0 { "100%" } else { "99%" };
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
width: "{width}",
|
2022-03-27 01:10:15 +00:00
|
|
|
div{
|
2022-12-06 23:38:04 +00:00
|
|
|
div{
|
|
|
|
p{}
|
|
|
|
}
|
|
|
|
p{
|
|
|
|
"hello"
|
|
|
|
}
|
|
|
|
div{
|
|
|
|
h1{}
|
|
|
|
}
|
|
|
|
p{
|
|
|
|
"world"
|
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mut vdom = VirtualDom::new(Base);
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
let mut dom: RealDom<CallCounterState> = RealDom::new();
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let (nodes_updated, _) = dom.apply_mutations(vdom.rebuild());
|
|
|
|
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
|
|
|
|
vdom.mark_dirty(ScopeId(0));
|
|
|
|
let (nodes_updated, _) = dom.apply_mutations(vdom.render_immediate());
|
|
|
|
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mut is_root = true;
|
2022-04-04 18:37:04 +00:00
|
|
|
dom.traverse_depth_first(|n| {
|
2022-12-06 23:38:04 +00:00
|
|
|
if is_root {
|
|
|
|
is_root = false;
|
|
|
|
assert_eq!(n.state.parent_counter.0, 1);
|
|
|
|
} else {
|
|
|
|
assert_eq!(n.state.parent_counter.0, 2);
|
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-16 17:23:31 +00:00
|
|
|
fn state_reduce_child_called_minimally_on_update() {
|
2022-03-27 01:10:15 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn Base(cx: Scope) -> Element {
|
2022-12-06 23:38:04 +00:00
|
|
|
let width = if cx.generation() == 0 { "100%" } else { "99%" };
|
|
|
|
cx.render(rsx! {
|
|
|
|
// updated: 2
|
|
|
|
div {
|
|
|
|
// updated: 2
|
2022-03-27 01:10:15 +00:00
|
|
|
div{
|
2022-12-06 23:38:04 +00:00
|
|
|
// updated: 2
|
|
|
|
div{
|
|
|
|
// updated: 2
|
|
|
|
p{
|
|
|
|
width: "{width}",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// updated: 1
|
2022-04-16 17:23:31 +00:00
|
|
|
p{
|
2022-12-06 23:38:04 +00:00
|
|
|
// updated: 1
|
|
|
|
"hello"
|
2022-04-16 17:23:31 +00:00
|
|
|
}
|
2022-12-06 23:38:04 +00:00
|
|
|
// updated: 1
|
|
|
|
div{
|
|
|
|
// updated: 1
|
|
|
|
h1{}
|
|
|
|
}
|
|
|
|
// updated: 1
|
2022-04-16 17:23:31 +00:00
|
|
|
p{
|
2022-12-06 23:38:04 +00:00
|
|
|
// updated: 1
|
|
|
|
"world"
|
2022-04-16 17:23:31 +00:00
|
|
|
}
|
2022-03-27 01:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-06 23:38:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut vdom = VirtualDom::new(Base);
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-04-12 12:51:57 +00:00
|
|
|
let mut dom: RealDom<CallCounterState> = RealDom::new();
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let (nodes_updated, _) = dom.apply_mutations(vdom.rebuild());
|
|
|
|
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
|
|
|
|
vdom.mark_dirty(ScopeId(0));
|
|
|
|
let (nodes_updated, _) = dom.apply_mutations(vdom.render_immediate());
|
|
|
|
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
|
2022-03-27 01:10:15 +00:00
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mut traverse_count = 0;
|
2022-04-04 18:37:04 +00:00
|
|
|
dom.traverse_depth_first(|n| {
|
2022-12-06 23:38:04 +00:00
|
|
|
assert_eq!(n.state.child_counter.0, {
|
|
|
|
if traverse_count > 4 {
|
|
|
|
1
|
2022-09-30 19:03:06 +00:00
|
|
|
} else {
|
2022-12-06 23:38:04 +00:00
|
|
|
2
|
2022-09-30 19:03:06 +00:00
|
|
|
}
|
2022-12-06 23:38:04 +00:00
|
|
|
});
|
|
|
|
traverse_count += 1;
|
2022-03-27 01:10:15 +00:00
|
|
|
});
|
|
|
|
}
|
2022-04-16 17:23:31 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, State)]
|
|
|
|
struct UnorderedDependanciesState {
|
|
|
|
#[node_dep_state(c)]
|
|
|
|
b: BDepCallCounter,
|
|
|
|
#[node_dep_state()]
|
|
|
|
c: CDepCallCounter,
|
|
|
|
#[node_dep_state(b)]
|
|
|
|
a: ADepCallCounter,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
|
|
struct ADepCallCounter(usize, BDepCallCounter);
|
2022-12-06 23:38:04 +00:00
|
|
|
impl NodeDepState for ADepCallCounter {
|
2022-04-16 17:23:31 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (BDepCallCounter,);
|
2022-04-16 17:23:31 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::NONE;
|
2022-07-07 04:50:22 +00:00
|
|
|
fn reduce(
|
|
|
|
&mut self,
|
|
|
|
_node: NodeView,
|
2022-12-06 23:38:04 +00:00
|
|
|
(sibling,): (&BDepCallCounter,),
|
2022-07-07 04:50:22 +00:00
|
|
|
_ctx: &Self::Ctx,
|
|
|
|
) -> bool {
|
2022-04-16 17:23:31 +00:00
|
|
|
self.0 += 1;
|
|
|
|
self.1 = sibling.clone();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
|
|
struct BDepCallCounter(usize, CDepCallCounter);
|
2022-12-06 23:38:04 +00:00
|
|
|
impl NodeDepState for BDepCallCounter {
|
2022-04-16 17:23:31 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = (CDepCallCounter,);
|
2022-04-16 17:23:31 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::NONE;
|
2022-07-07 04:50:22 +00:00
|
|
|
fn reduce(
|
|
|
|
&mut self,
|
|
|
|
_node: NodeView,
|
2022-12-06 23:38:04 +00:00
|
|
|
(sibling,): (&CDepCallCounter,),
|
2022-07-07 04:50:22 +00:00
|
|
|
_ctx: &Self::Ctx,
|
|
|
|
) -> bool {
|
2022-04-16 17:23:31 +00:00
|
|
|
self.0 += 1;
|
|
|
|
self.1 = sibling.clone();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
|
|
struct CDepCallCounter(usize);
|
2022-12-06 23:38:04 +00:00
|
|
|
impl NodeDepState for CDepCallCounter {
|
2022-04-16 17:23:31 +00:00
|
|
|
type Ctx = ();
|
2022-12-06 23:38:04 +00:00
|
|
|
type DepState = ();
|
2022-04-16 17:23:31 +00:00
|
|
|
const NODE_MASK: NodeMask = NodeMask::ALL;
|
2022-07-07 04:50:22 +00:00
|
|
|
fn reduce(&mut self, _node: NodeView, _sibling: (), _ctx: &Self::Ctx) -> bool {
|
2022-04-16 17:23:31 +00:00
|
|
|
self.0 += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dependancies_order_independant() {
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn Base(cx: Scope) -> Element {
|
2022-09-25 08:05:16 +00:00
|
|
|
render!(div {
|
2022-04-16 17:23:31 +00:00
|
|
|
width: "100%",
|
|
|
|
p{
|
|
|
|
"hello"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mut vdom = VirtualDom::new(Base);
|
2022-04-16 17:23:31 +00:00
|
|
|
|
|
|
|
let mut dom: RealDom<UnorderedDependanciesState> = RealDom::new();
|
|
|
|
|
2022-12-06 23:38:04 +00:00
|
|
|
let mutations = vdom.rebuild();
|
|
|
|
let (nodes_updated, _) = dom.apply_mutations(mutations);
|
|
|
|
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
|
2022-04-16 17:23:31 +00:00
|
|
|
|
|
|
|
let c = CDepCallCounter(1);
|
|
|
|
let b = BDepCallCounter(1, c.clone());
|
|
|
|
let a = ADepCallCounter(1, b.clone());
|
|
|
|
dom.traverse_depth_first(|n| {
|
|
|
|
assert_eq!(&n.state.a, &a);
|
|
|
|
assert_eq!(&n.state.b, &b);
|
|
|
|
assert_eq!(&n.state.c, &c);
|
|
|
|
});
|
|
|
|
}
|