dioxus/packages/core/src/arena.rs

200 lines
6.4 KiB
Rust
Raw Normal View History

use std::ptr::NonNull;
use crate::{
nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, AttributeValue, DynamicNode,
ScopeId,
};
2022-11-29 16:31:04 -05:00
use bumpalo::boxed::Box as BumpBox;
2022-11-01 18:42:29 -07:00
2022-12-02 16:24:49 -08: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-15 16:05:22 -08:00
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ElementId(pub usize);
2022-11-29 16:31:04 -05:00
pub(crate) struct ElementRef {
2022-11-17 20:00:39 -08:00
// the pathway of the real element inside the template
2022-11-27 09:38:40 -05:00
pub path: ElementPath,
2022-11-17 20:00:39 -08:00
// The actual template
pub template: Option<NonNull<VNode<'static>>>,
2022-11-01 18:42:29 -07:00
}
2022-11-27 09:38:40 -05:00
#[derive(Clone, Copy)]
pub enum ElementPath {
Deep(&'static [u8]),
Root(usize),
}
2022-11-17 20:00:39 -08:00
impl ElementRef {
pub(crate) fn none() -> Self {
2022-11-11 18:29:27 -08:00
Self {
template: None,
2022-11-27 09:38:40 -05:00
path: ElementPath::Root(0),
2022-11-11 18:29:27 -08:00
}
}
}
2022-11-29 16:46:25 -05:00
impl VirtualDom {
2022-11-29 16:31:04 -05:00
pub(crate) fn next_element(&mut self, template: &VNode, path: &'static [u8]) -> ElementId {
2022-12-12 18:31:30 -08:00
self.next_reference(template, ElementPath::Deep(path))
2022-11-27 09:38:40 -05:00
}
2022-11-29 16:31:04 -05:00
pub(crate) fn next_root(&mut self, template: &VNode, path: usize) -> ElementId {
2022-12-12 18:31:30 -08:00
self.next_reference(template, ElementPath::Root(path))
2022-12-05 14:16:54 -08:00
}
2023-01-07 19:01:29 -06:00
pub(crate) fn next_null(&mut self) -> ElementId {
let entry = self.elements.vacant_entry();
let id = entry.key();
entry.insert(ElementRef::none());
2023-01-07 19:01:29 -06:00
ElementId(id)
}
2022-12-12 18:31:30 -08:00
fn next_reference(&mut self, template: &VNode, path: ElementPath) -> ElementId {
2022-11-27 09:38:40 -05:00
let entry = self.elements.vacant_entry();
let id = entry.key();
2022-12-05 14:16:54 -08:00
2022-11-27 09:38:40 -05:00
entry.insert(ElementRef {
// We know this is non-null because it comes from a reference
template: Some(unsafe { NonNull::new_unchecked(template as *const _ as *mut _) }),
2022-12-05 14:16:54 -08:00
path,
2022-11-01 18:42:29 -07:00
});
ElementId(id)
}
2022-11-29 16:31:04 -05:00
pub(crate) fn reclaim(&mut self, el: ElementId) {
self.try_reclaim(el)
.unwrap_or_else(|| panic!("cannot reclaim {:?}", el));
}
2022-11-17 20:00:39 -08:00
2022-11-29 16:31:04 -05:00
pub(crate) fn try_reclaim(&mut self, el: ElementId) -> Option<ElementRef> {
2022-11-30 17:21:10 -05:00
if el.0 == 0 {
panic!(
2022-12-05 13:03:52 -08:00
"Cannot reclaim the root element - {:#?}",
2022-11-30 17:21:10 -05:00
std::backtrace::Backtrace::force_capture()
2022-12-05 13:03:52 -08:00
);
2022-11-30 17:21:10 -05:00
}
2022-11-29 16:31:04 -05:00
self.elements.try_remove(el.0)
}
2022-11-27 00:22:39 -05:00
2022-11-30 17:21:10 -05: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 16:31:04 -05:00
// Drop a scope and all its children
pub(crate) fn drop_scope(&mut self, id: ScopeId) {
2022-12-16 20:26:04 -08:00
self.ensure_drop_safety(id);
2022-12-05 13:03:52 -08:00
if let Some(root) = self.scopes[id.0].as_ref().try_root_node() {
2022-12-20 11:13:06 -08:00
if let RenderReturn::Ready(node) = unsafe { root.extend_lifetime_ref() } {
2022-11-29 16:46:25 -05:00
self.drop_scope_inner(node)
2022-11-29 16:31:04 -05:00
}
}
2022-12-16 20:26:04 -08:00
if let Some(root) = unsafe { self.scopes[id.0].as_ref().previous_frame().try_load_node() } {
2022-12-20 11:13:06 -08:00
if let RenderReturn::Ready(node) = unsafe { root.extend_lifetime_ref() } {
2022-12-16 20:26:04 -08:00
self.drop_scope_inner(node)
}
}
2022-12-05 13:03:52 -08:00
self.scopes[id.0].props.take();
2022-11-29 16:31:04 -05:00
2022-12-06 15:37:06 -08:00
let scope = &mut self.scopes[id.0];
2022-11-29 16:31:04 -05:00
// Drop all the hooks once the children are dropped
// this means we'll drop hooks bottom-up
2022-12-06 15:37:06 -08:00
for hook in scope.hook_list.get_mut().drain(..) {
drop(unsafe { BumpBox::from_raw(hook) });
}
2022-11-27 00:22:39 -05:00
}
2022-11-29 16:31:04 -05:00
fn drop_scope_inner(&mut self, node: &VNode) {
2022-12-05 13:03:52 -08:00
node.clear_listeners();
node.dynamic_nodes.iter().for_each(|node| match node {
2022-12-14 08:51:28 -08:00
DynamicNode::Component(c) => {
if let Some(f) = c.scope.get() {
2022-12-16 20:26:04 -08:00
self.drop_scope(f);
2022-12-14 08:51:28 -08:00
}
2022-12-16 20:26:04 -08:00
c.props.take();
2022-12-14 08:51:28 -08:00
}
2022-12-06 17:44:29 -08:00
DynamicNode::Fragment(nodes) => {
nodes.iter().for_each(|node| self.drop_scope_inner(node))
}
2022-12-05 14:16:54 -08:00
DynamicNode::Placeholder(t) => {
if let Some(id) = t.id.get() {
self.try_reclaim(id);
}
2022-12-05 14:16:54 -08:00
}
DynamicNode::Text(t) => {
if let Some(id) = t.id.get() {
self.try_reclaim(id);
}
2022-12-05 14:16:54 -08:00
}
});
for id in &node.root_ids {
if id.0 != 0 {
self.try_reclaim(id);
2022-12-05 14:16:54 -08:00
}
}
2022-12-05 13:03:52 -08:00
}
/// Descend through the tree, removing any borrowed props and listeners
2022-12-22 11:30:30 -05:00
pub(crate) fn ensure_drop_safety(&self, scope_id: ScopeId) {
let scope = &self.scopes[scope_id.0];
// make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
// run the hooks (which hold an &mut Reference)
// recursively call ensure_drop_safety on all children
let mut props = scope.borrowed_props.borrow_mut();
props.drain(..).for_each(|comp| {
let comp = unsafe { &*comp };
2022-12-22 11:30:30 -05:00
match comp.scope.get() {
Some(child) if child != scope_id => self.ensure_drop_safety(child),
_ => (),
2022-11-29 16:31:04 -05:00
}
2022-12-22 11:32:42 -05:00
if let Ok(mut props) = comp.props.try_borrow_mut() {
*props = None;
2022-11-29 16:31:04 -05:00
}
});
2022-12-05 13:03:52 -08:00
// Now that all the references are gone, we can safely drop our own references in our listeners.
2022-12-26 10:53:25 -06:00
let mut listeners = scope.attributes_to_drop.borrow_mut();
listeners.drain(..).for_each(|listener| {
let listener = unsafe { &*listener };
2022-12-26 10:53:25 -06:00
match &listener.value {
AttributeValue::Listener(l) => {
_ = l.take();
2022-12-26 10:53:25 -06:00
}
2023-01-02 21:26:12 -06:00
AttributeValue::Any(a) => {
_ = a.take();
2022-12-26 10:53:25 -06:00
}
_ => (),
}
2022-12-05 13:03:52 -08:00
});
2022-11-27 00:22:39 -05:00
}
}
2022-11-29 16:31:04 -05:00
2022-11-27 09:38:40 -05: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,
}
}
}