From 432bfd395e55cf84b6a3883cc1c73795183dce05 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 22 Dec 2022 16:54:49 -0600 Subject: [PATCH] fix swaping templetes before any instances are created --- packages/core/src/create.rs | 37 +++++++++++++++++++++++++++----- packages/core/src/diff.rs | 8 +++---- packages/core/src/virtual_dom.rs | 1 - 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/packages/core/src/create.rs b/packages/core/src/create.rs index c9e1437cd..b0bca26d5 100644 --- a/packages/core/src/create.rs +++ b/packages/core/src/create.rs @@ -62,6 +62,19 @@ impl<'b> VirtualDom { /// Create this template and write its mutations pub(crate) fn create(&mut self, node: &'b VNode<'b>) -> usize { + // check for a overriden template + #[cfg(debug_assertions)] + { + let (path, byte_index) = node.template.get().name.rsplit_once(':').unwrap(); + if let Some(template) = self + .templates + .get(path) + .and_then(|map| map.get(&byte_index.parse().unwrap())) + { + node.template.set(*template); + } + } + // Intialize the root nodes slice node.root_ids .intialize(vec![ElementId(0); node.template.get().roots.len()].into_boxed_slice()); @@ -369,10 +382,11 @@ impl<'b> VirtualDom { template.name = old_template.name; *old_template = template; } else { - panic!( - "Template {path} was not registered in\n{:#?}", - self.templates - ); + // This is a template without any current instances + self.templates + .entry(path) + .or_default() + .insert(usize::MAX, template); } // If it's all dynamic nodes, then we don't need to register it @@ -382,10 +396,23 @@ impl<'b> VirtualDom { } /// Insert a new template into the VirtualDom's template registry - pub(crate) fn register_template(&mut self, template: Template<'static>) { + pub(crate) fn register_template(&mut self, mut template: Template<'static>) { // First, make sure we mark the template as seen, regardless if we process it let (path, byte_index) = template.name.rsplit_once(':').unwrap(); let byte_index = byte_index.parse::().unwrap(); + + // if hot reloading is enabled, then we need to check for a template that has overriten this one + #[cfg(debug_assertions)] + if let Some(mut new_template) = self + .templates + .get_mut(path) + .and_then(|map| map.remove(&usize::MAX)) + { + // the byte index of the hot reloaded template could be different + new_template.name = template.name; + template = new_template; + } + self.templates .entry(path) .or_default() diff --git a/packages/core/src/diff.rs b/packages/core/src/diff.rs index 1d8d2da0a..0dcf07fff 100644 --- a/packages/core/src/diff.rs +++ b/packages/core/src/diff.rs @@ -62,11 +62,9 @@ impl<'b> VirtualDom { if let Some(map) = self.templates.get(path) { let byte_index = byte_index.parse::().unwrap(); if let Some(&template) = map.get(&byte_index) { - if template != right_template.template.get() { - right_template.template.set(template); - if template != left_template.template.get() { - return self.replace(left_template, [right_template]); - } + right_template.template.set(template); + if template != left_template.template.get() { + return self.replace(left_template, [right_template]); } } } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 089386584..c772b8317 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -478,7 +478,6 @@ impl VirtualDom { } } } - assert!(!self.dirty_scopes.is_empty()); } /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom from scratch.