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-16 09:13:39 +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-11-16 07:31:23 +00:00
|
|
|
|
2022-11-16 02:32:48 +00:00
|
|
|
let cur_scope = self.scope_stack.last().copied().unwrap();
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-16 07:22:41 +00:00
|
|
|
println!("creating template: {:#?}", template);
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
let mut on_stack = 0;
|
2022-11-16 09:13:39 +00:00
|
|
|
for (root_idx, root) in template.template.roots.iter().enumerate() {
|
2022-10-28 04:58:47 +00:00
|
|
|
on_stack += match root {
|
2022-11-16 09:13:39 +00:00
|
|
|
TemplateNode::Element { .. } | TemplateNode::Text(_) => {
|
|
|
|
mutations.push(LoadTemplate {
|
|
|
|
name: template.template.id,
|
|
|
|
index: root_idx,
|
|
|
|
});
|
|
|
|
1
|
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
|
2022-11-16 07:22:41 +00:00
|
|
|
TemplateNode::DynamicText(id) | TemplateNode::Dynamic(id) => {
|
2022-11-16 09:13:39 +00:00
|
|
|
let dynamic_node = &template.dynamic_nodes[*id];
|
|
|
|
|
|
|
|
match dynamic_node {
|
|
|
|
DynamicNode::Fragment { .. } | DynamicNode::Component { .. } => self
|
|
|
|
.create_dynamic_node(
|
|
|
|
mutations,
|
|
|
|
template,
|
|
|
|
&template.dynamic_nodes[*id],
|
|
|
|
*id,
|
|
|
|
),
|
|
|
|
DynamicNode::Text {
|
|
|
|
id: slot, value, ..
|
|
|
|
} => {
|
|
|
|
let id = self.next_element(template);
|
|
|
|
slot.set(id);
|
|
|
|
mutations.push(CreateTextNode {
|
|
|
|
value: value.clone(),
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
1
|
|
|
|
}
|
|
|
|
DynamicNode::Placeholder(id) => {
|
|
|
|
let id = self.next_element(template);
|
|
|
|
mutations.push(CreatePlaceholder { id });
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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-16 02:32:48 +00:00
|
|
|
let attribute = template.dynamic_attrs.get(attr_id).unwrap();
|
2022-11-09 18:58:11 +00:00
|
|
|
attribute.mounted_element.set(id);
|
2022-11-09 04:06:39 +00:00
|
|
|
|
2022-11-16 02:32:48 +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-16 02:32:48 +00:00
|
|
|
value: *value,
|
2022-11-16 07:31:23 +00:00
|
|
|
ns: attribute.namespace,
|
2022-11-03 00:29:18 +00:00
|
|
|
id,
|
2022-11-09 04:06:39 +00:00
|
|
|
}),
|
2022-11-16 00:05:22 +00:00
|
|
|
AttributeValue::Bool(value) => mutations.push(SetBoolAttribute {
|
|
|
|
name: attribute.name,
|
2022-11-16 02:32:48 +00:00
|
|
|
value: *value,
|
|
|
|
id,
|
|
|
|
}),
|
|
|
|
AttributeValue::Listener(_) => mutations.push(NewEventListener {
|
|
|
|
event_name: attribute.name,
|
|
|
|
scope: cur_scope,
|
2022-11-16 00:05:22 +00:00
|
|
|
id,
|
|
|
|
}),
|
2022-11-09 04:06:39 +00:00
|
|
|
AttributeValue::Float(_) => todo!(),
|
|
|
|
AttributeValue::Int(_) => 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-16 09:13:39 +00:00
|
|
|
// todo:
|
|
|
|
//
|
|
|
|
// we walk the roots front to back when creating nodes, bur want to fill in the dynamic nodes
|
|
|
|
// back to front. This is because the indices would shift around because the paths become invalid
|
|
|
|
//
|
|
|
|
// We could easily implement this without the vec by walking the indicies forward
|
|
|
|
let mut queued_changes = vec![];
|
|
|
|
|
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 {
|
2022-11-16 09:13:39 +00:00
|
|
|
queued_changes.push(ReplacePlaceholder {
|
2022-11-04 03:56:31 +00:00
|
|
|
m,
|
|
|
|
path: &path[1..],
|
|
|
|
});
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
2022-11-16 09:13:39 +00:00
|
|
|
|
|
|
|
for change in queued_changes.into_iter().rev() {
|
|
|
|
mutations.push(change);
|
|
|
|
}
|
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-12 02:29:27 +00:00
|
|
|
TemplateNode::Dynamic(_) => {
|
|
|
|
let id = self.next_element(template);
|
|
|
|
mutations.push(CreatePlaceholder { id })
|
|
|
|
}
|
2022-11-16 09:13:39 +00:00
|
|
|
TemplateNode::Text(value) => mutations.push(CreateStaticText { value }),
|
|
|
|
TemplateNode::DynamicText { .. } => mutations.push(CreateStaticText {
|
2022-11-02 01:42:29 +00:00
|
|
|
value: "placeholder",
|
|
|
|
}),
|
2022-11-16 00:05:22 +00:00
|
|
|
|
2022-11-02 01:42:29 +00:00
|
|
|
TemplateNode::Element {
|
|
|
|
attrs,
|
|
|
|
children,
|
|
|
|
namespace,
|
|
|
|
tag,
|
2022-11-16 00:05:22 +00:00
|
|
|
inner_opt,
|
2022-11-02 01:42:29 +00:00
|
|
|
} => {
|
|
|
|
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 {
|
2022-11-16 07:31:23 +00:00
|
|
|
TemplateAttribute::Static {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
namespace,
|
|
|
|
..
|
|
|
|
} => Some(SetAttribute {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
id,
|
|
|
|
ns: *namespace,
|
|
|
|
}),
|
2022-11-02 08:00:37 +00:00
|
|
|
_ => None,
|
2022-11-02 01:42:29 +00:00
|
|
|
}));
|
|
|
|
|
2022-11-16 00:05:22 +00:00
|
|
|
if children.is_empty() && inner_opt {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-11-16 00:05:22 +00:00
|
|
|
DynamicNode::Text { id, value, inner } => {
|
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,
|
|
|
|
});
|
2022-11-16 07:59:19 +00:00
|
|
|
0
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
2022-11-02 01:42:29 +00:00
|
|
|
|
2022-11-04 05:30:26 +00:00
|
|
|
DynamicNode::Component {
|
2022-11-16 00:05:22 +00:00
|
|
|
props,
|
|
|
|
placeholder,
|
|
|
|
scope: scope_slot,
|
|
|
|
..
|
2022-11-04 05:30:26 +00:00
|
|
|
} => {
|
2022-11-09 18:58:11 +00:00
|
|
|
let scope = self
|
|
|
|
.new_scope(unsafe { std::mem::transmute(props.get()) })
|
|
|
|
.id;
|
2022-11-16 00:05:22 +00:00
|
|
|
|
|
|
|
scope_slot.set(Some(scope));
|
|
|
|
|
2022-11-09 18:58:11 +00:00
|
|
|
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) };
|
|
|
|
|
2022-11-12 02:29:27 +00:00
|
|
|
boundary_mut.mutations.edits = 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-16 07:22:41 +00:00
|
|
|
DynamicNode::Fragment { nodes, .. } => {
|
|
|
|
//
|
|
|
|
nodes
|
|
|
|
.iter()
|
|
|
|
.fold(0, |acc, child| acc + self.create(mutations, child))
|
|
|
|
}
|
2022-11-04 00:34:42 +00:00
|
|
|
|
2022-11-16 07:59:19 +00:00
|
|
|
DynamicNode::Placeholder(slot) => {
|
2022-11-04 00:34:42 +00:00
|
|
|
let id = self.next_element(template);
|
2022-11-16 07:59:19 +00:00
|
|
|
slot.set(id);
|
|
|
|
mutations.push(AssignId {
|
|
|
|
path: &template.template.node_paths[idx][1..],
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
0
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|