switch to a mutations trait to fix some lifetime issues
This commit is contained in:
1 parent
0c76770da0
commit
35b461cd09
5 files changed
+634
-290
No files matched your search
+111
-79
@@ -1,6 +1,4 @@
|
|||||||
use crate::innerlude::{ElementPath, ElementRef, VComponent, VPlaceholder, VText};
|
use crate::innerlude::{ElementPath, ElementRef, VComponent, VPlaceholder, VText, WriteMutations};
|
||||||
use crate::mutations::Mutation;
|
|
||||||
use crate::mutations::Mutation::*;
|
|
||||||
use crate::nodes::VNode;
|
use crate::nodes::VNode;
|
||||||
use crate::nodes::{DynamicNode, TemplateNode};
|
use crate::nodes::{DynamicNode, TemplateNode};
|
||||||
use crate::virtual_dom::VirtualDom;
|
use crate::virtual_dom::VirtualDom;
|
||||||
@@ -64,15 +62,20 @@ impl VirtualDom {
|
|||||||
/// Create a new template [`VNode`] and write it to the [`Mutations`] buffer.
|
/// Create a new template [`VNode`] and write it to the [`Mutations`] buffer.
|
||||||
///
|
///
|
||||||
/// This method pushes the ScopeID to the internal scopestack and returns the number of nodes created.
|
/// This method pushes the ScopeID to the internal scopestack and returns the number of nodes created.
|
||||||
pub(crate) fn create_scope(&mut self, scope: ScopeId, template: &VNode) -> usize {
|
pub(crate) fn create_scope(
|
||||||
|
&mut self,
|
||||||
|
scope: ScopeId,
|
||||||
|
template: &VNode,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
self.runtime.scope_stack.borrow_mut().push(scope);
|
self.runtime.scope_stack.borrow_mut().push(scope);
|
||||||
let nodes = self.create(template);
|
let nodes = self.create(template, to);
|
||||||
self.runtime.scope_stack.borrow_mut().pop();
|
self.runtime.scope_stack.borrow_mut().pop();
|
||||||
nodes
|
nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create this template and write its mutations
|
/// Create this template and write its mutations
|
||||||
pub(crate) fn create(&mut self, node: &VNode) -> usize {
|
pub(crate) fn create(&mut self, node: &VNode, to: &mut impl WriteMutations) -> usize {
|
||||||
// check for a overriden template
|
// check for a overriden template
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
@@ -95,12 +98,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// The best renderers will have templates prehydrated and registered
|
// The best renderers will have templates prehydrated and registered
|
||||||
// Just in case, let's create the template using instructions anyways
|
// Just in case, let's create the template using instructions anyways
|
||||||
self.register_template(node.template.get());
|
self.register_template(node.template.get(), to);
|
||||||
|
|
||||||
// we know that this will generate at least one mutation per node
|
|
||||||
self.mutations
|
|
||||||
.edits
|
|
||||||
.reserve(node.template.get().roots.len());
|
|
||||||
|
|
||||||
// Walk the roots, creating nodes and assigning IDs
|
// Walk the roots, creating nodes and assigning IDs
|
||||||
// nodes in an iterator of ((dynamic_node_index, sorted_index), path)
|
// nodes in an iterator of ((dynamic_node_index, sorted_index), path)
|
||||||
@@ -153,30 +151,46 @@ impl VirtualDom {
|
|||||||
.map(|(idx, root)| match root {
|
.map(|(idx, root)| match root {
|
||||||
DynamicText { id } | Dynamic { id } => {
|
DynamicText { id } | Dynamic { id } => {
|
||||||
nodes.next().unwrap();
|
nodes.next().unwrap();
|
||||||
self.write_dynamic_root(node, *id)
|
self.write_dynamic_root(node, *id, to)
|
||||||
}
|
}
|
||||||
Element { .. } => {
|
Element { .. } => {
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
let id = self.write_element_root(node, idx, &mut attrs, &mut nodes, &[]);
|
let id = self.write_element_root(node, idx, &mut attrs, &mut nodes, &[]);
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
let id =
|
let id = self.write_element_root(
|
||||||
self.write_element_root(node, idx, &mut attrs, &mut nodes, &nodes_sorted);
|
node,
|
||||||
|
idx,
|
||||||
|
&mut attrs,
|
||||||
|
&mut nodes,
|
||||||
|
&nodes_sorted,
|
||||||
|
to,
|
||||||
|
);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
Text { .. } => self.write_static_text_root(node, idx),
|
Text { .. } => self.write_static_text_root(node, idx, to),
|
||||||
})
|
})
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_static_text_root(&mut self, node: &VNode, idx: usize) -> usize {
|
fn write_static_text_root(
|
||||||
|
&mut self,
|
||||||
|
node: &VNode,
|
||||||
|
idx: usize,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
// Simply just load the template root, no modifications needed
|
// Simply just load the template root, no modifications needed
|
||||||
self.load_template_root(node, idx);
|
self.load_template_root(node, idx, to);
|
||||||
|
|
||||||
// Text producs just one node on the stack
|
// Text producs just one node on the stack
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_dynamic_root(&mut self, template: &VNode, idx: usize) -> usize {
|
fn write_dynamic_root(
|
||||||
|
&mut self,
|
||||||
|
template: &VNode,
|
||||||
|
idx: usize,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
use DynamicNode::*;
|
use DynamicNode::*;
|
||||||
match &template.dynamic_nodes[idx] {
|
match &template.dynamic_nodes[idx] {
|
||||||
node @ Component { .. } | node @ Fragment(_) => {
|
node @ Component { .. } | node @ Fragment(_) => {
|
||||||
@@ -187,7 +201,7 @@ impl VirtualDom {
|
|||||||
element: template.clone(),
|
element: template.clone(),
|
||||||
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
||||||
};
|
};
|
||||||
self.create_dynamic_node(template_ref, node)
|
self.create_dynamic_node(template_ref, node, to)
|
||||||
}
|
}
|
||||||
Placeholder(VPlaceholder { id, parent }) => {
|
Placeholder(VPlaceholder { id, parent }) => {
|
||||||
let template_ref = ElementRef {
|
let template_ref = ElementRef {
|
||||||
@@ -199,21 +213,17 @@ impl VirtualDom {
|
|||||||
};
|
};
|
||||||
*parent.borrow_mut() = Some(template_ref);
|
*parent.borrow_mut() = Some(template_ref);
|
||||||
let id = self.set_slot(id);
|
let id = self.set_slot(id);
|
||||||
self.mutations.push(CreatePlaceholder { id });
|
to.create_placeholder(id);
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
Text(VText { id, value }) => {
|
Text(VText { id, value }) => {
|
||||||
let id = self.set_slot(id);
|
let id = self.set_slot(id);
|
||||||
self.create_static_text(value, id);
|
to.create_text_node(value, id);
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_static_text(&mut self, value: &str, id: ElementId) {
|
|
||||||
self.mutations.push(CreateTextNode { value, id });
|
|
||||||
}
|
|
||||||
|
|
||||||
/// We write all the descendent data for this element
|
/// We write all the descendent data for this element
|
||||||
///
|
///
|
||||||
/// Elements can contain other nodes - and those nodes can be dynamic or static
|
/// Elements can contain other nodes - and those nodes can be dynamic or static
|
||||||
@@ -226,15 +236,22 @@ impl VirtualDom {
|
|||||||
dynamic_attrs: &mut Peekable<impl Iterator<Item = (usize, &'static [u8])>>,
|
dynamic_attrs: &mut Peekable<impl Iterator<Item = (usize, &'static [u8])>>,
|
||||||
dynamic_nodes_iter: &mut Peekable<impl Iterator<Item = ((usize, usize), &'static [u8])>>,
|
dynamic_nodes_iter: &mut Peekable<impl Iterator<Item = ((usize, usize), &'static [u8])>>,
|
||||||
dynamic_nodes: &[(usize, &'static [u8])],
|
dynamic_nodes: &[(usize, &'static [u8])],
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
// Load the template root and get the ID for the node on the stack
|
// Load the template root and get the ID for the node on the stack
|
||||||
let root_on_stack = self.load_template_root(template, root_idx);
|
let root_on_stack = self.load_template_root(template, root_idx, to);
|
||||||
|
|
||||||
// Write all the attributes below this root
|
// Write all the attributes below this root
|
||||||
self.write_attrs_on_root(dynamic_attrs, root_idx as u8, root_on_stack, template);
|
self.write_attrs_on_root(dynamic_attrs, root_idx as u8, root_on_stack, template, to);
|
||||||
|
|
||||||
// Load in all of the placeholder or dynamic content under this root too
|
// Load in all of the placeholder or dynamic content under this root too
|
||||||
self.load_placeholders(dynamic_nodes_iter, dynamic_nodes, root_idx as u8, template);
|
self.load_placeholders(
|
||||||
|
dynamic_nodes_iter,
|
||||||
|
dynamic_nodes,
|
||||||
|
root_idx as u8,
|
||||||
|
template,
|
||||||
|
to,
|
||||||
|
);
|
||||||
|
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
@@ -259,6 +276,7 @@ impl VirtualDom {
|
|||||||
dynamic_nodes: &[(usize, &'static [u8])],
|
dynamic_nodes: &[(usize, &'static [u8])],
|
||||||
root_idx: u8,
|
root_idx: u8,
|
||||||
template: &VNode,
|
template: &VNode,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
let (start, end) = match collect_dyn_node_range(dynamic_nodes_iter, root_idx) {
|
let (start, end) = match collect_dyn_node_range(dynamic_nodes_iter, root_idx) {
|
||||||
Some((a, b)) => (a, b),
|
Some((a, b)) => (a, b),
|
||||||
@@ -281,11 +299,11 @@ impl VirtualDom {
|
|||||||
element: template.clone(),
|
element: template.clone(),
|
||||||
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
||||||
};
|
};
|
||||||
let m = self.create_dynamic_node(boundary_ref, &template.dynamic_nodes[idx]);
|
let m = self.create_dynamic_node(boundary_ref, &template.dynamic_nodes[idx], to);
|
||||||
if m > 0 {
|
if m > 0 {
|
||||||
// The path is one shorter because the top node is the root
|
// The path is one shorter because the top node is the root
|
||||||
let path = &template.template.get().node_paths[idx][1..];
|
let path = &template.template.get().node_paths[idx][1..];
|
||||||
self.mutations.push(ReplacePlaceholder { m, path });
|
to.replace_placeholder_with_nodes(path, m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -296,14 +314,15 @@ impl VirtualDom {
|
|||||||
root_idx: u8,
|
root_idx: u8,
|
||||||
root: ElementId,
|
root: ElementId,
|
||||||
node: &VNode,
|
node: &VNode,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
while let Some((mut attr_id, path)) =
|
while let Some((mut attr_id, path)) =
|
||||||
attrs.next_if(|(_, p)| p.first().copied() == Some(root_idx))
|
attrs.next_if(|(_, p)| p.first().copied() == Some(root_idx))
|
||||||
{
|
{
|
||||||
let id = self.assign_static_node_as_dynamic(path, root);
|
let id = self.assign_static_node_as_dynamic(path, root, to);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
self.write_attribute(node, attr_id, &node.dynamic_attrs[attr_id], id);
|
self.write_attribute(node, attr_id, &node.dynamic_attrs[attr_id], id, to);
|
||||||
|
|
||||||
// Only push the dynamic attributes forward if they match the current path (same element)
|
// Only push the dynamic attributes forward if they match the current path (same element)
|
||||||
match attrs.next_if(|(_, p)| *p == path) {
|
match attrs.next_if(|(_, p)| *p == path) {
|
||||||
@@ -320,6 +339,7 @@ impl VirtualDom {
|
|||||||
idx: usize,
|
idx: usize,
|
||||||
attribute: &crate::Attribute,
|
attribute: &crate::Attribute,
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
// Make sure we set the attribute's associated id
|
// Make sure we set the attribute's associated id
|
||||||
attribute.mounted_element.set(id);
|
attribute.mounted_element.set(id);
|
||||||
@@ -336,35 +356,27 @@ impl VirtualDom {
|
|||||||
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
scope: self.runtime.current_scope_id().unwrap_or(ScopeId(0)),
|
||||||
};
|
};
|
||||||
self.elements[id.0] = Some(element_ref);
|
self.elements[id.0] = Some(element_ref);
|
||||||
self.mutations.push(NewEventListener {
|
to.create_event_listener(&unbounded_name[2..], id);
|
||||||
// all listeners start with "on"
|
|
||||||
name: &unbounded_name[2..],
|
|
||||||
id,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let unbounded_value = &attribute.value;
|
let unbounded_value = &attribute.value;
|
||||||
|
|
||||||
self.mutations.push(SetAttribute {
|
to.set_attribute(unbounded_name, attribute.namespace, unbounded_value, id);
|
||||||
name: unbounded_name,
|
|
||||||
value: unbounded_value,
|
|
||||||
ns: attribute.namespace,
|
|
||||||
id,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_template_root(&mut self, template: &VNode, root_idx: usize) -> ElementId {
|
fn load_template_root(
|
||||||
|
&mut self,
|
||||||
|
template: &VNode,
|
||||||
|
root_idx: usize,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> ElementId {
|
||||||
// Get an ID for this root since it's a real root
|
// Get an ID for this root since it's a real root
|
||||||
let this_id = self.next_element();
|
let this_id = self.next_element();
|
||||||
template.root_ids.borrow_mut()[root_idx] = this_id;
|
template.root_ids.borrow_mut()[root_idx] = this_id;
|
||||||
|
|
||||||
self.mutations.push(LoadTemplate {
|
to.load_template(template.template.get().name, root_idx, this_id);
|
||||||
name: template.template.get().name,
|
|
||||||
index: root_idx,
|
|
||||||
id: this_id,
|
|
||||||
});
|
|
||||||
|
|
||||||
this_id
|
this_id
|
||||||
}
|
}
|
||||||
@@ -380,6 +392,7 @@ impl VirtualDom {
|
|||||||
&mut self,
|
&mut self,
|
||||||
path: &'static [u8],
|
path: &'static [u8],
|
||||||
this_id: ElementId,
|
this_id: ElementId,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> ElementId {
|
) -> ElementId {
|
||||||
if path.len() == 1 {
|
if path.len() == 1 {
|
||||||
return this_id;
|
return this_id;
|
||||||
@@ -389,16 +402,17 @@ impl VirtualDom {
|
|||||||
// Else, it's deep in the template and we should create a new id for it
|
// Else, it's deep in the template and we should create a new id for it
|
||||||
let id = self.next_element();
|
let id = self.next_element();
|
||||||
|
|
||||||
self.mutations.push(Mutation::AssignId {
|
to.assign_node_id(&path[1..], id);
|
||||||
path: &path[1..],
|
|
||||||
id,
|
|
||||||
});
|
|
||||||
|
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a new template into the VirtualDom's template registry
|
/// Insert a new template into the VirtualDom's template registry
|
||||||
pub(crate) fn register_template_first_byte_index(&mut self, mut template: Template<'static>) {
|
pub(crate) fn register_template_first_byte_index(
|
||||||
|
&mut self,
|
||||||
|
mut template: Template<'static>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
// First, make sure we mark the template as seen, regardless if we process it
|
// First, make sure we mark the template as seen, regardless if we process it
|
||||||
let (path, _) = template.name.rsplit_once(':').unwrap();
|
let (path, _) = template.name.rsplit_once(':').unwrap();
|
||||||
if let Some((_, old_template)) = self
|
if let Some((_, old_template)) = self
|
||||||
@@ -421,14 +435,18 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// If it's all dynamic nodes, then we don't need to register it
|
// If it's all dynamic nodes, then we don't need to register it
|
||||||
if !template.is_completely_dynamic() {
|
if !template.is_completely_dynamic() {
|
||||||
self.mutations.templates.push(template);
|
to.register_template(template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a new template into the VirtualDom's template registry
|
/// Insert a new template into the VirtualDom's template registry
|
||||||
// used in conditional compilation
|
// used in conditional compilation
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
pub(crate) fn register_template(&mut self, mut template: Template<'static>) {
|
pub(crate) fn register_template(
|
||||||
|
&mut self,
|
||||||
|
mut template: Template<'static>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
let (path, byte_index) = template.name.rsplit_once(':').unwrap();
|
let (path, byte_index) = template.name.rsplit_once(':').unwrap();
|
||||||
|
|
||||||
let byte_index = byte_index.parse::<usize>().unwrap();
|
let byte_index = byte_index.parse::<usize>().unwrap();
|
||||||
@@ -458,22 +476,32 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// If it's all dynamic nodes, then we don't need to register it
|
// If it's all dynamic nodes, then we don't need to register it
|
||||||
if !template.is_completely_dynamic() {
|
if !template.is_completely_dynamic() {
|
||||||
self.mutations.templates.push(template);
|
to.register_template(template)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn create_dynamic_node(&mut self, parent: ElementRef, node: &DynamicNode) -> usize {
|
pub(crate) fn create_dynamic_node(
|
||||||
|
&mut self,
|
||||||
|
parent: ElementRef,
|
||||||
|
node: &DynamicNode,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
use DynamicNode::*;
|
use DynamicNode::*;
|
||||||
match node {
|
match node {
|
||||||
Text(text) => self.create_dynamic_text(parent, text),
|
Text(text) => self.create_dynamic_text(parent, text, to),
|
||||||
Placeholder(place) => self.create_placeholder(place, parent),
|
Placeholder(place) => self.create_placeholder(place, parent, to),
|
||||||
Component(component) => self.create_component_node(Some(parent), component),
|
Component(component) => self.create_component_node(Some(parent), component, to),
|
||||||
Fragment(frag) => self.create_children(frag, Some(parent)),
|
Fragment(frag) => self.create_children(frag, Some(parent), to),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_dynamic_text(&mut self, parent: ElementRef, text: &VText) -> usize {
|
fn create_dynamic_text(
|
||||||
|
&mut self,
|
||||||
|
parent: ElementRef,
|
||||||
|
text: &VText,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
// Allocate a dynamic element reference for this text node
|
// Allocate a dynamic element reference for this text node
|
||||||
let new_id = self.next_element();
|
let new_id = self.next_element();
|
||||||
|
|
||||||
@@ -481,11 +509,7 @@ impl VirtualDom {
|
|||||||
text.id.set(Some(new_id));
|
text.id.set(Some(new_id));
|
||||||
|
|
||||||
// Add the mutation to the list
|
// Add the mutation to the list
|
||||||
self.mutations.push(HydrateText {
|
to.hydrate_text_node(&parent.path.path[1..], &text.value, new_id);
|
||||||
id: new_id,
|
|
||||||
path: &parent.path.path[1..],
|
|
||||||
value: &text.value,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Since we're hydrating an existing node, we don't create any new nodes
|
// Since we're hydrating an existing node, we don't create any new nodes
|
||||||
0
|
0
|
||||||
@@ -495,6 +519,7 @@ impl VirtualDom {
|
|||||||
&mut self,
|
&mut self,
|
||||||
placeholder: &VPlaceholder,
|
placeholder: &VPlaceholder,
|
||||||
parent: ElementRef,
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
// Allocate a dynamic element reference for this text node
|
// Allocate a dynamic element reference for this text node
|
||||||
let id = self.next_element();
|
let id = self.next_element();
|
||||||
@@ -506,10 +531,7 @@ impl VirtualDom {
|
|||||||
*placeholder.parent.borrow_mut() = Some(parent);
|
*placeholder.parent.borrow_mut() = Some(parent);
|
||||||
|
|
||||||
// Assign the ID to the existing node in the template
|
// Assign the ID to the existing node in the template
|
||||||
self.mutations.push(AssignId {
|
to.assign_node_id(&parent.path.path[1..], id);
|
||||||
path: &parent.path.path[1..],
|
|
||||||
id,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Since the placeholder is already in the DOM, we don't create any new nodes
|
// Since the placeholder is already in the DOM, we don't create any new nodes
|
||||||
0
|
0
|
||||||
@@ -519,11 +541,12 @@ impl VirtualDom {
|
|||||||
&mut self,
|
&mut self,
|
||||||
parent: Option<ElementRef>,
|
parent: Option<ElementRef>,
|
||||||
component: &VComponent,
|
component: &VComponent,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
use RenderReturn::*;
|
use RenderReturn::*;
|
||||||
|
|
||||||
// Load up a ScopeId for this vcomponent
|
// Load up a ScopeId for this vcomponent
|
||||||
let scope = self.load_scope_from_vcomponent(component);
|
let scope = self.load_scope_from_vcomponent(component, to);
|
||||||
|
|
||||||
component.scope.set(Some(scope));
|
component.scope.set(Some(scope));
|
||||||
|
|
||||||
@@ -535,14 +558,18 @@ impl VirtualDom {
|
|||||||
// Create the component's root element
|
// Create the component's root element
|
||||||
Ready(t) => {
|
Ready(t) => {
|
||||||
self.assign_boundary_ref(parent, t);
|
self.assign_boundary_ref(parent, t);
|
||||||
self.create_scope(scope, t)
|
self.create_scope(scope, t, to)
|
||||||
}
|
}
|
||||||
Aborted(t) => self.mount_aborted(t, parent),
|
Aborted(t) => self.mount_aborted(t, parent, to),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load a scope from a vcomponent. If the scope id doesn't exist, that means the component is currently "live"
|
/// Load a scope from a vcomponent. If the scope id doesn't exist, that means the component is currently "live"
|
||||||
fn load_scope_from_vcomponent(&mut self, component: &VComponent) -> ScopeId {
|
fn load_scope_from_vcomponent(
|
||||||
|
&mut self,
|
||||||
|
component: &VComponent,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> ScopeId {
|
||||||
component.scope.get().unwrap_or_else(|| {
|
component.scope.get().unwrap_or_else(|| {
|
||||||
self.new_scope(component.props.clone(), component.name)
|
self.new_scope(component.props.clone(), component.name)
|
||||||
.context()
|
.context()
|
||||||
@@ -550,9 +577,14 @@ impl VirtualDom {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mount_aborted(&mut self, placeholder: &VPlaceholder, parent: Option<ElementRef>) -> usize {
|
fn mount_aborted(
|
||||||
|
&mut self,
|
||||||
|
placeholder: &VPlaceholder,
|
||||||
|
parent: Option<ElementRef>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) -> usize {
|
||||||
let id = self.next_element();
|
let id = self.next_element();
|
||||||
self.mutations.push(Mutation::CreatePlaceholder { id });
|
to.create_placeholder(id);
|
||||||
placeholder.id.set(Some(id));
|
placeholder.id.set(Some(id));
|
||||||
*placeholder.parent.borrow_mut() = parent;
|
*placeholder.parent.borrow_mut() = parent;
|
||||||
|
|
||||||
|
|||||||
+189
-140
@@ -3,7 +3,9 @@ use std::ops::Deref;
|
|||||||
use crate::{
|
use crate::{
|
||||||
any_props::AnyProps,
|
any_props::AnyProps,
|
||||||
arena::ElementId,
|
arena::ElementId,
|
||||||
innerlude::{DirtyScope, ElementPath, ElementRef, VComponent, VPlaceholder, VText},
|
innerlude::{
|
||||||
|
DirtyScope, ElementPath, ElementRef, VComponent, VPlaceholder, VText, WriteMutations,
|
||||||
|
},
|
||||||
mutations::Mutation,
|
mutations::Mutation,
|
||||||
nodes::RenderReturn,
|
nodes::RenderReturn,
|
||||||
nodes::{DynamicNode, VNode},
|
nodes::{DynamicNode, VNode},
|
||||||
@@ -16,7 +18,12 @@ use rustc_hash::{FxHashMap, FxHashSet};
|
|||||||
use DynamicNode::*;
|
use DynamicNode::*;
|
||||||
|
|
||||||
impl VirtualDom {
|
impl VirtualDom {
|
||||||
pub(super) fn diff_scope(&mut self, scope: ScopeId, new_nodes: RenderReturn) {
|
pub(super) fn diff_scope(
|
||||||
|
&mut self,
|
||||||
|
scope: ScopeId,
|
||||||
|
new_nodes: RenderReturn,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
self.runtime.scope_stack.borrow_mut().push(scope);
|
self.runtime.scope_stack.borrow_mut().push(scope);
|
||||||
let scope_state = &mut self.scopes[scope.0];
|
let scope_state = &mut self.scopes[scope.0];
|
||||||
// Load the old and new bump arenas
|
// Load the old and new bump arenas
|
||||||
@@ -27,10 +34,10 @@ impl VirtualDom {
|
|||||||
|
|
||||||
match (&old, new) {
|
match (&old, new) {
|
||||||
// Normal pathway
|
// Normal pathway
|
||||||
(Ready(l), Ready(r)) => self.diff_node(l, r),
|
(Ready(l), Ready(r)) => self.diff_node(l, r, to),
|
||||||
|
|
||||||
// Unwind the mutations if need be
|
// Unwind the mutations if need be
|
||||||
(Ready(l), Aborted(p)) => self.diff_ok_to_err(l, p),
|
(Ready(l), Aborted(p)) => self.diff_ok_to_err(l, p, to),
|
||||||
|
|
||||||
// Just move over the placeholder
|
// Just move over the placeholder
|
||||||
(Aborted(l), Aborted(r)) => {
|
(Aborted(l), Aborted(r)) => {
|
||||||
@@ -44,24 +51,19 @@ impl VirtualDom {
|
|||||||
l,
|
l,
|
||||||
[r],
|
[r],
|
||||||
l.parent.borrow().expect("root node should not be none"),
|
l.parent.borrow().expect("root node should not be none"),
|
||||||
|
to,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
self.runtime.scope_stack.borrow_mut().pop();
|
self.runtime.scope_stack.borrow_mut().pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_ok_to_err(&mut self, l: &VNode, p: &VPlaceholder) {
|
fn diff_ok_to_err(&mut self, l: &VNode, p: &VPlaceholder, to: &mut impl WriteMutations) {
|
||||||
let id = self.next_element();
|
let id = self.next_element();
|
||||||
p.id.set(Some(id));
|
p.id.set(Some(id));
|
||||||
*p.parent.borrow_mut() = l.parent.borrow().clone();
|
*p.parent.borrow_mut() = l.parent.borrow().clone();
|
||||||
self.mutations.push(Mutation::CreatePlaceholder { id });
|
to.create_placeholder(id);
|
||||||
|
|
||||||
let pre_edits = self.mutations.edits.len();
|
self.remove_node(l, true, to);
|
||||||
|
|
||||||
self.remove_node(l, true);
|
|
||||||
|
|
||||||
// We should always have a remove mutation
|
|
||||||
// Eventually we don't want to generate placeholders, so this might not be true. But it's true today
|
|
||||||
assert!(self.mutations.edits.len() > pre_edits);
|
|
||||||
|
|
||||||
// We want to optimize the replace case to use one less mutation if possible
|
// We want to optimize the replace case to use one less mutation if possible
|
||||||
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
||||||
@@ -72,7 +74,12 @@ impl VirtualDom {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_node(&mut self, left_template: &VNode, right_template: &VNode) {
|
fn diff_node(
|
||||||
|
&mut self,
|
||||||
|
left_template: &VNode,
|
||||||
|
right_template: &VNode,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
// If hot reloading is enabled, we need to make sure we're using the latest template
|
// If hot reloading is enabled, we need to make sure we're using the latest template
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
@@ -83,7 +90,7 @@ impl VirtualDom {
|
|||||||
right_template.template.set(template);
|
right_template.template.set(template);
|
||||||
if template != left_template.template.get() {
|
if template != left_template.template.get() {
|
||||||
let parent = left_template.parent.take();
|
let parent = left_template.parent.take();
|
||||||
return self.replace(left_template, [right_template], parent);
|
return self.replace(left_template, [right_template], parent, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,7 +108,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// If the templates are different by name, we need to replace the entire template
|
// If the templates are different by name, we need to replace the entire template
|
||||||
if templates_are_different(left_template, right_template) {
|
if templates_are_different(left_template, right_template) {
|
||||||
return self.light_diff_templates(left_template, right_template);
|
return self.light_diff_templates(left_template, right_template, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the templates are the same, we can diff the attributes and children
|
// If the templates are the same, we can diff the attributes and children
|
||||||
@@ -117,7 +124,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// If the attributes are different (or volatile), we need to update them
|
// If the attributes are different (or volatile), we need to update them
|
||||||
if left_attr.value != right_attr.value || left_attr.volatile {
|
if left_attr.value != right_attr.value || left_attr.volatile {
|
||||||
self.update_attribute(right_attr, left_attr);
|
self.update_attribute(right_attr, left_attr, to);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -135,7 +142,7 @@ impl VirtualDom {
|
|||||||
},
|
},
|
||||||
scope: self.runtime.scope_stack.borrow().last().copied().unwrap(),
|
scope: self.runtime.scope_stack.borrow().last().copied().unwrap(),
|
||||||
};
|
};
|
||||||
self.diff_dynamic_node(left_node, right_node, current_ref);
|
self.diff_dynamic_node(left_node, right_node, current_ref, to);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Make sure the roots get transferred over while we're here
|
// Make sure the roots get transferred over while we're here
|
||||||
@@ -153,30 +160,36 @@ impl VirtualDom {
|
|||||||
left_node: &DynamicNode,
|
left_node: &DynamicNode,
|
||||||
right_node: &DynamicNode,
|
right_node: &DynamicNode,
|
||||||
parent: ElementRef,
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
match (left_node, right_node) {
|
match (left_node, right_node) {
|
||||||
(Text(left), Text(right)) => self.diff_vtext(left, right),
|
(Text(left), Text(right)) => self.diff_vtext(left, right, to),
|
||||||
(Fragment(left), Fragment(right)) => self.diff_non_empty_fragment(left, right, parent),
|
(Fragment(left), Fragment(right)) => self.diff_non_empty_fragment(left, right, parent, to),
|
||||||
(Placeholder(left), Placeholder(right)) => {
|
(Placeholder(left), Placeholder(right)) => {
|
||||||
right.id.set(left.id.get());
|
right.id.set(left.id.get());
|
||||||
*right.parent.borrow_mut() = left.parent.borrow().clone();
|
*right.parent.borrow_mut() = left.parent.borrow().clone();
|
||||||
},
|
},
|
||||||
(Component(left), Component(right)) => self.diff_vcomponent(left, right, Some(parent)),
|
(Component(left), Component(right)) => self.diff_vcomponent(left, right, Some(parent), to),
|
||||||
(Placeholder(left), Fragment(right)) => self.replace_placeholder(left, right, parent),
|
(Placeholder(left), Fragment(right)) => self.replace_placeholder(left, right, parent, to),
|
||||||
(Fragment(left), Placeholder(right)) => self.node_to_placeholder(left, right, parent),
|
(Fragment(left), Placeholder(right)) => self.node_to_placeholder(left, right, parent, to),
|
||||||
_ => todo!("This is an usual custom case for dynamic nodes. We don't know how to handle it yet."),
|
_ => todo!("This is an usual custom case for dynamic nodes. We don't know how to handle it yet."),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_attribute(&mut self, right_attr: &Attribute, left_attr: &Attribute) {
|
fn update_attribute(
|
||||||
|
&mut self,
|
||||||
|
right_attr: &Attribute,
|
||||||
|
left_attr: &Attribute,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
let name = &left_attr.name;
|
let name = &left_attr.name;
|
||||||
let value = &right_attr.value;
|
let value = &right_attr.value;
|
||||||
self.mutations.push(Mutation::SetAttribute {
|
to.set_attribute(
|
||||||
id: left_attr.mounted_element.get(),
|
|
||||||
ns: right_attr.namespace,
|
|
||||||
name,
|
name,
|
||||||
|
right_attr.namespace,
|
||||||
value,
|
value,
|
||||||
});
|
left_attr.mounted_element.get(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_vcomponent(
|
fn diff_vcomponent(
|
||||||
@@ -184,6 +197,7 @@ impl VirtualDom {
|
|||||||
left: &VComponent,
|
left: &VComponent,
|
||||||
right: &VComponent,
|
right: &VComponent,
|
||||||
parent: Option<ElementRef>,
|
parent: Option<ElementRef>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
if std::ptr::eq(left, right) {
|
if std::ptr::eq(left, right) {
|
||||||
return;
|
return;
|
||||||
@@ -191,7 +205,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// Replace components that have different render fns
|
// Replace components that have different render fns
|
||||||
if left.render_fn != right.render_fn {
|
if left.render_fn != right.render_fn {
|
||||||
return self.replace_vcomponent(right, left, parent);
|
return self.replace_vcomponent(right, left, parent, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the new vcomponent has the right scopeid associated to it
|
// Make sure the new vcomponent has the right scopeid associated to it
|
||||||
@@ -221,7 +235,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// Now run the component and diff it
|
// Now run the component and diff it
|
||||||
let new = self.run_scope(scope_id);
|
let new = self.run_scope(scope_id);
|
||||||
self.diff_scope(scope_id, new);
|
self.diff_scope(scope_id, new, to);
|
||||||
|
|
||||||
self.dirty_scopes.remove(&DirtyScope {
|
self.dirty_scopes.remove(&DirtyScope {
|
||||||
height: self.runtime.get_context(scope_id).unwrap().height,
|
height: self.runtime.get_context(scope_id).unwrap().height,
|
||||||
@@ -234,14 +248,11 @@ impl VirtualDom {
|
|||||||
right: &VComponent,
|
right: &VComponent,
|
||||||
left: &VComponent,
|
left: &VComponent,
|
||||||
parent: Option<ElementRef>,
|
parent: Option<ElementRef>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
let m = self.create_component_node(parent, right);
|
let m = self.create_component_node(parent, right);
|
||||||
|
|
||||||
let pre_edits = self.mutations.edits.len();
|
self.remove_component_node(left, true, to);
|
||||||
|
|
||||||
self.remove_component_node(left, true);
|
|
||||||
|
|
||||||
assert!(self.mutations.edits.len() > pre_edits);
|
|
||||||
|
|
||||||
// We want to optimize the replace case to use one less mutation if possible
|
// We want to optimize the replace case to use one less mutation if possible
|
||||||
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
||||||
@@ -289,13 +300,13 @@ impl VirtualDom {
|
|||||||
/// Component { ..props }
|
/// Component { ..props }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn light_diff_templates(&mut self, left: &VNode, right: &VNode) {
|
fn light_diff_templates(&mut self, left: &VNode, right: &VNode, to: &mut impl WriteMutations) {
|
||||||
let parent = left.parent.take();
|
let parent = left.parent.take();
|
||||||
match matching_components(left, right) {
|
match matching_components(left, right) {
|
||||||
None => self.replace(left, [right], parent),
|
None => self.replace(left, [right], parent, to),
|
||||||
Some(components) => components
|
Some(components) => components
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|(l, r)| self.diff_vcomponent(l, r, parent)),
|
.for_each(|(l, r)| self.diff_vcomponent(l, r, parent, to)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,19 +314,22 @@ impl VirtualDom {
|
|||||||
///
|
///
|
||||||
/// This just moves the ID of the old node over to the new node, and then sets the text of the new node if it's
|
/// This just moves the ID of the old node over to the new node, and then sets the text of the new node if it's
|
||||||
/// different.
|
/// different.
|
||||||
fn diff_vtext(&mut self, left: &VText, right: &VText) {
|
fn diff_vtext(&mut self, left: &VText, right: &VText, to: &mut impl WriteMutations) {
|
||||||
let id = left.id.get().unwrap_or_else(|| self.next_element());
|
let id = left.id.get().unwrap_or_else(|| self.next_element());
|
||||||
|
|
||||||
right.id.set(Some(id));
|
right.id.set(Some(id));
|
||||||
if left.value != right.value {
|
if left.value != right.value {
|
||||||
self.mutations.push(Mutation::SetText {
|
to.set_node_text(&right.value, id);
|
||||||
id,
|
|
||||||
value: &right.value,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_non_empty_fragment(&mut self, old: &[VNode], new: &[VNode], parent: ElementRef) {
|
fn diff_non_empty_fragment(
|
||||||
|
&mut self,
|
||||||
|
old: &[VNode],
|
||||||
|
new: &[VNode],
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
let new_is_keyed = new[0].key.is_some();
|
let new_is_keyed = new[0].key.is_some();
|
||||||
let old_is_keyed = old[0].key.is_some();
|
let old_is_keyed = old[0].key.is_some();
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
@@ -328,9 +342,9 @@ impl VirtualDom {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if new_is_keyed && old_is_keyed {
|
if new_is_keyed && old_is_keyed {
|
||||||
self.diff_keyed_children(old, new, parent);
|
self.diff_keyed_children(old, new, parent, to);
|
||||||
} else {
|
} else {
|
||||||
self.diff_non_keyed_children(old, new, parent);
|
self.diff_non_keyed_children(old, new, parent, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,7 +356,13 @@ impl VirtualDom {
|
|||||||
// [... parent]
|
// [... parent]
|
||||||
//
|
//
|
||||||
// the change list stack is in the same state when this function returns.
|
// the change list stack is in the same state when this function returns.
|
||||||
fn diff_non_keyed_children(&mut self, old: &[VNode], new: &[VNode], parent: ElementRef) {
|
fn diff_non_keyed_children(
|
||||||
|
&mut self,
|
||||||
|
old: &[VNode],
|
||||||
|
new: &[VNode],
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
// Handled these cases in `diff_children` before calling this function.
|
// Handled these cases in `diff_children` before calling this function.
|
||||||
@@ -350,15 +370,15 @@ impl VirtualDom {
|
|||||||
debug_assert!(!old.is_empty());
|
debug_assert!(!old.is_empty());
|
||||||
|
|
||||||
match old.len().cmp(&new.len()) {
|
match old.len().cmp(&new.len()) {
|
||||||
Ordering::Greater => self.remove_nodes(&old[new.len()..]),
|
Ordering::Greater => self.remove_nodes(&old[new.len()..], to),
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
self.create_and_insert_after(&new[old.len()..], old.last().unwrap(), parent)
|
self.create_and_insert_after(&new[old.len()..], old.last().unwrap(), parent, to)
|
||||||
}
|
}
|
||||||
Ordering::Equal => {}
|
Ordering::Equal => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (new, old) in new.iter().zip(old.iter()) {
|
for (new, old) in new.iter().zip(old.iter()) {
|
||||||
self.diff_node(old, new);
|
self.diff_node(old, new, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +398,13 @@ impl VirtualDom {
|
|||||||
// https://github.com/infernojs/inferno/blob/36fd96/packages/inferno/src/DOM/patching.ts#L530-L739
|
// https://github.com/infernojs/inferno/blob/36fd96/packages/inferno/src/DOM/patching.ts#L530-L739
|
||||||
//
|
//
|
||||||
// The stack is empty upon entry.
|
// The stack is empty upon entry.
|
||||||
fn diff_keyed_children(&mut self, old: &[VNode], new: &[VNode], parent: ElementRef) {
|
fn diff_keyed_children(
|
||||||
|
&mut self,
|
||||||
|
old: &[VNode],
|
||||||
|
new: &[VNode],
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
if cfg!(debug_assertions) {
|
if cfg!(debug_assertions) {
|
||||||
let mut keys = rustc_hash::FxHashSet::default();
|
let mut keys = rustc_hash::FxHashSet::default();
|
||||||
let mut assert_unique_keys = |children: &[VNode]| {
|
let mut assert_unique_keys = |children: &[VNode]| {
|
||||||
@@ -406,7 +432,7 @@ impl VirtualDom {
|
|||||||
//
|
//
|
||||||
// `shared_prefix_count` is the count of how many nodes at the start of
|
// `shared_prefix_count` is the count of how many nodes at the start of
|
||||||
// `new` and `old` share the same keys.
|
// `new` and `old` share the same keys.
|
||||||
let (left_offset, right_offset) = match self.diff_keyed_ends(old, new, parent) {
|
let (left_offset, right_offset) = match self.diff_keyed_ends(old, new, parent, to) {
|
||||||
Some(count) => count,
|
Some(count) => count,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
@@ -425,25 +451,25 @@ impl VirtualDom {
|
|||||||
|
|
||||||
if new_middle.is_empty() {
|
if new_middle.is_empty() {
|
||||||
// remove the old elements
|
// remove the old elements
|
||||||
self.remove_nodes(old_middle);
|
self.remove_nodes(old_middle, to);
|
||||||
} else if old_middle.is_empty() {
|
} else if old_middle.is_empty() {
|
||||||
// there were no old elements, so just create the new elements
|
// there were no old elements, so just create the new elements
|
||||||
// we need to find the right "foothold" though - we shouldn't use the "append" at all
|
// we need to find the right "foothold" though - we shouldn't use the "append" at all
|
||||||
if left_offset == 0 {
|
if left_offset == 0 {
|
||||||
// insert at the beginning of the old list
|
// insert at the beginning of the old list
|
||||||
let foothold = &old[old.len() - right_offset];
|
let foothold = &old[old.len() - right_offset];
|
||||||
self.create_and_insert_before(new_middle, foothold, parent);
|
self.create_and_insert_before(new_middle, foothold, parent, to);
|
||||||
} else if right_offset == 0 {
|
} else if right_offset == 0 {
|
||||||
// insert at the end the old list
|
// insert at the end the old list
|
||||||
let foothold = old.last().unwrap();
|
let foothold = old.last().unwrap();
|
||||||
self.create_and_insert_after(new_middle, foothold, parent);
|
self.create_and_insert_after(new_middle, foothold, parent, to);
|
||||||
} else {
|
} else {
|
||||||
// inserting in the middle
|
// inserting in the middle
|
||||||
let foothold = &old[left_offset - 1];
|
let foothold = &old[left_offset - 1];
|
||||||
self.create_and_insert_after(new_middle, foothold, parent);
|
self.create_and_insert_after(new_middle, foothold, parent, to);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.diff_keyed_middle(old_middle, new_middle, parent);
|
self.diff_keyed_middle(old_middle, new_middle, parent, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,6 +483,7 @@ impl VirtualDom {
|
|||||||
old: &[VNode],
|
old: &[VNode],
|
||||||
new: &[VNode],
|
new: &[VNode],
|
||||||
parent: ElementRef,
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> Option<(usize, usize)> {
|
) -> Option<(usize, usize)> {
|
||||||
let mut left_offset = 0;
|
let mut left_offset = 0;
|
||||||
|
|
||||||
@@ -465,21 +492,21 @@ impl VirtualDom {
|
|||||||
if old.key != new.key {
|
if old.key != new.key {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.diff_node(old, new);
|
self.diff_node(old, new, to);
|
||||||
left_offset += 1;
|
left_offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If that was all of the old children, then create and append the remaining
|
// If that was all of the old children, then create and append the remaining
|
||||||
// new children and we're finished.
|
// new children and we're finished.
|
||||||
if left_offset == old.len() {
|
if left_offset == old.len() {
|
||||||
self.create_and_insert_after(&new[left_offset..], old.last().unwrap(), parent);
|
self.create_and_insert_after(&new[left_offset..], old.last().unwrap(), parent, to);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// And if that was all of the new children, then remove all of the remaining
|
// And if that was all of the new children, then remove all of the remaining
|
||||||
// old children and we're finished.
|
// old children and we're finished.
|
||||||
if left_offset == new.len() {
|
if left_offset == new.len() {
|
||||||
self.remove_nodes(&old[left_offset..]);
|
self.remove_nodes(&old[left_offset..], to);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +517,7 @@ impl VirtualDom {
|
|||||||
if old.key != new.key {
|
if old.key != new.key {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.diff_node(old, new);
|
self.diff_node(old, new, to);
|
||||||
right_offset += 1;
|
right_offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -511,7 +538,13 @@ impl VirtualDom {
|
|||||||
//
|
//
|
||||||
// Upon exit from this function, it will be restored to that same self.
|
// Upon exit from this function, it will be restored to that same self.
|
||||||
#[allow(clippy::too_many_lines)]
|
#[allow(clippy::too_many_lines)]
|
||||||
fn diff_keyed_middle(&mut self, old: &[VNode], new: &[VNode], parent: ElementRef) {
|
fn diff_keyed_middle(
|
||||||
|
&mut self,
|
||||||
|
old: &[VNode],
|
||||||
|
new: &[VNode],
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
/*
|
/*
|
||||||
1. Map the old keys into a numerical ordering based on indices.
|
1. Map the old keys into a numerical ordering based on indices.
|
||||||
2. Create a map of old key to its index
|
2. Create a map of old key to its index
|
||||||
@@ -567,8 +600,8 @@ impl VirtualDom {
|
|||||||
// create the new children afresh.
|
// create the new children afresh.
|
||||||
if shared_keys.is_empty() {
|
if shared_keys.is_empty() {
|
||||||
if !old.is_empty() {
|
if !old.is_empty() {
|
||||||
self.remove_nodes(&old[1..]);
|
self.remove_nodes(&old[1..], to);
|
||||||
self.replace(&old[0], new, Some(parent));
|
self.replace(&old[0], new, Some(parent), to);
|
||||||
} else {
|
} else {
|
||||||
// I think this is wrong - why are we appending?
|
// I think this is wrong - why are we appending?
|
||||||
// only valid of the if there are no trailing elements
|
// only valid of the if there are no trailing elements
|
||||||
@@ -584,7 +617,7 @@ impl VirtualDom {
|
|||||||
for child in old {
|
for child in old {
|
||||||
let key = child.key.unwrap();
|
let key = child.key.unwrap();
|
||||||
if !shared_keys.contains(&key) {
|
if !shared_keys.contains(&key) {
|
||||||
self.remove_node(child, true);
|
self.remove_node(child, true, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -611,7 +644,7 @@ impl VirtualDom {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for idx in &lis_sequence {
|
for idx in &lis_sequence {
|
||||||
self.diff_node(&old[new_index_to_old_index[*idx]], &new[*idx]);
|
self.diff_node(&old[new_index_to_old_index[*idx]], &new[*idx], to);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut nodes_created = 0;
|
let mut nodes_created = 0;
|
||||||
@@ -625,17 +658,14 @@ impl VirtualDom {
|
|||||||
if old_index == u32::MAX as usize {
|
if old_index == u32::MAX as usize {
|
||||||
nodes_created += self.create(new_node);
|
nodes_created += self.create(new_node);
|
||||||
} else {
|
} else {
|
||||||
self.diff_node(&old[old_index], new_node);
|
self.diff_node(&old[old_index], new_node, to);
|
||||||
nodes_created += self.push_all_real_nodes(new_node);
|
nodes_created += self.push_all_real_nodes(new_node, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = self.find_last_element(&new[last]);
|
let id = self.find_last_element(&new[last]);
|
||||||
if nodes_created > 0 {
|
if nodes_created > 0 {
|
||||||
self.mutations.push(Mutation::InsertAfter {
|
to.insert_nodes_after(id, nodes_created)
|
||||||
id,
|
|
||||||
m: nodes_created,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
nodes_created = 0;
|
nodes_created = 0;
|
||||||
}
|
}
|
||||||
@@ -651,17 +681,14 @@ impl VirtualDom {
|
|||||||
if old_index == u32::MAX as usize {
|
if old_index == u32::MAX as usize {
|
||||||
nodes_created += self.create(new_node);
|
nodes_created += self.create(new_node);
|
||||||
} else {
|
} else {
|
||||||
self.diff_node(&old[old_index], new_node);
|
self.diff_node(&old[old_index], new_node, to);
|
||||||
nodes_created += self.push_all_real_nodes(new_node);
|
nodes_created += self.push_all_real_nodes(new_node, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = self.find_first_element(&new[last]);
|
let id = self.find_first_element(&new[last]);
|
||||||
if nodes_created > 0 {
|
if nodes_created > 0 {
|
||||||
self.mutations.push(Mutation::InsertBefore {
|
to.insert_nodes_before(id, nodes_created);
|
||||||
id,
|
|
||||||
m: nodes_created,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes_created = 0;
|
nodes_created = 0;
|
||||||
@@ -677,23 +704,20 @@ impl VirtualDom {
|
|||||||
if old_index == u32::MAX as usize {
|
if old_index == u32::MAX as usize {
|
||||||
nodes_created += self.create(new_node);
|
nodes_created += self.create(new_node);
|
||||||
} else {
|
} else {
|
||||||
self.diff_node(&old[old_index], new_node);
|
self.diff_node(&old[old_index], new_node, to);
|
||||||
nodes_created += self.push_all_real_nodes(new_node);
|
nodes_created += self.push_all_real_nodes(new_node, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = self.find_first_element(&new[first_lis]);
|
let id = self.find_first_element(&new[first_lis]);
|
||||||
if nodes_created > 0 {
|
if nodes_created > 0 {
|
||||||
self.mutations.push(Mutation::InsertBefore {
|
to.insert_nodes_before(id, nodes_created);
|
||||||
id,
|
|
||||||
m: nodes_created,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push all the real nodes on the stack
|
/// Push all the real nodes on the stack
|
||||||
fn push_all_real_nodes(&mut self, node: &VNode) -> usize {
|
fn push_all_real_nodes(&mut self, node: &VNode, to: &mut impl WriteMutations) -> usize {
|
||||||
node.template
|
node.template
|
||||||
.get()
|
.get()
|
||||||
.roots
|
.roots
|
||||||
@@ -703,35 +727,29 @@ impl VirtualDom {
|
|||||||
let node = match node.dynamic_root(idx) {
|
let node = match node.dynamic_root(idx) {
|
||||||
Some(node) => node,
|
Some(node) => node,
|
||||||
None => {
|
None => {
|
||||||
self.mutations.push(Mutation::PushRoot {
|
to.push_root(node.root_ids.borrow()[idx]);
|
||||||
id: node.root_ids.borrow()[idx],
|
|
||||||
});
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match node {
|
match node {
|
||||||
Text(t) => {
|
Text(t) => {
|
||||||
self.mutations.push(Mutation::PushRoot {
|
to.push_root(t.id.get().unwrap());
|
||||||
id: t.id.get().unwrap(),
|
|
||||||
});
|
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
Placeholder(t) => {
|
Placeholder(t) => {
|
||||||
self.mutations.push(Mutation::PushRoot {
|
to.push_root(t.id.get().unwrap());
|
||||||
id: t.id.get().unwrap(),
|
|
||||||
});
|
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
Fragment(nodes) => nodes
|
Fragment(nodes) => nodes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|node| self.push_all_real_nodes(node))
|
.map(|node| self.push_all_real_nodes(node, to))
|
||||||
.sum(),
|
.sum(),
|
||||||
|
|
||||||
Component(comp) => {
|
Component(comp) => {
|
||||||
let scope = comp.scope.get().unwrap();
|
let scope = comp.scope.get().unwrap();
|
||||||
match self.get_scope(scope).unwrap().root_node() {
|
match self.get_scope(scope).unwrap().root_node() {
|
||||||
RenderReturn::Ready(node) => self.push_all_real_nodes(node),
|
RenderReturn::Ready(node) => self.push_all_real_nodes(node, to),
|
||||||
RenderReturn::Aborted(_node) => todo!(),
|
RenderReturn::Aborted(_node) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -744,6 +762,7 @@ impl VirtualDom {
|
|||||||
&mut self,
|
&mut self,
|
||||||
nodes: impl IntoIterator<Item = &'a VNode>,
|
nodes: impl IntoIterator<Item = &'a VNode>,
|
||||||
parent: Option<ElementRef>,
|
parent: Option<ElementRef>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
nodes
|
nodes
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -754,16 +773,28 @@ impl VirtualDom {
|
|||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_and_insert_before(&mut self, new: &[VNode], before: &VNode, parent: ElementRef) {
|
fn create_and_insert_before(
|
||||||
let m = self.create_children(new, Some(parent));
|
&mut self,
|
||||||
|
new: &[VNode],
|
||||||
|
before: &VNode,
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
|
let m = self.create_children(new, Some(parent), to);
|
||||||
let id = self.find_first_element(before);
|
let id = self.find_first_element(before);
|
||||||
self.mutations.push(Mutation::InsertBefore { id, m })
|
to.insert_nodes_before(id, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_and_insert_after(&mut self, new: &[VNode], after: &VNode, parent: ElementRef) {
|
fn create_and_insert_after(
|
||||||
let m = self.create_children(new, Some(parent));
|
&mut self,
|
||||||
|
new: &[VNode],
|
||||||
|
after: &VNode,
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
|
let m = self.create_children(new, Some(parent), to);
|
||||||
let id = self.find_last_element(after);
|
let id = self.find_last_element(after);
|
||||||
self.mutations.push(Mutation::InsertAfter { id, m })
|
to.insert_nodes_after(id, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simply replace a placeholder with a list of nodes
|
/// Simply replace a placeholder with a list of nodes
|
||||||
@@ -772,10 +803,11 @@ impl VirtualDom {
|
|||||||
l: &VPlaceholder,
|
l: &VPlaceholder,
|
||||||
r: impl IntoIterator<Item = &'a VNode>,
|
r: impl IntoIterator<Item = &'a VNode>,
|
||||||
parent: ElementRef,
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
let m = self.create_children(r, Some(parent));
|
let m = self.create_children(r, Some(parent), to);
|
||||||
let id = l.id.get().unwrap();
|
let id = l.id.get().unwrap();
|
||||||
self.mutations.push(Mutation::ReplaceWith { id, m });
|
to.replace_with(id, m);
|
||||||
self.reclaim(id);
|
self.reclaim(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -784,16 +816,11 @@ impl VirtualDom {
|
|||||||
left: &VNode,
|
left: &VNode,
|
||||||
right: impl IntoIterator<Item = &'a VNode>,
|
right: impl IntoIterator<Item = &'a VNode>,
|
||||||
parent: Option<ElementRef>,
|
parent: Option<ElementRef>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
) {
|
) {
|
||||||
let m = self.create_children(right, parent);
|
let m = self.create_children(right, parent, to);
|
||||||
|
|
||||||
let pre_edits = self.mutations.edits.len();
|
self.remove_node(left, true, to);
|
||||||
|
|
||||||
self.remove_node(left, true);
|
|
||||||
|
|
||||||
// We should always have a remove mutation
|
|
||||||
// Eventually we don't want to generate placeholders, so this might not be true. But it's true today
|
|
||||||
assert!(self.mutations.edits.len() > pre_edits);
|
|
||||||
|
|
||||||
// We want to optimize the replace case to use one less mutation if possible
|
// We want to optimize the replace case to use one less mutation if possible
|
||||||
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
||||||
@@ -804,17 +831,22 @@ impl VirtualDom {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn node_to_placeholder(&mut self, l: &[VNode], r: &VPlaceholder, parent: ElementRef) {
|
fn node_to_placeholder(
|
||||||
|
&mut self,
|
||||||
|
l: &[VNode],
|
||||||
|
r: &VPlaceholder,
|
||||||
|
parent: ElementRef,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
// Create the placeholder first, ensuring we get a dedicated ID for the placeholder
|
// Create the placeholder first, ensuring we get a dedicated ID for the placeholder
|
||||||
let placeholder = self.next_element();
|
let placeholder = self.next_element();
|
||||||
|
|
||||||
r.id.set(Some(placeholder));
|
r.id.set(Some(placeholder));
|
||||||
r.parent.borrow_mut().replace(parent);
|
r.parent.borrow_mut().replace(parent);
|
||||||
|
|
||||||
self.mutations
|
to.create_placeholder(placeholder);
|
||||||
.push(Mutation::CreatePlaceholder { id: placeholder });
|
|
||||||
|
|
||||||
self.remove_nodes(l);
|
self.remove_nodes(l, to);
|
||||||
|
|
||||||
// We want to optimize the replace case to use one less mutation if possible
|
// We want to optimize the replace case to use one less mutation if possible
|
||||||
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
// Since mutations are done in reverse, the last node removed will be the first in the stack
|
||||||
@@ -827,43 +859,43 @@ impl VirtualDom {
|
|||||||
|
|
||||||
/// Remove these nodes from the dom
|
/// Remove these nodes from the dom
|
||||||
/// Wont generate mutations for the inner nodes
|
/// Wont generate mutations for the inner nodes
|
||||||
fn remove_nodes(&mut self, nodes: &[VNode]) {
|
fn remove_nodes(&mut self, nodes: &[VNode], to: &mut impl WriteMutations) {
|
||||||
nodes
|
nodes
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.for_each(|node| self.remove_node(node, true));
|
.for_each(|node| self.remove_node(node, true, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_node(&mut self, node: &VNode, gen_muts: bool) {
|
fn remove_node(&mut self, node: &VNode, gen_muts: bool, to: &mut impl WriteMutations) {
|
||||||
// Clean up any attributes that have claimed a static node as dynamic for mount/unmounta
|
// Clean up any attributes that have claimed a static node as dynamic for mount/unmounta
|
||||||
// Will not generate mutations!
|
// Will not generate mutations!
|
||||||
self.reclaim_attributes(node);
|
self.reclaim_attributes(node, to);
|
||||||
|
|
||||||
// Remove the nested dynamic nodes
|
// Remove the nested dynamic nodes
|
||||||
// We don't generate mutations for these, as they will be removed by the parent (in the next line)
|
// We don't generate mutations for these, as they will be removed by the parent (in the next line)
|
||||||
// But we still need to make sure to reclaim them from the arena and drop their hooks, etc
|
// But we still need to make sure to reclaim them from the arena and drop their hooks, etc
|
||||||
self.remove_nested_dyn_nodes(node);
|
self.remove_nested_dyn_nodes(node, to);
|
||||||
|
|
||||||
// Clean up the roots, assuming we need to generate mutations for these
|
// Clean up the roots, assuming we need to generate mutations for these
|
||||||
// This is done last in order to preserve Node ID reclaim order (reclaim in reverse order of claim)
|
// This is done last in order to preserve Node ID reclaim order (reclaim in reverse order of claim)
|
||||||
self.reclaim_roots(node, gen_muts);
|
self.reclaim_roots(node, gen_muts, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reclaim_roots(&mut self, node: &VNode, gen_muts: bool) {
|
fn reclaim_roots(&mut self, node: &VNode, gen_muts: bool, to: &mut impl WriteMutations) {
|
||||||
for (idx, _) in node.template.get().roots.iter().enumerate() {
|
for (idx, _) in node.template.get().roots.iter().enumerate() {
|
||||||
if let Some(dy) = node.dynamic_root(idx) {
|
if let Some(dy) = node.dynamic_root(idx) {
|
||||||
self.remove_dynamic_node(dy, gen_muts);
|
self.remove_dynamic_node(dy, gen_muts, to);
|
||||||
} else {
|
} else {
|
||||||
let id = node.root_ids.borrow()[idx];
|
let id = node.root_ids.borrow()[idx];
|
||||||
if gen_muts {
|
if gen_muts {
|
||||||
self.mutations.push(Mutation::Remove { id });
|
to.remove_node(id);
|
||||||
}
|
}
|
||||||
self.reclaim(id);
|
self.reclaim(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reclaim_attributes(&mut self, node: &VNode) {
|
fn reclaim_attributes(&mut self, node: &VNode, to: &mut impl WriteMutations) {
|
||||||
let mut id = None;
|
let mut id = None;
|
||||||
for (idx, attr) in node.dynamic_attrs.iter().enumerate() {
|
for (idx, attr) in node.dynamic_attrs.iter().enumerate() {
|
||||||
// We'll clean up the root nodes either way, so don't worry
|
// We'll clean up the root nodes either way, so don't worry
|
||||||
@@ -894,7 +926,7 @@ impl VirtualDom {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_nested_dyn_nodes(&mut self, node: &VNode) {
|
fn remove_nested_dyn_nodes(&mut self, node: &VNode, to: &mut impl WriteMutations) {
|
||||||
for (idx, dyn_node) in node.dynamic_nodes.iter().enumerate() {
|
for (idx, dyn_node) in node.dynamic_nodes.iter().enumerate() {
|
||||||
let path_len = node
|
let path_len = node
|
||||||
.template
|
.template
|
||||||
@@ -904,41 +936,56 @@ impl VirtualDom {
|
|||||||
.map(|path| path.len());
|
.map(|path| path.len());
|
||||||
// Roots are cleaned up automatically above and nodes with a empty path are placeholders
|
// Roots are cleaned up automatically above and nodes with a empty path are placeholders
|
||||||
if let Some(2..) = path_len {
|
if let Some(2..) = path_len {
|
||||||
self.remove_dynamic_node(dyn_node, false)
|
self.remove_dynamic_node(dyn_node, false, to)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_dynamic_node(&mut self, node: &DynamicNode, gen_muts: bool) {
|
fn remove_dynamic_node(
|
||||||
|
&mut self,
|
||||||
|
node: &DynamicNode,
|
||||||
|
gen_muts: bool,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
match node {
|
match node {
|
||||||
Component(comp) => self.remove_component_node(comp, gen_muts),
|
Component(comp) => self.remove_component_node(comp, gen_muts, to),
|
||||||
Text(t) => self.remove_text_node(t, gen_muts),
|
Text(t) => self.remove_text_node(t, gen_muts, to),
|
||||||
Placeholder(t) => self.remove_placeholder(t, gen_muts),
|
Placeholder(t) => self.remove_placeholder(t, gen_muts, to),
|
||||||
Fragment(nodes) => nodes
|
Fragment(nodes) => nodes
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|node| self.remove_node(node, gen_muts)),
|
.for_each(|node| self.remove_node(node, gen_muts, to)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_placeholder(&mut self, t: &VPlaceholder, gen_muts: bool) {
|
fn remove_placeholder(
|
||||||
|
&mut self,
|
||||||
|
t: &VPlaceholder,
|
||||||
|
gen_muts: bool,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
if let Some(id) = t.id.take() {
|
if let Some(id) = t.id.take() {
|
||||||
if gen_muts {
|
if gen_muts {
|
||||||
self.mutations.push(Mutation::Remove { id });
|
to.remove_node(id);
|
||||||
}
|
}
|
||||||
self.reclaim(id)
|
self.reclaim(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_text_node(&mut self, t: &VText, gen_muts: bool) {
|
fn remove_text_node(&mut self, t: &VText, gen_muts: bool, to: &mut impl WriteMutations) {
|
||||||
if let Some(id) = t.id.take() {
|
if let Some(id) = t.id.take() {
|
||||||
if gen_muts {
|
if gen_muts {
|
||||||
self.mutations.push(Mutation::Remove { id });
|
to.remove_node(id);
|
||||||
}
|
}
|
||||||
self.reclaim(id)
|
self.reclaim(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_component_node(&mut self, comp: &VComponent, gen_muts: bool) {
|
fn remove_component_node(
|
||||||
|
&mut self,
|
||||||
|
comp: &VComponent,
|
||||||
|
gen_muts: bool,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
// Remove the component reference from the vcomponent so they're not tied together
|
// Remove the component reference from the vcomponent so they're not tied together
|
||||||
let scope = comp
|
let scope = comp
|
||||||
.scope
|
.scope
|
||||||
@@ -947,8 +994,10 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// Remove the component from the dom
|
// Remove the component from the dom
|
||||||
match unsafe { self.get_scope(scope).unwrap().root_node() } {
|
match unsafe { self.get_scope(scope).unwrap().root_node() } {
|
||||||
RenderReturn::Ready(t) => self.remove_node(t, gen_muts),
|
RenderReturn::Ready(t) => self.remove_node(t, gen_muts, to),
|
||||||
RenderReturn::Aborted(placeholder) => self.remove_placeholder(placeholder, gen_muts),
|
RenderReturn::Aborted(placeholder) => {
|
||||||
|
self.remove_placeholder(placeholder, gen_muts, to)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Now drop all the resouces
|
// Now drop all the resouces
|
||||||
|
|||||||
@@ -74,9 +74,9 @@ pub(crate) mod innerlude {
|
|||||||
|
|
||||||
pub use crate::innerlude::{
|
pub use crate::innerlude::{
|
||||||
fc_to_builder, vdom_is_rendering, AnyValue, Attribute, AttributeValue, CapturedError,
|
fc_to_builder, vdom_is_rendering, AnyValue, Attribute, AttributeValue, CapturedError,
|
||||||
Component, DynamicNode, Element, ElementId, Event, Fragment, IntoDynNode, Mutation, Mutations,
|
Component, DynamicNode, Element, ElementId, Event, Fragment, IntoDynNode, Mutation,
|
||||||
Properties, RenderReturn, ScopeId, ScopeState, TaskId, Template, TemplateAttribute,
|
MutationsVec, Properties, RenderReturn, ScopeId, ScopeState, TaskId, Template,
|
||||||
TemplateNode, VComponent, VNode, VPlaceholder, VText, VirtualDom,
|
TemplateAttribute, TemplateNode, VComponent, VNode, VPlaceholder, VText, VirtualDom,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The purpose of this module is to alleviate imports of many common types
|
/// The purpose of this module is to alleviate imports of many common types
|
||||||
|
|||||||
+314
-45
@@ -2,7 +2,7 @@ use rustc_hash::FxHashSet;
|
|||||||
|
|
||||||
use crate::{arena::ElementId, AttributeValue, ScopeId, Template};
|
use crate::{arena::ElementId, AttributeValue, ScopeId, Template};
|
||||||
|
|
||||||
/// A container for all the relevant steps to modify the Real DOM
|
/// Something that can handle the mutations that are generated by the diffing process and apply them to the Real DOM
|
||||||
///
|
///
|
||||||
/// This object provides a bunch of important information for a renderer to use patch the Real Dom with the state of the
|
/// This object provides a bunch of important information for a renderer to use patch the Real Dom with the state of the
|
||||||
/// VirtualDom. This includes the scopes that were modified, the templates that were discovered, and a list of changes
|
/// VirtualDom. This includes the scopes that were modified, the templates that were discovered, and a list of changes
|
||||||
@@ -13,42 +13,128 @@ use crate::{arena::ElementId, AttributeValue, ScopeId, Template};
|
|||||||
/// Templates, however, apply to all subtrees, not just target subtree.
|
/// Templates, however, apply to all subtrees, not just target subtree.
|
||||||
///
|
///
|
||||||
/// Mutations are the only link between the RealDOM and the VirtualDOM.
|
/// Mutations are the only link between the RealDOM and the VirtualDOM.
|
||||||
#[derive(Debug, Default)]
|
pub trait WriteMutations {
|
||||||
#[must_use = "not handling edits can lead to visual inconsistencies in UI"]
|
/// Register a template with the renderer
|
||||||
pub struct Mutations<'a> {
|
fn register_template(&mut self, template: Template<'static>);
|
||||||
/// The ID of the subtree that these edits are targetting
|
|
||||||
pub subtree: usize,
|
|
||||||
|
|
||||||
/// The list of Scopes that were diffed, created, and removed during the Diff process.
|
/// Add these m children to the target element
|
||||||
pub dirty_scopes: FxHashSet<ScopeId>,
|
|
||||||
|
|
||||||
/// Any templates encountered while diffing the DOM.
|
|
||||||
///
|
///
|
||||||
/// These must be loaded into a cache before applying the edits
|
/// Id: The ID of the element being mounted to
|
||||||
pub templates: Vec<Template<'static>>,
|
/// M: The number of nodes on the stack to append to the target element
|
||||||
|
fn append_children(&mut self, id: ElementId, m: usize);
|
||||||
|
|
||||||
/// Any mutations required to patch the renderer to match the layout of the VirtualDom
|
/// Assign the element at the given path the target ElementId.
|
||||||
pub edits: Vec<Mutation<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Mutations<'a> {
|
|
||||||
/// Rewrites IDs to just be "template", so you can compare the mutations
|
|
||||||
///
|
///
|
||||||
/// Used really only for testing
|
/// The path is in the form of a list of indices based on children. Templates cannot have more than 255 children per
|
||||||
pub fn santize(mut self) -> Self {
|
/// element, hence the use of a single byte.
|
||||||
for edit in self.edits.iter_mut() {
|
///
|
||||||
if let Mutation::LoadTemplate { name, .. } = edit {
|
/// Path: The path of the child of the topmost node on the stack. A path of `[]` represents the topmost node. A path of `[0]` represents the first child. `[0,1,2]` represents 1st child's 2nd child's 3rd child.
|
||||||
*name = "template"
|
/// Id: The ID we're assigning to this element/placeholder. This will be used later to modify the element or replace it with another element.
|
||||||
}
|
fn assign_node_id(&mut self, path: &'static [u8], id: ElementId);
|
||||||
}
|
|
||||||
|
|
||||||
self
|
/// Create a placeholder in the DOM that we will use later.
|
||||||
}
|
///
|
||||||
|
/// Dioxus currently requires the use of placeholders to maintain a re-entrance point for things like list diffing
|
||||||
|
///
|
||||||
|
/// Id: The ID we're assigning to this element/placeholder. This will be used later to modify the element or replace it with another element.
|
||||||
|
fn create_placeholder(&mut self, id: ElementId);
|
||||||
|
|
||||||
/// Push a new mutation into the dom_edits list
|
/// Create a node specifically for text with the given value
|
||||||
pub(crate) fn push(&mut self, mutation: Mutation<'static>) {
|
///
|
||||||
self.edits.push(mutation)
|
/// Value: The text content of this text node
|
||||||
}
|
/// Id: The ID we're assigning to this specific text nodes. This will be used later to modify the element or replace it with another element.
|
||||||
|
fn create_text_node(&mut self, value: &str, id: ElementId);
|
||||||
|
|
||||||
|
/// Hydrate an existing text node at the given path with the given text.
|
||||||
|
///
|
||||||
|
/// Assign this text node the given ID since we will likely need to modify this text at a later point
|
||||||
|
///
|
||||||
|
/// Path: The path of the child of the topmost node on the stack. A path of `[]` represents the topmost node. A path of `[0]` represents the first child. `[0,1,2]` represents 1st child's 2nd child's 3rd child.
|
||||||
|
/// Value: The value of the textnode that we want to set the placeholder with
|
||||||
|
/// Id: The ID we're assigning to this specific text nodes. This will be used later to modify the element or replace it with another element.
|
||||||
|
fn hydrate_text_node(&mut self, path: &'static [u8], value: &str, id: ElementId);
|
||||||
|
|
||||||
|
/// Load and clone an existing node from a template saved under that specific name
|
||||||
|
///
|
||||||
|
/// Dioxus guarantees that the renderer will have already been provided the template.
|
||||||
|
/// When the template is picked up in the template list, it should be saved under its "name" - here, the name
|
||||||
|
///
|
||||||
|
/// Name: The unique "name" of the template based on the template location. When paired with `rsx!`, this is autogenerated
|
||||||
|
/// Index: The index root we loading from the template. The template is stored as a list of nodes. This index represents the position of that root
|
||||||
|
/// Id: The ID we're assigning to this element being loaded from the template (This will be used later to move the element around in lists)
|
||||||
|
fn load_template(&mut self, name: &'static str, index: usize, id: ElementId);
|
||||||
|
|
||||||
|
/// Replace the target element (given by its ID) with the topmost m nodes on the stack
|
||||||
|
///
|
||||||
|
/// id: The ID of the node we're going to replace with new nodes
|
||||||
|
/// m: The number of nodes on the stack to replace the target element with
|
||||||
|
fn replace_node_with(&mut self, id: ElementId, m: usize);
|
||||||
|
|
||||||
|
/// Replace an existing element in the template at the given path with the m nodes on the stack
|
||||||
|
///
|
||||||
|
/// Path: The path of the child of the topmost node on the stack. A path of `[]` represents the topmost node. A path of `[0]` represents the first child. `[0,1,2]` represents 1st child's 2nd child's 3rd child.
|
||||||
|
/// M: The number of nodes on the stack to replace the target element with
|
||||||
|
fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize);
|
||||||
|
|
||||||
|
/// Insert a number of nodes after a given node.
|
||||||
|
///
|
||||||
|
/// Id: The ID of the node to insert after.
|
||||||
|
/// M: The number of nodes on the stack to insert after the target node.
|
||||||
|
fn insert_nodes_after(&mut self, id: ElementId, m: usize);
|
||||||
|
|
||||||
|
/// Insert a number of nodes before a given node.
|
||||||
|
///
|
||||||
|
/// Id: The ID of the node to insert before.
|
||||||
|
/// M: The number of nodes on the stack to insert before the target node.
|
||||||
|
fn insert_nodes_before(&mut self, id: ElementId, m: usize);
|
||||||
|
|
||||||
|
/// Set the value of a node's attribute.
|
||||||
|
///
|
||||||
|
/// Name: The name of the attribute to set.
|
||||||
|
/// NS: The (optional) namespace of the attribute. For instance, "style" is in the "style" namespace.
|
||||||
|
/// Value: The value of the attribute.
|
||||||
|
/// Id: The ID of the node to set the attribute of.
|
||||||
|
fn set_attribute(
|
||||||
|
&mut self,
|
||||||
|
name: &'static str,
|
||||||
|
ns: Option<&'static str>,
|
||||||
|
value: &AttributeValue,
|
||||||
|
id: ElementId,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Set the text content of a node.
|
||||||
|
///
|
||||||
|
/// Value: The textcontent of the node
|
||||||
|
/// Id: The ID of the node to set the textcontent of.
|
||||||
|
fn set_node_text(&mut self, value: &str, id: ElementId);
|
||||||
|
|
||||||
|
/// Create a new Event Listener.
|
||||||
|
///
|
||||||
|
/// Name: The name of the event to listen for.
|
||||||
|
/// Id: The ID of the node to attach the listener to.
|
||||||
|
fn create_event_listener(&mut self, name: &'static str, id: ElementId);
|
||||||
|
|
||||||
|
/// Remove an existing Event Listener.
|
||||||
|
///
|
||||||
|
/// Name: The name of the event to remove.
|
||||||
|
/// Id: The ID of the node to remove.
|
||||||
|
fn remove_event_listener(&mut self, name: &'static str, id: ElementId);
|
||||||
|
|
||||||
|
/// Remove a particular node from the DOM
|
||||||
|
///
|
||||||
|
/// Id: The ID of the node to remove.
|
||||||
|
fn remove_node(&mut self, id: ElementId);
|
||||||
|
|
||||||
|
/// Push the given root node onto our stack.
|
||||||
|
///
|
||||||
|
/// Id: The ID of the root node to push.
|
||||||
|
fn push_root(&mut self, id: ElementId) {}
|
||||||
|
|
||||||
|
/// Swap to a new subtree
|
||||||
|
fn swap_subtree(&mut self, subtree_index: usize) {}
|
||||||
|
|
||||||
|
/// Mark a scope as dirty
|
||||||
|
fn mark_scope_dirty(&mut self, scope_id: ScopeId) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `Mutation` represents a single instruction for the renderer to use to modify the UI tree to match the state
|
/// A `Mutation` represents a single instruction for the renderer to use to modify the UI tree to match the state
|
||||||
@@ -56,7 +142,7 @@ impl<'a> Mutations<'a> {
|
|||||||
///
|
///
|
||||||
/// These edits can be serialized and sent over the network or through any interface
|
/// These edits can be serialized and sent over the network or through any interface
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Mutation<'a> {
|
pub enum Mutation {
|
||||||
/// Add these m children to the target element
|
/// Add these m children to the target element
|
||||||
AppendChildren {
|
AppendChildren {
|
||||||
/// The ID of the element being mounted to
|
/// The ID of the element being mounted to
|
||||||
@@ -98,7 +184,7 @@ pub enum Mutation<'a> {
|
|||||||
/// Create a node specifically for text with the given value
|
/// Create a node specifically for text with the given value
|
||||||
CreateTextNode {
|
CreateTextNode {
|
||||||
/// The text content of this text node
|
/// The text content of this text node
|
||||||
value: &'a str,
|
value: String,
|
||||||
|
|
||||||
/// The ID we're assigning to this specific text nodes
|
/// The ID we're assigning to this specific text nodes
|
||||||
///
|
///
|
||||||
@@ -117,7 +203,7 @@ pub enum Mutation<'a> {
|
|||||||
path: &'static [u8],
|
path: &'static [u8],
|
||||||
|
|
||||||
/// The value of the textnode that we want to set the placeholder with
|
/// The value of the textnode that we want to set the placeholder with
|
||||||
value: &'a str,
|
value: String,
|
||||||
|
|
||||||
/// The ID we're assigning to this specific text nodes
|
/// The ID we're assigning to this specific text nodes
|
||||||
///
|
///
|
||||||
@@ -186,23 +272,23 @@ pub enum Mutation<'a> {
|
|||||||
/// Set the value of a node's attribute.
|
/// Set the value of a node's attribute.
|
||||||
SetAttribute {
|
SetAttribute {
|
||||||
/// The name of the attribute to set.
|
/// The name of the attribute to set.
|
||||||
name: &'a str,
|
name: &'static str,
|
||||||
|
|
||||||
/// The value of the attribute.
|
|
||||||
value: &'a AttributeValue,
|
|
||||||
|
|
||||||
/// The ID of the node to set the attribute of.
|
|
||||||
id: ElementId,
|
|
||||||
|
|
||||||
/// The (optional) namespace of the attribute.
|
/// The (optional) namespace of the attribute.
|
||||||
/// For instance, "style" is in the "style" namespace.
|
/// For instance, "style" is in the "style" namespace.
|
||||||
ns: Option<&'a str>,
|
ns: Option<&'static str>,
|
||||||
|
|
||||||
|
/// The value of the attribute.
|
||||||
|
value: AttributeValue,
|
||||||
|
|
||||||
|
/// The ID of the node to set the attribute of.
|
||||||
|
id: ElementId,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Set the textcontent of a node.
|
/// Set the textcontent of a node.
|
||||||
SetText {
|
SetText {
|
||||||
/// The textcontent of the node
|
/// The textcontent of the node
|
||||||
value: &'a str,
|
value: String,
|
||||||
|
|
||||||
/// The ID of the node to set the textcontent of.
|
/// The ID of the node to set the textcontent of.
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
@@ -211,7 +297,7 @@ pub enum Mutation<'a> {
|
|||||||
/// Create a new Event Listener.
|
/// Create a new Event Listener.
|
||||||
NewEventListener {
|
NewEventListener {
|
||||||
/// The name of the event to listen for.
|
/// The name of the event to listen for.
|
||||||
name: &'a str,
|
name: String,
|
||||||
|
|
||||||
/// The ID of the node to attach the listener to.
|
/// The ID of the node to attach the listener to.
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
@@ -220,7 +306,7 @@ pub enum Mutation<'a> {
|
|||||||
/// Remove an existing Event Listener.
|
/// Remove an existing Event Listener.
|
||||||
RemoveEventListener {
|
RemoveEventListener {
|
||||||
/// The name of the event to remove.
|
/// The name of the event to remove.
|
||||||
name: &'a str,
|
name: String,
|
||||||
|
|
||||||
/// The ID of the node to remove.
|
/// The ID of the node to remove.
|
||||||
id: ElementId,
|
id: ElementId,
|
||||||
@@ -238,3 +324,186 @@ pub enum Mutation<'a> {
|
|||||||
id: ElementId,
|
id: ElementId,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A static list of mutations that can be applied to the DOM. Note: this list does not contain any `Any` attribute values
|
||||||
|
pub struct MutationsVec {
|
||||||
|
/// The list of Scopes that were diffed, created, and removed during the Diff process.
|
||||||
|
pub dirty_scopes: FxHashSet<ScopeId>,
|
||||||
|
|
||||||
|
/// Any templates encountered while diffing the DOM.
|
||||||
|
///
|
||||||
|
/// These must be loaded into a cache before applying the edits
|
||||||
|
pub templates: Vec<Template<'static>>,
|
||||||
|
|
||||||
|
/// Any mutations required to patch the renderer to match the layout of the VirtualDom
|
||||||
|
pub edits: Vec<Mutation>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MutationsVec {
|
||||||
|
/// Rewrites IDs to just be "template", so you can compare the mutations
|
||||||
|
///
|
||||||
|
/// Used really only for testing
|
||||||
|
pub fn santize(mut self) -> Self {
|
||||||
|
for edit in self.edits.iter_mut() {
|
||||||
|
if let Mutation::LoadTemplate { name, .. } = edit {
|
||||||
|
*name = "template"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WriteMutations for MutationsVec {
|
||||||
|
fn register_template(&mut self, template: Template<'static>) {
|
||||||
|
self.templates.push(template)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_children(&mut self, id: ElementId, m: usize) {
|
||||||
|
self.edits.push(Mutation::AppendChildren { id, m })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assign_node_id(&mut self, path: &'static [u8], id: ElementId) {
|
||||||
|
self.edits.push(Mutation::AssignId { path, id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_placeholder(&mut self, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::CreatePlaceholder { id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_text_node(&mut self, value: &str, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::CreateTextNode {
|
||||||
|
value: value.into(),
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hydrate_text_node(&mut self, path: &'static [u8], value: &str, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::HydrateText {
|
||||||
|
path,
|
||||||
|
value: value.into(),
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_template(&mut self, name: &'static str, index: usize, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::LoadTemplate { name, index, id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_node_with(&mut self, id: ElementId, m: usize) {
|
||||||
|
self.edits.push(Mutation::ReplaceWith { id, m })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize) {
|
||||||
|
self.edits.push(Mutation::ReplacePlaceholder { path, m })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_nodes_after(&mut self, id: ElementId, m: usize) {
|
||||||
|
self.edits.push(Mutation::InsertAfter { id, m })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_nodes_before(&mut self, id: ElementId, m: usize) {
|
||||||
|
self.edits.push(Mutation::InsertBefore { id, m })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_attribute(
|
||||||
|
&mut self,
|
||||||
|
name: &'static str,
|
||||||
|
ns: Option<&'static str>,
|
||||||
|
value: &AttributeValue,
|
||||||
|
id: ElementId,
|
||||||
|
) {
|
||||||
|
self.edits.push(Mutation::SetAttribute {
|
||||||
|
name,
|
||||||
|
ns,
|
||||||
|
value: match value {
|
||||||
|
AttributeValue::Text(s) => AttributeValue::Text(s.clone()),
|
||||||
|
AttributeValue::Bool(b) => AttributeValue::Bool(*b),
|
||||||
|
AttributeValue::Float(n) => AttributeValue::Float(*n),
|
||||||
|
AttributeValue::Int(n) => AttributeValue::Int(*n),
|
||||||
|
AttributeValue::None => AttributeValue::None,
|
||||||
|
_ => panic!("Cannot serialize attribute value"),
|
||||||
|
},
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_node_text(&mut self, value: &str, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::SetText {
|
||||||
|
value: value.into(),
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_event_listener(&mut self, name: &'static str, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::NewEventListener {
|
||||||
|
name: name.into(),
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_event_listener(&mut self, name: &'static str, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::RemoveEventListener {
|
||||||
|
name: name.into(),
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_node(&mut self, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::Remove { id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_root(&mut self, id: ElementId) {
|
||||||
|
self.edits.push(Mutation::PushRoot { id })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn swap_subtree(&mut self, _subtree_index: usize) {}
|
||||||
|
|
||||||
|
fn mark_scope_dirty(&mut self, scope_id: ScopeId) {
|
||||||
|
self.dirty_scopes.insert(scope_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A struct that ignores all mutations
|
||||||
|
pub struct NoOpMutations;
|
||||||
|
|
||||||
|
impl WriteMutations for NoOpMutations {
|
||||||
|
fn register_template(&mut self, template: Template<'static>) {}
|
||||||
|
|
||||||
|
fn append_children(&mut self, id: ElementId, m: usize) {}
|
||||||
|
|
||||||
|
fn assign_node_id(&mut self, path: &'static [u8], id: ElementId) {}
|
||||||
|
|
||||||
|
fn create_placeholder(&mut self, id: ElementId) {}
|
||||||
|
|
||||||
|
fn create_text_node(&mut self, value: &str, id: ElementId) {}
|
||||||
|
|
||||||
|
fn hydrate_text_node(&mut self, path: &'static [u8], value: &str, id: ElementId) {}
|
||||||
|
|
||||||
|
fn load_template(&mut self, name: &'static str, index: usize, id: ElementId) {}
|
||||||
|
|
||||||
|
fn replace_node_with(&mut self, id: ElementId, m: usize) {}
|
||||||
|
|
||||||
|
fn replace_placeholder_with_nodes(&mut self, path: &'static [u8], m: usize) {}
|
||||||
|
|
||||||
|
fn insert_nodes_after(&mut self, id: ElementId, m: usize) {}
|
||||||
|
|
||||||
|
fn insert_nodes_before(&mut self, id: ElementId, m: usize) {}
|
||||||
|
|
||||||
|
fn set_attribute(
|
||||||
|
&mut self,
|
||||||
|
name: &'static str,
|
||||||
|
ns: Option<&'static str>,
|
||||||
|
value: &AttributeValue,
|
||||||
|
id: ElementId,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_node_text(&mut self, value: &str, id: ElementId) {}
|
||||||
|
|
||||||
|
fn create_event_listener(&mut self, name: &'static str, id: ElementId) {}
|
||||||
|
|
||||||
|
fn remove_event_listener(&mut self, name: &'static str, id: ElementId) {}
|
||||||
|
|
||||||
|
fn remove_node(&mut self, id: ElementId) {}
|
||||||
|
}
|
||||||
@@ -5,7 +5,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
any_props::{BoxedAnyProps, VProps},
|
any_props::{BoxedAnyProps, VProps},
|
||||||
arena::ElementId,
|
arena::ElementId,
|
||||||
innerlude::{DirtyScope, ElementRef, ErrorBoundary, Mutations, Scheduler, SchedulerMsg},
|
innerlude::{
|
||||||
|
DirtyScope, ElementRef, ErrorBoundary, Mutations, NoOpMutations, Scheduler, SchedulerMsg,
|
||||||
|
WriteMutations,
|
||||||
|
},
|
||||||
mutations::Mutation,
|
mutations::Mutation,
|
||||||
nodes::RenderReturn,
|
nodes::RenderReturn,
|
||||||
nodes::{Template, TemplateId},
|
nodes::{Template, TemplateId},
|
||||||
@@ -188,8 +191,6 @@ pub struct VirtualDom {
|
|||||||
// The element ids that are used in the renderer
|
// The element ids that are used in the renderer
|
||||||
pub(crate) elements: Slab<Option<ElementRef>>,
|
pub(crate) elements: Slab<Option<ElementRef>>,
|
||||||
|
|
||||||
pub(crate) mutations: Mutations<'static>,
|
|
||||||
|
|
||||||
pub(crate) runtime: Rc<Runtime>,
|
pub(crate) runtime: Rc<Runtime>,
|
||||||
|
|
||||||
// Currently suspended scopes
|
// Currently suspended scopes
|
||||||
@@ -263,7 +264,6 @@ impl VirtualDom {
|
|||||||
dirty_scopes: Default::default(),
|
dirty_scopes: Default::default(),
|
||||||
templates: Default::default(),
|
templates: Default::default(),
|
||||||
elements: Default::default(),
|
elements: Default::default(),
|
||||||
mutations: Mutations::default(),
|
|
||||||
suspended_scopes: Default::default(),
|
suspended_scopes: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -544,38 +544,33 @@ impl VirtualDom {
|
|||||||
///
|
///
|
||||||
/// apply_edits(edits);
|
/// apply_edits(edits);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn rebuild(&mut self) -> Mutations {
|
pub fn rebuild(&mut self, to: &mut impl WriteMutations) {
|
||||||
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
||||||
match unsafe { self.run_scope(ScopeId::ROOT) } {
|
match unsafe { self.run_scope(ScopeId::ROOT) } {
|
||||||
// Rebuilding implies we append the created elements to the root
|
// Rebuilding implies we append the created elements to the root
|
||||||
RenderReturn::Ready(node) => {
|
RenderReturn::Ready(node) => {
|
||||||
let m = self.create_scope(ScopeId::ROOT, &node);
|
let m = self.create_scope(ScopeId::ROOT, &node);
|
||||||
self.mutations.edits.push(Mutation::AppendChildren {
|
to.append_children(ElementId(0), m);
|
||||||
id: ElementId(0),
|
|
||||||
m,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// If an error occurs, we should try to render the default error component and context where the error occured
|
// If an error occurs, we should try to render the default error component and context where the error occured
|
||||||
RenderReturn::Aborted(placeholder) => {
|
RenderReturn::Aborted(placeholder) => {
|
||||||
tracing::debug!("Ran into suspended or aborted scope during rebuild");
|
tracing::debug!("Ran into suspended or aborted scope during rebuild");
|
||||||
let id = self.next_element();
|
let id = self.next_element();
|
||||||
placeholder.id.set(Some(id));
|
placeholder.id.set(Some(id));
|
||||||
self.mutations.push(Mutation::CreatePlaceholder { id });
|
to.create_placeholder(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.finalize()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render whatever the VirtualDom has ready as fast as possible without requiring an executor to progress
|
/// Render whatever the VirtualDom has ready as fast as possible without requiring an executor to progress
|
||||||
/// suspended subtrees.
|
/// suspended subtrees.
|
||||||
pub fn render_immediate(&mut self) -> Mutations {
|
pub fn render_immediate(&mut self, to: &mut impl WriteMutations) {
|
||||||
// Build a waker that won't wake up since our deadline is already expired when it's polled
|
// Build a waker that won't wake up since our deadline is already expired when it's polled
|
||||||
let waker = futures_util::task::noop_waker();
|
let waker = futures_util::task::noop_waker();
|
||||||
let mut cx = std::task::Context::from_waker(&waker);
|
let mut cx = std::task::Context::from_waker(&waker);
|
||||||
|
|
||||||
// Now run render with deadline but dont even try to poll any async tasks
|
// Now run render with deadline but dont even try to poll any async tasks
|
||||||
let fut = self.render_with_deadline(std::future::ready(()));
|
let fut = self.render_with_deadline(std::future::ready(()), to);
|
||||||
pin_mut!(fut);
|
pin_mut!(fut);
|
||||||
|
|
||||||
// The root component is not allowed to be async
|
// The root component is not allowed to be async
|
||||||
@@ -596,7 +591,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
self.wait_for_work().await;
|
self.wait_for_work().await;
|
||||||
|
|
||||||
_ = self.render_immediate();
|
_ = self.render_immediate(&mut NoOpMutations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -605,7 +600,11 @@ impl VirtualDom {
|
|||||||
/// It's generally a good idea to put some sort of limit on the suspense process in case a future is having issues.
|
/// It's generally a good idea to put some sort of limit on the suspense process in case a future is having issues.
|
||||||
///
|
///
|
||||||
/// If no suspense trees are present
|
/// If no suspense trees are present
|
||||||
pub async fn render_with_deadline(&mut self, deadline: impl Future<Output = ()>) -> Mutations {
|
pub async fn render_with_deadline(
|
||||||
|
&mut self,
|
||||||
|
deadline: impl Future<Output = ()>,
|
||||||
|
to: &mut impl WriteMutations,
|
||||||
|
) {
|
||||||
pin_mut!(deadline);
|
pin_mut!(deadline);
|
||||||
|
|
||||||
self.process_events();
|
self.process_events();
|
||||||
@@ -625,7 +624,7 @@ impl VirtualDom {
|
|||||||
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
||||||
// Run the scope and get the mutations
|
// Run the scope and get the mutations
|
||||||
let new_nodes = self.run_scope(dirty.id);
|
let new_nodes = self.run_scope(dirty.id);
|
||||||
self.diff_scope(dirty.id, new_nodes);
|
self.diff_scope(dirty.id, new_nodes, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -645,16 +644,11 @@ impl VirtualDom {
|
|||||||
if let Either::Left((_, _)) = select(&mut deadline, pinned).await {
|
if let Either::Left((_, _)) = select(&mut deadline, pinned).await {
|
||||||
// release the borrowed
|
// release the borrowed
|
||||||
drop(work);
|
drop(work);
|
||||||
return self.finalize();
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Swap the current mutations with a new
|
|
||||||
fn finalize(&mut self) -> Mutations {
|
|
||||||
std::mem::take(&mut self.mutations)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the current runtime
|
/// Get the current runtime
|
||||||
pub fn runtime(&self) -> Rc<Runtime> {
|
pub fn runtime(&self) -> Rc<Runtime> {
|
||||||
self.runtime.clone()
|
self.runtime.clone()
|
||||||
|
|||||||
Reference in new issue
Block a user