Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ bundle/

# in debugging we frequently dump wasm to wat with `wasm-tools print`
*.wat

# External macos drives have extra ._ files
._*
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ futures-util = { workspace = true, default-features = false, features = ["alloc"
serde = { workspace = true, optional = true, features = ["derive"] }
subsecond = { workspace = true }
anyhow = { workspace = true }
xxhash-rust = { workspace = true, features = ["const_xxh64"] }

[dev-dependencies]
dioxus = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/diff/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ impl VNode {
let mount = mounts.get(self.mount.get().0).unwrap();

template
.roots
.roots()
.iter()
.enumerate()
.map(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ impl VirtualDom {
#[allow(dead_code)]
fn is_dyn_node_only_child(node: &VNode, idx: usize) -> bool {
let template = node.template;
let path = template.node_paths[idx];
let path = template.node_paths()[idx];

// use a loop to index every static node's children until the path has run out
// only break if the last path index is a dynamic node
let mut static_node = &template.roots[path[0] as usize];
let mut static_node = &template.roots()[path[0] as usize];

for i in 1..path.len() - 1 {
match static_node {
Expand Down
28 changes: 14 additions & 14 deletions packages/core/src/diff/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl VNode {
&self,
root_idx: usize,
) -> Option<(usize, &DynamicNode)> {
self.template.roots[root_idx]
self.template.roots()[root_idx]
.dynamic_id()
.map(|id| (id, &self.dynamic_nodes[id]))
}
Expand Down Expand Up @@ -180,7 +180,7 @@ impl VNode {

pub(crate) fn find_last_element(&self, dom: &VirtualDom) -> ElementId {
let mount_id = self.mount.get();
let last_root_index = self.template.roots.len() - 1;
let last_root_index = self.template.roots().len() - 1;
let last = match self.get_dynamic_root_node_and_id(last_root_index) {
// This node is static, just get the root id
None => dom.get_mounted_root_node(mount_id, last_root_index),
Expand Down Expand Up @@ -306,7 +306,7 @@ impl VNode {
destroy_component_state: bool,
replace_with: Option<usize>,
) {
let roots = self.template.roots;
let roots = self.template.roots();
for (idx, node) in roots.iter().enumerate() {
let last_node = idx == roots.len() - 1;
if let Some(id) = node.dynamic_id() {
Expand Down Expand Up @@ -343,7 +343,7 @@ impl VNode {
) {
let template = self.template;
for (idx, dyn_node) in self.dynamic_nodes.iter().enumerate() {
let path_len = template.node_paths.get(idx).map(|path| path.len());
let path_len = template.node_paths().get(idx).map(|path| path.len());
// Roots are cleaned up automatically above and nodes with a empty path are placeholders
if let Some(2..) = path_len {
self.remove_dynamic_node(
Expand Down Expand Up @@ -398,7 +398,7 @@ impl VNode {

pub(super) fn reclaim_attributes(&self, mount: MountId, dom: &mut VirtualDom) {
let mut next_id = None;
for (idx, path) in self.template.attr_paths.iter().enumerate() {
for (idx, path) in self.template.attr_paths().iter().enumerate() {
// We clean up the roots in the next step, so don't worry about them here
if path.len() <= 1 {
continue;
Expand Down Expand Up @@ -429,7 +429,7 @@ impl VNode {
let mut old_attributes_iter = old_attrs.iter().peekable();
let mut new_attributes_iter = new_attrs.iter().peekable();
let attribute_id = dom.get_mounted_dyn_attr(mount_id, idx);
let path = self.template.attr_paths[idx];
let path = self.template.attr_paths()[idx];

loop {
match (old_attributes_iter.peek(), new_attributes_iter.peek()) {
Expand Down Expand Up @@ -558,18 +558,18 @@ impl VNode {
entry.insert(VNodeMount {
node: self.clone(),
parent,
root_ids: vec![ElementId(0); template.roots.len()].into_boxed_slice(),
mounted_attributes: vec![ElementId(0); template.attr_paths.len()]
root_ids: vec![ElementId(0); template.roots().len()].into_boxed_slice(),
mounted_attributes: vec![ElementId(0); template.attr_paths().len()]
.into_boxed_slice(),
mounted_dynamic_nodes: vec![usize::MAX; template.node_paths.len()]
mounted_dynamic_nodes: vec![usize::MAX; template.node_paths().len()]
.into_boxed_slice(),
});
}

// Walk the roots, creating nodes and assigning IDs
// nodes in an iterator of (dynamic_node_index, path) and attrs in an iterator of (attr_index, path)
let mut nodes = template.node_paths.iter().copied().enumerate().peekable();
let mut attrs = template.attr_paths.iter().copied().enumerate().peekable();
let mut nodes = template.node_paths().iter().copied().enumerate().peekable();
let mut attrs = template.attr_paths().iter().copied().enumerate().peekable();

// Get the mounted id of this block
// At this point, we should have already mounted the block
Expand All @@ -588,7 +588,7 @@ impl VNode {
// Go through each root node and create the node, adding it to the stack.
// Each node already exists in the template, so we can just clone it from the template
let nodes_created = template
.roots
.roots()
.iter()
.enumerate()
.map(|(root_idx, root)| {
Expand Down Expand Up @@ -646,7 +646,7 @@ impl VNode {
fn reference_to_dynamic_node(&self, mount: MountId, dynamic_node_id: usize) -> ElementRef {
ElementRef {
path: ElementPath {
path: self.template.node_paths[dynamic_node_id],
path: self.template.node_paths()[dynamic_node_id],
},
mount,
}
Expand Down Expand Up @@ -772,7 +772,7 @@ impl VNode {
// If we actually created real new nodes, we need to replace the placeholder for this dynamic node with the new dynamic nodes
if m > 0 {
// The path is one shorter because the top node is the root
let path = &self.template.node_paths[dynamic_node_id][1..];
let path = &self.template.node_paths()[dynamic_node_id][1..];
to.replace_placeholder_with_nodes(path, m);
}
}
Expand Down
29 changes: 13 additions & 16 deletions packages/core/src/error_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ impl<F: Fn(ErrorContext) -> Element + 'static> From<F> for ErrorHandler {
}

fn default_handler(errors: ErrorContext) -> Element {
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Element {
static TEMPLATE: Template = Template::new(
&[TemplateNode::Element {
tag: "div",
namespace: None,
attrs: &[TemplateAttribute::Static {
Expand All @@ -164,29 +164,29 @@ fn default_handler(errors: ErrorContext) -> Element {
}],
children: &[TemplateNode::Dynamic { id: 0usize }],
}],
node_paths: &[&[0u8, 0u8]],
attr_paths: &[],
};
&[&[0u8, 0u8]],
&[],
);
std::result::Result::Ok(VNode::new(
None,
TEMPLATE,
Box::new([errors
.error()
.iter()
.map(|e| {
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Element {
static INNER_TEMPLATE: Template = Template::new(
&[TemplateNode::Element {
tag: "pre",
namespace: None,
attrs: &[],
children: &[TemplateNode::Dynamic { id: 0usize }],
}],
node_paths: &[&[0u8, 0u8]],
attr_paths: &[],
};
&[&[0u8, 0u8]],
&[],
);
VNode::new(
None,
TEMPLATE,
INNER_TEMPLATE,
Box::new([e.to_string().into_dyn_node()]),
Default::default(),
)
Expand Down Expand Up @@ -320,11 +320,8 @@ pub fn ErrorBoundary(props: ErrorBoundaryProps) -> Element {
(props.handle_error.0)(error_boundary.clone())
} else {
std::result::Result::Ok({
static TEMPLATE: Template = Template {
roots: &[TemplateNode::Dynamic { id: 0usize }],
node_paths: &[&[0u8]],
attr_paths: &[],
};
static TEMPLATE: Template =
Template::new(&[TemplateNode::Dynamic { id: 0usize }], &[&[0u8]], &[]);
VNode::new(
None,
TEMPLATE,
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/hotreload_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,7 @@ impl HotReloadedTemplate {
let node_paths = Self::node_paths(roots);
let attr_paths = Self::attr_paths(roots);

let template = Template {
roots,
node_paths,
attr_paths,
};
let template = Template::new(roots, node_paths, attr_paths);
Self {
key,
dynamic_nodes,
Expand Down
Loading
Loading