feat: use walking pattern
This commit is contained in:
1 parent
7cbb4d52dd
commit
22d4bf7346
18 files changed
+751
-843
No files matched your search
+128
-201
@@ -112,9 +112,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
|
||||
self.scope_stack.push(scopeid);
|
||||
self.element_stack.push(scope.container);
|
||||
{
|
||||
self.diff_node(old, new);
|
||||
}
|
||||
self.diff_node(old, new);
|
||||
self.element_stack.pop();
|
||||
self.scope_stack.pop();
|
||||
|
||||
@@ -122,58 +120,45 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
|
||||
pub fn diff_node(&mut self, old_node: &'b VNode<'b>, new_node: &'b VNode<'b>) {
|
||||
use VNode::{Component, Element, Fragment, Placeholder, Template, Text};
|
||||
use VNode::{Component, Element, Fragment, Template, Text};
|
||||
|
||||
// Same node by ref, no need to diff.
|
||||
if std::ptr::eq(old_node, new_node) {
|
||||
return;
|
||||
}
|
||||
|
||||
match (old_node, new_node) {
|
||||
(Text(old), Text(new)) => {
|
||||
self.diff_text_nodes(old, new, old_node, new_node);
|
||||
}
|
||||
|
||||
(Element(old), Element(new)) => {
|
||||
self.diff_element_nodes(old, new, old_node, new_node);
|
||||
}
|
||||
|
||||
(Component(old), Component(new)) => {
|
||||
self.diff_component_nodes(old_node, new_node, *old, *new);
|
||||
}
|
||||
|
||||
(Fragment(old), Fragment(new)) => {
|
||||
self.diff_fragment_nodes(old, new);
|
||||
}
|
||||
|
||||
(Template(old), Template(new)) => {
|
||||
self.diff_templates(old, new, old_node, new_node);
|
||||
}
|
||||
|
||||
(Placeholder(_), Placeholder(_)) => {
|
||||
self.diff_placeholder_nodes(old_node, new_node);
|
||||
}
|
||||
(Text(old), Text(new)) => self.diff_text(old, new, old_node, new_node),
|
||||
(Element(old), Element(new)) => self.diff_element(old, new, old_node, new_node),
|
||||
(Component(old), Component(new)) => self.diff_component(old_node, new_node, *old, *new),
|
||||
(Fragment(old), Fragment(new)) => self.diff_fragment(old, new),
|
||||
(Template(old), Template(new)) => self.diff_templates(old, new, old_node, new_node),
|
||||
|
||||
(
|
||||
Component(_) | Text(_) | Element(_) | Template(_) | Fragment(_) | Placeholder(_),
|
||||
Component(_) | Text(_) | Element(_) | Template(_) | Fragment(_) | Placeholder(_),
|
||||
Component(_) | Text(_) | Element(_) | Template(_) | Fragment(_),
|
||||
Component(_) | Text(_) | Element(_) | Template(_) | Fragment(_),
|
||||
) => self.replace_node(old_node, new_node),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_node(&mut self, node: &'b VNode<'b>) -> usize {
|
||||
match node {
|
||||
VNode::Text(vtext) => self.create_text_node(vtext, node),
|
||||
VNode::Element(element) => self.create_element_node(element, node),
|
||||
VNode::Fragment(frag) => self.create_fragment_node(*frag),
|
||||
VNode::Text(vtext) => self.create_text(vtext, node),
|
||||
VNode::Element(element) => self.create_element(element, node),
|
||||
VNode::Fragment(frag) => self.create_fragment(frag),
|
||||
VNode::Component(component) => self.create_component_node(*component),
|
||||
VNode::Template(template) => self.create_template_node(template, node),
|
||||
VNode::Placeholder(placeholder) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_text_node(&mut self, text: &'b VText<'b>, node: &'b VNode<'b>) -> usize {
|
||||
fn create_text(&mut self, text: &'b VText<'b>, node: &'b VNode<'b>) -> usize {
|
||||
let real_id = self.scopes.reserve_node(node);
|
||||
text.id.set(Some(real_id));
|
||||
self.mutations.create_text_node(text.text, real_id);
|
||||
1
|
||||
}
|
||||
|
||||
fn create_element_node(&mut self, element: &'b VElement<'b>, node: &'b VNode<'b>) -> usize {
|
||||
fn create_element(&mut self, element: &'b VElement<'b>, node: &'b VNode<'b>) -> usize {
|
||||
let VElement {
|
||||
tag: tag_name,
|
||||
listeners,
|
||||
@@ -198,7 +183,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
let cur_scope_id = self.current_scope();
|
||||
|
||||
for listener in listeners.iter() {
|
||||
listener.mounted_node.set(Some(real_id));
|
||||
listener.mounted_node.set(real_id);
|
||||
self.mutations.new_event_listener(listener, cur_scope_id);
|
||||
}
|
||||
|
||||
@@ -216,7 +201,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
1
|
||||
}
|
||||
|
||||
fn create_fragment_node(&mut self, frag: &'b VFragment<'b>) -> usize {
|
||||
fn create_fragment(&mut self, frag: &'b VFragment<'b>) -> usize {
|
||||
self.create_children(frag.children)
|
||||
}
|
||||
|
||||
@@ -274,17 +259,13 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
created
|
||||
}
|
||||
|
||||
pub(crate) fn diff_text_nodes(
|
||||
pub(crate) fn diff_text(
|
||||
&mut self,
|
||||
old: &'b VText<'b>,
|
||||
new: &'b VText<'b>,
|
||||
_old_node: &'b VNode<'b>,
|
||||
new_node: &'b VNode<'b>,
|
||||
) {
|
||||
if std::ptr::eq(old, new) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if the node is comming back not assigned, that means it was borrowed but removed
|
||||
let root = match old.id.get() {
|
||||
Some(id) => id,
|
||||
@@ -307,183 +288,138 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
old_node: &'b VNode<'b>,
|
||||
new_node: &'b VNode<'b>,
|
||||
) {
|
||||
if old.template.id == new.template.id {
|
||||
// if they're the same, just diff the dynamic nodes directly
|
||||
for (left, right) in old.dynamic_nodes.iter().zip(new.dynamic_nodes.iter()) {
|
||||
self.diff_node(left, right);
|
||||
if old.template.id != new.template.id {
|
||||
return self.replace_node(old_node, new_node);
|
||||
}
|
||||
|
||||
// if they're the same, just diff the dynamic nodes directly
|
||||
for (left, right) in old.dynamic_nodes.iter().zip(new.dynamic_nodes.iter()) {
|
||||
self.diff_node(&left.node, &right.node);
|
||||
}
|
||||
|
||||
for (left, right) in old.dynamic_attrs.iter().zip(new.dynamic_attrs.iter()) {
|
||||
let id = left.mounted_element.get();
|
||||
right.mounted_element.set(id);
|
||||
|
||||
for (left, right) in right.attrs.iter().zip(left.attrs.iter()) {
|
||||
if right.value != left.value || right.volatile {
|
||||
self.mutations
|
||||
.set_attribute(right.name, right.value, right.namespace, id);
|
||||
}
|
||||
}
|
||||
|
||||
// todo: need a way to load up the element bound to these attributes
|
||||
for (left, right) in old.dynamic_attrs.iter().zip(new.dynamic_attrs.iter()) {
|
||||
//
|
||||
// There's not really any diffing that needs to happen for listeners
|
||||
for listener in right.listeners {
|
||||
listener.mounted_node.set(id);
|
||||
}
|
||||
|
||||
// hmm, what we do here?
|
||||
for (left, right) in old.listeners.iter().zip(new.listeners.iter()) {
|
||||
//
|
||||
}
|
||||
} else {
|
||||
// else, diff them manually, taking the slow path
|
||||
self.replace_node(old_node, new_node);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_static_template_nodes(&mut self, node: &'b TemplateNode) {
|
||||
let id = ElementId(999999);
|
||||
match node {
|
||||
fn create_static_template_nodes(&mut self, node: &'b TemplateNode, id: ElementId) {
|
||||
match *node {
|
||||
TemplateNode::Element {
|
||||
tag,
|
||||
attrs,
|
||||
children,
|
||||
namespace,
|
||||
} => {
|
||||
self.mutations.create_element(tag, None, id);
|
||||
|
||||
for attr in *attrs {
|
||||
match attr {
|
||||
crate::TemplateAttribute::Dynamic(_) => todo!(),
|
||||
crate::TemplateAttribute::Static { name, value } => {
|
||||
self.mutations.set_attribute(
|
||||
name,
|
||||
AttributeValue::Text(value),
|
||||
None,
|
||||
id,
|
||||
);
|
||||
}
|
||||
}
|
||||
self.mutations.create_element(tag, namespace, id);
|
||||
for attr in attrs {
|
||||
self.mutations.set_attribute(
|
||||
attr.name,
|
||||
AttributeValue::Text(attr.value),
|
||||
attr.namespace,
|
||||
id,
|
||||
);
|
||||
}
|
||||
|
||||
for child in children.iter() {
|
||||
self.create_static_template_nodes(child);
|
||||
self.create_static_template_nodes(child, id);
|
||||
}
|
||||
self.mutations.append_children(children.len() as u32);
|
||||
}
|
||||
TemplateNode::Text(ref text) => self.mutations.create_text_node(text, id),
|
||||
TemplateNode::Text(text) => self.mutations.create_text_node(text, id),
|
||||
TemplateNode::Dynamic(_) => self.mutations.create_placeholder(id),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create the template from scratch using instructions, cache it, and then use the instructions to build it
|
||||
///
|
||||
/// This would be way easier if the ID could just be unique *after* cloning
|
||||
///
|
||||
/// If we traversed the template
|
||||
fn create_template_node(&mut self, template: &'b VTemplate<'b>, node: &'b VNode<'b>) -> usize {
|
||||
let template_id = template.template.id;
|
||||
let templates = self.scopes.template_cache.borrow_mut();
|
||||
// Reserve a single node for all the template nodes to reuse
|
||||
template.node_id.set(self.scopes.reserve_node(node));
|
||||
|
||||
// create and insert the template if it doesn't exist within the VirtualDom (it won't exist on the renderer either)
|
||||
// Save the template if it doesn't exist
|
||||
// todo: use &mut cache instead of borrowed cache
|
||||
let mut templates = self.scopes.template_cache.borrow_mut();
|
||||
if !templates.contains(&template.template) {
|
||||
template
|
||||
.template
|
||||
.roots
|
||||
.into_iter()
|
||||
.for_each(|node| self.create_static_template_nodes(node));
|
||||
.for_each(|node| self.create_static_template_nodes(node, template.node_id.get()));
|
||||
|
||||
self.mutations
|
||||
.save(template_id, template.template.roots.len() as u32);
|
||||
.save(template.template.id, template.template.roots.len() as u32);
|
||||
|
||||
templates.insert(template.template);
|
||||
}
|
||||
|
||||
self.mutations.load(template_id);
|
||||
// Walk the roots backwards, creating nodes and assigning IDs
|
||||
let mut dynamic_attrs = template.dynamic_attrs.iter().peekable();
|
||||
let mut dynamic_nodes = template.dynamic_nodes.iter().peekable();
|
||||
|
||||
let mut created = 0;
|
||||
let mut on_stack = 0;
|
||||
for (root_idx, root) in template.template.roots.iter().enumerate() {
|
||||
on_stack += match root {
|
||||
TemplateNode::Dynamic(id) => self.create_node(&template.dynamic_nodes[*id].node),
|
||||
TemplateNode::Element { .. } | TemplateNode::Text(_) => 1,
|
||||
};
|
||||
|
||||
// create the dynamic nodes
|
||||
for node in template.dynamic_nodes.iter() {
|
||||
created += self.create_node(node);
|
||||
// we're on top of a node that has a dynamic attribute for a descndent
|
||||
// Set that attribute now before the stack gets in a weird state
|
||||
// Roots may not be more than 255 nodes long, enforced by the macro
|
||||
while let Some(loc) = dynamic_attrs.next_if(|attr| attr.pathway[0] == root_idx as u8) {
|
||||
// Attach all the elementIDs to the nodes with dynamic content
|
||||
let id = self.scopes.reserve_node(node);
|
||||
self.mutations.assign_id(&loc.pathway[1..], id);
|
||||
loc.mounted_element.set(id);
|
||||
|
||||
for attr in loc.attrs {
|
||||
self.mutations
|
||||
.set_attribute(attr.name, attr.value, attr.namespace, id);
|
||||
}
|
||||
|
||||
for listener in loc.listeners {
|
||||
listener.mounted_node.set(id);
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(dyn_node) = dynamic_nodes.next_if(|f| f.pathway[0] == root_idx as u8) {
|
||||
// we're on top of a node that has a dynamic node for a descndent
|
||||
// Set that node now
|
||||
// Roots may not be more than 255 nodes long, enforced by the macro
|
||||
if dyn_node.pathway[0] == root_idx as u8 {
|
||||
let created = self.create_node(&dyn_node.node);
|
||||
|
||||
self.mutations
|
||||
.replace_descendant(&dyn_node.pathway[1..], created as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set any dynamic attributes
|
||||
for attr in template.dynamic_attrs.iter() {
|
||||
// assign an ID to the element
|
||||
let id = self.scopes.reserve_node(node);
|
||||
|
||||
self.mutations
|
||||
.set_attribute(attr.name, attr.value, attr.namespace, id);
|
||||
}
|
||||
|
||||
todo!()
|
||||
|
||||
// let def = template.template;
|
||||
|
||||
// let mut templates = self.scopes.template_cache.borrow_mut();
|
||||
|
||||
// if !templates.contains(&def) {
|
||||
// // self.create_template_def(def);
|
||||
// templates.insert(def);
|
||||
// }
|
||||
|
||||
// let mut nodes_created = 0;
|
||||
// let mut dynamic_nodes = template.dynamic_nodes.iter().enumerate().rev();
|
||||
|
||||
// for node in template.rendered_nodes {
|
||||
// match node {
|
||||
// // Give it an ID
|
||||
// crate::innerlude::TemplateRoots::Static(_) => todo!(),
|
||||
|
||||
// // let the creation step give it an ID
|
||||
// crate::innerlude::TemplateRoots::Runtime(_) => todo!(),
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Create all the dynamic nodes and merge them into the template
|
||||
// for (index, node) in dynamic_nodes {
|
||||
// let new_created = self.create_node(node);
|
||||
// self.mutations.edits.push(DomEdit::MergeTemplate {
|
||||
// index: index as u32,
|
||||
// num_children: new_created as u32,
|
||||
// })
|
||||
// }
|
||||
|
||||
// nodes_created
|
||||
on_stack
|
||||
}
|
||||
|
||||
fn create_template_static_node(&mut self, nodes: &'static [VNode<'static>]) -> usize {
|
||||
todo!()
|
||||
// let mut created = 0;
|
||||
// for node in nodes {
|
||||
// match *node {
|
||||
// TemplateNode::Element(el) => {
|
||||
// for attr in el.attributes {
|
||||
// match attr {
|
||||
// crate::template::TemplateAttribute::Dynamic => todo!(),
|
||||
// crate::template::TemplateAttribute::Static { attr } => {
|
||||
// self.mutations.set_attribute(attr, 0);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// self.mutations.create_element(el.tag, None, ElementId(0));
|
||||
|
||||
// if !nodes.is_empty() {
|
||||
// let res = self.create_template_static_node(nodes);
|
||||
// self.mutations.append_children(res as u32);
|
||||
// }
|
||||
|
||||
// created += 1;
|
||||
// }
|
||||
// TemplateNode::StaticText(text) => {
|
||||
// self.mutations.create_text_node(text, ElementId(0));
|
||||
// created += 1;
|
||||
// }
|
||||
// TemplateNode::DynamicText(_)
|
||||
// | TemplateNode::DynamicExpr(_)
|
||||
// | TemplateNode::DynamicComponent(_) => {
|
||||
// self.mutations.create_placeholder(ElementId(0));
|
||||
// created += 1;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// created
|
||||
}
|
||||
|
||||
fn diff_element_nodes(
|
||||
fn diff_element(
|
||||
&mut self,
|
||||
old: &'b VElement<'b>,
|
||||
new: &'b VElement<'b>,
|
||||
old_node: &'b VNode<'b>,
|
||||
new_node: &'b VNode<'b>,
|
||||
) {
|
||||
if std::ptr::eq(old, new) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if the node is comming back not assigned, that means it was borrowed but removed
|
||||
let root = match old.id.get() {
|
||||
Some(id) => id,
|
||||
@@ -563,7 +499,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
self.mutations.remove_event_listener(listener.event, root);
|
||||
}
|
||||
for listener in new.listeners {
|
||||
listener.mounted_node.set(Some(root));
|
||||
listener.mounted_node.set(root);
|
||||
self.mutations.new_event_listener(listener, cur_scope_id);
|
||||
}
|
||||
}
|
||||
@@ -580,7 +516,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
};
|
||||
}
|
||||
|
||||
fn diff_component_nodes(
|
||||
fn diff_component(
|
||||
&mut self,
|
||||
old_node: &'b VNode<'b>,
|
||||
new_node: &'b VNode<'b>,
|
||||
@@ -592,10 +528,6 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
.get()
|
||||
.expect("existing component nodes should have a scope");
|
||||
|
||||
if std::ptr::eq(old, new) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we're dealing with the same component (by function pointer)
|
||||
if old.user_fc == new.user_fc {
|
||||
self.enter_scope(scope_addr);
|
||||
@@ -654,11 +586,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
}
|
||||
|
||||
fn diff_fragment_nodes(&mut self, old: &'b VFragment<'b>, new: &'b VFragment<'b>) {
|
||||
if std::ptr::eq(old, new) {
|
||||
return;
|
||||
}
|
||||
|
||||
fn diff_fragment(&mut self, old: &'b VFragment<'b>, new: &'b VFragment<'b>) {
|
||||
todo!()
|
||||
// // This is the case where options or direct vnodes might be used.
|
||||
// // In this case, it's faster to just skip ahead to their diff
|
||||
@@ -1086,7 +1014,8 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
self.scopes.collect_garbage(id);
|
||||
}
|
||||
|
||||
VNode::Text(_) | VNode::Placeholder(_) => {
|
||||
// | VNode::Placeholder(_)
|
||||
VNode::Text(_) => {
|
||||
let id = old
|
||||
.try_mounted_id()
|
||||
.unwrap_or_else(|| panic!("broke on {:?}", old));
|
||||
@@ -1145,15 +1074,15 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
}
|
||||
}
|
||||
VNode::Placeholder(a) => {
|
||||
let id = a.id.get().unwrap();
|
||||
self.scopes.collect_garbage(id);
|
||||
a.id.set(None);
|
||||
// VNode::Placeholder(a) => {
|
||||
// let id = a.id.get().unwrap();
|
||||
// self.scopes.collect_garbage(id);
|
||||
// a.id.set(None);
|
||||
|
||||
if gen_muts {
|
||||
self.mutations.remove(id);
|
||||
}
|
||||
}
|
||||
// if gen_muts {
|
||||
// self.mutations.remove(id);
|
||||
// }
|
||||
// }
|
||||
VNode::Element(e) => {
|
||||
let id = e.id.get().unwrap();
|
||||
|
||||
@@ -1242,8 +1171,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
VNode::Template(c) => {
|
||||
todo!()
|
||||
}
|
||||
VNode::Placeholder(_) => todo!(),
|
||||
} // VNode::Placeholder(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1261,8 +1189,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
VNode::Template(t) => {
|
||||
todo!()
|
||||
}
|
||||
VNode::Placeholder(_) => todo!(),
|
||||
} // VNode::Placeholder(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1270,7 +1197,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
// recursively push all the nodes of a tree onto the stack and return how many are there
|
||||
fn push_all_real_nodes(&mut self, node: &'b VNode<'b>) -> usize {
|
||||
match node {
|
||||
VNode::Text(_) | VNode::Element(_) | VNode::Placeholder(_) => {
|
||||
VNode::Text(_) | VNode::Element(_) => {
|
||||
self.mutations.push_root(node.mounted_id());
|
||||
1
|
||||
}
|
||||
@@ -1294,7 +1221,7 @@ impl<'a, 'b, R: Renderer<'b>> DiffState<'a, 'b, R> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn diff_placeholder_nodes(&self, old_node: &VNode, new_node: &VNode) {
|
||||
pub(crate) fn diff_placeholder(&self, old_node: &VNode, new_node: &VNode) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ pub trait Renderer<'a> {
|
||||
fn pop_root(&mut self);
|
||||
/// Replace the given element with the next m elements on the stack
|
||||
fn replace_with(&mut self, root: ElementId, m: u32);
|
||||
|
||||
/// Insert the next m elements on the stack after the given element
|
||||
fn insert_after(&mut self, root: ElementId, n: u32);
|
||||
/// Insert the next m elements on the stack before the given element
|
||||
@@ -41,6 +42,8 @@ pub trait Renderer<'a> {
|
||||
fn remove(&mut self, root: ElementId);
|
||||
/// Remove an attribute from an existing element
|
||||
fn remove_attribute(&mut self, attribute: &Attribute, root: ElementId);
|
||||
/// Remove all the children of the given element
|
||||
fn remove_children(&mut self, root: ElementId);
|
||||
|
||||
/// Attach a new listener to the dom
|
||||
fn new_event_listener(&mut self, listener: &Listener, scope: ScopeId);
|
||||
@@ -58,13 +61,38 @@ pub trait Renderer<'a> {
|
||||
root: ElementId,
|
||||
);
|
||||
|
||||
/// Save the current n nodes to the ID to be loaded later
|
||||
fn save(&mut self, id: &str, num: u32);
|
||||
/// Loads a set of saved nodes from the ID
|
||||
fn load(&mut self, id: &str);
|
||||
|
||||
// General statistics for doing things that extend outside of the renderer
|
||||
|
||||
///
|
||||
/// General statistics for doing things that extend outside of the renderer
|
||||
fn mark_dirty_scope(&mut self, scope: ScopeId);
|
||||
|
||||
/// Save the current n nodes to the ID to be loaded later
|
||||
fn save(&mut self, id: &'static str, num: u32);
|
||||
/// Loads a set of saved nodes from the ID into a scratch space
|
||||
fn load(&mut self, id: &'static str, index: u32);
|
||||
/// Assign the element on the stack's descendent the given ID
|
||||
fn assign_id(&mut self, descendent: &'static [u8], id: ElementId);
|
||||
/// Replace the given element of the topmost element with the next m elements on the stack
|
||||
/// Is essentially a combination of assign_id and replace_with
|
||||
fn replace_descendant(&mut self, descendent: &'static [u8], m: u32);
|
||||
}
|
||||
|
||||
/*
|
||||
div {
|
||||
div {
|
||||
div {
|
||||
div {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
push_child(0)
|
||||
push_child(1)
|
||||
push_child(3)
|
||||
push_child(4)
|
||||
pop
|
||||
pop
|
||||
|
||||
clone_node(0)
|
||||
set_node(el, [1,2,3,4])
|
||||
set_attribute("class", "foo")
|
||||
append_child(1)
|
||||
*/
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::{
|
||||
any::Any,
|
||||
fmt::{Arguments, Display, Formatter},
|
||||
fmt::{Arguments, Formatter},
|
||||
};
|
||||
|
||||
use bumpalo::Bump;
|
||||
@@ -8,41 +8,40 @@ use bumpalo::Bump;
|
||||
/// Possible values for an attribute
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum AttributeValue<'a> {
|
||||
/// Reference strs, most common
|
||||
Text(&'a str),
|
||||
Float32(f32),
|
||||
/// Basic float values
|
||||
Float(f32),
|
||||
/// Basic Int values
|
||||
Int(i32),
|
||||
/// Basic bool values
|
||||
Bool(bool),
|
||||
/// Everything else
|
||||
Any(&'a dyn AnyAttributeValue),
|
||||
}
|
||||
|
||||
// #[cfg(feature = "serialize")]
|
||||
|
||||
impl<'a> PartialEq for AttributeValue<'a> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self, other) {
|
||||
(Self::Text(l0), Self::Text(r0)) => l0 == r0,
|
||||
(Self::Float32(l0), Self::Float32(r0)) => l0 == r0,
|
||||
(Self::Float(l0), Self::Float(r0)) => l0 == r0,
|
||||
(Self::Int(l0), Self::Int(r0)) => l0 == r0,
|
||||
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
|
||||
// (Self::Any(l0), Self::Any(r0)) => l0.cmp(r0),
|
||||
(Self::Any(l0), Self::Any(r0)) => (*l0).cmp_any(*r0),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Display for AttributeValue<'a> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for AttributeValue<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
todo!()
|
||||
// match self {
|
||||
// AttributeValue::Text(s) => write!(f, "AttributeValue::Text({:?})", s),
|
||||
// AttributeValue::Float32(f) => write!(f, "AttributeValue::Float32({:?})", f),
|
||||
// AttributeValue::Bool(b) => write!(f, "AttributeValue::Bool({:?})", b),
|
||||
// AttributeValue::Any(a) => write!(f, "AttributeValue::Any({:?})", a),
|
||||
// }
|
||||
match self {
|
||||
AttributeValue::Text(s) => write!(f, "AttributeValue::Text({:?})", s),
|
||||
AttributeValue::Float(v) => write!(f, "AttributeValue::Float({:?})", v),
|
||||
AttributeValue::Int(v) => write!(f, "AttributeValue::Int({:?})", v),
|
||||
AttributeValue::Bool(b) => write!(f, "AttributeValue::Bool({:?})", b),
|
||||
AttributeValue::Any(_) => write!(f, "AttributeValue::Any()"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,24 +50,27 @@ pub trait IntoAttributeValue<'a> {
|
||||
/// Convert into an attribute value
|
||||
fn into_value(self, bump: &'a Bump) -> AttributeValue<'a>;
|
||||
}
|
||||
impl<'a> IntoAttributeValue<'a> for f32 {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Float32(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoAttributeValue<'a> for bool {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Bool(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoAttributeValue<'a> for &'a str {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Text(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoAttributeValue<'a> for f32 {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Float(self)
|
||||
}
|
||||
}
|
||||
impl<'a> IntoAttributeValue<'a> for i32 {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Int(self)
|
||||
}
|
||||
}
|
||||
impl<'a> IntoAttributeValue<'a> for bool {
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
AttributeValue::Bool(self)
|
||||
}
|
||||
}
|
||||
impl<'a> IntoAttributeValue<'a> for Arguments<'_> {
|
||||
fn into_value(self, bump: &'a Bump) -> AttributeValue<'a> {
|
||||
use bumpalo::core_alloc::fmt::Write;
|
||||
@@ -78,29 +80,6 @@ impl<'a> IntoAttributeValue<'a> for Arguments<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> IntoAttributeValue<'a> for &'a T
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
|
||||
todo!()
|
||||
// AttributeValue::Any(ArbitraryAttributeValue {
|
||||
// value: self,
|
||||
// cmp: |a, b| {
|
||||
// if let Some(a) = a.as_any().downcast_ref::<T>() {
|
||||
// if let Some(b) = b.as_any().downcast_ref::<T>() {
|
||||
// a == b
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// } else {
|
||||
// false
|
||||
// }
|
||||
// },
|
||||
// })
|
||||
}
|
||||
}
|
||||
|
||||
// todo
|
||||
#[allow(missing_docs)]
|
||||
impl<'a> AttributeValue<'a> {
|
||||
@@ -130,16 +109,55 @@ impl<'a> AttributeValue<'a> {
|
||||
/// If you want to override the default behavior, you should implement PartialEq through a wrapper type
|
||||
pub trait AnyAttributeValue: Any {
|
||||
/// Perform a comparison between two values
|
||||
fn cmp_any(&self, _other: &dyn Any) -> bool {
|
||||
fn cmp_any<'a>(&'a self, _other: &'a dyn AnyAttributeValue) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Any + PartialEq> AnyAttributeValue for T {
|
||||
fn cmp_any(&self, other: &dyn Any) -> bool {
|
||||
match other.downcast_ref::<T>() {
|
||||
Some(t) => self == t,
|
||||
fn cmp_any(&self, other: &dyn AnyAttributeValue) -> bool {
|
||||
// we can't, for whatever reason use other as &dyn Any
|
||||
let right: &dyn Any = unsafe { std::mem::transmute(other) };
|
||||
|
||||
match right.downcast_ref::<T>() {
|
||||
Some(right) => self == right,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmp_any_works_even_though_it_transmutes() {
|
||||
// same type, same value
|
||||
let a = 2;
|
||||
let b = 2;
|
||||
assert!(a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
|
||||
// same type, different value
|
||||
let a = "asds";
|
||||
let b = "asdsasd";
|
||||
assert!(!a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
|
||||
// different type, different values
|
||||
let a = 123;
|
||||
let b = "asdsasd";
|
||||
assert!(!a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
|
||||
// Custom structs
|
||||
#[derive(PartialEq)]
|
||||
struct CustomStruct {
|
||||
a: i32,
|
||||
}
|
||||
|
||||
let a = CustomStruct { a: 1 };
|
||||
let b = CustomStruct { a: 1 };
|
||||
assert!(a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
|
||||
let a = CustomStruct { a: 1 };
|
||||
let b = CustomStruct { a: 123 };
|
||||
assert!(!a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
|
||||
let a = CustomStruct { a: 1 };
|
||||
let b = "asdasd";
|
||||
assert!(!a.cmp_any(&b as &dyn AnyAttributeValue));
|
||||
}
|
||||
@@ -70,10 +70,6 @@ pub struct Attribute<'a> {
|
||||
/// Used in controlled components to ensure changes are propagated.
|
||||
pub volatile: bool,
|
||||
|
||||
/// A reverse lookup for tracking down attributes for templates
|
||||
/// Not used for anything else
|
||||
pub mounted_node: Cell<Option<ElementId>>,
|
||||
|
||||
/// The value of the attribute.
|
||||
pub value: AttributeValue<'a>,
|
||||
}
|
||||
@@ -83,7 +79,7 @@ pub struct Attribute<'a> {
|
||||
pub struct Listener<'bump> {
|
||||
/// The ID of the node that this listener is mounted to
|
||||
/// Used to generate the event listener's ID on the DOM
|
||||
pub mounted_node: Cell<Option<ElementId>>,
|
||||
pub mounted_node: Cell<ElementId>,
|
||||
|
||||
/// The type of event to listen for.
|
||||
///
|
||||
|
||||
@@ -108,7 +108,6 @@ impl<'a> NodeFactory<'a> {
|
||||
name,
|
||||
namespace,
|
||||
volatile: is_volatile,
|
||||
mounted_node: Default::default(),
|
||||
value: val.into_value(self.bump),
|
||||
}
|
||||
}
|
||||
@@ -125,7 +124,6 @@ impl<'a> NodeFactory<'a> {
|
||||
name,
|
||||
namespace,
|
||||
volatile: is_volatile,
|
||||
mounted_node: Default::default(),
|
||||
value,
|
||||
}
|
||||
}
|
||||
@@ -172,7 +170,7 @@ impl<'a> NodeFactory<'a> {
|
||||
pub fn listener(self, event: &'static str, callback: InternalHandler<'a>) -> Listener<'a> {
|
||||
Listener {
|
||||
event,
|
||||
mounted_node: Cell::new(None),
|
||||
mounted_node: Cell::new(ElementId(0)),
|
||||
callback,
|
||||
}
|
||||
}
|
||||
@@ -234,27 +232,20 @@ impl<'a> NodeFactory<'a> {
|
||||
/// Create a refrence to a template
|
||||
pub fn template_ref(
|
||||
&self,
|
||||
template: fn() -> Template<'static>,
|
||||
template: Template<'static>,
|
||||
nodes: &'a [VNode<'a>],
|
||||
attributes: &'a [Attribute<'a>],
|
||||
listeners: &'a [Listener<'a>],
|
||||
key: Option<Arguments>,
|
||||
) -> VNode<'a> {
|
||||
// let borrow_ref = self.scope.templates.borrow();
|
||||
// // We only create the template if it doesn't already exist to allow for hot reloading
|
||||
// if !borrow_ref.contains_key(&id) {
|
||||
// drop(borrow_ref);
|
||||
// let mut borrow_mut = self.scope.templates.borrow_mut();
|
||||
// borrow_mut.insert(id.clone(), Rc::new(RefCell::new(template)));
|
||||
// }
|
||||
todo!()
|
||||
// VNode::TemplateRef(self.bump.alloc(VTemplate {
|
||||
// dynamic_context,
|
||||
// template_id: id,
|
||||
// node_ids: RefCell::new(Vec::new()),
|
||||
// parent: Cell::new(None),
|
||||
// template_ref_id: Cell::new(None),
|
||||
// }))
|
||||
VNode::Template(self.bump.alloc(VTemplate {
|
||||
key: None,
|
||||
node_id: Cell::new(ElementId(0)),
|
||||
template,
|
||||
dynamic_nodes: self.bump.alloc([]),
|
||||
dynamic_attrs: self.bump.alloc([]),
|
||||
listeners,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ pub struct VFragment<'src> {
|
||||
/// The key of the fragment to be used during keyed diffing.
|
||||
pub key: Option<&'src str>,
|
||||
|
||||
/// The [`ElementId`] of the placeholder.
|
||||
/// The [`ElementId`] of the placeholder if it exists
|
||||
pub placeholder: Cell<Option<ElementId>>,
|
||||
|
||||
/// Fragments can never have zero children. Enforced by NodeFactory.
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
mod Placeholder;
|
||||
mod arbitrary_value;
|
||||
mod component;
|
||||
mod element;
|
||||
@@ -25,7 +24,7 @@ pub use suspense::*;
|
||||
pub use template::*;
|
||||
pub use text::*;
|
||||
|
||||
use self::Placeholder::VPlaceholder;
|
||||
use self::placeholder::VPlaceholder;
|
||||
|
||||
/// A composable "VirtualNode" to declare a User Interface in the Dioxus VirtualDOM.
|
||||
///
|
||||
@@ -117,9 +116,6 @@ pub enum VNode<'src> {
|
||||
///
|
||||
///
|
||||
Template(&'src VTemplate<'src>),
|
||||
|
||||
///
|
||||
Placeholder(&'src VPlaceholder),
|
||||
}
|
||||
|
||||
/// An Element's unique identifier.
|
||||
@@ -140,7 +136,6 @@ impl<'src> VNode<'src> {
|
||||
VNode::Fragment(f) => f.key,
|
||||
VNode::Template(t) => t.key,
|
||||
VNode::Text(_t) => None,
|
||||
VNode::Placeholder(_p) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +156,6 @@ impl<'src> VNode<'src> {
|
||||
VNode::Fragment(_) => None,
|
||||
VNode::Component(_) => None,
|
||||
VNode::Template(_) => None,
|
||||
VNode::Placeholder(el) => el.id.get(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,7 +167,6 @@ impl<'src> VNode<'src> {
|
||||
VNode::Component(c) => VNode::Component(c),
|
||||
VNode::Fragment(f) => VNode::Fragment(f),
|
||||
VNode::Template(t) => VNode::Template(t),
|
||||
VNode::Placeholder(p) => VNode::Placeholder(p),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,10 +202,6 @@ impl Debug for VNode<'_> {
|
||||
.debug_struct("VNode::Templates")
|
||||
.field("template_id", &temp.template.id)
|
||||
.finish(),
|
||||
VNode::Placeholder(place) => s
|
||||
.debug_struct("VNode::Placeholder")
|
||||
.field("id", &place.id)
|
||||
.finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::ElementId;
|
||||
use std::cell::Cell;
|
||||
|
||||
use crate::ElementId;
|
||||
|
||||
/// A placeholder node
|
||||
pub struct VPlaceholder {
|
||||
pub id: Cell<Option<ElementId>>,
|
||||
pub dynamic_index: Option<usize>,
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
use std::hash::Hash;
|
||||
use std::{cell::Cell, hash::Hash};
|
||||
|
||||
use crate::{Attribute, Listener, VNode};
|
||||
use crate::{Attribute, ElementId, Listener, VNode};
|
||||
|
||||
/// A reference to a template along with any context needed to hydrate it
|
||||
pub struct VTemplate<'a> {
|
||||
pub key: Option<&'a str>,
|
||||
|
||||
// The ID assigned for all nodes in this template
|
||||
pub node_id: Cell<ElementId>,
|
||||
|
||||
// Position this template for fragments and stuff
|
||||
pub head_id: Cell<ElementId>,
|
||||
|
||||
pub tail_id: Cell<ElementId>,
|
||||
|
||||
pub template: Template<'static>,
|
||||
|
||||
pub dynamic_nodes: &'a [VNode<'a>],
|
||||
/// All the non-root dynamic nodes
|
||||
pub dynamic_nodes: &'a [NodeLocation<'a>],
|
||||
|
||||
pub dynamic_attrs: &'a [Attribute<'a>],
|
||||
pub dynamic_attrs: &'a [AttributeLocation<'a>],
|
||||
|
||||
pub listeners: &'a [Listener<'a>],
|
||||
}
|
||||
@@ -26,13 +35,11 @@ pub struct Template<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Eq for Template<'a> {}
|
||||
|
||||
impl<'a> PartialEq for Template<'a> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Hash for Template<'a> {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.id.hash(state);
|
||||
@@ -41,8 +48,10 @@ impl<'a> Hash for Template<'a> {
|
||||
|
||||
/// A weird-ish variant of VNodes with way more limited types
|
||||
pub enum TemplateNode<'a> {
|
||||
/// A simple element
|
||||
Element {
|
||||
tag: &'static str,
|
||||
namespace: Option<&'static str>,
|
||||
attrs: &'a [TemplateAttribute<'a>],
|
||||
children: &'a [TemplateNode<'a>],
|
||||
},
|
||||
@@ -50,8 +59,21 @@ pub enum TemplateNode<'a> {
|
||||
Dynamic(usize),
|
||||
}
|
||||
|
||||
pub enum TemplateAttribute<'a> {
|
||||
// todo: more values
|
||||
Static { name: &'static str, value: &'a str },
|
||||
Dynamic(usize),
|
||||
pub struct TemplateAttribute<'a> {
|
||||
pub name: &'static str,
|
||||
pub value: &'a str,
|
||||
pub namespace: Option<&'static str>,
|
||||
pub volatile: bool,
|
||||
}
|
||||
|
||||
pub struct AttributeLocation<'a> {
|
||||
pub pathway: &'static [u8],
|
||||
pub mounted_element: Cell<ElementId>,
|
||||
pub attrs: &'a [Attribute<'a>],
|
||||
pub listeners: &'a [Listener<'a>],
|
||||
}
|
||||
|
||||
pub struct NodeLocation<'a> {
|
||||
pub pathway: &'static [u8],
|
||||
pub node: VNode<'a>,
|
||||
}
|
||||
@@ -1,15 +1,5 @@
|
||||
use crate::{
|
||||
innerlude::{
|
||||
AttributeValue, ComponentPtr, Element, IntoAttributeValue, Properties, Scope, ScopeId,
|
||||
ScopeState, Template,
|
||||
},
|
||||
AnyEvent, Component, ElementId,
|
||||
};
|
||||
use bumpalo::{boxed::Box as BumpBox, Bump};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
fmt::{Arguments, Debug, Formatter},
|
||||
};
|
||||
use crate::ElementId;
|
||||
use std::cell::Cell;
|
||||
|
||||
/// A bump-allocated string slice and metadata.
|
||||
pub struct VText<'src> {
|
||||
|
||||
@@ -12,7 +12,6 @@ use std::{collections::VecDeque, iter::FromIterator, task::Poll};
|
||||
|
||||
/// A virtual node system that progresses user events and diffs UI trees.
|
||||
///
|
||||
///
|
||||
/// ## Guide
|
||||
///
|
||||
/// Components are defined as simple functions that take [`Scope`] and return an [`Element`].
|
||||
@@ -505,6 +504,13 @@ impl VirtualDom {
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the virtualdom, waiting for all async components to finish rendering
|
||||
///
|
||||
/// As they finish rendering, the virtualdom will apply the mutations to the renderer.
|
||||
pub async fn render(&mut self, renderer: &mut impl Renderer<'_>) {
|
||||
//
|
||||
}
|
||||
|
||||
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom from scratch.
|
||||
///
|
||||
/// The diff machine expects the RealDom's stack to be the root of the application.
|
||||
|
||||
Reference in new issue
Block a user