fix swaping templetes before any instances are created
This commit is contained in:
1 parent
1530445972
commit
432bfd395e
3 files changed
+35
-11
No files matched your search
@@ -62,6 +62,19 @@ impl<'b> VirtualDom {
|
|||||||
|
|
||||||
/// Create this template and write its mutations
|
/// Create this template and write its mutations
|
||||||
pub(crate) fn create(&mut self, node: &'b VNode<'b>) -> usize {
|
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
|
// Intialize the root nodes slice
|
||||||
node.root_ids
|
node.root_ids
|
||||||
.intialize(vec![ElementId(0); node.template.get().roots.len()].into_boxed_slice());
|
.intialize(vec![ElementId(0); node.template.get().roots.len()].into_boxed_slice());
|
||||||
@@ -369,10 +382,11 @@ impl<'b> VirtualDom {
|
|||||||
template.name = old_template.name;
|
template.name = old_template.name;
|
||||||
*old_template = template;
|
*old_template = template;
|
||||||
} else {
|
} else {
|
||||||
panic!(
|
// This is a template without any current instances
|
||||||
"Template {path} was not registered in\n{:#?}",
|
self.templates
|
||||||
self.templates
|
.entry(path)
|
||||||
);
|
.or_default()
|
||||||
|
.insert(usize::MAX, template);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's all dynamic nodes, then we don't need to register it
|
// 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
|
/// 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
|
// First, make sure we mark the template as seen, regardless if we process it
|
||||||
let (path, byte_index) = template.name.rsplit_once(':').unwrap();
|
let (path, byte_index) = template.name.rsplit_once(':').unwrap();
|
||||||
let byte_index = byte_index.parse::<usize>().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
|
self.templates
|
||||||
.entry(path)
|
.entry(path)
|
||||||
.or_default()
|
.or_default()
|
||||||
|
|||||||
@@ -62,11 +62,9 @@ impl<'b> VirtualDom {
|
|||||||
if let Some(map) = self.templates.get(path) {
|
if let Some(map) = self.templates.get(path) {
|
||||||
let byte_index = byte_index.parse::<usize>().unwrap();
|
let byte_index = byte_index.parse::<usize>().unwrap();
|
||||||
if let Some(&template) = map.get(&byte_index) {
|
if let Some(&template) = map.get(&byte_index) {
|
||||||
if template != right_template.template.get() {
|
right_template.template.set(template);
|
||||||
right_template.template.set(template);
|
if template != left_template.template.get() {
|
||||||
if template != left_template.template.get() {
|
return self.replace(left_template, [right_template]);
|
||||||
return self.replace(left_template, [right_template]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom from scratch.
|
||||||
|
|||||||
Reference in new issue
Block a user