dioxus/packages/core/src/create.rs

445 lines
16 KiB
Rust
Raw Normal View History

2022-11-22 01:00:34 +00:00
use std::cell::Cell;
2022-11-09 18:58:11 +00:00
use crate::factory::RenderReturn;
2022-11-22 01:00:34 +00:00
use crate::innerlude::{Mutations, VComponent, VFragment, VText};
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-22 01:00:34 +00:00
use crate::{AttributeValue, ElementId, ScopeId, SuspenseContext, TemplateAttribute};
impl VirtualDom {
2022-11-22 01:00:34 +00:00
/// 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.
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 {
2022-11-22 01:00:34 +00:00
let mutations_to_this_point = mutations.len();
2022-11-09 18:58:11 +00:00
self.scope_stack.push(scope);
let out = self.create(mutations, template);
self.scope_stack.pop();
2022-11-22 01:00:34 +00:00
if !self.collected_leaves.is_empty() {
if let Some(boundary) = self.scopes[scope.0].has_context::<SuspenseContext>() {
println!("Boundary detected and pending leaves!");
}
}
2022-11-09 18:58:11 +00:00
out
}
/// 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
// Just in case, let's create the template using instructions anyways
if !self.templates.contains_key(&template.template.id) {
2022-11-22 01:00:34 +00:00
self.register_template(template, mutations);
}
2022-11-09 18:58:11 +00:00
// Walk the roots, creating nodes and assigning IDs
// 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();
let cur_scope = self.scope_stack.last().copied().unwrap();
let mut on_stack = 0;
2022-11-16 09:13:39 +00:00
for (root_idx, root) in template.template.roots.iter().enumerate() {
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 19:48:47 +00:00
match &template.dynamic_nodes[*id] {
2022-11-16 09:13:39 +00:00
DynamicNode::Fragment { .. } | DynamicNode::Component { .. } => self
.create_dynamic_node(
mutations,
template,
&template.dynamic_nodes[*id],
*id,
),
2022-11-20 23:58:05 +00:00
DynamicNode::Text(VText { id: slot, value }) => {
2022-11-18 04:00:39 +00:00
let id = self.next_element(template, template.template.node_paths[*id]);
2022-11-16 09:13:39 +00:00
slot.set(id);
2022-11-16 19:48:47 +00:00
mutations.push(CreateTextNode { value, id });
2022-11-16 09:13:39 +00:00
1
}
2022-11-16 19:48:47 +00:00
DynamicNode::Placeholder(slot) => {
2022-11-18 04:00:39 +00:00
let id = self.next_element(template, template.template.node_paths[*id]);
2022-11-16 19:48:47 +00:00
slot.set(id);
2022-11-16 09:13:39 +00:00
mutations.push(CreatePlaceholder { id });
1
}
}
}
};
// 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((mut attr_id, path)) =
dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8)
{
2022-11-18 04:00:39 +00:00
let id = self.next_element(template, template.template.attr_paths[attr_id]);
mutations.push(AssignId {
path: &path[1..],
id,
});
loop {
let attribute = template.dynamic_attrs.get(attr_id).unwrap();
2022-11-09 18:58:11 +00:00
attribute.mounted_element.set(id);
match &attribute.value {
AttributeValue::Text(value) => mutations.push(SetAttribute {
2022-11-09 18:58:11 +00:00
name: attribute.name,
value: *value,
ns: attribute.namespace,
2022-11-03 00:29:18 +00:00
id,
}),
2022-11-16 00:05:22 +00:00
AttributeValue::Bool(value) => mutations.push(SetBoolAttribute {
name: attribute.name,
value: *value,
id,
}),
AttributeValue::Listener(_) => mutations.push(NewEventListener {
2022-11-18 04:00:39 +00:00
// all listeners start with "on"
event_name: &attribute.name[2..],
scope: cur_scope,
2022-11-16 00:05:22 +00:00
id,
}),
AttributeValue::Float(_) => todo!(),
AttributeValue::Int(_) => todo!(),
AttributeValue::Any(_) => todo!(),
AttributeValue::None => todo!(),
2022-11-03 00:29:18 +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-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
2022-11-16 19:48:47 +00:00
let mut start = None;
let mut end = None;
while let Some((idx, p)) = dynamic_nodes.next_if(|(_, p)| p[0] == root_idx as u8) {
if p.len() == 1 {
continue;
}
if start.is_none() {
start = Some(idx);
2022-11-04 03:56:31 +00:00
}
2022-11-16 19:48:47 +00:00
end = Some(idx);
}
2022-11-16 09:13:39 +00:00
2022-11-16 19:48:47 +00:00
if let (Some(start), Some(end)) = (start, end) {
for idx in start..=end {
let node = &template.dynamic_nodes[idx];
let m = self.create_dynamic_node(mutations, template, node, idx);
if m > 0 {
mutations.push(ReplacePlaceholder {
m,
path: &template.template.node_paths[idx][1..],
});
}
}
2022-11-16 09:13:39 +00:00
}
}
on_stack
}
2022-11-22 01:00:34 +00:00
/// Insert a new template into the VirtualDom's template registry
fn register_template<'a>(&mut self, template: &'a VNode<'a>, mutations: &mut Mutations<'a>) {
for node in template.template.roots {
self.create_static_node(&mut mutations.template_mutations, template, node);
}
mutations.template_mutations.push(SaveTemplate {
name: template.template.id,
m: template.template.roots.len(),
});
self.templates
.insert(template.template.id, template.template);
}
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-18 04:00:39 +00:00
TemplateNode::Dynamic(idx) => {
let id = self.next_element(template, template.template.node_paths[idx]);
2022-11-12 02:29:27 +00:00
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
} => {
2022-11-18 04:00:39 +00:00
let id = self.next_element(template, &[]); // never gets referenced, empty path is fine, I think?
2022-11-02 01:42:29 +00:00
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,
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-11-09 18:58:11 +00:00
pub(crate) fn create_dynamic_node<'a>(
&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>,
node: &'a DynamicNode<'a>,
2022-11-03 08:24:20 +00:00
idx: usize,
) -> usize {
2022-11-22 01:00:34 +00:00
use DynamicNode::*;
match node {
Text(text) => self.create_dynamic_text(mutations, template, text, idx),
Placeholder(slot) => self.create_placeholder(template, idx, slot, mutations),
Fragment(frag) => self.create_fragment(frag, mutations),
Component(component) => self.create_component_node(mutations, template, component, idx),
}
}
2022-11-02 01:42:29 +00:00
2022-11-22 01:00:34 +00:00
fn create_dynamic_text<'a>(
&mut self,
mutations: &mut Mutations<'a>,
template: &VNode<'a>,
text: &VText<'a>,
idx: usize,
) -> usize {
// Allocate a dynamic element reference for this text node
let new_id = self.next_element(template, template.template.node_paths[idx]);
2022-11-16 00:05:22 +00:00
2022-11-22 01:00:34 +00:00
// Make sure the text node is assigned to the correct element
text.id.set(new_id);
2022-11-16 00:05:22 +00:00
2022-11-22 01:00:34 +00:00
// Add the mutation to the list
mutations.push(HydrateText {
id: new_id,
path: &template.template.node_paths[idx][1..],
value: text.value,
});
2022-11-04 05:30:26 +00:00
2022-11-22 01:00:34 +00:00
// Since we're hydrating an existing node, we don't create any new nodes
0
}
2022-11-16 19:48:47 +00:00
2022-11-22 01:00:34 +00:00
fn create_placeholder(
&mut self,
template: &VNode,
idx: usize,
slot: &Cell<ElementId>,
mutations: &mut Mutations,
) -> usize {
// Allocate a dynamic element reference for this text node
let id = self.next_element(template, template.template.node_paths[idx]);
2022-11-16 19:48:47 +00:00
2022-11-22 01:00:34 +00:00
// Make sure the text node is assigned to the correct element
slot.set(id);
2022-11-16 19:48:47 +00:00
2022-11-22 01:00:34 +00:00
// Assign the ID to the existing node in the template
mutations.push(AssignId {
path: &template.template.node_paths[idx][1..],
id,
});
2022-11-16 19:48:47 +00:00
2022-11-22 01:00:34 +00:00
// Since the placeholder is already in the DOM, we don't create any new nodes
0
}
2022-11-16 19:48:47 +00:00
2022-11-22 01:00:34 +00:00
fn create_fragment<'a>(
&mut self,
frag: &'a VFragment<'a>,
mutations: &mut Mutations<'a>,
) -> usize {
frag.nodes
.iter()
.fold(0, |acc, child| acc + self.create(mutations, child))
}
2022-11-08 06:55:22 +00:00
2022-11-22 01:00:34 +00:00
fn create_component_node<'a>(
&mut self,
mutations: &mut Mutations<'a>,
template: &'a VNode<'a>,
component: &'a VComponent<'a>,
idx: usize,
) -> usize {
let props = component.props.replace(None).unwrap();
2022-11-09 03:39:37 +00:00
2022-11-22 01:00:34 +00:00
let prop_ptr = unsafe { std::mem::transmute(props.as_ref()) };
let scope = self.new_scope(prop_ptr).id;
2022-11-02 01:42:29 +00:00
2022-11-22 01:00:34 +00:00
component.props.replace(Some(props));
component.scope.set(Some(scope));
2022-11-04 00:34:42 +00:00
2022-11-22 01:00:34 +00:00
let return_nodes = unsafe { self.run_scope(scope).extend_lifetime_ref() };
use RenderReturn::*;
2022-11-16 07:59:19 +00:00
2022-11-22 01:00:34 +00:00
match return_nodes {
Sync(Some(t)) => self.create_component(mutations, scope, t, idx, component),
Sync(None) | Async(_) => {
self.create_component_placeholder(template, idx, component, scope, mutations)
}
}
}
2022-11-22 01:00:34 +00:00
fn create_component<'a>(
&mut self,
mutations: &mut Mutations<'a>,
scope: ScopeId,
template: &'a VNode<'a>,
idx: usize,
component: &'a VComponent<'a>,
) -> usize {
// // Keep track of how many mutations are in the buffer in case we need to split them out if a suspense boundary
// // is encountered
// let mutations_to_this_point = mutations.len();
// Create the component's root element
let created = self.create_scope(scope, mutations, template);
if !self.collected_leaves.is_empty() {
println!("collected leaves: {:?}", self.collected_leaves);
}
created
// // If running the scope has collected some leaves and *this* component is a boundary, then handle the suspense
// let boundary = match self.scopes[scope.0].has_context::<SuspenseContext>() {
// Some(boundary) if !self.collected_leaves.is_empty() => boundary,
// _ => return created,
// };
// // Since this is a boundary, use it as a placeholder
// let new_id = self.next_element(template, template.template.node_paths[idx]);
// component.placeholder.set(Some(new_id));
// self.scopes[scope.0].placeholder.set(Some(new_id));
// mutations.push(AssignId {
// id: new_id,
// path: &template.template.node_paths[idx][1..],
// });
// // Now connect everything to the boundary
// let boundary_mut = boundary;
// let split_off = mutations.split_off(mutations_to_this_point);
// let split_off: Vec<Mutation> = unsafe { std::mem::transmute(split_off) };
// if boundary_mut.placeholder.get().is_none() {
// boundary_mut.placeholder.set(Some(new_id));
// }
// // In the generated edits, we want to pick off from where we left off.
// boundary_mut.mutations.borrow_mut().edits.extend(split_off);
// boundary_mut
// .waiting_on
// .borrow_mut()
// .extend(self.collected_leaves.drain(..));
// 0
// let boudary = self.scopes[scope.0]
// .consume_context::<SuspenseContext>()
// .unwrap();
// boudary
// .waiting_on
// .borrow_mut()
// .extend(self.collected_leaves.drain(..));
// if boudary.placeholder.get().is_none() {
// boudary.placeholder.set(Some(new_id));
// }
}
/// Take the rendered nodes from a component and handle them if they were async
///
/// IE simply assign an ID to the placeholder
fn create_component_placeholder(
&mut self,
template: &VNode,
idx: usize,
component: &VComponent,
scope: ScopeId,
mutations: &mut Mutations,
) -> usize {
let new_id = self.next_element(template, template.template.node_paths[idx]);
// Set the placeholder of the component
component.placeholder.set(Some(new_id));
// Set the placeholder of the scope
self.scopes[scope.0].placeholder.set(Some(new_id));
// Since the placeholder is already in the DOM, we don't create any new nodes
mutations.push(AssignId {
id: new_id,
path: &template.template.node_paths[idx][1..],
});
0
}
}