1
0

scope_arena.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use crate::innerlude::ScopeOrder;
  2. use crate::{
  3. any_props::{AnyProps, BoxedAnyProps},
  4. innerlude::ScopeState,
  5. nodes::RenderReturn,
  6. scope_context::Scope,
  7. scopes::ScopeId,
  8. virtual_dom::VirtualDom,
  9. };
  10. impl VirtualDom {
  11. pub(super) fn new_scope(&mut self, props: BoxedAnyProps, name: &'static str) -> &ScopeState {
  12. let parent_id = self.runtime.current_scope_id();
  13. let height = parent_id
  14. .and_then(|parent_id| self.runtime.get_state(parent_id).map(|f| f.height + 1))
  15. .unwrap_or(0);
  16. let entry = self.scopes.vacant_entry();
  17. let id = ScopeId(entry.key());
  18. let scope = entry.insert(ScopeState {
  19. runtime: self.runtime.clone(),
  20. context_id: id,
  21. props,
  22. last_rendered_node: Default::default(),
  23. });
  24. self.runtime
  25. .create_scope(Scope::new(name, id, parent_id, height));
  26. scope
  27. }
  28. pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> RenderReturn {
  29. debug_assert!(
  30. crate::Runtime::current().is_some(),
  31. "Must be in a dioxus runtime"
  32. );
  33. self.runtime.scope_stack.borrow_mut().push(scope_id);
  34. let scope = &self.scopes[scope_id.0];
  35. let new_nodes = {
  36. let context = scope.state();
  37. context.hook_index.set(0);
  38. // Run all pre-render hooks
  39. for pre_run in context.before_render.borrow_mut().iter_mut() {
  40. pre_run();
  41. }
  42. // safety: due to how we traverse the tree, we know that the scope is not currently aliased
  43. let props: &dyn AnyProps = &*scope.props;
  44. let span = tracing::trace_span!("render", scope = %scope.state().name);
  45. span.in_scope(|| props.render())
  46. };
  47. let context = scope.state();
  48. // Run all post-render hooks
  49. for post_run in context.after_render.borrow_mut().iter_mut() {
  50. post_run();
  51. }
  52. // And move the render generation forward by one
  53. context.render_count.set(context.render_count.get() + 1);
  54. // remove this scope from dirty scopes
  55. self.dirty_scopes
  56. .remove(&ScopeOrder::new(context.height, scope_id));
  57. if let Some(task) = context.last_suspendable_task.take() {
  58. if matches!(new_nodes, RenderReturn::Aborted(_)) {
  59. tracing::trace!("Suspending {:?} on {:?}", scope_id, task);
  60. self.runtime.tasks.borrow().get(task.0).unwrap().suspend();
  61. self.runtime
  62. .suspended_tasks
  63. .set(self.runtime.suspended_tasks.get() + 1);
  64. }
  65. }
  66. self.runtime.scope_stack.borrow_mut().pop();
  67. new_nodes
  68. }
  69. }