scope_arena.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use crate::{
  2. any_props::AnyProps,
  3. bump_frame::BumpFrame,
  4. innerlude::DirtyScope,
  5. nodes::RenderReturn,
  6. scope_context::ScopeContext,
  7. scopes::{ScopeId, ScopeState},
  8. virtual_dom::VirtualDom,
  9. };
  10. impl VirtualDom {
  11. pub(super) fn new_scope(
  12. &mut self,
  13. props: Box<dyn AnyProps<'static>>,
  14. name: &'static str,
  15. ) -> &ScopeState {
  16. let parent_id = self.runtime.current_scope_id();
  17. let height = parent_id
  18. .and_then(|parent_id| self.get_scope(parent_id).map(|f| f.context().height + 1))
  19. .unwrap_or(0);
  20. let entry = self.scopes.vacant_entry();
  21. let id = ScopeId(entry.key());
  22. let scope = entry.insert(Box::new(ScopeState {
  23. runtime: self.runtime.clone(),
  24. context_id: id,
  25. props: Some(props),
  26. node_arena_1: BumpFrame::new(0),
  27. node_arena_2: BumpFrame::new(0),
  28. render_cnt: Default::default(),
  29. hooks: Default::default(),
  30. hook_idx: Default::default(),
  31. borrowed_props: Default::default(),
  32. attributes_to_drop_before_render: Default::default(),
  33. element_refs_to_drop: Default::default(),
  34. }));
  35. let context =
  36. ScopeContext::new(name, id, parent_id, height, self.runtime.scheduler.clone());
  37. self.runtime.create_context_at(id, context);
  38. scope
  39. }
  40. pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> &RenderReturn {
  41. self.runtime.scope_stack.borrow_mut().push(scope_id);
  42. // Cycle to the next frame and then reset it
  43. // This breaks any latent references, invalidating every pointer referencing into it.
  44. // Remove all the outdated listeners
  45. self.ensure_drop_safety(scope_id);
  46. let new_nodes = unsafe {
  47. let scope = &self.scopes[scope_id.0];
  48. scope.previous_frame().reset();
  49. scope.context().suspended.set(false);
  50. scope.hook_idx.set(0);
  51. // safety: due to how we traverse the tree, we know that the scope is not currently aliased
  52. let props: &dyn AnyProps = scope.props.as_ref().unwrap().as_ref();
  53. let props: &dyn AnyProps = std::mem::transmute(props);
  54. let _span = tracing::trace_span!("render", scope = %scope.context().name);
  55. props.render(scope).extend_lifetime()
  56. };
  57. let scope = &self.scopes[scope_id.0];
  58. // We write on top of the previous frame and then make it the current by pushing the generation forward
  59. let frame = scope.previous_frame();
  60. // set the new head of the bump frame
  61. let allocated = &*frame.bump().alloc(new_nodes);
  62. frame.node.set(allocated);
  63. // And move the render generation forward by one
  64. scope.render_cnt.set(scope.render_cnt.get() + 1);
  65. let context = scope.context();
  66. // remove this scope from dirty scopes
  67. self.dirty_scopes.remove(&DirtyScope {
  68. height: context.height,
  69. id: context.id,
  70. });
  71. if context.suspended.get() {
  72. if matches!(allocated, RenderReturn::Aborted(_)) {
  73. self.suspended_scopes.insert(context.id);
  74. }
  75. } else if !self.suspended_scopes.is_empty() {
  76. _ = self.suspended_scopes.remove(&context.id);
  77. }
  78. // rebind the lifetime now that its stored internally
  79. let result = unsafe { allocated.extend_lifetime_ref() };
  80. self.runtime.scope_stack.borrow_mut().pop();
  81. result
  82. }
  83. }