arena.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.mounted() {
  26. Some(self.0)
  27. } else {
  28. None
  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. let mut elements = self.runtime.elements.borrow_mut();
  50. ElementId(elements.insert(None))
  51. }
  52. pub(crate) fn reclaim(&mut self, el: ElementId) {
  53. if !self.try_reclaim(el) {
  54. tracing::error!("cannot reclaim {:?}", el);
  55. }
  56. }
  57. pub(crate) fn try_reclaim(&mut self, el: ElementId) -> bool {
  58. // We never reclaim the unmounted elements or the root element
  59. if el.0 == 0 || el.0 == usize::MAX {
  60. return true;
  61. }
  62. let mut elements = self.runtime.elements.borrow_mut();
  63. elements.try_remove(el.0).is_some()
  64. }
  65. // Drop a scope without dropping its children
  66. //
  67. // Note: This will not remove any ids from the arena
  68. pub(crate) fn drop_scope(&mut self, id: ScopeId) {
  69. let height = {
  70. let scope = self.scopes.remove(id.0);
  71. let context = scope.state();
  72. context.height
  73. };
  74. self.dirty_scopes.remove(&ScopeOrder::new(height, id));
  75. // If this scope was a suspense boundary, remove it from the resolved scopes
  76. self.resolved_scopes.retain(|s| s != &id);
  77. }
  78. }
  79. impl ElementPath {
  80. pub(crate) fn is_descendant(&self, small: &[u8]) -> bool {
  81. small.len() <= self.path.len() && small == &self.path[..small.len()]
  82. }
  83. }
  84. #[test]
  85. fn is_descendant() {
  86. let event_path = ElementPath {
  87. path: &[1, 2, 3, 4, 5],
  88. };
  89. assert!(event_path.is_descendant(&[1, 2, 3, 4, 5]));
  90. assert!(event_path.is_descendant(&[1, 2, 3, 4]));
  91. assert!(event_path.is_descendant(&[1, 2, 3]));
  92. assert!(event_path.is_descendant(&[1, 2]));
  93. assert!(event_path.is_descendant(&[1]));
  94. assert!(!event_path.is_descendant(&[1, 2, 3, 4, 5, 6]));
  95. assert!(!event_path.is_descendant(&[2, 3, 4]));
  96. }
  97. impl PartialEq<&[u8]> for ElementPath {
  98. fn eq(&self, other: &&[u8]) -> bool {
  99. self.path.eq(*other)
  100. }
  101. }