diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 239c90aea..f40aaf474 100644 --- a/packages/native-core/src/real_dom.rs +++ b/packages/native-core/src/real_dom.rs @@ -1,3 +1,4 @@ +use anymap::AnyMap; use fxhash::{FxHashMap, FxHashSet}; use std::{ collections::VecDeque, @@ -6,25 +7,27 @@ use std::{ use dioxus_core::{ElementId, Mutations, VNode, VirtualDom}; +use crate::real_dom_new_api::{State, NodeMask}; + /// 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 [RealDom::apply_mutations] to update the dom and [RealDom::update_state] to update the state of the nodes. #[derive(Debug)] -pub struct RealDom { +pub struct RealDom { root: usize, - nodes: Vec>>, + nodes: Vec>>, nodes_listening: FxHashMap<&'static str, FxHashSet>, node_stack: smallvec::SmallVec<[usize; 10]>, } -impl Default for RealDom { +impl Default for RealDom { fn default() -> Self { Self::new() } } -impl RealDom { - pub fn new() -> RealDom { +impl RealDom { + pub fn new() -> RealDom { RealDom { root: 0, nodes: { @@ -191,8 +194,7 @@ impl RealDom { &mut self, vdom: &VirtualDom, nodes_updated: Vec, - us_ctx: &mut US::Ctx, - ds_ctx: &mut DS::Ctx, + ctx: AnyMap, ) -> Option> { let mut to_rerender = FxHashSet::default(); to_rerender.extend(nodes_updated.iter()); @@ -210,24 +212,13 @@ impl RealDom { let node = &mut self[id as usize]; let vnode = node.element(vdom); let node_type = &node.node_type; - let up_state = &mut node.up_state; - let children = match node_type { - NodeType::Element { children, .. } => Some(children), - _ => None, - }; + if let + NodeType::Element { children, tag, namespace } =node.node_type{ + + } + let mask = NodeMask::new(attritutes, tag, namespace) // todo: reduce cloning state - let old = up_state.clone(); - let mut new = up_state.clone(); - let parent = node.parent; - new.reduce( - children - .unwrap_or(&Vec::new()) - .clone() - .iter() - .map(|c| &self[*c].up_state), - vnode, - us_ctx, - ); + for id in node.state.child_dep_types(mask) {} if new != old { to_rerender.insert(id); if let Some(p) = parent { @@ -300,12 +291,9 @@ impl RealDom { } // remove a node and it's children from the dom. - fn remove(&mut self, id: usize) -> Option> { + fn remove(&mut self, id: usize) -> Option> { // We do not need to remove the node from the parent's children list for children. - fn inner( - dom: &mut RealDom, - id: usize, - ) -> Option> { + fn inner(dom: &mut RealDom, id: usize) -> Option> { let mut node = dom.nodes[id as usize].take()?; if let NodeType::Element { children, .. } = &mut node.node_type { for c in children { @@ -327,7 +315,7 @@ impl RealDom { Some(node) } - fn insert(&mut self, node: Node) { + fn insert(&mut self, node: Node) { let current_len = self.nodes.len(); let id = node.id.0; if current_len - 1 < node.id.0 { @@ -337,15 +325,15 @@ impl RealDom { self.nodes[id] = Some(node); } - pub fn get(&self, id: usize) -> Option<&Node> { + pub fn get(&self, id: usize) -> Option<&Node> { self.nodes.get(id)?.as_ref() } - pub fn get_mut(&mut self, id: usize) -> Option<&mut Node> { + pub fn get_mut(&mut self, id: usize) -> Option<&mut Node> { self.nodes.get_mut(id)?.as_mut() } - pub fn get_listening_sorted(&self, event: &'static str) -> Vec<&Node> { + pub fn get_listening_sorted(&self, event: &'static str) -> Vec<&Node> { if let Some(nodes) = self.nodes_listening.get(event) { let mut listening: Vec<_> = nodes.iter().map(|id| &self[*id]).collect(); listening.sort_by(|n1, n2| (n1.height).cmp(&n2.height).reverse()); @@ -416,12 +404,8 @@ impl RealDom { } /// Call a function for each node in the dom, depth first. - pub fn traverse_depth_first(&self, mut f: impl FnMut(&Node)) { - fn inner( - dom: &RealDom, - id: ElementId, - f: &mut impl FnMut(&Node), - ) { + pub fn traverse_depth_first(&self, mut f: impl FnMut(&Node)) { + fn inner(dom: &RealDom, id: ElementId, f: &mut impl FnMut(&Node)) { let node = &dom[id]; f(node); if let NodeType::Element { children, .. } = &node.node_type { @@ -438,12 +422,8 @@ impl RealDom { } /// 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)) { - fn inner( - dom: &mut RealDom, - id: ElementId, - f: &mut impl FnMut(&mut Node), - ) { + pub fn traverse_depth_first_mut(&mut self, mut f: impl FnMut(&mut Node)) { + fn inner(dom: &mut RealDom, id: ElementId, f: &mut impl FnMut(&mut Node)) { let node = &mut dom[id]; f(node); if let NodeType::Element { children, .. } = &mut node.node_type { @@ -461,28 +441,28 @@ impl RealDom { } } -impl Index for RealDom { - type Output = Node; +impl Index for RealDom { + type Output = Node; fn index(&self, idx: usize) -> &Self::Output { self.get(idx).expect("Node does not exist") } } -impl Index for RealDom { - type Output = Node; +impl Index for RealDom { + type Output = Node; fn index(&self, idx: ElementId) -> &Self::Output { &self[idx.0] } } -impl IndexMut for RealDom { +impl IndexMut for RealDom { fn index_mut(&mut self, idx: usize) -> &mut Self::Output { self.get_mut(idx).expect("Node does not exist") } } -impl IndexMut for RealDom { +impl IndexMut for RealDom { fn index_mut(&mut self, idx: ElementId) -> &mut Self::Output { &mut self[idx.0] } @@ -490,15 +470,13 @@ impl IndexMut for RealDom { +pub struct Node { /// The id of the node this node was created from. pub id: ElementId, /// The parent id of the node. pub parent: Option, - /// State of the node that is bubbled up to its parents. The state must depend only on the node and its children. - pub up_state: US, - /// State of the node that is pushed down to the children. The state must depend only on the node itself and its parent. - pub down_state: DS, + /// State of the node. + pub state: S, /// Additional inforation specific to the node type pub node_type: NodeType, /// The number of parents before the root node. The root node has height 1. @@ -518,14 +496,13 @@ pub enum NodeType { Placeholder, } -impl Node { +impl Node { fn new(id: u64, node_type: NodeType) -> Self { Node { id: ElementId(id as usize), parent: None, node_type, - down_state: DS::default(), - up_state: US::default(), + state: S::default(), height: 0, } } diff --git a/packages/native-core/src/real_dom_new_api.rs b/packages/native-core/src/real_dom_new_api.rs index 269364c81..9cc5d0550 100644 --- a/packages/native-core/src/real_dom_new_api.rs +++ b/packages/native-core/src/real_dom_new_api.rs @@ -111,7 +111,7 @@ pub trait NodeDepState { fn reduce(&mut self, node: NodeView, ctx: &Self::Ctx) -> bool; } -pub trait State { +pub trait State: Default { fn update_node_dep_state<'a>( &'a mut self, ty: TypeId,