diffsupport.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /// Destroy a scope and all of its descendents.
  2. ///
  3. /// Calling this will run the destuctors on all hooks in the tree.
  4. /// It will also add the destroyed nodes to the `seen_nodes` cache to prevent them from being renderered.
  5. fn destroy_scopes(&mut self, old_scope: ScopeId) {
  6. let mut nodes_to_delete = vec![old_scope];
  7. let mut scopes_to_explore = vec![old_scope];
  8. // explore the scope tree breadth first
  9. while let Some(scope_id) = scopes_to_explore.pop() {
  10. // If we're planning on deleting this node, then we don't need to both rendering it
  11. self.seen_scopes.insert(scope_id);
  12. let scope = self.get_scope(&scope_id).unwrap();
  13. for child in scope.descendents.borrow().iter() {
  14. // Add this node to be explored
  15. scopes_to_explore.push(child.clone());
  16. // Also add it for deletion
  17. nodes_to_delete.push(child.clone());
  18. }
  19. }
  20. // Delete all scopes that we found as part of this subtree
  21. for node in nodes_to_delete {
  22. log::debug!("Removing scope {:#?}", node);
  23. let _scope = self.vdom.try_remove(node).unwrap();
  24. // do anything we need to do to delete the scope
  25. // I think we need to run the destructors on the hooks
  26. // TODO
  27. }
  28. }
  29. pub(crate) fn get_scope_mut(&mut self, id: &ScopeId) -> Option<&'bump mut Scope> {
  30. // ensure we haven't seen this scope before
  31. // if we have, then we're trying to alias it, which is not allowed
  32. debug_assert!(!self.seen_scopes.contains(id));
  33. unsafe { self.vdom.get_scope_mut(*id) }
  34. }
  35. pub(crate) fn get_scope(&mut self, id: &ScopeId) -> Option<&'bump Scope> {
  36. // ensure we haven't seen this scope before
  37. // if we have, then we're trying to alias it, which is not allowed
  38. unsafe { self.vdom.get_scope(*id) }
  39. }
  40. fn compare_strs(a: &str, b: &str) -> bool {
  41. // Check by pointer, optimizing for static strs
  42. if !std::ptr::eq(a, b) {
  43. // If the pointers are different then check by value
  44. a == b
  45. } else {
  46. true
  47. }
  48. }
  49. fn find_first_real_node<'a>(
  50. nodes: impl IntoIterator<Item = &'a VNode<'a>>,
  51. scopes: &'a SharedResources,
  52. ) -> Option<&'a VNode<'a>> {
  53. for node in nodes {
  54. let mut iter = RealChildIterator::new(node, scopes);
  55. if let Some(node) = iter.next() {
  56. return Some(node);
  57. }
  58. }
  59. None
  60. }
  61. fn remove_children(&mut self, old: &'bump [VNode<'bump>]) {
  62. self.replace_and_create_many_with_many(old, None)
  63. }