wip: structure coming together and tests

This commit is contained in:
Jonathan Kelley committed 2021-08-23 12:11:45 -04:00
1 parent 16bfc6d248
commit 2d541eca64
13 files changed
+251 -141

No files matched your search

+37 -56
View File
@@ -167,8 +167,10 @@ impl<'bump> DiffMachine<'bump> {
DiffInstruction::PopScope => {
self.stack.pop_scope();
}
DiffInstruction::PopElement => {
self.mutations.pop();
todo!();
// self.mutations.pop();
}
DiffInstruction::DiffNode { old, new, .. } => {
@@ -179,20 +181,16 @@ impl<'bump> DiffMachine<'bump> {
self.diff_children(old, new);
}
DiffInstruction::Create { node, and } => {
DiffInstruction::Create { node } => {
self.create_node(node);
}
DiffInstruction::Remove { child } => {
for child in RealChildIterator::new(child, self.vdom) {
self.mutations.remove(child.direct_id().as_u64());
}
self.remove_nodes(Some(child));
}
DiffInstruction::RemoveChildren { children } => {
for child in RealChildIterator::new_from_slice(children, self.vdom) {
self.mutations.remove(child.direct_id().as_u64());
}
self.remove_nodes(children);
}
DiffInstruction::Mount { and } => {
@@ -204,13 +202,13 @@ impl<'bump> DiffMachine<'bump> {
Ok(())
}
fn mount(&mut self, and: MountType) {
let nodes_created = self.stack.nodes_created_stack.pop().unwrap();
fn mount(&mut self, and: MountType<'bump>) {
let nodes_created = self.stack.pop_nodes_created();
match and {
// add the nodes from this virtual list to the parent
// used by fragments and components
MountType::Absorb => {
*self.stack.nodes_created_stack.last_mut().unwrap() += nodes_created;
self.stack.add_child_count(nodes_created);
}
MountType::Append => {
self.mutations.edits.push(AppendChildren {
@@ -218,15 +216,21 @@ impl<'bump> DiffMachine<'bump> {
});
}
MountType::Replace { old } => {
todo!()
// self.mutations.replace_with(with as u32, many as u32);
let root = todo!();
self.mutations.replace_with(root, nodes_created as u32);
}
MountType::ReplaceByElementId { old } => {
self.mutations.replace_with(old, nodes_created as u32);
}
MountType::InsertAfter { other_node } => {
self.mutations.insert_after(nodes_created as u32);
let root = self.find_last_element(other_node).direct_id();
self.mutations.insert_after(root, nodes_created as u32);
}
MountType::InsertBefore { other_node } => {
self.mutations.insert_before(nodes_created as u32);
let root = self.find_first_element(other_node).direct_id();
self.mutations.insert_before(root, nodes_created as u32);
}
}
}
@@ -373,17 +377,19 @@ impl<'bump> DiffMachine<'bump> {
match (old_node, new_node) {
// Check the most common cases first
(Text(old), Text(new)) => self.diff_text_nodes(old, new),
(Element(old), Element(new)) => self.diff_element_nodes(old, new),
(Component(old), Component(new)) => self.diff_component_nodes(old, new),
(Fragment(old), Fragment(new)) => self.diff_fragment_nodes(old, new),
(Anchor(old), Anchor(new)) => new.dom_id.set(old.dom_id.get()),
(Suspended(old), Suspended(new)) => new.node.set(old.node.get()),
(Element(old), Element(new)) => self.diff_element_nodes(old, new),
// Anything else is just a basic replace and create
(
Component(_) | Fragment(_) | Text(_) | Element(_) | Anchor(_) | Suspended(_),
Component(_) | Fragment(_) | Text(_) | Element(_) | Anchor(_) | Suspended(_),
) => self.replace_and_create_one_with_one(old_node, new_node),
) => self
.stack
.create_node(new_node, MountType::Replace { old: old_node }),
}
}
@@ -407,8 +413,8 @@ impl<'bump> DiffMachine<'bump> {
//
// This case is rather rare (typically only in non-keyed lists)
if new.tag_name != old.tag_name || new.namespace != old.namespace {
todo!();
// self.replace_node_with_node(root, old_node, new_node);
todo!("element changed");
// self.replace_node_with_node(old, new);
return;
}
@@ -779,21 +785,6 @@ impl<'bump> DiffMachine<'bump> {
KeyedPrefixResult::MoreWorkToDo(shared_prefix_count)
}
// Create the given children and append them to the parent node.
//
// The parent node must currently be on top of the change list stack:
//
// [... parent]
//
// When this function returns, the change list stack is in the same state.
fn create_and_append_children(&mut self, new: &'bump [VNode<'bump>]) {
for child in new {
todo!();
// let meta = self.create_vnode(child);
// self.mutations.append_children(meta.added_to_stack);
}
}
// The most-general, expensive code path for keyed children diffing.
//
// We find the longest subsequence within `old` of children that are relatively
@@ -957,12 +948,12 @@ impl<'bump> DiffMachine<'bump> {
self.mutations.insert_before(1);
}
} else {
self.stack.push(DiffInstruction::Create {
node: next_new,
and: MountType::InsertBefore {
other_node: Some(new_anchor),
self.stack.create_node(
next_new,
MountType::InsertBefore {
other_node: new_anchor,
},
});
);
}
}
}
@@ -1081,7 +1072,7 @@ impl<'bump> DiffMachine<'bump> {
self.stack.create_children(
&new[old.len()..],
MountType::InsertAfter {
other_node: old.last(),
other_node: old.last().unwrap(),
},
);
}
@@ -1098,6 +1089,10 @@ impl<'bump> DiffMachine<'bump> {
}
}
// =====================
// Utilities
// =====================
fn find_last_element(&mut self, vnode: &'bump VNode<'bump>) -> &'bump VNode<'bump> {
let mut search_node = Some(vnode);
@@ -1144,18 +1139,6 @@ impl<'bump> DiffMachine<'bump> {
}
}
// fn remove_child(&mut self, node: &'bump VNode<'bump>) {
// self.replace_and_create_many_with_many(Some(node), None);
// }
fn replace_and_create_one_with_one(
&mut self,
old: &'bump VNode<'bump>,
new: &'bump VNode<'bump>,
) {
self.stack.create_node(new, MountType::Replace { old });
}
fn replace_many_with_many(&mut self, old: &'bump [VNode<'bump>], new: &'bump [VNode<'bump>]) {
//
}
@@ -1316,10 +1299,8 @@ impl<'bump> DiffMachine<'bump> {
old_node: &'bump VNode<'bump>,
new_node: &'bump VNode<'bump>,
) {
self.stack.instructions.push(DiffInstruction::Create {
and: MountType::Replace { old: old_node },
node: new_node,
});
self.stack
.create_node(new_node, MountType::Replace { old: old_node });
}
fn fix_listener<'a>(&mut self, listener: &'a Listener<'a>) {
+13 -15
View File
@@ -16,7 +16,6 @@ pub enum DiffInstruction<'a> {
Create {
node: &'a VNode<'a>,
and: MountType<'a>,
},
Remove {
@@ -41,13 +40,14 @@ pub enum MountType<'a> {
Absorb,
Append,
Replace { old: &'a VNode<'a> },
InsertAfter { other_node: Option<&'a VNode<'a>> },
InsertBefore { other_node: Option<&'a VNode<'a>> },
ReplaceByElementId { old: ElementId },
InsertAfter { other_node: &'a VNode<'a> },
InsertBefore { other_node: &'a VNode<'a> },
}
pub struct DiffStack<'bump> {
pub instructions: Vec<DiffInstruction<'bump>>,
pub nodes_created_stack: SmallVec<[usize; 10]>,
instructions: Vec<DiffInstruction<'bump>>,
nodes_created_stack: SmallVec<[usize; 10]>,
pub scope_stack: SmallVec<[ScopeId; 5]>,
}
@@ -77,24 +77,25 @@ impl<'bump> DiffStack<'bump> {
self.instructions.push(DiffInstruction::Mount { and });
for child in children.into_iter().rev() {
self.instructions.push(DiffInstruction::Create {
and: MountType::Absorb,
node: child,
});
self.instructions
.push(DiffInstruction::Create { node: child });
}
}
pub fn create_node(&mut self, node: &'bump VNode<'bump>, and: MountType<'bump>) {
self.nodes_created_stack.push(0);
self.instructions.push(DiffInstruction::Mount { and });
self.instructions
.push(DiffInstruction::Create { and, node });
self.instructions.push(DiffInstruction::Create { node });
}
pub fn add_child_count(&mut self, count: usize) {
*self.nodes_created_stack.last_mut().unwrap() += count;
}
pub fn pop_nodes_created(&mut self) -> usize {
self.nodes_created_stack.pop().unwrap()
}
pub fn current_scope(&self) -> Option<ScopeId> {
self.scope_stack.last().map(|f| f.clone())
}
@@ -107,9 +108,6 @@ impl<'bump> DiffStack<'bump> {
// Run the creation algorithm with this scope on the stack
// ?? I think we treat components as framgnets??
self.instructions.push(DiffInstruction::Create {
node,
and: MountType::Absorb,
});
self.instructions.push(DiffInstruction::Create { node });
}
}
@@ -20,22 +20,29 @@ pub enum DomEdit<'bump> {
id: u64,
},
PopRoot,
AppendChildren {
many: u32,
},
// "Root" refers to the item direclty
// it's a waste of an instruction to push the root directly
ReplaceWith {
root: u64,
m: u32,
},
InsertAfter {
root: u64,
n: u32,
},
InsertBefore {
root: u64,
n: u32,
},
// remove roots directly
Remove {
root: u64,
},
RemoveAllChildren,
CreateTextNode {
text: &'bump str,
+5 -5
View File
@@ -11,9 +11,9 @@
//!
pub use crate::innerlude::{
format_args_f, html, rsx, Context, DioxusElement, DomEdit, DomTree, ElementId, EventPriority,
EventTrigger, LazyNodes, Mutations, NodeFactory, Properties, ScopeId, SuspendedContext, VNode,
VirtualDom, VirtualEvent, FC,
format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId,
EventPriority, EventTrigger, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId,
SuspendedContext, VNode, VirtualDom, VirtualEvent, FC,
};
pub mod prelude {
@@ -35,7 +35,7 @@ pub(crate) mod innerlude {
pub use crate::context::*;
pub use crate::diff::*;
pub use crate::diff_stack::*;
pub use crate::editor::*;
pub use crate::dom_edits::*;
pub use crate::error::*;
pub use crate::events::*;
pub use crate::heuristics::*;
@@ -67,7 +67,7 @@ pub mod component;
pub mod context;
pub mod diff;
pub mod diff_stack;
pub mod editor;
pub mod dom_edits;
pub mod error;
pub mod events;
pub mod heuristics;
+9 -6
View File
@@ -31,16 +31,19 @@ impl<'a> Mutations<'a> {
self.edits.push(PopRoot {});
}
pub(crate) fn replace_with(&mut self, m: u32) {
self.edits.push(ReplaceWith { m });
pub(crate) fn replace_with(&mut self, root: ElementId, m: u32) {
let root = root.as_u64();
self.edits.push(ReplaceWith { m, root });
}
pub(crate) fn insert_after(&mut self, n: u32) {
self.edits.push(InsertAfter { n });
pub(crate) fn insert_after(&mut self, root: ElementId, n: u32) {
let root = root.as_u64();
self.edits.push(InsertAfter { n, root });
}
pub(crate) fn insert_before(&mut self, n: u32) {
self.edits.push(InsertBefore { n });
pub(crate) fn insert_before(&mut self, root: ElementId, n: u32) {
let root = root.as_u64();
self.edits.push(InsertBefore { n, root });
}
// Remove Nodesfrom the dom
+3 -4
View File
@@ -173,10 +173,9 @@ impl VirtualDom {
// // We run the component. If it succeeds, then we can diff it and add the changes to the dom.
if cur_component.run_scope().is_ok() {
diff_machine.stack.push(DiffInstruction::Create {
node: cur_component.frames.fin_head(),
and: MountType::Append,
});
diff_machine
.stack
.create_node(cur_component.frames.fin_head(), MountType::Append);
diff_machine.work().await.unwrap();
} else {
// todo: should this be a hard error?