2022-10-28 04:58:47 +00:00
|
|
|
use crate::mutations::Mutation;
|
2022-11-02 01:42:29 +00:00
|
|
|
use crate::mutations::Mutation::*;
|
|
|
|
use crate::nodes::VNode;
|
|
|
|
use crate::nodes::{DynamicNode, DynamicNodeKind, TemplateNode};
|
|
|
|
use crate::virtualdom::VirtualDom;
|
2022-11-02 08:00:37 +00:00
|
|
|
use crate::TemplateAttribute;
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
impl VirtualDom {
|
|
|
|
/// Create this template and write its mutations
|
|
|
|
pub fn create<'a>(
|
|
|
|
&mut self,
|
|
|
|
mutations: &mut Vec<Mutation<'a>>,
|
2022-11-02 01:42:29 +00:00
|
|
|
template: &'a VNode<'a>,
|
2022-10-28 04:58:47 +00:00
|
|
|
) -> usize {
|
2022-11-02 01:42:29 +00:00
|
|
|
// The best renderers will have templates prehydrated
|
2022-10-28 04:58:47 +00:00
|
|
|
// Just in case, let's create the template using instructions anyways
|
|
|
|
if !self.templates.contains_key(&template.template.id) {
|
2022-11-02 01:42:29 +00:00
|
|
|
for node in template.template.roots {
|
|
|
|
self.create_static_node(mutations, template, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutations.push(SaveTemplate {
|
|
|
|
name: template.template.id,
|
|
|
|
m: template.template.roots.len(),
|
|
|
|
});
|
|
|
|
|
|
|
|
self.templates
|
|
|
|
.insert(template.template.id, template.template.clone());
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the roots backwards, creating nodes and assigning IDs
|
|
|
|
// todo: adjust dynamic nodes to be in the order of roots and then leaves (ie BFS)
|
|
|
|
let mut dynamic_attrs = template.dynamic_attrs.iter().peekable();
|
|
|
|
let mut dynamic_nodes = template.dynamic_nodes.iter().peekable();
|
|
|
|
|
|
|
|
let mut on_stack = 0;
|
|
|
|
for (root_idx, root) in template.template.roots.iter().enumerate() {
|
|
|
|
on_stack += match root {
|
2022-11-02 01:42:29 +00:00
|
|
|
TemplateNode::Element { .. } | TemplateNode::Text(_) => {
|
|
|
|
mutations.push(LoadTemplate {
|
|
|
|
name: template.template.id,
|
|
|
|
index: root_idx,
|
|
|
|
});
|
|
|
|
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
TemplateNode::Dynamic(id) => {
|
|
|
|
self.create_dynamic_node(mutations, template, &template.dynamic_nodes[*id])
|
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
|
|
|
|
TemplateNode::DynamicText { .. } => 1,
|
2022-10-28 04:58:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// we're on top of a node that has a dynamic attribute for a descendant
|
|
|
|
// Set that attribute now before the stack gets in a weird state
|
|
|
|
while let Some(loc) = dynamic_attrs.next_if(|a| a.path[0] == root_idx as u8) {
|
|
|
|
// Attach all the elementIDs to the nodes with dynamic content
|
2022-11-02 01:42:29 +00:00
|
|
|
let id = self.next_element(template);
|
|
|
|
|
|
|
|
mutations.push(AssignId {
|
2022-10-28 04:58:47 +00:00
|
|
|
path: &loc.path[1..],
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
loc.mounted_element.set(id);
|
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
for attr in loc.attrs.iter() {
|
|
|
|
mutations.push(SetAttribute {
|
2022-10-28 04:58:47 +00:00
|
|
|
name: attr.name,
|
|
|
|
value: attr.value,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
// We're on top of a node that has a dynamic child for a descendant
|
2022-10-28 04:58:47 +00:00
|
|
|
while let Some(node) = dynamic_nodes.next_if(|f| f.path[0] == root_idx as u8) {
|
2022-11-02 01:42:29 +00:00
|
|
|
let m = self.create_dynamic_node(mutations, template, node);
|
|
|
|
mutations.push(ReplacePlaceholder {
|
|
|
|
m,
|
|
|
|
path: &node.path[1..],
|
|
|
|
});
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on_stack
|
|
|
|
}
|
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
fn create_static_node<'a>(
|
|
|
|
&mut self,
|
|
|
|
mutations: &mut Vec<Mutation<'a>>,
|
|
|
|
template: &'a VNode<'a>,
|
|
|
|
node: &'a TemplateNode<'static>,
|
|
|
|
) {
|
|
|
|
match *node {
|
|
|
|
TemplateNode::Dynamic(_) => mutations.push(CreatePlaceholder),
|
|
|
|
TemplateNode::Text(value) => mutations.push(CreateText { value }),
|
|
|
|
TemplateNode::DynamicText { .. } => mutations.push(CreateText {
|
|
|
|
value: "placeholder",
|
|
|
|
}),
|
|
|
|
TemplateNode::Element {
|
|
|
|
attrs,
|
|
|
|
children,
|
|
|
|
namespace,
|
|
|
|
tag,
|
|
|
|
} => {
|
|
|
|
let id = self.next_element(template);
|
|
|
|
|
|
|
|
mutations.push(CreateElement {
|
|
|
|
name: tag,
|
|
|
|
namespace,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
2022-11-02 08:00:37 +00:00
|
|
|
mutations.extend(attrs.into_iter().filter_map(|attr| match attr {
|
|
|
|
TemplateAttribute::Static { name, value, .. } => {
|
|
|
|
Some(SetAttribute { name, value, id })
|
|
|
|
}
|
|
|
|
_ => None,
|
2022-11-02 01:42:29 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
children
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|child| self.create_static_node(mutations, template, child));
|
|
|
|
|
|
|
|
mutations.push(AppendChildren { m: children.len() })
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_dynamic_node<'a>(
|
|
|
|
&mut self,
|
|
|
|
mutations: &mut Vec<Mutation<'a>>,
|
2022-11-02 01:42:29 +00:00
|
|
|
template: &'a VNode<'a>,
|
2022-10-28 04:58:47 +00:00
|
|
|
node: &'a DynamicNode<'a>,
|
|
|
|
) -> usize {
|
|
|
|
match &node.kind {
|
|
|
|
DynamicNodeKind::Text { id, value } => {
|
2022-11-02 01:42:29 +00:00
|
|
|
let new_id = self.next_element(template);
|
2022-10-28 04:58:47 +00:00
|
|
|
id.set(new_id);
|
2022-11-02 01:42:29 +00:00
|
|
|
mutations.push(HydrateText {
|
2022-10-28 04:58:47 +00:00
|
|
|
id: new_id,
|
|
|
|
path: &node.path[1..],
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
1
|
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
|
|
|
|
DynamicNodeKind::Component { props, .. } => {
|
|
|
|
let id = self.new_scope(*props);
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
let template = self.run_scope(id);
|
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
// shut up about lifetimes please, I know what I'm doing
|
|
|
|
let template: &VNode = unsafe { std::mem::transmute(template) };
|
|
|
|
|
|
|
|
self.scope_stack.push(id);
|
|
|
|
let created = self.create(mutations, template);
|
|
|
|
self.scope_stack.pop();
|
|
|
|
|
|
|
|
created
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicNodeKind::Fragment { children } => {
|
|
|
|
//
|
|
|
|
children
|
|
|
|
.iter()
|
|
|
|
.fold(0, |acc, child| acc + self.create(mutations, child))
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|