2022-11-09 18:58:11 +00:00
|
|
|
use crate::factory::RenderReturn;
|
|
|
|
use crate::innerlude::{Mutations, SuspenseContext};
|
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;
|
2022-11-03 08:24:20 +00:00
|
|
|
use crate::nodes::{DynamicNode, TemplateNode};
|
2022-11-09 03:39:37 +00:00
|
|
|
use crate::virtual_dom::VirtualDom;
|
2022-11-09 18:58:11 +00:00
|
|
|
use crate::{AttributeValue, ElementId, ScopeId, TemplateAttribute};
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
impl VirtualDom {
|
2022-11-09 18:58:11 +00:00
|
|
|
pub(crate) fn create_scope<'a>(
|
|
|
|
&mut self,
|
|
|
|
scope: ScopeId,
|
|
|
|
mutations: &mut Mutations<'a>,
|
|
|
|
template: &'a VNode<'a>,
|
|
|
|
) -> usize {
|
|
|
|
self.scope_stack.push(scope);
|
|
|
|
let out = self.create(mutations, template);
|
|
|
|
self.scope_stack.pop();
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
/// Create this template and write its mutations
|
2022-11-09 18:58:11 +00:00
|
|
|
pub(crate) fn create<'a>(
|
|
|
|
&mut self,
|
|
|
|
mutations: &mut Mutations<'a>,
|
|
|
|
template: &'a VNode<'a>,
|
|
|
|
) -> usize {
|
|
|
|
// The best renderers will have templates prehydrated and registered
|
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 {
|
2022-11-09 03:39:37 +00:00
|
|
|
let mutations = &mut mutations.template_mutations;
|
2022-11-02 01:42:29 +00:00
|
|
|
self.create_static_node(mutations, template, node);
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:39:37 +00:00
|
|
|
mutations.template_mutations.push(SaveTemplate {
|
2022-11-02 01:42:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
// Walk the roots, creating nodes and assigning IDs
|
2022-10-28 04:58:47 +00:00
|
|
|
// todo: adjust dynamic nodes to be in the order of roots and then leaves (ie BFS)
|
2022-11-03 08:24:20 +00:00
|
|
|
let mut dynamic_attrs = template.template.attr_paths.iter().enumerate().peekable();
|
|
|
|
let mut dynamic_nodes = template.template.node_paths.iter().enumerate().peekable();
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
let mut on_stack = 0;
|
|
|
|
for (root_idx, root) in template.template.roots.iter().enumerate() {
|
|
|
|
on_stack += match root {
|
2022-11-09 18:58:11 +00:00
|
|
|
TemplateNode::Element { .. }
|
|
|
|
| TemplateNode::Text(_)
|
|
|
|
| TemplateNode::DynamicText { .. } => {
|
2022-11-02 01:42:29 +00:00
|
|
|
mutations.push(LoadTemplate {
|
|
|
|
name: template.template.id,
|
|
|
|
index: root_idx,
|
|
|
|
});
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
TemplateNode::Dynamic(id) => {
|
2022-11-03 08:24:20 +00:00
|
|
|
self.create_dynamic_node(mutations, template, &template.dynamic_nodes[*id], *id)
|
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
|
2022-11-09 04:06:39 +00:00
|
|
|
while let Some((mut attr_id, path)) =
|
|
|
|
dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8)
|
|
|
|
{
|
|
|
|
let id = self.next_element(template);
|
|
|
|
mutations.push(AssignId {
|
|
|
|
path: &path[1..],
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
loop {
|
2022-11-09 18:58:11 +00:00
|
|
|
let attribute = &template.dynamic_attrs[attr_id];
|
|
|
|
attribute.mounted_element.set(id);
|
2022-11-09 04:06:39 +00:00
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
match attribute.value {
|
2022-11-09 04:06:39 +00:00
|
|
|
AttributeValue::Text(value) => mutations.push(SetAttribute {
|
2022-11-09 18:58:11 +00:00
|
|
|
name: attribute.name,
|
2022-11-03 00:29:18 +00:00
|
|
|
value,
|
|
|
|
id,
|
2022-11-09 04:06:39 +00:00
|
|
|
}),
|
2022-11-09 18:58:11 +00:00
|
|
|
AttributeValue::Listener(_) => todo!("create listener attributes"),
|
2022-11-09 04:06:39 +00:00
|
|
|
AttributeValue::Float(_) => todo!(),
|
|
|
|
AttributeValue::Int(_) => todo!(),
|
|
|
|
AttributeValue::Bool(_) => todo!(),
|
|
|
|
AttributeValue::Any(_) => todo!(),
|
|
|
|
AttributeValue::None => todo!(),
|
2022-11-03 00:29:18 +00:00
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
// Only push the dynamic attributes forward if they match the current path (same element)
|
|
|
|
match dynamic_attrs.next_if(|(_, p)| *p == path) {
|
|
|
|
Some((next_attr_id, _)) => attr_id = next_attr_id,
|
|
|
|
None => break,
|
2022-11-03 00:29:18 +00:00
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
// We're on top of a node that has a dynamic child for a descendant
|
2022-11-06 22:28:41 +00:00
|
|
|
// Skip any node that's a root
|
|
|
|
while let Some((idx, path)) =
|
|
|
|
dynamic_nodes.next_if(|(_, p)| p.len() > 1 && p[0] == root_idx as u8)
|
|
|
|
{
|
2022-11-03 08:24:20 +00:00
|
|
|
let node = &template.dynamic_nodes[idx];
|
|
|
|
let m = self.create_dynamic_node(mutations, template, node, idx);
|
2022-11-04 03:56:31 +00:00
|
|
|
if m > 0 {
|
|
|
|
mutations.push(ReplacePlaceholder {
|
|
|
|
m,
|
|
|
|
path: &path[1..],
|
|
|
|
});
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on_stack
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
pub(crate) fn create_static_node<'a>(
|
2022-11-02 01:42:29 +00:00
|
|
|
&mut self,
|
|
|
|
mutations: &mut Vec<Mutation<'a>>,
|
|
|
|
template: &'a VNode<'a>,
|
|
|
|
node: &'a TemplateNode<'static>,
|
|
|
|
) {
|
|
|
|
match *node {
|
2022-11-03 00:29:18 +00:00
|
|
|
// Todo: create the children's template
|
2022-11-04 00:34:42 +00:00
|
|
|
TemplateNode::Dynamic(_) => mutations.push(CreatePlaceholder { id: ElementId(0) }),
|
2022-11-02 01:42:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
pub(crate) fn create_dynamic_node<'a>(
|
2022-10-28 04:58:47 +00:00
|
|
|
&mut self,
|
2022-11-09 18:58:11 +00:00
|
|
|
mutations: &mut Mutations<'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>,
|
2022-11-03 08:24:20 +00:00
|
|
|
idx: usize,
|
2022-10-28 04:58:47 +00:00
|
|
|
) -> usize {
|
2022-11-03 08:24:20 +00:00
|
|
|
match &node {
|
|
|
|
DynamicNode::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,
|
2022-11-03 08:24:20 +00:00
|
|
|
path: &template.template.node_paths[idx][1..],
|
2022-10-28 04:58:47 +00:00
|
|
|
value,
|
|
|
|
});
|
|
|
|
1
|
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
|
2022-11-04 05:30:26 +00:00
|
|
|
DynamicNode::Component {
|
|
|
|
props, placeholder, ..
|
|
|
|
} => {
|
2022-11-09 18:58:11 +00:00
|
|
|
let scope = self
|
|
|
|
.new_scope(unsafe { std::mem::transmute(props.get()) })
|
|
|
|
.id;
|
|
|
|
let return_nodes = unsafe { self.run_scope(scope).extend_lifetime_ref() };
|
2022-11-04 05:30:26 +00:00
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
match return_nodes {
|
2022-11-09 03:39:37 +00:00
|
|
|
RenderReturn::Sync(None) | RenderReturn::Async(_) => {
|
2022-11-04 05:30:26 +00:00
|
|
|
let new_id = self.next_element(template);
|
|
|
|
placeholder.set(Some(new_id));
|
2022-11-09 18:58:11 +00:00
|
|
|
self.scopes[scope.0].placeholder.set(Some(new_id));
|
2022-11-04 05:30:26 +00:00
|
|
|
mutations.push(AssignId {
|
|
|
|
id: new_id,
|
|
|
|
path: &template.template.node_paths[idx][1..],
|
|
|
|
});
|
|
|
|
0
|
|
|
|
}
|
2022-11-08 06:55:22 +00:00
|
|
|
|
2022-11-09 03:39:37 +00:00
|
|
|
RenderReturn::Sync(Some(template)) => {
|
|
|
|
let mutations_to_this_point = mutations.len();
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
self.scope_stack.push(scope);
|
2022-11-09 03:39:37 +00:00
|
|
|
let mut created = self.create(mutations, template);
|
|
|
|
self.scope_stack.pop();
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
if !self.collected_leaves.is_empty() {
|
2022-11-09 03:39:37 +00:00
|
|
|
if let Some(boundary) =
|
2022-11-09 18:58:11 +00:00
|
|
|
self.scopes[scope.0].has_context::<SuspenseContext>()
|
2022-11-09 03:39:37 +00:00
|
|
|
{
|
|
|
|
let mut boundary_mut = boundary.borrow_mut();
|
|
|
|
let split_off = mutations.split_off(mutations_to_this_point);
|
|
|
|
|
|
|
|
let split_off = unsafe { std::mem::transmute(split_off) };
|
|
|
|
|
|
|
|
boundary_mut.mutations.mutations = split_off;
|
2022-11-09 18:58:11 +00:00
|
|
|
boundary_mut
|
|
|
|
.waiting_on
|
|
|
|
.extend(self.collected_leaves.drain(..));
|
2022-11-09 03:39:37 +00:00
|
|
|
|
|
|
|
// Since this is a boundary, use it as a placeholder
|
|
|
|
let new_id = self.next_element(template);
|
|
|
|
placeholder.set(Some(new_id));
|
2022-11-09 18:58:11 +00:00
|
|
|
self.scopes[scope.0].placeholder.set(Some(new_id));
|
2022-11-09 03:39:37 +00:00
|
|
|
mutations.push(AssignId {
|
|
|
|
id: new_id,
|
|
|
|
path: &template.template.node_paths[idx][1..],
|
|
|
|
});
|
|
|
|
created = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle any waiting on futures accumulated by async calls down the tree
|
|
|
|
// if this is a boundary, we split off the tree
|
|
|
|
created
|
2022-11-04 03:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 00:34:42 +00:00
|
|
|
DynamicNode::Fragment(children) => children
|
|
|
|
.iter()
|
|
|
|
.fold(0, |acc, child| acc + self.create(mutations, child)),
|
|
|
|
|
|
|
|
DynamicNode::Placeholder(_) => {
|
|
|
|
let id = self.next_element(template);
|
|
|
|
mutations.push(CreatePlaceholder { id });
|
|
|
|
1
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|