2022-12-05 21:03:52 +00:00
|
|
|
use crate::{nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, DynamicNode, ScopeId};
|
2022-11-29 21:31:04 +00:00
|
|
|
use bumpalo::boxed::Box as BumpBox;
|
2022-11-02 01:42:29 +00:00
|
|
|
|
2022-12-03 00:24:49 +00:00
|
|
|
/// An Element's unique identifier.
|
|
|
|
///
|
|
|
|
/// `ElementId` is a `usize` that is unique across the entire VirtualDOM - but not unique across time. If a component is
|
|
|
|
/// unmounted, then the `ElementId` will be reused for a new component.
|
2022-11-16 00:05:22 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
2022-10-28 04:58:47 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
|
|
pub struct ElementId(pub usize);
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) struct ElementRef {
|
2022-11-18 04:00:39 +00:00
|
|
|
// the pathway of the real element inside the template
|
2022-11-27 14:38:40 +00:00
|
|
|
pub path: ElementPath,
|
2022-11-18 04:00:39 +00:00
|
|
|
|
|
|
|
// The actual template
|
2022-11-30 22:21:10 +00:00
|
|
|
pub template: *const VNode<'static>,
|
2022-11-02 01:42:29 +00:00
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-27 14:38:40 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum ElementPath {
|
|
|
|
Deep(&'static [u8]),
|
|
|
|
Root(usize),
|
|
|
|
}
|
|
|
|
|
2022-11-18 04:00:39 +00:00
|
|
|
impl ElementRef {
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) fn null() -> Self {
|
2022-11-12 02:29:27 +00:00
|
|
|
Self {
|
|
|
|
template: std::ptr::null_mut(),
|
2022-11-27 14:38:40 +00:00
|
|
|
path: ElementPath::Root(0),
|
2022-11-12 02:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:46:25 +00:00
|
|
|
impl VirtualDom {
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) fn next_element(&mut self, template: &VNode, path: &'static [u8]) -> ElementId {
|
2022-11-02 01:42:29 +00:00
|
|
|
let entry = self.elements.vacant_entry();
|
|
|
|
let id = entry.key();
|
2022-11-18 04:00:39 +00:00
|
|
|
entry.insert(ElementRef {
|
2022-11-02 01:42:29 +00:00
|
|
|
template: template as *const _ as *mut _,
|
2022-11-27 14:38:40 +00:00
|
|
|
path: ElementPath::Deep(path),
|
|
|
|
});
|
|
|
|
ElementId(id)
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) fn next_root(&mut self, template: &VNode, path: usize) -> ElementId {
|
2022-11-27 14:38:40 +00:00
|
|
|
let entry = self.elements.vacant_entry();
|
|
|
|
let id = entry.key();
|
|
|
|
entry.insert(ElementRef {
|
|
|
|
template: template as *const _ as *mut _,
|
|
|
|
path: ElementPath::Root(path),
|
2022-11-02 01:42:29 +00:00
|
|
|
});
|
|
|
|
ElementId(id)
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) fn reclaim(&mut self, el: ElementId) {
|
|
|
|
self.try_reclaim(el)
|
|
|
|
.unwrap_or_else(|| panic!("cannot reclaim {:?}", el));
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
2022-11-18 04:00:39 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
pub(crate) fn try_reclaim(&mut self, el: ElementId) -> Option<ElementRef> {
|
2022-11-30 22:21:10 +00:00
|
|
|
if el.0 == 0 {
|
|
|
|
panic!(
|
2022-12-05 21:03:52 +00:00
|
|
|
"Cannot reclaim the root element - {:#?}",
|
2022-11-30 22:21:10 +00:00
|
|
|
std::backtrace::Backtrace::force_capture()
|
2022-12-05 21:03:52 +00:00
|
|
|
);
|
2022-11-30 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
self.elements.try_remove(el.0)
|
|
|
|
}
|
2022-11-27 05:22:39 +00:00
|
|
|
|
2022-11-30 22:21:10 +00:00
|
|
|
pub(crate) fn update_template(&mut self, el: ElementId, node: &VNode) {
|
|
|
|
let node: *const VNode = node as *const _;
|
|
|
|
self.elements[el.0].template = unsafe { std::mem::transmute(node) };
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
// Drop a scope and all its children
|
|
|
|
pub(crate) fn drop_scope(&mut self, id: ScopeId) {
|
2022-12-05 21:03:52 +00:00
|
|
|
if let Some(root) = self.scopes[id.0].as_ref().try_root_node() {
|
|
|
|
if let RenderReturn::Sync(Ok(node)) = unsafe { root.extend_lifetime_ref() } {
|
2022-11-29 21:46:25 +00:00
|
|
|
self.drop_scope_inner(node)
|
2022-11-29 21:31:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-27 05:22:39 +00:00
|
|
|
|
2022-12-05 21:03:52 +00:00
|
|
|
self.scopes[id.0].props.take();
|
2022-11-29 21:31:04 +00:00
|
|
|
|
|
|
|
// Drop all the hooks once the children are dropped
|
|
|
|
// this means we'll drop hooks bottom-up
|
2022-12-05 21:03:52 +00:00
|
|
|
self.scopes[id.0]
|
|
|
|
.hook_list
|
|
|
|
.get_mut()
|
|
|
|
.drain(..)
|
|
|
|
.for_each(|hook| drop(unsafe { BumpBox::from_raw(hook) }));
|
2022-11-27 05:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
fn drop_scope_inner(&mut self, node: &VNode) {
|
2022-12-05 21:03:52 +00:00
|
|
|
node.clear_listeners();
|
|
|
|
node.dynamic_nodes.iter().for_each(|node| match node {
|
|
|
|
DynamicNode::Component(c) => self.drop_scope(c.scope.get().unwrap()),
|
|
|
|
DynamicNode::Fragment(nodes) => nodes
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|node| self.drop_scope_inner(node)),
|
|
|
|
_ => {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Descend through the tree, removing any borrowed props and listeners
|
|
|
|
pub(crate) fn ensure_drop_safety(&self, scope: ScopeId) {
|
|
|
|
let node = unsafe { self.scopes[scope.0].previous_frame().try_load_node() };
|
|
|
|
|
|
|
|
// And now we want to make sure the previous frame has dropped anything that borrows self
|
|
|
|
if let Some(RenderReturn::Sync(Ok(node))) = node {
|
|
|
|
self.ensure_drop_safety_inner(node);
|
2022-11-29 21:31:04 +00:00
|
|
|
}
|
2022-12-05 21:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_drop_safety_inner(&self, node: &VNode) {
|
|
|
|
node.clear_listeners();
|
2022-11-29 21:31:04 +00:00
|
|
|
|
2022-12-05 21:03:52 +00:00
|
|
|
node.dynamic_nodes.iter().for_each(|child| match child {
|
|
|
|
// Only descend if the props are borrowed
|
|
|
|
DynamicNode::Component(c) if !c.static_props => {
|
|
|
|
self.ensure_drop_safety(c.scope.get().unwrap());
|
|
|
|
c.props.set(None);
|
2022-11-29 21:31:04 +00:00
|
|
|
}
|
2022-12-05 21:03:52 +00:00
|
|
|
|
|
|
|
DynamicNode::Fragment(f) => f
|
|
|
|
.iter()
|
|
|
|
.for_each(|node| self.ensure_drop_safety_inner(node)),
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
});
|
2022-11-27 05:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 21:31:04 +00:00
|
|
|
|
2022-11-27 14:38:40 +00:00
|
|
|
impl ElementPath {
|
|
|
|
pub(crate) fn is_ascendant(&self, big: &&[u8]) -> bool {
|
|
|
|
match *self {
|
|
|
|
ElementPath::Deep(small) => small.len() <= big.len() && small == &big[..small.len()],
|
|
|
|
ElementPath::Root(r) => big.len() == 1 && big[0] == r as u8,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<&[u8]> for ElementPath {
|
|
|
|
fn eq(&self, other: &&[u8]) -> bool {
|
|
|
|
match *self {
|
|
|
|
ElementPath::Deep(deep) => deep.eq(*other),
|
|
|
|
ElementPath::Root(r) => other.len() == 1 && other[0] == r as u8,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|