|
@@ -1,4 +1,5 @@
|
|
//! This module contains the stateful DiffMachine and all methods to diff VNodes, their properties, and their children.
|
|
//! This module contains the stateful DiffMachine and all methods to diff VNodes, their properties, and their children.
|
|
|
|
+//! The DiffMachine calculates the diffs between the old and new frames, updates the new nodes, and modifies the real dom.
|
|
//!
|
|
//!
|
|
//! Notice:
|
|
//! Notice:
|
|
//! ------
|
|
//! ------
|
|
@@ -9,15 +10,10 @@
|
|
//! Implementation Details:
|
|
//! Implementation Details:
|
|
//! -----------------------
|
|
//! -----------------------
|
|
//!
|
|
//!
|
|
-//! Diff the `old` node with the `new` node. Emits instructions to modify a
|
|
|
|
-//! physical DOM node that reflects `old` into something that reflects `new`.
|
|
|
|
|
|
+//! All nodes are addressed by their IDs. The RealDom provides an imperative interface for making changes to these nodes.
|
|
|
|
+//! We don't necessarily intend for changes to happen exactly during the diffing process, so the implementor may choose
|
|
|
|
+//! to batch nodes if it is more performant for their application. The u32 should be a no-op to hash,
|
|
//!
|
|
//!
|
|
-//! Upon entry to this function, the physical DOM node must be on the top of the
|
|
|
|
-//! change list stack:
|
|
|
|
-//!
|
|
|
|
-//! [... node]
|
|
|
|
-//!
|
|
|
|
-//! The change list stack is in the same state when this function exits.
|
|
|
|
//!
|
|
//!
|
|
//! Further Reading and Thoughts
|
|
//! Further Reading and Thoughts
|
|
//! ----------------------------
|
|
//! ----------------------------
|
|
@@ -30,11 +26,59 @@ use crate::{arena::ScopeArena, innerlude::*};
|
|
use fxhash::{FxHashMap, FxHashSet};
|
|
use fxhash::{FxHashMap, FxHashSet};
|
|
|
|
|
|
use std::{
|
|
use std::{
|
|
|
|
+ any::Any,
|
|
|
|
+ cell::Cell,
|
|
cmp::Ordering,
|
|
cmp::Ordering,
|
|
rc::{Rc, Weak},
|
|
rc::{Rc, Weak},
|
|
- sync::atomic::AtomicU32,
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+/// The accompanying "real dom" exposes an imperative API for controlling the UI layout
|
|
|
|
+///
|
|
|
|
+/// Instead of having handles directly over nodes, Dioxus uses simple u32s as node IDs.
|
|
|
|
+/// This allows layouts with up to 4,294,967,295 nodes. If we use nohasher, then retrieving is very fast.
|
|
|
|
+
|
|
|
|
+/// The "RealDom" abstracts over the... real dom. Elements are mapped by ID. The RealDom is inteded to maintain a stack
|
|
|
|
+/// of real nodes as the diffing algorithm descenes through the tree. This means that whatever is on top of the stack
|
|
|
|
+/// will receive modifications. However, instead of using child-based methods for descending through the tree, we instead
|
|
|
|
+/// ask the RealDom to either push or pop real nodes onto the stack. This saves us the indexing cost while working on a
|
|
|
|
+/// single node
|
|
|
|
+pub trait RealDom {
|
|
|
|
+ // Navigation
|
|
|
|
+ fn push_root(&mut self, root: RealDomNode);
|
|
|
|
+
|
|
|
|
+ // Add Nodes to the dom
|
|
|
|
+ fn append_child(&mut self);
|
|
|
|
+ fn replace_with(&mut self);
|
|
|
|
+
|
|
|
|
+ // Remove Nodesfrom the dom
|
|
|
|
+ fn remove(&mut self);
|
|
|
|
+ fn remove_all_children(&mut self);
|
|
|
|
+
|
|
|
|
+ // Create
|
|
|
|
+ fn create_text_node(&mut self, text: &str) -> RealDomNode;
|
|
|
|
+ fn create_element(&mut self, tag: &str) -> RealDomNode;
|
|
|
|
+ fn create_element_ns(&mut self, tag: &str, namespace: &str) -> RealDomNode;
|
|
|
|
+
|
|
|
|
+ // events
|
|
|
|
+ fn new_event_listener(
|
|
|
|
+ &mut self,
|
|
|
|
+ event: &str,
|
|
|
|
+ scope: ScopeIdx,
|
|
|
|
+ element_id: usize,
|
|
|
|
+ realnode: RealDomNode,
|
|
|
|
+ );
|
|
|
|
+ // fn new_event_listener(&mut self, event: &str);
|
|
|
|
+ fn remove_event_listener(&mut self, event: &str);
|
|
|
|
+
|
|
|
|
+ // modify
|
|
|
|
+ fn set_text(&mut self, text: &str);
|
|
|
|
+ fn set_attribute(&mut self, name: &str, value: &str, is_namespaced: bool);
|
|
|
|
+ fn remove_attribute(&mut self, name: &str);
|
|
|
|
+
|
|
|
|
+ // node ref
|
|
|
|
+ fn raw_node_as_any_mut(&self) -> &mut dyn Any;
|
|
|
|
+}
|
|
|
|
+
|
|
/// The DiffState is a cursor internal to the VirtualDOM's diffing algorithm that allows persistence of state while
|
|
/// The DiffState is a cursor internal to the VirtualDOM's diffing algorithm that allows persistence of state while
|
|
/// diffing trees of components. This means we can "re-enter" a subtree of a component by queuing a "NeedToDiff" event.
|
|
/// diffing trees of components. This means we can "re-enter" a subtree of a component by queuing a "NeedToDiff" event.
|
|
///
|
|
///
|
|
@@ -47,169 +91,203 @@ use std::{
|
|
/// The order of these re-entrances is stored in the DiffState itself. The DiffState comes pre-loaded with a set of components
|
|
/// The order of these re-entrances is stored in the DiffState itself. The DiffState comes pre-loaded with a set of components
|
|
/// that were modified by the eventtrigger. This prevents doubly evaluating components if they were both updated via
|
|
/// that were modified by the eventtrigger. This prevents doubly evaluating components if they were both updated via
|
|
/// subscriptions and props changes.
|
|
/// subscriptions and props changes.
|
|
-pub struct DiffMachine<'a> {
|
|
|
|
- pub create_diffs: bool,
|
|
|
|
|
|
+pub struct DiffMachine<'a, Dom: RealDom> {
|
|
|
|
+ pub dom: &'a mut Dom,
|
|
pub cur_idx: ScopeIdx,
|
|
pub cur_idx: ScopeIdx,
|
|
- pub change_list: EditMachine<'a>,
|
|
|
|
pub diffed: FxHashSet<ScopeIdx>,
|
|
pub diffed: FxHashSet<ScopeIdx>,
|
|
pub components: ScopeArena,
|
|
pub components: ScopeArena,
|
|
pub event_queue: EventQueue,
|
|
pub event_queue: EventQueue,
|
|
pub seen_nodes: FxHashSet<ScopeIdx>,
|
|
pub seen_nodes: FxHashSet<ScopeIdx>,
|
|
}
|
|
}
|
|
|
|
|
|
-static COUNTER: AtomicU32 = AtomicU32::new(1);
|
|
|
|
-fn next_id() -> u32 {
|
|
|
|
- COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<'a> DiffMachine<'a> {
|
|
|
|
- pub fn new(components: ScopeArena, cur_idx: ScopeIdx, event_queue: EventQueue) -> Self {
|
|
|
|
|
|
+impl<'a, Dom: RealDom> DiffMachine<'a, Dom> {
|
|
|
|
+ pub fn new(
|
|
|
|
+ dom: &'a mut Dom,
|
|
|
|
+ components: ScopeArena,
|
|
|
|
+ cur_idx: ScopeIdx,
|
|
|
|
+ event_queue: EventQueue,
|
|
|
|
+ ) -> Self {
|
|
Self {
|
|
Self {
|
|
components,
|
|
components,
|
|
|
|
+ dom,
|
|
cur_idx,
|
|
cur_idx,
|
|
event_queue,
|
|
event_queue,
|
|
- create_diffs: true,
|
|
|
|
- change_list: EditMachine::new(),
|
|
|
|
diffed: FxHashSet::default(),
|
|
diffed: FxHashSet::default(),
|
|
seen_nodes: FxHashSet::default(),
|
|
seen_nodes: FxHashSet::default(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- pub fn consume(self) -> EditList<'a> {
|
|
|
|
- self.change_list.emitter
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ // Diff the `old` node with the `new` node. Emits instructions to modify a
|
|
|
|
+ // physical DOM node that reflects `old` into something that reflects `new`.
|
|
|
|
+ //
|
|
|
|
+ // Upon entry to this function, the physical DOM node must be on the top of the
|
|
|
|
+ // change list stack:
|
|
|
|
+ //
|
|
|
|
+ // [... node]
|
|
|
|
+ //
|
|
|
|
+ // The change list stack is in the same state when this function exits.
|
|
|
|
+ // In the case of Fragments, the parent node is on the stack
|
|
pub fn diff_node(&mut self, old_node: &VNode<'a>, new_node: &VNode<'a>) {
|
|
pub fn diff_node(&mut self, old_node: &VNode<'a>, new_node: &VNode<'a>) {
|
|
- // pub fn diff_node(&mut self, old: &VNode<'a>, new: &VNode<'a>) {
|
|
|
|
|
|
+ // pub fn diff_node(&self, old: &VNode<'a>, new: &VNode<'a>) {
|
|
/*
|
|
/*
|
|
For each valid case, we "commit traversal", meaning we save this current position in the tree.
|
|
For each valid case, we "commit traversal", meaning we save this current position in the tree.
|
|
Then, we diff and queue an edit event (via chagelist). s single trees - when components show up, we save that traversal and then re-enter later.
|
|
Then, we diff and queue an edit event (via chagelist). s single trees - when components show up, we save that traversal and then re-enter later.
|
|
When re-entering, we reuse the EditList in DiffState
|
|
When re-entering, we reuse the EditList in DiffState
|
|
*/
|
|
*/
|
|
- match (old_node, new_node) {
|
|
|
|
- (VNode::Text(old_text), VNode::Text(new_text)) => {
|
|
|
|
- if old_text != new_text {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.set_text(new_text);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- (VNode::Text(_), VNode::Element(_)) => {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create(new_node);
|
|
|
|
- self.change_list.replace_with();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- (VNode::Element(_), VNode::Text(_)) => {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create(new_node);
|
|
|
|
- self.change_list.replace_with();
|
|
|
|
- }
|
|
|
|
|
|
+ match old_node {
|
|
|
|
+ VNode::Element(old) => match new_node {
|
|
|
|
+ // New node is an element, old node was en element, need to investiage more deeply
|
|
|
|
+ VNode::Element(new) => {
|
|
|
|
+ // If the element type is completely different, the element needs to be re-rendered completely
|
|
|
|
+ // This is an optimization React makes due to how users structure their code
|
|
|
|
+ if new.tag_name != old.tag_name || new.namespace != old.namespace {
|
|
|
|
+ self.create(new_node);
|
|
|
|
+ self.dom.replace_with();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ new.dom_id.set(old.dom_id.get());
|
|
|
|
|
|
- (VNode::Element(eold), VNode::Element(enew)) => {
|
|
|
|
- // If the element type is completely different, the element needs to be re-rendered completely
|
|
|
|
- if enew.tag_name != eold.tag_name || enew.namespace != eold.namespace {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.replace_with();
|
|
|
|
- return;
|
|
|
|
|
|
+ self.diff_listeners(old.listeners, new.listeners);
|
|
|
|
+ self.diff_attr(old.attributes, new.attributes, new.namespace.is_some());
|
|
|
|
+ self.diff_children(old.children, new.children);
|
|
|
|
+ }
|
|
|
|
+ // New node is a text element, need to replace the element with a simple text node
|
|
|
|
+ VNode::Text(_) => {
|
|
|
|
+ log::debug!("Replacing el with text");
|
|
|
|
+ self.create(new_node);
|
|
|
|
+ self.dom.replace_with();
|
|
}
|
|
}
|
|
|
|
|
|
- self.diff_listeners(eold.listeners, enew.listeners);
|
|
|
|
- self.diff_attr(eold.attributes, enew.attributes, enew.namespace.is_some());
|
|
|
|
- self.diff_children(eold.children, enew.children);
|
|
|
|
- }
|
|
|
|
|
|
+ // New node is a component
|
|
|
|
+ // Make the component and replace our element on the stack with it
|
|
|
|
+ VNode::Component(_) => {
|
|
|
|
+ self.create(new_node);
|
|
|
|
+ self.dom.replace_with();
|
|
|
|
+ }
|
|
|
|
|
|
- (VNode::Component(cold), VNode::Component(cnew)) => {
|
|
|
|
- // Make sure we're dealing with the same component (by function pointer)
|
|
|
|
- if cold.user_fc == cnew.user_fc {
|
|
|
|
- // Make sure the new component vnode is referencing the right scope id
|
|
|
|
- let scope_id = cold.ass_scope.borrow().clone();
|
|
|
|
- *cnew.ass_scope.borrow_mut() = scope_id;
|
|
|
|
-
|
|
|
|
- // make sure the component's caller function is up to date
|
|
|
|
- self.components
|
|
|
|
- .with_scope(scope_id.unwrap(), |scope| {
|
|
|
|
- scope.caller = Rc::downgrade(&cnew.caller)
|
|
|
|
- })
|
|
|
|
- .unwrap();
|
|
|
|
-
|
|
|
|
- // React doesn't automatically memoize, but we do.
|
|
|
|
- // The cost is low enough to make it worth checking
|
|
|
|
- let should_render = match cold.comparator {
|
|
|
|
- Some(comparator) => comparator(cnew),
|
|
|
|
- None => true,
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- if should_render {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.components
|
|
|
|
- .with_scope(scope_id.unwrap(), |f| {
|
|
|
|
- f.run_scope().unwrap();
|
|
|
|
- })
|
|
|
|
- .unwrap();
|
|
|
|
- // diff_machine.change_list.load_known_root(root_id);
|
|
|
|
- // run the scope
|
|
|
|
- //
|
|
|
|
- } else {
|
|
|
|
- // Component has memoized itself and doesn't need to be re-rendered.
|
|
|
|
- // We still need to make sure the child's props are up-to-date.
|
|
|
|
- // Don't commit traversal
|
|
|
|
|
|
+ // New node is actually a sequence of nodes.
|
|
|
|
+ // We need to replace this one node with a sequence of nodes
|
|
|
|
+ // Not yet implement because it's kinda hairy
|
|
|
|
+ VNode::Fragment(_) => todo!(),
|
|
|
|
+
|
|
|
|
+ // New Node is actually suspended. Todo
|
|
|
|
+ VNode::Suspended => todo!(),
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Old element was text
|
|
|
|
+ VNode::Text(old) => match new_node {
|
|
|
|
+ VNode::Text(new) => {
|
|
|
|
+ if old.text != new.text {
|
|
|
|
+ log::debug!("Text has changed {}, {}", old.text, new.text);
|
|
|
|
+ self.dom.set_text(new.text);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- // A new component has shown up! We need to destroy the old node
|
|
|
|
-
|
|
|
|
- // Wipe the old one and plant the new one
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
|
|
+ new.dom_id.set(old.dom_id.get());
|
|
|
|
+ }
|
|
|
|
+ VNode::Element(_) | VNode::Component(_) => {
|
|
self.create(new_node);
|
|
self.create(new_node);
|
|
- self.change_list.replace_with();
|
|
|
|
-
|
|
|
|
- // Now we need to remove the old scope and all of its descendents
|
|
|
|
- let old_scope = cold.ass_scope.borrow().as_ref().unwrap().clone();
|
|
|
|
- self.destroy_scopes(old_scope);
|
|
|
|
|
|
+ self.dom.replace_with();
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- // todo: knock out any listeners
|
|
|
|
- (_, VNode::Component(_)) => {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create(new_node);
|
|
|
|
- self.change_list.replace_with();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // A component is being torn down in favor of a non-component node
|
|
|
|
- (VNode::Component(_old), _) => {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create(new_node);
|
|
|
|
- self.change_list.replace_with();
|
|
|
|
-
|
|
|
|
- // Destroy the original scope and any of its children
|
|
|
|
- self.destroy_scopes(_old.ass_scope.borrow().unwrap());
|
|
|
|
- }
|
|
|
|
|
|
+ // TODO on handling these types
|
|
|
|
+ VNode::Fragment(_) => todo!(),
|
|
|
|
+ VNode::Suspended => todo!(),
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // Old element was a component
|
|
|
|
+ VNode::Component(old) => {
|
|
|
|
+ match new_node {
|
|
|
|
+ // It's something entirely different
|
|
|
|
+ VNode::Element(_) | VNode::Text(_) => {
|
|
|
|
+ self.create(new_node);
|
|
|
|
+ self.dom.replace_with();
|
|
|
|
+ }
|
|
|
|
|
|
- // Anything suspended is not enabled ATM
|
|
|
|
- (VNode::Suspended, _) | (_, VNode::Suspended) => {
|
|
|
|
- todo!("Suspended components not currently available")
|
|
|
|
|
|
+ // It's also a component
|
|
|
|
+ VNode::Component(new) => {
|
|
|
|
+ match old.user_fc == new.user_fc {
|
|
|
|
+ // Make sure we're dealing with the same component (by function pointer)
|
|
|
|
+ true => {
|
|
|
|
+ // Make sure the new component vnode is referencing the right scope id
|
|
|
|
+ let scope_id = old.ass_scope.borrow().clone();
|
|
|
|
+ *new.ass_scope.borrow_mut() = scope_id;
|
|
|
|
+
|
|
|
|
+ // make sure the component's caller function is up to date
|
|
|
|
+ self.components
|
|
|
|
+ .with_scope(scope_id.unwrap(), |scope| {
|
|
|
|
+ scope.caller = Rc::downgrade(&new.caller)
|
|
|
|
+ })
|
|
|
|
+ .unwrap();
|
|
|
|
+
|
|
|
|
+ // React doesn't automatically memoize, but we do.
|
|
|
|
+ // The cost is low enough to make it worth checking
|
|
|
|
+ let should_render = match old.comparator {
|
|
|
|
+ Some(comparator) => comparator(new),
|
|
|
|
+ None => true,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if should_render {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ self.components
|
|
|
|
+ .with_scope(scope_id.unwrap(), |f| {
|
|
|
|
+ f.run_scope().unwrap();
|
|
|
|
+ })
|
|
|
|
+ .unwrap();
|
|
|
|
+ // diff_machine.change_list.load_known_root(root_id);
|
|
|
|
+ // run the scope
|
|
|
|
+ //
|
|
|
|
+ } else {
|
|
|
|
+ // Component has memoized itself and doesn't need to be re-rendered.
|
|
|
|
+ // We still need to make sure the child's props are up-to-date.
|
|
|
|
+ // Don't commit traversal
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // It's an entirely different component
|
|
|
|
+ false => {
|
|
|
|
+ // A new component has shown up! We need to destroy the old node
|
|
|
|
+
|
|
|
|
+ // Wipe the old one and plant the new one
|
|
|
|
+ // self.dom.commit_traversal();
|
|
|
|
+ // self.dom.replace_node_with(old.dom_id, new.dom_id);
|
|
|
|
+ // self.create(new_node);
|
|
|
|
+ // self.dom.replace_with();
|
|
|
|
+ self.create(new_node);
|
|
|
|
+ // self.create_and_repalce(new_node, old.mounted_root.get());
|
|
|
|
+
|
|
|
|
+ // Now we need to remove the old scope and all of its descendents
|
|
|
|
+ let old_scope = old.ass_scope.borrow().as_ref().unwrap().clone();
|
|
|
|
+ self.destroy_scopes(old_scope);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ VNode::Fragment(_) => todo!(),
|
|
|
|
+ VNode::Suspended => todo!(),
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- // Fragments are special
|
|
|
|
- // we actually have to remove a bunch of nodes
|
|
|
|
- (VNode::Fragment(_), _) => {
|
|
|
|
- todo!("Fragments not currently supported in diffing")
|
|
|
|
- }
|
|
|
|
|
|
+ VNode::Fragment(old) => {
|
|
|
|
+ //
|
|
|
|
+ match new_node {
|
|
|
|
+ VNode::Fragment(_) => todo!(),
|
|
|
|
|
|
- (VNode::Fragment(_), VNode::Fragment(_)) => {
|
|
|
|
- todo!("Fragments not currently supported in diffing")
|
|
|
|
|
|
+ // going from fragment to element means we're going from many (or potentially none) to one
|
|
|
|
+ VNode::Element(new) => {}
|
|
|
|
+ VNode::Text(_) => todo!(),
|
|
|
|
+ VNode::Suspended => todo!(),
|
|
|
|
+ VNode::Component(_) => todo!(),
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- (old_n, VNode::Fragment(_)) => {
|
|
|
|
- match old_n {
|
|
|
|
|
|
+ // a suspended node will perform a mem-copy of the previous elements until it is ready
|
|
|
|
+ // this means that event listeners will need to be disabled and removed
|
|
|
|
+ // it also means that props will need to disabled - IE if the node "came out of hibernation" any props should be considered outdated
|
|
|
|
+ VNode::Suspended => {
|
|
|
|
+ //
|
|
|
|
+ match new_node {
|
|
|
|
+ VNode::Suspended => todo!(),
|
|
VNode::Element(_) => todo!(),
|
|
VNode::Element(_) => todo!(),
|
|
VNode::Text(_) => todo!(),
|
|
VNode::Text(_) => todo!(),
|
|
VNode::Fragment(_) => todo!(),
|
|
VNode::Fragment(_) => todo!(),
|
|
- VNode::Suspended => todo!(),
|
|
|
|
VNode::Component(_) => todo!(),
|
|
VNode::Component(_) => todo!(),
|
|
}
|
|
}
|
|
- todo!("Fragments not currently supported in diffing")
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -224,33 +302,38 @@ impl<'a> DiffMachine<'a> {
|
|
//
|
|
//
|
|
// [... node]
|
|
// [... node]
|
|
fn create(&mut self, node: &VNode<'a>) {
|
|
fn create(&mut self, node: &VNode<'a>) {
|
|
- debug_assert!(self.change_list.traversal_is_committed());
|
|
|
|
|
|
+ // debug_assert!(self.dom.traversal_is_committed());
|
|
match node {
|
|
match node {
|
|
VNode::Text(text) => {
|
|
VNode::Text(text) => {
|
|
- self.change_list.create_text_node(text);
|
|
|
|
|
|
+ let real_id = self.dom.create_text_node(text.text);
|
|
|
|
+ text.dom_id.set(real_id);
|
|
}
|
|
}
|
|
- VNode::Element(&VElement {
|
|
|
|
- key,
|
|
|
|
- tag_name,
|
|
|
|
- listeners,
|
|
|
|
- attributes,
|
|
|
|
- children,
|
|
|
|
- namespace,
|
|
|
|
- }) => {
|
|
|
|
|
|
+ VNode::Element(el) => {
|
|
|
|
+ let VElement {
|
|
|
|
+ key,
|
|
|
|
+ tag_name,
|
|
|
|
+ listeners,
|
|
|
|
+ attributes,
|
|
|
|
+ children,
|
|
|
|
+ namespace,
|
|
|
|
+ dom_id,
|
|
|
|
+ } = el;
|
|
// log::info!("Creating {:#?}", node);
|
|
// log::info!("Creating {:#?}", node);
|
|
- if let Some(namespace) = namespace {
|
|
|
|
- self.change_list.create_element_ns(tag_name, namespace);
|
|
|
|
|
|
+ let real_id = if let Some(namespace) = namespace {
|
|
|
|
+ self.dom.create_element_ns(tag_name, namespace)
|
|
} else {
|
|
} else {
|
|
- self.change_list.create_element(tag_name);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- listeners.iter().enumerate().for_each(|(_id, listener)| {
|
|
|
|
- self.change_list
|
|
|
|
- .new_event_listener(listener.event, listener.scope, listener.id)
|
|
|
|
|
|
+ self.dom.create_element(tag_name)
|
|
|
|
+ };
|
|
|
|
+ el.dom_id.set(real_id);
|
|
|
|
+
|
|
|
|
+ listeners.iter().enumerate().for_each(|(idx, listener)| {
|
|
|
|
+ self.dom
|
|
|
|
+ .new_event_listener(listener.event, listener.scope, idx, real_id);
|
|
|
|
+ listener.mounted_node.set(real_id);
|
|
});
|
|
});
|
|
|
|
|
|
- for attr in attributes {
|
|
|
|
- self.change_list
|
|
|
|
|
|
+ for attr in *attributes {
|
|
|
|
+ self.dom
|
|
.set_attribute(&attr.name, &attr.value, namespace.is_some());
|
|
.set_attribute(&attr.name, &attr.value, namespace.is_some());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -260,30 +343,29 @@ impl<'a> DiffMachine<'a> {
|
|
// to emit three instructions to (1) create a text node, (2) set its
|
|
// to emit three instructions to (1) create a text node, (2) set its
|
|
// text content, and finally (3) append the text node to this
|
|
// text content, and finally (3) append the text node to this
|
|
// parent.
|
|
// parent.
|
|
- if children.len() == 1 {
|
|
|
|
- if let VNode::Text(text) = children[0] {
|
|
|
|
- self.change_list.set_text(text);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for child in children {
|
|
|
|
|
|
+ // if children.len() == 1 {
|
|
|
|
+ // if let VNode::Text(text) = &children[0] {
|
|
|
|
+ // self.dom.set_text(text.text);
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ for child in *children {
|
|
self.create(child);
|
|
self.create(child);
|
|
if let VNode::Fragment(_) = child {
|
|
if let VNode::Fragment(_) = child {
|
|
// do nothing
|
|
// do nothing
|
|
// fragments append themselves
|
|
// fragments append themselves
|
|
} else {
|
|
} else {
|
|
- self.change_list.append_child();
|
|
|
|
|
|
+ self.dom.append_child();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
VNode::Component(component) => {
|
|
VNode::Component(component) => {
|
|
- self.change_list
|
|
|
|
- .create_text_node("placeholder for vcomponent");
|
|
|
|
|
|
+ self.dom.create_text_node("placeholder for vcomponent");
|
|
|
|
|
|
- let root_id = next_id();
|
|
|
|
- self.change_list.save_known_root(root_id);
|
|
|
|
|
|
+ // let root_id = next_id();
|
|
|
|
+ // self.dom.save_known_root(root_id);
|
|
|
|
|
|
log::debug!("Mounting a new component");
|
|
log::debug!("Mounting a new component");
|
|
let caller: Weak<OpaqueComponent> = Rc::downgrade(&component.caller);
|
|
let caller: Weak<OpaqueComponent> = Rc::downgrade(&component.caller);
|
|
@@ -333,6 +415,7 @@ impl<'a> DiffMachine<'a> {
|
|
new_component.run_scope().unwrap();
|
|
new_component.run_scope().unwrap();
|
|
|
|
|
|
// And then run the diff algorithm
|
|
// And then run the diff algorithm
|
|
|
|
+ // todo!();
|
|
self.diff_node(new_component.old_frame(), new_component.next_frame());
|
|
self.diff_node(new_component.old_frame(), new_component.next_frame());
|
|
|
|
|
|
// Finally, insert this node as a seen node.
|
|
// Finally, insert this node as a seen node.
|
|
@@ -343,8 +426,9 @@ impl<'a> DiffMachine<'a> {
|
|
VNode::Fragment(frag) => {
|
|
VNode::Fragment(frag) => {
|
|
// create the children directly in the space
|
|
// create the children directly in the space
|
|
for child in frag.children {
|
|
for child in frag.children {
|
|
- self.create(child);
|
|
|
|
- self.change_list.append_child();
|
|
|
|
|
|
+ todo!()
|
|
|
|
+ // self.create(child);
|
|
|
|
+ // self.dom.append_child();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -353,7 +437,9 @@ impl<'a> DiffMachine<'a> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
|
|
+impl<'a, Dom: RealDom> DiffMachine<'a, Dom> {
|
|
/// Destroy a scope and all of its descendents.
|
|
/// Destroy a scope and all of its descendents.
|
|
///
|
|
///
|
|
/// Calling this will run the destuctors on all hooks in the tree.
|
|
/// Calling this will run the destuctors on all hooks in the tree.
|
|
@@ -395,8 +481,10 @@ impl<'a> DiffMachine<'a> {
|
|
// The change list stack is left unchanged.
|
|
// The change list stack is left unchanged.
|
|
fn diff_listeners(&mut self, old: &[Listener<'_>], new: &[Listener<'_>]) {
|
|
fn diff_listeners(&mut self, old: &[Listener<'_>], new: &[Listener<'_>]) {
|
|
if !old.is_empty() || !new.is_empty() {
|
|
if !old.is_empty() || !new.is_empty() {
|
|
- self.change_list.commit_traversal();
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
}
|
|
}
|
|
|
|
+ // TODO
|
|
|
|
+ // what does "diffing listeners" even mean?
|
|
|
|
|
|
'outer1: for (_l_idx, new_l) in new.iter().enumerate() {
|
|
'outer1: for (_l_idx, new_l) in new.iter().enumerate() {
|
|
// go through each new listener
|
|
// go through each new listener
|
|
@@ -404,33 +492,34 @@ impl<'a> DiffMachine<'a> {
|
|
// if any characteristics changed, remove and then re-add
|
|
// if any characteristics changed, remove and then re-add
|
|
|
|
|
|
// if nothing changed, then just move on
|
|
// if nothing changed, then just move on
|
|
-
|
|
|
|
let event_type = new_l.event;
|
|
let event_type = new_l.event;
|
|
|
|
|
|
for old_l in old {
|
|
for old_l in old {
|
|
if new_l.event == old_l.event {
|
|
if new_l.event == old_l.event {
|
|
- if new_l.id != old_l.id {
|
|
|
|
- self.change_list.remove_event_listener(event_type);
|
|
|
|
- self.change_list
|
|
|
|
- .update_event_listener(event_type, new_l.scope, new_l.id)
|
|
|
|
- }
|
|
|
|
|
|
+ new_l.mounted_node.set(old_l.mounted_node.get());
|
|
|
|
+ // if new_l.id != old_l.id {
|
|
|
|
+ // self.dom.remove_event_listener(event_type);
|
|
|
|
+ // // TODO! we need to mess with events and assign them by RealDomNode
|
|
|
|
+ // // self.dom
|
|
|
|
+ // // .update_event_listener(event_type, new_l.scope, new_l.id)
|
|
|
|
+ // }
|
|
|
|
|
|
continue 'outer1;
|
|
continue 'outer1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- self.change_list
|
|
|
|
- .new_event_listener(event_type, new_l.scope, new_l.id);
|
|
|
|
|
|
+ // self.dom
|
|
|
|
+ // .new_event_listener(event_type, new_l.scope, new_l.id);
|
|
}
|
|
}
|
|
|
|
|
|
- 'outer2: for old_l in old {
|
|
|
|
- for new_l in new {
|
|
|
|
- if new_l.event == old_l.event {
|
|
|
|
- continue 'outer2;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- self.change_list.remove_event_listener(old_l.event);
|
|
|
|
- }
|
|
|
|
|
|
+ // 'outer2: for old_l in old {
|
|
|
|
+ // for new_l in new {
|
|
|
|
+ // if new_l.event == old_l.event {
|
|
|
|
+ // continue 'outer2;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // self.dom.remove_event_listener(old_l.event);
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
|
|
|
|
// Diff a node's attributes.
|
|
// Diff a node's attributes.
|
|
@@ -453,19 +542,16 @@ impl<'a> DiffMachine<'a> {
|
|
// With the Rsx and Html macros, this will almost always be the case
|
|
// With the Rsx and Html macros, this will almost always be the case
|
|
'outer: for new_attr in new {
|
|
'outer: for new_attr in new {
|
|
if new_attr.is_volatile() {
|
|
if new_attr.is_volatile() {
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
|
|
+ self.dom
|
|
.set_attribute(new_attr.name, new_attr.value, is_namespaced);
|
|
.set_attribute(new_attr.name, new_attr.value, is_namespaced);
|
|
} else {
|
|
} else {
|
|
for old_attr in old {
|
|
for old_attr in old {
|
|
if old_attr.name == new_attr.name {
|
|
if old_attr.name == new_attr.name {
|
|
if old_attr.value != new_attr.value {
|
|
if old_attr.value != new_attr.value {
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.set_attribute(
|
|
|
|
- new_attr.name,
|
|
|
|
- new_attr.value,
|
|
|
|
- is_namespaced,
|
|
|
|
- );
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
|
|
+ self.dom
|
|
|
|
+ .set_attribute(new_attr.name, new_attr.value, is_namespaced);
|
|
}
|
|
}
|
|
continue 'outer;
|
|
continue 'outer;
|
|
} else {
|
|
} else {
|
|
@@ -473,8 +559,8 @@ impl<'a> DiffMachine<'a> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
|
|
+ self.dom
|
|
.set_attribute(new_attr.name, new_attr.value, is_namespaced);
|
|
.set_attribute(new_attr.name, new_attr.value, is_namespaced);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -486,8 +572,8 @@ impl<'a> DiffMachine<'a> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.remove_attribute(old_attr.name);
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
|
|
+ self.dom.remove_attribute(old_attr.name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -502,23 +588,26 @@ impl<'a> DiffMachine<'a> {
|
|
fn diff_children(&mut self, old: &'a [VNode<'a>], new: &'a [VNode<'a>]) {
|
|
fn diff_children(&mut self, old: &'a [VNode<'a>], new: &'a [VNode<'a>]) {
|
|
if new.is_empty() {
|
|
if new.is_empty() {
|
|
if !old.is_empty() {
|
|
if !old.is_empty() {
|
|
- self.change_list.commit_traversal();
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
self.remove_all_children(old);
|
|
self.remove_all_children(old);
|
|
}
|
|
}
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
if new.len() == 1 {
|
|
if new.len() == 1 {
|
|
- match (old.first(), &new[0]) {
|
|
|
|
- (Some(&VNode::Text(old_text)), &VNode::Text(new_text)) if old_text == new_text => {
|
|
|
|
|
|
+ match (&old.first(), &new[0]) {
|
|
|
|
+ (Some(VNode::Text(old_vtext)), VNode::Text(new_vtext))
|
|
|
|
+ if old_vtext.text == new_vtext.text =>
|
|
|
|
+ {
|
|
// Don't take this fast path...
|
|
// Don't take this fast path...
|
|
}
|
|
}
|
|
|
|
|
|
- (_, &VNode::Text(text)) => {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.set_text(text);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ // (_, VNode::Text(text)) => {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // log::debug!("using optimized text set");
|
|
|
|
+ // self.dom.set_text(text.text);
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
|
|
// todo: any more optimizations
|
|
// todo: any more optimizations
|
|
(_, _) => {}
|
|
(_, _) => {}
|
|
@@ -527,7 +616,7 @@ impl<'a> DiffMachine<'a> {
|
|
|
|
|
|
if old.is_empty() {
|
|
if old.is_empty() {
|
|
if !new.is_empty() {
|
|
if !new.is_empty() {
|
|
- self.change_list.commit_traversal();
|
|
|
|
|
|
+ // self.dom.commit_traversal();
|
|
self.create_and_append_children(new);
|
|
self.create_and_append_children(new);
|
|
}
|
|
}
|
|
return;
|
|
return;
|
|
@@ -546,9 +635,10 @@ impl<'a> DiffMachine<'a> {
|
|
);
|
|
);
|
|
|
|
|
|
if new_is_keyed && old_is_keyed {
|
|
if new_is_keyed && old_is_keyed {
|
|
- let t = self.change_list.next_temporary();
|
|
|
|
- self.diff_keyed_children(old, new);
|
|
|
|
- self.change_list.set_next_temporary(t);
|
|
|
|
|
|
+ todo!("Not yet implemented a migration away from temporaries");
|
|
|
|
+ // let t = self.dom.next_temporary();
|
|
|
|
+ // self.diff_keyed_children(old, new);
|
|
|
|
+ // self.dom.set_next_temporary(t);
|
|
} else {
|
|
} else {
|
|
self.diff_non_keyed_children(old, new);
|
|
self.diff_non_keyed_children(old, new);
|
|
}
|
|
}
|
|
@@ -575,7 +665,8 @@ impl<'a> DiffMachine<'a> {
|
|
// [... parent]
|
|
// [... parent]
|
|
//
|
|
//
|
|
// Upon exiting, the change list stack is in the same state.
|
|
// Upon exiting, the change list stack is in the same state.
|
|
- fn diff_keyed_children(&mut self, old: &[VNode<'a>], new: &[VNode<'a>]) {
|
|
|
|
|
|
+ fn diff_keyed_children(&self, old: &[VNode<'a>], new: &[VNode<'a>]) {
|
|
|
|
+ todo!();
|
|
// if cfg!(debug_assertions) {
|
|
// if cfg!(debug_assertions) {
|
|
// let mut keys = fxhash::FxHashSet::default();
|
|
// let mut keys = fxhash::FxHashSet::default();
|
|
// let mut assert_unique_keys = |children: &[VNode]| {
|
|
// let mut assert_unique_keys = |children: &[VNode]| {
|
|
@@ -658,42 +749,43 @@ impl<'a> DiffMachine<'a> {
|
|
// [... parent]
|
|
// [... parent]
|
|
//
|
|
//
|
|
// Upon exit, the change list stack is the same.
|
|
// Upon exit, the change list stack is the same.
|
|
- fn diff_keyed_prefix(&mut self, old: &[VNode<'a>], new: &[VNode<'a>]) -> KeyedPrefixResult {
|
|
|
|
- self.change_list.go_down();
|
|
|
|
- let mut shared_prefix_count = 0;
|
|
|
|
|
|
+ fn diff_keyed_prefix(&self, old: &[VNode<'a>], new: &[VNode<'a>]) -> KeyedPrefixResult {
|
|
|
|
+ todo!()
|
|
|
|
+ // self.dom.go_down();
|
|
|
|
+ // let mut shared_prefix_count = 0;
|
|
|
|
|
|
- for (i, (old, new)) in old.iter().zip(new.iter()).enumerate() {
|
|
|
|
- if old.key() != new.key() {
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ // for (i, (old, new)) in old.iter().zip(new.iter()).enumerate() {
|
|
|
|
+ // if old.key() != new.key() {
|
|
|
|
+ // break;
|
|
|
|
+ // }
|
|
|
|
|
|
- self.change_list.go_to_sibling(i);
|
|
|
|
|
|
+ // self.dom.go_to_sibling(i);
|
|
|
|
|
|
- self.diff_node(old, new);
|
|
|
|
|
|
+ // self.diff_node(old, new);
|
|
|
|
|
|
- shared_prefix_count += 1;
|
|
|
|
- }
|
|
|
|
|
|
+ // shared_prefix_count += 1;
|
|
|
|
+ // }
|
|
|
|
|
|
- // If that was all of the old children, then create and append the remaining
|
|
|
|
- // new children and we're finished.
|
|
|
|
- if shared_prefix_count == old.len() {
|
|
|
|
- self.change_list.go_up();
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create_and_append_children(&new[shared_prefix_count..]);
|
|
|
|
- return KeyedPrefixResult::Finished;
|
|
|
|
- }
|
|
|
|
|
|
+ // // If that was all of the old children, then create and append the remaining
|
|
|
|
+ // // new children and we're finished.
|
|
|
|
+ // if shared_prefix_count == old.len() {
|
|
|
|
+ // self.dom.go_up();
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.create_and_append_children(&new[shared_prefix_count..]);
|
|
|
|
+ // return KeyedPrefixResult::Finished;
|
|
|
|
+ // }
|
|
|
|
|
|
- // And if that was all of the new children, then remove all of the remaining
|
|
|
|
- // old children and we're finished.
|
|
|
|
- if shared_prefix_count == new.len() {
|
|
|
|
- self.change_list.go_to_sibling(shared_prefix_count);
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.remove_self_and_next_siblings(&old[shared_prefix_count..]);
|
|
|
|
- return KeyedPrefixResult::Finished;
|
|
|
|
- }
|
|
|
|
|
|
+ // // And if that was all of the new children, then remove all of the remaining
|
|
|
|
+ // // old children and we're finished.
|
|
|
|
+ // if shared_prefix_count == new.len() {
|
|
|
|
+ // self.dom.go_to_sibling(shared_prefix_count);
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.remove_self_and_next_siblings(&old[shared_prefix_count..]);
|
|
|
|
+ // return KeyedPrefixResult::Finished;
|
|
|
|
+ // }
|
|
|
|
|
|
- self.change_list.go_up();
|
|
|
|
- KeyedPrefixResult::MoreWorkToDo(shared_prefix_count)
|
|
|
|
|
|
+ // self.dom.go_up();
|
|
|
|
+ // KeyedPrefixResult::MoreWorkToDo(shared_prefix_count)
|
|
}
|
|
}
|
|
|
|
|
|
// The most-general, expensive code path for keyed children diffing.
|
|
// The most-general, expensive code path for keyed children diffing.
|
|
@@ -710,222 +802,223 @@ impl<'a> DiffMachine<'a> {
|
|
//
|
|
//
|
|
// Upon exit from this function, it will be restored to that same state.
|
|
// Upon exit from this function, it will be restored to that same state.
|
|
fn diff_keyed_middle(
|
|
fn diff_keyed_middle(
|
|
- &mut self,
|
|
|
|
|
|
+ &self,
|
|
old: &[VNode<'a>],
|
|
old: &[VNode<'a>],
|
|
mut new: &[VNode<'a>],
|
|
mut new: &[VNode<'a>],
|
|
shared_prefix_count: usize,
|
|
shared_prefix_count: usize,
|
|
shared_suffix_count: usize,
|
|
shared_suffix_count: usize,
|
|
old_shared_suffix_start: usize,
|
|
old_shared_suffix_start: usize,
|
|
) {
|
|
) {
|
|
- // Should have already diffed the shared-key prefixes and suffixes.
|
|
|
|
- debug_assert_ne!(new.first().map(|n| n.key()), old.first().map(|o| o.key()));
|
|
|
|
- debug_assert_ne!(new.last().map(|n| n.key()), old.last().map(|o| o.key()));
|
|
|
|
-
|
|
|
|
- // The algorithm below relies upon using `u32::MAX` as a sentinel
|
|
|
|
- // value, so if we have that many new nodes, it won't work. This
|
|
|
|
- // check is a bit academic (hence only enabled in debug), since
|
|
|
|
- // wasm32 doesn't have enough address space to hold that many nodes
|
|
|
|
- // in memory.
|
|
|
|
- debug_assert!(new.len() < u32::MAX as usize);
|
|
|
|
-
|
|
|
|
- // Map from each `old` node's key to its index within `old`.
|
|
|
|
- let mut old_key_to_old_index = FxHashMap::default();
|
|
|
|
- old_key_to_old_index.reserve(old.len());
|
|
|
|
- old_key_to_old_index.extend(old.iter().enumerate().map(|(i, o)| (o.key(), i)));
|
|
|
|
-
|
|
|
|
- // The set of shared keys between `new` and `old`.
|
|
|
|
- let mut shared_keys = FxHashSet::default();
|
|
|
|
- // Map from each index in `new` to the index of the node in `old` that
|
|
|
|
- // has the same key.
|
|
|
|
- let mut new_index_to_old_index = Vec::with_capacity(new.len());
|
|
|
|
- new_index_to_old_index.extend(new.iter().map(|n| {
|
|
|
|
- let key = n.key();
|
|
|
|
- if let Some(&i) = old_key_to_old_index.get(&key) {
|
|
|
|
- shared_keys.insert(key);
|
|
|
|
- i
|
|
|
|
- } else {
|
|
|
|
- u32::MAX as usize
|
|
|
|
- }
|
|
|
|
- }));
|
|
|
|
-
|
|
|
|
- // If none of the old keys are reused by the new children, then we
|
|
|
|
- // remove all the remaining old children and create the new children
|
|
|
|
- // afresh.
|
|
|
|
- if shared_suffix_count == 0 && shared_keys.is_empty() {
|
|
|
|
- if shared_prefix_count == 0 {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.remove_all_children(old);
|
|
|
|
- } else {
|
|
|
|
- self.change_list.go_down_to_child(shared_prefix_count);
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.remove_self_and_next_siblings(&old[shared_prefix_count..]);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- self.create_and_append_children(new);
|
|
|
|
-
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Save each of the old children whose keys are reused in the new
|
|
|
|
- // children.
|
|
|
|
- let mut old_index_to_temp = vec![u32::MAX; old.len()];
|
|
|
|
- let mut start = 0;
|
|
|
|
- loop {
|
|
|
|
- let end = (start..old.len())
|
|
|
|
- .find(|&i| {
|
|
|
|
- let key = old[i].key();
|
|
|
|
- !shared_keys.contains(&key)
|
|
|
|
- })
|
|
|
|
- .unwrap_or(old.len());
|
|
|
|
-
|
|
|
|
- if end - start > 0 {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- let mut t = self.change_list.save_children_to_temporaries(
|
|
|
|
- shared_prefix_count + start,
|
|
|
|
- shared_prefix_count + end,
|
|
|
|
- );
|
|
|
|
- for i in start..end {
|
|
|
|
- old_index_to_temp[i] = t;
|
|
|
|
- t += 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- debug_assert!(end <= old.len());
|
|
|
|
- if end == old.len() {
|
|
|
|
- break;
|
|
|
|
- } else {
|
|
|
|
- start = end + 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ todo!()
|
|
|
|
+ // // Should have already diffed the shared-key prefixes and suffixes.
|
|
|
|
+ // debug_assert_ne!(new.first().map(|n| n.key()), old.first().map(|o| o.key()));
|
|
|
|
+ // debug_assert_ne!(new.last().map(|n| n.key()), old.last().map(|o| o.key()));
|
|
|
|
+
|
|
|
|
+ // // The algorithm below relies upon using `u32::MAX` as a sentinel
|
|
|
|
+ // // value, so if we have that many new nodes, it won't work. This
|
|
|
|
+ // // check is a bit academic (hence only enabled in debug), since
|
|
|
|
+ // // wasm32 doesn't have enough address space to hold that many nodes
|
|
|
|
+ // // in memory.
|
|
|
|
+ // debug_assert!(new.len() < u32::MAX as usize);
|
|
|
|
+
|
|
|
|
+ // // Map from each `old` node's key to its index within `old`.
|
|
|
|
+ // let mut old_key_to_old_index = FxHashMap::default();
|
|
|
|
+ // old_key_to_old_index.reserve(old.len());
|
|
|
|
+ // old_key_to_old_index.extend(old.iter().enumerate().map(|(i, o)| (o.key(), i)));
|
|
|
|
+
|
|
|
|
+ // // The set of shared keys between `new` and `old`.
|
|
|
|
+ // let mut shared_keys = FxHashSet::default();
|
|
|
|
+ // // Map from each index in `new` to the index of the node in `old` that
|
|
|
|
+ // // has the same key.
|
|
|
|
+ // let mut new_index_to_old_index = Vec::with_capacity(new.len());
|
|
|
|
+ // new_index_to_old_index.extend(new.iter().map(|n| {
|
|
|
|
+ // let key = n.key();
|
|
|
|
+ // if let Some(&i) = old_key_to_old_index.get(&key) {
|
|
|
|
+ // shared_keys.insert(key);
|
|
|
|
+ // i
|
|
|
|
+ // } else {
|
|
|
|
+ // u32::MAX as usize
|
|
|
|
+ // }
|
|
|
|
+ // }));
|
|
|
|
+
|
|
|
|
+ // // If none of the old keys are reused by the new children, then we
|
|
|
|
+ // // remove all the remaining old children and create the new children
|
|
|
|
+ // // afresh.
|
|
|
|
+ // if shared_suffix_count == 0 && shared_keys.is_empty() {
|
|
|
|
+ // if shared_prefix_count == 0 {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.remove_all_children(old);
|
|
|
|
+ // } else {
|
|
|
|
+ // self.dom.go_down_to_child(shared_prefix_count);
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.remove_self_and_next_siblings(&old[shared_prefix_count..]);
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // self.create_and_append_children(new);
|
|
|
|
+
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
|
|
- // Remove any old children whose keys were not reused in the new
|
|
|
|
- // children. Remove from the end first so that we don't mess up indices.
|
|
|
|
- let mut removed_count = 0;
|
|
|
|
- for (i, old_child) in old.iter().enumerate().rev() {
|
|
|
|
- if !shared_keys.contains(&old_child.key()) {
|
|
|
|
- // registry.remove_subtree(old_child);
|
|
|
|
- // todo
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.change_list.remove_child(i + shared_prefix_count);
|
|
|
|
- removed_count += 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // // Save each of the old children whose keys are reused in the new
|
|
|
|
+ // // children.
|
|
|
|
+ // let mut old_index_to_temp = vec![u32::MAX; old.len()];
|
|
|
|
+ // let mut start = 0;
|
|
|
|
+ // loop {
|
|
|
|
+ // let end = (start..old.len())
|
|
|
|
+ // .find(|&i| {
|
|
|
|
+ // let key = old[i].key();
|
|
|
|
+ // !shared_keys.contains(&key)
|
|
|
|
+ // })
|
|
|
|
+ // .unwrap_or(old.len());
|
|
|
|
+
|
|
|
|
+ // if end - start > 0 {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // let mut t = self.dom.save_children_to_temporaries(
|
|
|
|
+ // shared_prefix_count + start,
|
|
|
|
+ // shared_prefix_count + end,
|
|
|
|
+ // );
|
|
|
|
+ // for i in start..end {
|
|
|
|
+ // old_index_to_temp[i] = t;
|
|
|
|
+ // t += 1;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // debug_assert!(end <= old.len());
|
|
|
|
+ // if end == old.len() {
|
|
|
|
+ // break;
|
|
|
|
+ // } else {
|
|
|
|
+ // start = end + 1;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
|
|
- // If there aren't any more new children, then we are done!
|
|
|
|
- if new.is_empty() {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ // // Remove any old children whose keys were not reused in the new
|
|
|
|
+ // // children. Remove from the end first so that we don't mess up indices.
|
|
|
|
+ // let mut removed_count = 0;
|
|
|
|
+ // for (i, old_child) in old.iter().enumerate().rev() {
|
|
|
|
+ // if !shared_keys.contains(&old_child.key()) {
|
|
|
|
+ // // registry.remove_subtree(old_child);
|
|
|
|
+ // // todo
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.dom.remove_child(i + shared_prefix_count);
|
|
|
|
+ // removed_count += 1;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
|
|
- // The longest increasing subsequence within `new_index_to_old_index`. This
|
|
|
|
- // is the longest sequence on DOM nodes in `old` that are relatively ordered
|
|
|
|
- // correctly within `new`. We will leave these nodes in place in the DOM,
|
|
|
|
- // and only move nodes that are not part of the LIS. This results in the
|
|
|
|
- // maximum number of DOM nodes left in place, AKA the minimum number of DOM
|
|
|
|
- // nodes moved.
|
|
|
|
- let mut new_index_is_in_lis = FxHashSet::default();
|
|
|
|
- new_index_is_in_lis.reserve(new_index_to_old_index.len());
|
|
|
|
- let mut predecessors = vec![0; new_index_to_old_index.len()];
|
|
|
|
- let mut starts = vec![0; new_index_to_old_index.len()];
|
|
|
|
- longest_increasing_subsequence::lis_with(
|
|
|
|
- &new_index_to_old_index,
|
|
|
|
- &mut new_index_is_in_lis,
|
|
|
|
- |a, b| a < b,
|
|
|
|
- &mut predecessors,
|
|
|
|
- &mut starts,
|
|
|
|
- );
|
|
|
|
|
|
+ // // If there aren't any more new children, then we are done!
|
|
|
|
+ // if new.is_empty() {
|
|
|
|
+ // return;
|
|
|
|
+ // }
|
|
|
|
|
|
- // Now we will iterate from the end of the new children back to the
|
|
|
|
- // beginning, diffing old children we are reusing and if they aren't in the
|
|
|
|
- // LIS moving them to their new destination, or creating new children. Note
|
|
|
|
- // that iterating in reverse order lets us use `Node.prototype.insertBefore`
|
|
|
|
- // to move/insert children.
|
|
|
|
- //
|
|
|
|
- // But first, we ensure that we have a child on the change list stack that
|
|
|
|
- // we can `insertBefore`. We handle this once before looping over `new`
|
|
|
|
- // children, so that we don't have to keep checking on every loop iteration.
|
|
|
|
- if shared_suffix_count > 0 {
|
|
|
|
- // There is a shared suffix after these middle children. We will be
|
|
|
|
- // inserting before that shared suffix, so add the first child of that
|
|
|
|
- // shared suffix to the change list stack.
|
|
|
|
- //
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list
|
|
|
|
- .go_down_to_child(old_shared_suffix_start - removed_count);
|
|
|
|
- // [... parent first_child_of_shared_suffix]
|
|
|
|
- } else {
|
|
|
|
- // There is no shared suffix coming after these middle children.
|
|
|
|
- // Therefore we have to process the last child in `new` and move it to
|
|
|
|
- // the end of the parent's children if it isn't already there.
|
|
|
|
- let last_index = new.len() - 1;
|
|
|
|
- // uhhhh why an unwrap?
|
|
|
|
- let last = new.last().unwrap();
|
|
|
|
- // let last = new.last().unwrap_throw();
|
|
|
|
- new = &new[..new.len() - 1];
|
|
|
|
- if shared_keys.contains(&last.key()) {
|
|
|
|
- let old_index = new_index_to_old_index[last_index];
|
|
|
|
- let temp = old_index_to_temp[old_index];
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.go_down_to_temp_child(temp);
|
|
|
|
- // [... parent last]
|
|
|
|
- self.diff_node(&old[old_index], last);
|
|
|
|
-
|
|
|
|
- if new_index_is_in_lis.contains(&last_index) {
|
|
|
|
- // Don't move it, since it is already where it needs to be.
|
|
|
|
- } else {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- // [... parent last]
|
|
|
|
- self.change_list.append_child();
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.go_down_to_temp_child(temp);
|
|
|
|
- // [... parent last]
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- // [... parent]
|
|
|
|
- self.create(last);
|
|
|
|
-
|
|
|
|
- // [... parent last]
|
|
|
|
- self.change_list.append_child();
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.go_down_to_reverse_child(0);
|
|
|
|
- // [... parent last]
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // // The longest increasing subsequence within `new_index_to_old_index`. This
|
|
|
|
+ // // is the longest sequence on DOM nodes in `old` that are relatively ordered
|
|
|
|
+ // // correctly within `new`. We will leave these nodes in place in the DOM,
|
|
|
|
+ // // and only move nodes that are not part of the LIS. This results in the
|
|
|
|
+ // // maximum number of DOM nodes left in place, AKA the minimum number of DOM
|
|
|
|
+ // // nodes moved.
|
|
|
|
+ // let mut new_index_is_in_lis = FxHashSet::default();
|
|
|
|
+ // new_index_is_in_lis.reserve(new_index_to_old_index.len());
|
|
|
|
+ // let mut predecessors = vec![0; new_index_to_old_index.len()];
|
|
|
|
+ // let mut starts = vec![0; new_index_to_old_index.len()];
|
|
|
|
+ // longest_increasing_subsequence::lis_with(
|
|
|
|
+ // &new_index_to_old_index,
|
|
|
|
+ // &mut new_index_is_in_lis,
|
|
|
|
+ // |a, b| a < b,
|
|
|
|
+ // &mut predecessors,
|
|
|
|
+ // &mut starts,
|
|
|
|
+ // );
|
|
|
|
+
|
|
|
|
+ // // Now we will iterate from the end of the new children back to the
|
|
|
|
+ // // beginning, diffing old children we are reusing and if they aren't in the
|
|
|
|
+ // // LIS moving them to their new destination, or creating new children. Note
|
|
|
|
+ // // that iterating in reverse order lets us use `Node.prototype.insertBefore`
|
|
|
|
+ // // to move/insert children.
|
|
|
|
+ // //
|
|
|
|
+ // // But first, we ensure that we have a child on the change list stack that
|
|
|
|
+ // // we can `insertBefore`. We handle this once before looping over `new`
|
|
|
|
+ // // children, so that we don't have to keep checking on every loop iteration.
|
|
|
|
+ // if shared_suffix_count > 0 {
|
|
|
|
+ // // There is a shared suffix after these middle children. We will be
|
|
|
|
+ // // inserting before that shared suffix, so add the first child of that
|
|
|
|
+ // // shared suffix to the change list stack.
|
|
|
|
+ // //
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom
|
|
|
|
+ // .go_down_to_child(old_shared_suffix_start - removed_count);
|
|
|
|
+ // // [... parent first_child_of_shared_suffix]
|
|
|
|
+ // } else {
|
|
|
|
+ // // There is no shared suffix coming after these middle children.
|
|
|
|
+ // // Therefore we have to process the last child in `new` and move it to
|
|
|
|
+ // // the end of the parent's children if it isn't already there.
|
|
|
|
+ // let last_index = new.len() - 1;
|
|
|
|
+ // // uhhhh why an unwrap?
|
|
|
|
+ // let last = new.last().unwrap();
|
|
|
|
+ // // let last = new.last().unwrap_throw();
|
|
|
|
+ // new = &new[..new.len() - 1];
|
|
|
|
+ // if shared_keys.contains(&last.key()) {
|
|
|
|
+ // let old_index = new_index_to_old_index[last_index];
|
|
|
|
+ // let temp = old_index_to_temp[old_index];
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom.go_down_to_temp_child(temp);
|
|
|
|
+ // // [... parent last]
|
|
|
|
+ // self.diff_node(&old[old_index], last);
|
|
|
|
+
|
|
|
|
+ // if new_index_is_in_lis.contains(&last_index) {
|
|
|
|
+ // // Don't move it, since it is already where it needs to be.
|
|
|
|
+ // } else {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // // [... parent last]
|
|
|
|
+ // self.dom.append_child();
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom.go_down_to_temp_child(temp);
|
|
|
|
+ // // [... parent last]
|
|
|
|
+ // }
|
|
|
|
+ // } else {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.create(last);
|
|
|
|
+
|
|
|
|
+ // // [... parent last]
|
|
|
|
+ // self.dom.append_child();
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom.go_down_to_reverse_child(0);
|
|
|
|
+ // // [... parent last]
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
|
|
- for (new_index, new_child) in new.iter().enumerate().rev() {
|
|
|
|
- let old_index = new_index_to_old_index[new_index];
|
|
|
|
- if old_index == u32::MAX as usize {
|
|
|
|
- debug_assert!(!shared_keys.contains(&new_child.key()));
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- // [... parent successor]
|
|
|
|
- self.create(new_child);
|
|
|
|
- // [... parent successor new_child]
|
|
|
|
- self.change_list.insert_before();
|
|
|
|
- // [... parent new_child]
|
|
|
|
- } else {
|
|
|
|
- debug_assert!(shared_keys.contains(&new_child.key()));
|
|
|
|
- let temp = old_index_to_temp[old_index];
|
|
|
|
- debug_assert_ne!(temp, u32::MAX);
|
|
|
|
-
|
|
|
|
- if new_index_is_in_lis.contains(&new_index) {
|
|
|
|
- // [... parent successor]
|
|
|
|
- self.change_list.go_to_temp_sibling(temp);
|
|
|
|
- // [... parent new_child]
|
|
|
|
- } else {
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- // [... parent successor]
|
|
|
|
- self.change_list.push_temporary(temp);
|
|
|
|
- // [... parent successor new_child]
|
|
|
|
- self.change_list.insert_before();
|
|
|
|
- // [... parent new_child]
|
|
|
|
- }
|
|
|
|
|
|
+ // for (new_index, new_child) in new.iter().enumerate().rev() {
|
|
|
|
+ // let old_index = new_index_to_old_index[new_index];
|
|
|
|
+ // if old_index == u32::MAX as usize {
|
|
|
|
+ // debug_assert!(!shared_keys.contains(&new_child.key()));
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // // [... parent successor]
|
|
|
|
+ // self.create(new_child);
|
|
|
|
+ // // [... parent successor new_child]
|
|
|
|
+ // self.dom.insert_before();
|
|
|
|
+ // // [... parent new_child]
|
|
|
|
+ // } else {
|
|
|
|
+ // debug_assert!(shared_keys.contains(&new_child.key()));
|
|
|
|
+ // let temp = old_index_to_temp[old_index];
|
|
|
|
+ // debug_assert_ne!(temp, u32::MAX);
|
|
|
|
+
|
|
|
|
+ // if new_index_is_in_lis.contains(&new_index) {
|
|
|
|
+ // // [... parent successor]
|
|
|
|
+ // self.dom.go_to_temp_sibling(temp);
|
|
|
|
+ // // [... parent new_child]
|
|
|
|
+ // } else {
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // // [... parent successor]
|
|
|
|
+ // self.dom.push_temporary(temp);
|
|
|
|
+ // // [... parent successor new_child]
|
|
|
|
+ // self.dom.insert_before();
|
|
|
|
+ // // [... parent new_child]
|
|
|
|
+ // }
|
|
|
|
|
|
- self.diff_node(&old[old_index], new_child);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // self.diff_node(&old[old_index], new_child);
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
|
|
- // [... parent child]
|
|
|
|
- self.change_list.go_up();
|
|
|
|
|
|
+ // // [... parent child]
|
|
|
|
+ // self.dom.go_up();
|
|
// [... parent]
|
|
// [... parent]
|
|
}
|
|
}
|
|
|
|
|
|
@@ -937,25 +1030,26 @@ impl<'a> DiffMachine<'a> {
|
|
//
|
|
//
|
|
// When this function exits, the change list stack remains the same.
|
|
// When this function exits, the change list stack remains the same.
|
|
fn diff_keyed_suffix(
|
|
fn diff_keyed_suffix(
|
|
- &mut self,
|
|
|
|
|
|
+ &self,
|
|
old: &[VNode<'a>],
|
|
old: &[VNode<'a>],
|
|
new: &[VNode<'a>],
|
|
new: &[VNode<'a>],
|
|
new_shared_suffix_start: usize,
|
|
new_shared_suffix_start: usize,
|
|
) {
|
|
) {
|
|
- debug_assert_eq!(old.len(), new.len());
|
|
|
|
- debug_assert!(!old.is_empty());
|
|
|
|
|
|
+ todo!()
|
|
|
|
+ // debug_assert_eq!(old.len(), new.len());
|
|
|
|
+ // debug_assert!(!old.is_empty());
|
|
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.go_down();
|
|
|
|
- // [... parent new_child]
|
|
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom.go_down();
|
|
|
|
+ // // [... parent new_child]
|
|
|
|
|
|
- for (i, (old_child, new_child)) in old.iter().zip(new.iter()).enumerate() {
|
|
|
|
- self.change_list.go_to_sibling(new_shared_suffix_start + i);
|
|
|
|
- self.diff_node(old_child, new_child);
|
|
|
|
- }
|
|
|
|
|
|
+ // for (i, (old_child, new_child)) in old.iter().zip(new.iter()).enumerate() {
|
|
|
|
+ // self.dom.go_to_sibling(new_shared_suffix_start + i);
|
|
|
|
+ // self.diff_node(old_child, new_child);
|
|
|
|
+ // }
|
|
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.go_up();
|
|
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // self.dom.go_up();
|
|
}
|
|
}
|
|
|
|
|
|
// Diff children that are not keyed.
|
|
// Diff children that are not keyed.
|
|
@@ -972,42 +1066,57 @@ impl<'a> DiffMachine<'a> {
|
|
debug_assert!(!old.is_empty());
|
|
debug_assert!(!old.is_empty());
|
|
|
|
|
|
// [... parent]
|
|
// [... parent]
|
|
- self.change_list.go_down();
|
|
|
|
|
|
+ // self.dom.go_down();
|
|
|
|
+ // self.dom.push_root()
|
|
// [... parent child]
|
|
// [... parent child]
|
|
|
|
|
|
|
|
+ // todo!()
|
|
for (i, (new_child, old_child)) in new.iter().zip(old.iter()).enumerate() {
|
|
for (i, (new_child, old_child)) in new.iter().zip(old.iter()).enumerate() {
|
|
// [... parent prev_child]
|
|
// [... parent prev_child]
|
|
- self.change_list.go_to_sibling(i);
|
|
|
|
|
|
+ // self.dom.go_to_sibling(i);
|
|
// [... parent this_child]
|
|
// [... parent this_child]
|
|
|
|
+ self.dom.push_root(old_child.get_mounted_id().unwrap());
|
|
self.diff_node(old_child, new_child);
|
|
self.diff_node(old_child, new_child);
|
|
- }
|
|
|
|
|
|
|
|
- match old.len().cmp(&new.len()) {
|
|
|
|
- // old.len > new.len -> removing some nodes
|
|
|
|
- Ordering::Greater => {
|
|
|
|
- // [... parent prev_child]
|
|
|
|
- self.change_list.go_to_sibling(new.len());
|
|
|
|
- // [... parent first_child_to_remove]
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- // support::remove_self_and_next_siblings(state, &old[new.len()..]);
|
|
|
|
- self.remove_self_and_next_siblings(&old[new.len()..]);
|
|
|
|
- // [... parent]
|
|
|
|
- }
|
|
|
|
- // old.len < new.len -> adding some nodes
|
|
|
|
- Ordering::Less => {
|
|
|
|
- // [... parent last_child]
|
|
|
|
- self.change_list.go_up();
|
|
|
|
- // [... parent]
|
|
|
|
- self.change_list.commit_traversal();
|
|
|
|
- self.create_and_append_children(&new[old.len()..]);
|
|
|
|
- }
|
|
|
|
- // old.len == new.len -> no nodes added/removed, but πerhaps changed
|
|
|
|
- Ordering::Equal => {
|
|
|
|
- // [... parent child]
|
|
|
|
- self.change_list.go_up();
|
|
|
|
- // [... parent]
|
|
|
|
|
|
+ let old_id = old_child.get_mounted_id().unwrap();
|
|
|
|
+ let new_id = new_child.get_mounted_id().unwrap();
|
|
|
|
+
|
|
|
|
+ log::debug!(
|
|
|
|
+ "pushed root. {:?}, {:?}",
|
|
|
|
+ old_child.get_mounted_id().unwrap(),
|
|
|
|
+ new_child.get_mounted_id().unwrap()
|
|
|
|
+ );
|
|
|
|
+ if old_id != new_id {
|
|
|
|
+ log::debug!("Mismatch: {:?}", new_child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // match old.len().cmp(&new.len()) {
|
|
|
|
+ // // old.len > new.len -> removing some nodes
|
|
|
|
+ // Ordering::Greater => {
|
|
|
|
+ // // [... parent prev_child]
|
|
|
|
+ // self.dom.go_to_sibling(new.len());
|
|
|
|
+ // // [... parent first_child_to_remove]
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // // support::remove_self_and_next_siblings(state, &old[new.len()..]);
|
|
|
|
+ // self.remove_self_and_next_siblings(&old[new.len()..]);
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // }
|
|
|
|
+ // // old.len < new.len -> adding some nodes
|
|
|
|
+ // Ordering::Less => {
|
|
|
|
+ // // [... parent last_child]
|
|
|
|
+ // self.dom.go_up();
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // // self.dom.commit_traversal();
|
|
|
|
+ // self.create_and_append_children(&new[old.len()..]);
|
|
|
|
+ // }
|
|
|
|
+ // // old.len == new.len -> no nodes added/removed, but πerhaps changed
|
|
|
|
+ // Ordering::Equal => {
|
|
|
|
+ // // [... parent child]
|
|
|
|
+ // self.dom.go_up();
|
|
|
|
+ // // [... parent]
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
|
|
|
|
// ======================
|
|
// ======================
|
|
@@ -1022,14 +1131,15 @@ impl<'a> DiffMachine<'a> {
|
|
//
|
|
//
|
|
// When this function returns, the change list stack is in the same state.
|
|
// When this function returns, the change list stack is in the same state.
|
|
pub fn remove_all_children(&mut self, old: &[VNode<'a>]) {
|
|
pub fn remove_all_children(&mut self, old: &[VNode<'a>]) {
|
|
- debug_assert!(self.change_list.traversal_is_committed());
|
|
|
|
|
|
+ // debug_assert!(self.dom.traversal_is_committed());
|
|
log::debug!("REMOVING CHILDREN");
|
|
log::debug!("REMOVING CHILDREN");
|
|
for _child in old {
|
|
for _child in old {
|
|
// registry.remove_subtree(child);
|
|
// registry.remove_subtree(child);
|
|
}
|
|
}
|
|
// Fast way to remove all children: set the node's textContent to an empty
|
|
// Fast way to remove all children: set the node's textContent to an empty
|
|
// string.
|
|
// string.
|
|
- self.change_list.set_text("");
|
|
|
|
|
|
+ todo!()
|
|
|
|
+ // self.dom.set_inner_text("");
|
|
}
|
|
}
|
|
|
|
|
|
// Create the given children and append them to the parent node.
|
|
// Create the given children and append them to the parent node.
|
|
@@ -1040,10 +1150,11 @@ impl<'a> DiffMachine<'a> {
|
|
//
|
|
//
|
|
// When this function returns, the change list stack is in the same state.
|
|
// When this function returns, the change list stack is in the same state.
|
|
pub fn create_and_append_children(&mut self, new: &[VNode<'a>]) {
|
|
pub fn create_and_append_children(&mut self, new: &[VNode<'a>]) {
|
|
- debug_assert!(self.change_list.traversal_is_committed());
|
|
|
|
|
|
+ // debug_assert!(self.dom.traversal_is_committed());
|
|
for child in new {
|
|
for child in new {
|
|
|
|
+ // self.create_and_append(node, parent)
|
|
self.create(child);
|
|
self.create(child);
|
|
- self.change_list.append_child();
|
|
|
|
|
|
+ self.dom.append_child();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1056,11 +1167,11 @@ impl<'a> DiffMachine<'a> {
|
|
// After the function returns, the child is no longer on the change list stack:
|
|
// After the function returns, the child is no longer on the change list stack:
|
|
//
|
|
//
|
|
// [... parent]
|
|
// [... parent]
|
|
- pub fn remove_self_and_next_siblings(&mut self, old: &[VNode<'a>]) {
|
|
|
|
- debug_assert!(self.change_list.traversal_is_committed());
|
|
|
|
|
|
+ pub fn remove_self_and_next_siblings(&self, old: &[VNode<'a>]) {
|
|
|
|
+ // debug_assert!(self.dom.traversal_is_committed());
|
|
for child in old {
|
|
for child in old {
|
|
if let VNode::Component(vcomp) = child {
|
|
if let VNode::Component(vcomp) = child {
|
|
- // self.change_list
|
|
|
|
|
|
+ // dom
|
|
// .create_text_node("placeholder for vcomponent");
|
|
// .create_text_node("placeholder for vcomponent");
|
|
|
|
|
|
todo!()
|
|
todo!()
|
|
@@ -1071,7 +1182,7 @@ impl<'a> DiffMachine<'a> {
|
|
// })
|
|
// })
|
|
// let id = get_id();
|
|
// let id = get_id();
|
|
// *component.stable_addr.as_ref().borrow_mut() = Some(id);
|
|
// *component.stable_addr.as_ref().borrow_mut() = Some(id);
|
|
- // self.change_list.save_known_root(id);
|
|
|
|
|
|
+ // self.dom.save_known_root(id);
|
|
// let scope = Rc::downgrade(&component.ass_scope);
|
|
// let scope = Rc::downgrade(&component.ass_scope);
|
|
// self.lifecycle_events.push_back(LifeCycleEvent::Mount {
|
|
// self.lifecycle_events.push_back(LifeCycleEvent::Mount {
|
|
// caller: Rc::downgrade(&component.caller),
|
|
// caller: Rc::downgrade(&component.caller),
|
|
@@ -1082,7 +1193,8 @@ impl<'a> DiffMachine<'a> {
|
|
|
|
|
|
// registry.remove_subtree(child);
|
|
// registry.remove_subtree(child);
|
|
}
|
|
}
|
|
- self.change_list.remove_self_and_next_siblings();
|
|
|
|
|
|
+ todo!()
|
|
|
|
+ // self.dom.remove_self_and_next_siblings();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|