dioxus/packages/core/src/create.rs

259 lines
9.5 KiB
Rust
Raw Normal View History

2022-11-04 05:30:26 +00:00
use std::pin::Pin;
2022-11-04 03:56:31 +00:00
2022-11-04 05:30:26 +00:00
use crate::factory::{FiberLeaf, RenderReturn};
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-04 05:30:26 +00:00
use crate::suspense::LeafLocation;
2022-11-02 01:42:29 +00:00
use crate::virtualdom::VirtualDom;
2022-11-04 03:56:31 +00:00
use crate::{AttributeValue, Element, ElementId, TemplateAttribute};
2022-11-04 05:30:26 +00:00
use bumpalo::boxed::Box as BumpBox;
use futures_util::Future;
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>,
) -> usize {
2022-11-02 01:42:29 +00:00
// The best renderers will have templates prehydrated
// 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());
}
// 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)
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();
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
}
TemplateNode::Dynamic(id) => {
2022-11-03 08:24:20 +00:00
self.create_dynamic_node(mutations, template, &template.dynamic_nodes[*id], *id)
}
2022-11-02 01:42:29 +00:00
TemplateNode::DynamicText { .. } => 1,
};
2022-11-03 00:29:18 +00:00
let mut cur_route = None;
// 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-03 08:24:20 +00:00
while let Some((idx, path)) = dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8) {
let attr = &template.dynamic_attrs[idx];
2022-11-03 00:29:18 +00:00
if cur_route.is_none() {
2022-11-03 08:24:20 +00:00
cur_route = Some((self.next_element(template), &path[1..]));
2022-11-03 00:29:18 +00:00
}
// Attach all the elementIDs to the nodes with dynamic content
2022-11-03 00:29:18 +00:00
let (id, path) = cur_route.unwrap();
2022-11-02 01:42:29 +00:00
2022-11-03 00:29:18 +00:00
mutations.push(AssignId { path, id });
attr.mounted_element.set(id);
2022-11-03 00:29:18 +00:00
match attr.value {
AttributeValue::Text(value) => {
mutations.push(SetAttribute {
name: attr.name,
value,
id,
});
}
2022-11-03 00:29:18 +00:00
AttributeValue::Listener(_) => {
//
}
AttributeValue::Float(_) => todo!(),
AttributeValue::Int(_) => todo!(),
AttributeValue::Bool(_) => todo!(),
AttributeValue::Any(_) => todo!(),
// Optional attributes
AttributeValue::None => todo!(),
}
}
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-03 08:24:20 +00:00
while let Some((idx, path)) = dynamic_nodes.next_if(|(_, p)| p[0] == root_idx as u8) {
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..],
});
}
}
}
on_stack
}
2022-11-04 00:34:42 +00:00
pub 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-11-04 00:34:42 +00:00
pub fn create_dynamic_node<'a>(
&mut self,
mutations: &mut Vec<Mutation<'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-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);
id.set(new_id);
2022-11-02 01:42:29 +00:00
mutations.push(HydrateText {
id: new_id,
2022-11-03 08:24:20 +00:00
path: &template.template.node_paths[idx][1..],
value,
});
1
}
2022-11-02 01:42:29 +00:00
2022-11-04 05:30:26 +00:00
DynamicNode::Component {
props, placeholder, ..
} => {
2022-11-04 00:34:42 +00:00
let id = self.new_scope(unsafe { std::mem::transmute(props.get()) });
2022-11-04 03:56:31 +00:00
let render_ret = self.run_scope(id);
2022-11-02 01:42:29 +00:00
// shut up about lifetimes please, I know what I'm doing
2022-11-04 03:56:31 +00:00
let render_ret: &mut RenderReturn = unsafe { std::mem::transmute(render_ret) };
match render_ret {
RenderReturn::Sync(Some(template)) => {
self.scope_stack.push(id);
let created = self.create(mutations, template);
self.scope_stack.pop();
created
}
2022-11-04 05:30:26 +00:00
// whenever the future is polled later, we'll revisit it
// For now, just set the placeholder
RenderReturn::Sync(None) => {
let new_id = self.next_element(template);
placeholder.set(Some(new_id));
mutations.push(AssignId {
id: new_id,
path: &template.template.node_paths[idx][1..],
});
0
}
2022-11-04 03:56:31 +00:00
RenderReturn::Async(fut) => {
2022-11-04 05:30:26 +00:00
let new_id = self.next_element(template);
// move up the tree looking for the first suspense boundary
// our current component can not be a suspense boundary, so we skip it
for scope_id in self.scope_stack.iter().rev().skip(1) {
let scope = &mut self.scopes[scope_id.0];
if let Some(fiber) = &mut scope.suspense_boundary {
// save the fiber leaf onto the fiber itself
let detached: &mut FiberLeaf<'static> =
unsafe { std::mem::transmute(fut) };
// And save the fiber leaf using the placeholder node
// this way, when we resume the fiber, we just need to "pick up placeholder"
fiber.futures.insert(
LeafLocation {
element: new_id,
scope: *scope_id,
},
detached,
);
self.suspended_scopes.insert(*scope_id);
break;
2022-11-04 03:56:31 +00:00
}
}
2022-11-04 05:30:26 +00:00
placeholder.set(Some(new_id));
mutations.push(AssignId {
id: new_id,
path: &template.template.node_paths[idx][1..],
});
0
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
}
}
}
}