2023-02-04 23:56:30 +00:00
|
|
|
use dioxus_native_core::node::NodeType;
|
|
|
|
use dioxus_native_core::prelude::*;
|
2023-03-12 20:35:48 +00:00
|
|
|
use dioxus_native_core_macro::partial_derive_state;
|
2023-02-05 02:45:29 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2023-03-12 20:35:48 +00:00
|
|
|
use shipyard::Component;
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
fn create_blank_element() -> NodeType {
|
|
|
|
NodeType::Element(ElementNode {
|
|
|
|
tag: "div".to_owned(),
|
|
|
|
namespace: None,
|
2023-02-05 02:45:29 +00:00
|
|
|
attributes: FxHashMap::default(),
|
|
|
|
listeners: FxHashSet::default(),
|
2023-02-04 23:56:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_pass() {
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct Number(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for Number {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = ();
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
self.0 += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-04 23:56:30 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-04 23:56:30 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:35:11 +00:00
|
|
|
let mut tree: RealDom = RealDom::new([Number::to_type_erased()]);
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(
|
|
|
|
tree.get(tree.root_id()).unwrap().get().as_deref(),
|
|
|
|
Some(&Number(1))
|
|
|
|
);
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
// mark the node as dirty
|
|
|
|
tree.get_mut(tree.root_id()).unwrap().get_mut::<Number>();
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
|
|
|
assert_eq!(
|
|
|
|
tree.get(tree.root_id()).unwrap().get().as_deref(),
|
|
|
|
Some(&Number(2))
|
|
|
|
);
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dependant_node_pass() {
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct AddNumber(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for AddNumber {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = (SubtractNumber,);
|
|
|
|
type ParentDependencies = ();
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
self.0 += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-04 23:56:30 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-04 23:56:30 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct SubtractNumber(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for SubtractNumber {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = ();
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
self.0 -= 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-04 23:56:30 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-04 23:56:30 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:35:11 +00:00
|
|
|
let mut tree: RealDom = RealDom::new([
|
2023-02-04 23:56:30 +00:00
|
|
|
AddNumber::to_type_erased(),
|
|
|
|
SubtractNumber::to_type_erased(),
|
2023-03-14 21:35:11 +00:00
|
|
|
]);
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(1)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-1)));
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
// mark the subtract state as dirty, it should update the add state
|
|
|
|
tree.get_mut(tree.root_id())
|
|
|
|
.unwrap()
|
|
|
|
.get_mut::<SubtractNumber>();
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(2)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-2)));
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
// mark the add state as dirty, it should ~not~ update the subtract state
|
|
|
|
tree.get_mut(tree.root_id()).unwrap().get_mut::<AddNumber>();
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(3)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-2)));
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn independant_node_pass() {
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct AddNumber(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for AddNumber {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = ();
|
|
|
|
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
self.0 += 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-04 23:56:30 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-04 23:56:30 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct SubtractNumber(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for SubtractNumber {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = ();
|
|
|
|
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
self.0 -= 1;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-04 23:56:30 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-04 23:56:30 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:35:11 +00:00
|
|
|
let mut tree: RealDom = RealDom::new([
|
2023-02-04 23:56:30 +00:00
|
|
|
AddNumber::to_type_erased(),
|
|
|
|
SubtractNumber::to_type_erased(),
|
2023-03-14 21:35:11 +00:00
|
|
|
]);
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(1)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-1)));
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
// mark the subtract state as dirty, it should ~not~ update the add state
|
|
|
|
tree.get_mut(tree.root_id())
|
|
|
|
.unwrap()
|
|
|
|
.get_mut::<SubtractNumber>();
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(1)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-2)));
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
// mark the add state as dirty, it should ~not~ update the subtract state
|
|
|
|
tree.get_mut(tree.root_id()).unwrap().get_mut::<AddNumber>();
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(2)));
|
|
|
|
assert_eq!(root.get().as_deref(), Some(&SubtractNumber(-2)));
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn down_pass() {
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Component)]
|
2023-02-04 23:56:30 +00:00
|
|
|
struct AddNumber(i32);
|
|
|
|
|
|
|
|
impl Default for AddNumber {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for AddNumber {
|
2023-02-04 23:56:30 +00:00
|
|
|
type ChildDependencies = ();
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = (AddNumber,);
|
|
|
|
|
2023-02-05 02:45:29 +00:00
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-04 23:56:30 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
2023-02-04 23:56:30 +00:00
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
_: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-04 23:56:30 +00:00
|
|
|
) -> bool {
|
|
|
|
if let Some((parent,)) = parent {
|
2023-02-05 02:45:29 +00:00
|
|
|
self.0 += parent.0;
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 02:45:29 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self::default();
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-05 02:45:29 +00:00
|
|
|
myself
|
|
|
|
}
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 21:35:11 +00:00
|
|
|
let mut tree: RealDom = RealDom::new([AddNumber::to_type_erased()]);
|
2023-02-06 19:40:22 +00:00
|
|
|
let grandchild1 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
let grandchild1 = grandchild1.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let mut child1 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
child1.add_child(grandchild1);
|
|
|
|
let child1 = child1.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let grandchild2 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
let grandchild2 = grandchild2.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let mut child2 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
child2.add_child(grandchild2);
|
|
|
|
let child2 = child2.id();
|
|
|
|
let mut parent = tree.get_mut(tree.root_id()).unwrap();
|
|
|
|
parent.add_child(child1);
|
|
|
|
parent.add_child(child2);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-04 23:56:30 +00:00
|
|
|
|
2023-02-05 02:45:29 +00:00
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
|
|
|
dbg!(root.id());
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(1)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let child1 = tree.get(child1).unwrap();
|
|
|
|
dbg!(child1.id());
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(child1.get().as_deref(), Some(&AddNumber(2)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let grandchild1 = tree.get(grandchild1).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(grandchild1.get().as_deref(), Some(&AddNumber(3)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let child2 = tree.get(child2).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(child2.get().as_deref(), Some(&AddNumber(2)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let grandchild2 = tree.get(grandchild2).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(grandchild2.get().as_deref(), Some(&AddNumber(3)));
|
2023-02-04 23:56:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 02:45:29 +00:00
|
|
|
#[test]
|
|
|
|
fn up_pass() {
|
|
|
|
// Tree before:
|
|
|
|
// 1=\
|
|
|
|
// 1=\
|
|
|
|
// 1
|
|
|
|
// 1=\
|
|
|
|
// 1
|
|
|
|
// Tree after:
|
|
|
|
// 4=\
|
|
|
|
// 2=\
|
|
|
|
// 1
|
|
|
|
// 2=\
|
|
|
|
// 1
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Component)]
|
2023-02-05 02:45:29 +00:00
|
|
|
struct AddNumber(i32);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
#[partial_derive_state]
|
2023-02-11 23:43:08 +00:00
|
|
|
impl State for AddNumber {
|
2023-02-05 02:45:29 +00:00
|
|
|
type ChildDependencies = (AddNumber,);
|
|
|
|
type NodeDependencies = ();
|
|
|
|
type ParentDependencies = ();
|
|
|
|
|
|
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
|
|
|
|
|
2023-02-11 23:43:08 +00:00
|
|
|
fn update<'a>(
|
2023-02-05 02:45:29 +00:00
|
|
|
&mut self,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: NodeView,
|
|
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 15:39:55 +00:00
|
|
|
_: &SendAnyMap,
|
2023-02-05 02:45:29 +00:00
|
|
|
) -> bool {
|
2023-03-12 20:35:48 +00:00
|
|
|
self.0 += children.iter().map(|(i,)| i.0).sum::<i32>();
|
2023-02-05 02:45:29 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create<'a>(
|
|
|
|
node_view: NodeView<()>,
|
|
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-03-12 20:35:48 +00:00
|
|
|
children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
2023-02-05 02:45:29 +00:00
|
|
|
context: &SendAnyMap,
|
|
|
|
) -> Self {
|
|
|
|
let mut myself = Self(1);
|
2023-02-11 23:43:08 +00:00
|
|
|
myself.update(node_view, node, parent, children, context);
|
2023-02-05 02:45:29 +00:00
|
|
|
myself
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:35:11 +00:00
|
|
|
let mut tree: RealDom = RealDom::new([AddNumber::to_type_erased()]);
|
2023-02-06 19:40:22 +00:00
|
|
|
let grandchild1 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
let grandchild1 = grandchild1.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let mut child1 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
child1.add_child(grandchild1);
|
|
|
|
let child1 = child1.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let grandchild2 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
let grandchild2 = grandchild2.id();
|
2023-02-06 19:40:22 +00:00
|
|
|
let mut child2 = tree.create_node(create_blank_element());
|
2023-02-05 02:45:29 +00:00
|
|
|
child2.add_child(grandchild2);
|
|
|
|
let child2 = child2.id();
|
|
|
|
let mut parent = tree.get_mut(tree.root_id()).unwrap();
|
|
|
|
parent.add_child(child1);
|
|
|
|
parent.add_child(child2);
|
|
|
|
|
2023-03-12 20:35:48 +00:00
|
|
|
tree.update_state(SendAnyMap::new());
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let root = tree.get(tree.root_id()).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(root.get().as_deref(), Some(&AddNumber(5)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let child1 = tree.get(child1).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(child1.get().as_deref(), Some(&AddNumber(2)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let grandchild1 = tree.get(grandchild1).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(grandchild1.get().as_deref(), Some(&AddNumber(1)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let child2 = tree.get(child2).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(child2.get().as_deref(), Some(&AddNumber(2)));
|
2023-02-05 02:45:29 +00:00
|
|
|
|
|
|
|
let grandchild2 = tree.get(grandchild2).unwrap();
|
2023-03-12 20:35:48 +00:00
|
|
|
assert_eq!(grandchild2.get().as_deref(), Some(&AddNumber(1)));
|
2023-02-05 02:45:29 +00:00
|
|
|
}
|