fix swaping templetes before any instances are created

This commit is contained in:
Evan Almloff 2022-12-22 16:54:49 -06:00
parent 1530445972
commit 432bfd395e
3 changed files with 35 additions and 11 deletions

View file

@ -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::<usize>().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()

View file

@ -62,11 +62,9 @@ impl<'b> VirtualDom {
if let Some(map) = self.templates.get(path) {
let byte_index = byte_index.parse::<usize>().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]);
}
}
}

View file

@ -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.