Skip to main content

dioxus_core/
arena.rs

1use crate::innerlude::ScopeOrder;
2use crate::{ScopeId, virtual_dom::VirtualDom};
3
4/// An Element's unique identifier.
5///
6/// `ElementId` is a `usize` that is unique across the entire VirtualDOM - but not unique across time. If a component is
7/// unmounted, then the `ElementId` will be reused for a new component.
8#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
9#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
10pub struct ElementId(pub usize);
11
12/// An Element that can be bubbled to's unique identifier.
13///
14/// `BubbleId` is a `usize` that is unique across the entire VirtualDOM - but not unique across time. If a component is
15/// unmounted, then the `BubbleId` will be reused for a new component.
16#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
17#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
18pub(crate) struct MountId(pub(crate) usize);
19
20impl Default for MountId {
21    fn default() -> Self {
22        Self::PLACEHOLDER
23    }
24}
25
26impl MountId {
27    pub(crate) const PLACEHOLDER: Self = Self(usize::MAX);
28
29    pub(crate) fn as_usize(self) -> Option<usize> {
30        if self.mounted() { Some(self.0) } else { None }
31    }
32
33    #[allow(unused)]
34    pub(crate) fn mounted(self) -> bool {
35        self != Self::PLACEHOLDER
36    }
37}
38
39#[derive(Debug, Clone, Copy)]
40pub struct ElementRef {
41    // the pathway of the real element inside the template
42    pub(crate) path: ElementPath,
43
44    // The actual element
45    pub(crate) mount: MountId,
46}
47
48#[derive(Clone, Copy, Debug)]
49pub struct ElementPath {
50    pub(crate) path: &'static [u8],
51}
52
53impl VirtualDom {
54    pub(crate) fn next_element(&mut self) -> ElementId {
55        let mut elements = self.runtime.elements.borrow_mut();
56        ElementId(elements.insert(None))
57    }
58
59    pub(crate) fn reclaim(&mut self, el: ElementId) {
60        if !self.try_reclaim(el) {
61            tracing::error!("cannot reclaim {:?}", el);
62        }
63    }
64
65    pub(crate) fn try_reclaim(&mut self, el: ElementId) -> bool {
66        // We never reclaim the unmounted elements or the root element
67        if el.0 == 0 || el.0 == usize::MAX {
68            return true;
69        }
70
71        let mut elements = self.runtime.elements.borrow_mut();
72        elements.try_remove(el.0).is_some()
73    }
74
75    // Drop a scope without dropping its children
76    //
77    // Note: This will not remove any ids from the arena
78    pub(crate) fn drop_scope(&mut self, id: ScopeId) {
79        let height = {
80            let scope = self.scopes.remove(id.0);
81            let context = scope.state();
82            context.height
83        };
84
85        self.dirty_scopes.remove(&ScopeOrder::new(height, id));
86
87        // If this scope was a suspense boundary, remove it from the resolved scopes
88        self.resolved_scopes.retain(|s| s != &id);
89    }
90}
91
92impl ElementPath {
93    pub(crate) fn is_descendant(&self, small: &[u8]) -> bool {
94        small.len() <= self.path.len() && small == &self.path[..small.len()]
95    }
96}
97
98#[test]
99fn is_descendant() {
100    let event_path = ElementPath {
101        path: &[1, 2, 3, 4, 5],
102    };
103
104    assert!(event_path.is_descendant(&[1, 2, 3, 4, 5]));
105    assert!(event_path.is_descendant(&[1, 2, 3, 4]));
106    assert!(event_path.is_descendant(&[1, 2, 3]));
107    assert!(event_path.is_descendant(&[1, 2]));
108    assert!(event_path.is_descendant(&[1]));
109
110    assert!(!event_path.is_descendant(&[1, 2, 3, 4, 5, 6]));
111    assert!(!event_path.is_descendant(&[2, 3, 4]));
112}
113
114impl PartialEq<&[u8]> for ElementPath {
115    fn eq(&self, other: &&[u8]) -> bool {
116        self.path.eq(*other)
117    }
118}