use crate::innerlude::ScopeOrder;
use crate::{virtual_dom::VirtualDom, ScopeId};
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ElementId(pub usize);
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct MountId(pub(crate) usize);
impl Default for MountId {
fn default() -> Self {
Self::PLACEHOLDER
}
}
impl MountId {
pub(crate) const PLACEHOLDER: Self = Self(usize::MAX);
pub(crate) fn as_usize(self) -> Option<usize> {
if self.mounted() {
Some(self.0)
} else {
None
}
}
#[allow(unused)]
pub(crate) fn mounted(self) -> bool {
self != Self::PLACEHOLDER
}
}
#[derive(Debug, Clone, Copy)]
pub struct ElementRef {
pub(crate) path: ElementPath,
pub(crate) mount: MountId,
}
#[derive(Clone, Copy, Debug)]
pub struct ElementPath {
pub(crate) path: &'static [u8],
}
impl VirtualDom {
pub(crate) fn next_element(&mut self) -> ElementId {
let mut elements = self.runtime.elements.borrow_mut();
ElementId(elements.insert(None))
}
pub(crate) fn reclaim(&mut self, el: ElementId) {
if !self.try_reclaim(el) {
tracing::error!("cannot reclaim {:?}", el);
}
}
pub(crate) fn try_reclaim(&mut self, el: ElementId) -> bool {
if el.0 == 0 || el.0 == usize::MAX {
return true;
}
let mut elements = self.runtime.elements.borrow_mut();
elements.try_remove(el.0).is_some()
}
pub(crate) fn drop_scope(&mut self, id: ScopeId) {
let height = {
let scope = self.scopes.remove(id.0);
let context = scope.state();
context.height
};
self.dirty_scopes.remove(&ScopeOrder::new(height, id));
self.resolved_scopes.retain(|s| s != &id);
}
}
impl ElementPath {
pub(crate) fn is_descendant(&self, small: &[u8]) -> bool {
small.len() <= self.path.len() && small == &self.path[..small.len()]
}
}
#[test]
fn is_descendant() {
let event_path = ElementPath {
path: &[1, 2, 3, 4, 5],
};
assert!(event_path.is_descendant(&[1, 2, 3, 4, 5]));
assert!(event_path.is_descendant(&[1, 2, 3, 4]));
assert!(event_path.is_descendant(&[1, 2, 3]));
assert!(event_path.is_descendant(&[1, 2]));
assert!(event_path.is_descendant(&[1]));
assert!(!event_path.is_descendant(&[1, 2, 3, 4, 5, 6]));
assert!(!event_path.is_descendant(&[2, 3, 4]));
}
impl PartialEq<&[u8]> for ElementPath {
fn eq(&self, other: &&[u8]) -> bool {
self.path.eq(*other)
}
}