arena.rs 3.5 KB

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