scope_arena.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }));
  34. let context =
  35. ScopeContext::new(name, id, parent_id, height, self.runtime.scheduler.clone());
  36. self.runtime.create_context_at(id, context);
  37. scope
  38. }
  39. pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> &RenderReturn {
  40. self.runtime.scope_stack.borrow_mut().push(scope_id);
  41. // Cycle to the next frame and then reset it
  42. // This breaks any latent references, invalidating every pointer referencing into it.
  43. // Remove all the outdated listeners
  44. self.ensure_drop_safety(scope_id);
  45. let new_nodes = unsafe {
  46. let scope = &self.scopes[scope_id.0];
  47. scope.previous_frame().reset();
  48. scope.context().suspended.set(false);
  49. scope.hook_idx.set(0);
  50. // safety: due to how we traverse the tree, we know that the scope is not currently aliased
  51. let props: &dyn AnyProps = scope.props.as_ref().unwrap().as_ref();
  52. let props: &dyn AnyProps = std::mem::transmute(props);
  53. let _span = tracing::trace_span!("render", scope = %scope.context().name);
  54. props.render(scope).extend_lifetime()
  55. };
  56. let scope = &self.scopes[scope_id.0];
  57. // We write on top of the previous frame and then make it the current by pushing the generation forward
  58. let frame = scope.previous_frame();
  59. // set the new head of the bump frame
  60. let allocated = &*frame.bump().alloc(new_nodes);
  61. frame.node.set(allocated);
  62. // And move the render generation forward by one
  63. scope.render_cnt.set(scope.render_cnt.get() + 1);
  64. let context = scope.context();
  65. // remove this scope from dirty scopes
  66. self.dirty_scopes.remove(&DirtyScope {
  67. height: context.height,
  68. id: context.id,
  69. });
  70. if context.suspended.get() {
  71. if matches!(allocated, RenderReturn::Aborted(_)) {
  72. self.suspended_scopes.insert(context.id);
  73. }
  74. } else if !self.suspended_scopes.is_empty() {
  75. _ = self.suspended_scopes.remove(&context.id);
  76. }
  77. // rebind the lifetime now that its stored internally
  78. let result = unsafe { allocated.extend_lifetime_ref() };
  79. self.runtime.scope_stack.borrow_mut().pop();
  80. result
  81. }
  82. }