Add flush_sync and pre_render methods to core

This commit is contained in:
Jonathan Kelley committed 2024-01-28 01:30:33 -08:00
1 parent 3ccf520205
commit 7b0dcb3206
17 files changed
+378 -282

No files matched your search

+18 -9
View File
@@ -2,7 +2,7 @@ use crate::{
any_props::{AnyProps, BoxedAnyProps},
innerlude::{DirtyScope, ScopeState},
nodes::RenderReturn,
scope_context::ScopeContext,
scope_context::Scope,
scopes::ScopeId,
virtual_dom::VirtualDom,
};
@@ -11,7 +11,7 @@ impl VirtualDom {
pub(super) fn new_scope(&mut self, props: BoxedAnyProps, name: &'static str) -> &ScopeState {
let parent_id = self.runtime.current_scope_id();
let height = parent_id
.and_then(|parent_id| self.runtime.get_context(parent_id).map(|f| f.height + 1))
.and_then(|parent_id| self.runtime.get_state(parent_id).map(|f| f.height + 1))
.unwrap_or(0);
let entry = self.scopes.vacant_entry();
let id = ScopeId(entry.key());
@@ -19,34 +19,43 @@ impl VirtualDom {
let scope = entry.insert(ScopeState {
runtime: self.runtime.clone(),
context_id: id,
props,
last_rendered_node: Default::default(),
});
let context = ScopeContext::new(name, id, parent_id, height);
self.runtime.create_context_at(id, context);
self.runtime
.create_scope(Scope::new(name, id, parent_id, height));
scope
}
pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> RenderReturn {
self.runtime.scope_stack.borrow_mut().push(scope_id);
debug_assert!(
crate::Runtime::current().is_some(),
"Must be in a dioxus runtime"
);
self.runtime.scope_stack.borrow_mut().push(scope_id);
let scope = &self.scopes[scope_id.0];
let new_nodes = {
let context = scope.context();
let context = scope.state();
context.suspended.set(false);
context.hook_index.set(0);
// Run all pre-render hooks
for pre_run in context.before_render.borrow_mut().iter_mut() {
pre_run();
}
// safety: due to how we traverse the tree, we know that the scope is not currently aliased
let props: &dyn AnyProps = &*scope.props;
let span = tracing::trace_span!("render", scope = %scope.context().name);
let span = tracing::trace_span!("render", scope = %scope.state().name);
span.in_scope(|| props.render())
};
let context = scope.context();
let context = scope.state();
// And move the render generation forward by one
context.render_count.set(context.render_count.get() + 1);