scope_arena.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use crate::{
  2. any_props::{AnyProps, BoxedAnyProps},
  3. innerlude::DirtyScope,
  4. nodes::RenderReturn,
  5. scope_context::ScopeContext,
  6. scopes::{ScopeId, ScopeState},
  7. virtual_dom::VirtualDom,
  8. };
  9. impl VirtualDom {
  10. pub(super) fn new_scope(&mut self, props: BoxedAnyProps, name: &'static str) -> &ScopeState {
  11. let parent_id = self.runtime.current_scope_id();
  12. let height = parent_id
  13. .and_then(|parent_id| self.runtime.get_context(parent_id).map(|f| f.height + 1))
  14. .unwrap_or(0);
  15. let entry = self.scopes.vacant_entry();
  16. let id = ScopeId(entry.key());
  17. let scope = entry.insert(ScopeState {
  18. runtime: self.runtime.clone(),
  19. context_id: id,
  20. props,
  21. last_rendered_node: Default::default(),
  22. });
  23. let context =
  24. ScopeContext::new(name, id, parent_id, height, self.runtime.scheduler.clone());
  25. self.runtime.create_context_at(id, context);
  26. scope
  27. }
  28. pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> RenderReturn {
  29. self.runtime.scope_stack.borrow_mut().push(scope_id);
  30. let new_nodes = {
  31. let scope = &self.scopes[scope_id.0];
  32. let context = scope.context();
  33. context.suspended.set(false);
  34. context.hook_index.set(0);
  35. // safety: due to how we traverse the tree, we know that the scope is not currently aliased
  36. let props: &dyn AnyProps = &*scope.props;
  37. let _span = tracing::trace_span!("render", scope = %scope.context().name);
  38. props.render()
  39. };
  40. let scope = &mut self.scopes[scope_id.0];
  41. let context = scope.context();
  42. // And move the render generation forward by one
  43. context.render_count.set(context.render_count.get() + 1);
  44. // remove this scope from dirty scopes
  45. self.dirty_scopes.remove(&DirtyScope {
  46. height: context.height,
  47. id: context.id,
  48. });
  49. if context.suspended.get() {
  50. if matches!(new_nodes, RenderReturn::Aborted(_)) {
  51. self.suspended_scopes.insert(context.id);
  52. }
  53. } else if !self.suspended_scopes.is_empty() {
  54. _ = self.suspended_scopes.remove(&context.id);
  55. }
  56. self.runtime.scope_stack.borrow_mut().pop();
  57. new_nodes
  58. }
  59. }