component.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use std::ops::Deref;
  2. use crate::{
  3. any_props::AnyProps,
  4. innerlude::{DirtyScope, ElementRef, MountId, VComponent, WriteMutations},
  5. nodes::RenderReturn,
  6. nodes::VNode,
  7. scopes::ScopeId,
  8. virtual_dom::VirtualDom,
  9. };
  10. impl VirtualDom {
  11. pub(crate) fn diff_scope(
  12. &mut self,
  13. to: &mut impl WriteMutations,
  14. scope: ScopeId,
  15. new_nodes: RenderReturn,
  16. ) {
  17. self.runtime.scope_stack.borrow_mut().push(scope);
  18. let scope_state = &mut self.scopes[scope.0];
  19. // Load the old and new bump arenas
  20. let new = &new_nodes;
  21. let old = scope_state.last_rendered_node.take().unwrap();
  22. old.diff_node(new, self, to);
  23. let scope_state = &mut self.scopes[scope.0];
  24. scope_state.last_rendered_node = Some(new_nodes);
  25. self.runtime.scope_stack.borrow_mut().pop();
  26. }
  27. /// Create a new template [`VNode`] and write it to the [`Mutations`] buffer.
  28. ///
  29. /// This method pushes the ScopeID to the internal scopestack and returns the number of nodes created.
  30. pub(crate) fn create_scope(
  31. &mut self,
  32. to: &mut impl WriteMutations,
  33. scope: ScopeId,
  34. new_node: RenderReturn,
  35. parent: Option<ElementRef>,
  36. ) -> usize {
  37. self.runtime.scope_stack.borrow_mut().push(scope);
  38. // Create the node
  39. let nodes = new_node.create(self, to, parent);
  40. // Then set the new node as the last rendered node
  41. self.scopes[scope.0].last_rendered_node = Some(new_node);
  42. self.runtime.scope_stack.borrow_mut().pop();
  43. nodes
  44. }
  45. }
  46. impl VNode {
  47. pub(crate) fn diff_vcomponent(
  48. &self,
  49. mount: MountId,
  50. idx: usize,
  51. new: &VComponent,
  52. old: &VComponent,
  53. scope_id: ScopeId,
  54. parent: Option<ElementRef>,
  55. dom: &mut VirtualDom,
  56. to: &mut impl WriteMutations,
  57. ) {
  58. println!(
  59. "diffing vcomponent: {new:#?} vs {old:#?}",
  60. new = new,
  61. old = old
  62. );
  63. // Replace components that have different render fns
  64. if old.render_fn != new.render_fn {
  65. println!("render fns are different, replacing");
  66. return self.replace_vcomponent(mount, idx, new, parent, dom, to);
  67. }
  68. println!("render fns are the same, continuing");
  69. // copy out the box for both
  70. let old_scope = &mut dom.scopes[scope_id.0];
  71. let old_props: &dyn AnyProps = old_scope.props.deref();
  72. let new_props: &dyn AnyProps = new.props.deref();
  73. // If the props are static, then we try to memoize by setting the new with the old
  74. // The target ScopeState still has the reference to the old props, so there's no need to update anything
  75. // This also implicitly drops the new props since they're not used
  76. if old_props.memoize(new_props.props()) {
  77. tracing::trace!(
  78. "Memoized props for component {:#?} ({})",
  79. scope_id,
  80. old_scope.context().name
  81. );
  82. return;
  83. }
  84. // First, move over the props from the old to the new, dropping old props in the process
  85. dom.scopes[scope_id.0].props = new.props.clone();
  86. // Now run the component and diff it
  87. let new = dom.run_scope(scope_id);
  88. dom.diff_scope(to, scope_id, new);
  89. let height = dom.runtime.get_context(scope_id).unwrap().height;
  90. dom.dirty_scopes.remove(&DirtyScope {
  91. height,
  92. id: scope_id,
  93. });
  94. }
  95. fn replace_vcomponent(
  96. &self,
  97. mount: MountId,
  98. idx: usize,
  99. new: &VComponent,
  100. parent: Option<ElementRef>,
  101. dom: &mut VirtualDom,
  102. to: &mut impl WriteMutations,
  103. ) {
  104. let scope = ScopeId(dom.mounts[mount.0].mounted_dynamic_nodes[idx]);
  105. let m = self.create_component_node(mount, idx, new, parent, dom, to);
  106. // Instead of *just* removing it, we can use the replace mutation
  107. dom.remove_component_node(to, scope, Some(m), true);
  108. }
  109. pub(super) fn create_component_node(
  110. &self,
  111. mount: MountId,
  112. idx: usize,
  113. component: &VComponent,
  114. parent: Option<ElementRef>,
  115. dom: &mut VirtualDom,
  116. to: &mut impl WriteMutations,
  117. ) -> usize {
  118. // Load up a ScopeId for this vcomponent. If it's already mounted, then we can just use that
  119. let scope = dom
  120. .new_scope(component.props.clone(), component.name)
  121. .context()
  122. .id;
  123. // Store the scope id for the next render
  124. dom.mounts[mount.0].mounted_dynamic_nodes[idx] = scope.0;
  125. let new = dom.run_scope(scope);
  126. dom.create_scope(to, scope, new, parent)
  127. }
  128. }