feat: simple tests passing

This commit is contained in:
Jonathan Kelley committed 2022-11-01 18:42:29 -07:00
1 parent 23603aaaf5
commit 4a31b29703
27 files changed
+1565 -372

No files matched your search

+99 -41
View File
@@ -1,33 +1,30 @@
use crate::VirtualDom;
use crate::any_props::VComponentProps;
use crate::arena::ElementArena;
use crate::component::Component;
use crate::mutations::Mutation;
use crate::nodes::{
AttributeLocation, DynamicNode, DynamicNodeKind, Template, TemplateId, TemplateNode,
};
use crate::scopes::Scope;
use crate::{
any_props::AnyProps,
arena::ElementId,
bump_frame::BumpFrame,
nodes::VTemplate,
scopes::{ComponentPtr, ScopeId, ScopeState},
};
use slab::Slab;
use crate::mutations::Mutation::*;
use crate::nodes::VNode;
use crate::nodes::{DynamicNode, DynamicNodeKind, TemplateNode};
use crate::virtualdom::VirtualDom;
impl VirtualDom {
/// Create this template and write its mutations
pub fn create<'a>(
&mut self,
mutations: &mut Vec<Mutation<'a>>,
template: &'a VTemplate<'a>,
template: &'a VNode<'a>,
) -> usize {
// The best renderers will have tempaltes prehydrated
// 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) {
self.create_static_template(mutations, template.template);
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
@@ -38,28 +35,37 @@ impl VirtualDom {
let mut on_stack = 0;
for (root_idx, root) in template.template.roots.iter().enumerate() {
on_stack += match root {
TemplateNode::Element { .. } | TemplateNode::Text(_) => {
mutations.push(LoadTemplate {
name: template.template.id,
index: root_idx,
});
1
}
TemplateNode::Dynamic(id) => {
self.create_dynamic_node(mutations, template, &template.dynamic_nodes[*id])
}
TemplateNode::DynamicText { .. }
| TemplateNode::Element { .. }
| TemplateNode::Text(_) => 1,
TemplateNode::DynamicText { .. } => 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(loc) = dynamic_attrs.next_if(|a| a.path[0] == root_idx as u8) {
// Attach all the elementIDs to the nodes with dynamic content
let id = self.elements.next();
mutations.push(Mutation::AssignId {
let id = self.next_element(template);
mutations.push(AssignId {
path: &loc.path[1..],
id,
});
loc.mounted_element.set(id);
for attr in loc.attrs {
mutations.push(Mutation::SetAttribute {
for attr in loc.attrs.iter() {
mutations.push(SetAttribute {
name: attr.name,
value: attr.value,
id,
@@ -67,31 +73,71 @@ impl VirtualDom {
}
}
// We're on top of a node that has a dynamic child for a descndent
// Might as well set it now while we can
// We're on top of a node that has a dynamic child for a descendant
while let Some(node) = dynamic_nodes.next_if(|f| f.path[0] == root_idx as u8) {
self.create_dynamic_node(mutations, template, node);
let m = self.create_dynamic_node(mutations, template, node);
mutations.push(ReplacePlaceholder {
m,
path: &node.path[1..],
});
}
}
on_stack
}
fn create_static_template(&mut self, mutations: &mut Vec<Mutation>, template: &Template) {
todo!("load template")
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,
});
mutations.extend(attrs.into_iter().map(|attr| SetAttribute {
name: attr.name,
value: attr.value,
id,
}));
children
.into_iter()
.for_each(|child| self.create_static_node(mutations, template, child));
mutations.push(AppendChildren { m: children.len() })
}
}
}
fn create_dynamic_node<'a>(
&mut self,
mutations: &mut Vec<Mutation<'a>>,
template: &VTemplate<'a>,
template: &'a VNode<'a>,
node: &'a DynamicNode<'a>,
) -> usize {
match &node.kind {
DynamicNodeKind::Text { id, value } => {
let new_id = self.elements.next();
let new_id = self.next_element(template);
id.set(new_id);
mutations.push(Mutation::HydrateText {
mutations.push(HydrateText {
id: new_id,
path: &node.path[1..],
value,
@@ -99,16 +145,28 @@ impl VirtualDom {
1
}
DynamicNodeKind::Component { props, fn_ptr, .. } => {
let id = self.new_scope(*fn_ptr, None, ElementId(0), *props);
DynamicNodeKind::Component { props, .. } => {
let id = self.new_scope(*props);
let template = self.run_scope(id);
todo!("create component has bad data");
// 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))
}
DynamicNodeKind::Fragment { children } => children
.iter()
.fold(0, |acc, child| acc + self.create(mutations, child)),
}
}
}