dioxus/packages/core/src/scope_arena.rs

178 lines
6.2 KiB
Rust
Raw Normal View History

use crate::{
any_props::AnyProps,
bump_frame::BumpFrame,
2022-11-04 03:56:31 +00:00
factory::RenderReturn,
2022-11-23 02:38:27 +00:00
innerlude::DirtyScope,
2022-11-08 06:55:22 +00:00
innerlude::{SuspenseId, SuspenseLeaf},
2022-11-09 18:58:11 +00:00
scheduler::RcWake,
2022-11-02 01:42:29 +00:00
scopes::{ScopeId, ScopeState},
2022-11-09 03:39:37 +00:00
virtual_dom::VirtualDom,
AttributeValue, DynamicNode, VNode,
};
2022-11-09 18:58:11 +00:00
use futures_util::FutureExt;
use std::{
mem,
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
impl VirtualDom {
2022-11-29 21:31:04 +00:00
pub(super) fn new_scope(&mut self, props: Box<dyn AnyProps<'static>>) -> &mut ScopeState {
2022-11-02 01:42:29 +00:00
let parent = self.acquire_current_scope_raw();
let entry = self.scopes.vacant_entry();
2022-11-23 05:32:26 +00:00
let height = unsafe { parent.map(|f| (*f).height + 1).unwrap_or(0) };
2022-11-02 01:42:29 +00:00
let id = ScopeId(entry.key());
2022-11-29 21:31:04 +00:00
entry.insert(Box::new(ScopeState {
parent,
2022-11-02 01:42:29 +00:00
id,
height,
props,
2022-11-20 01:07:29 +00:00
placeholder: Default::default(),
node_arena_1: BumpFrame::new(50),
node_arena_2: BumpFrame::new(50),
2022-11-09 18:58:11 +00:00
spawned_tasks: Default::default(),
render_cnt: Default::default(),
hook_arena: Default::default(),
hook_list: Default::default(),
hook_idx: Default::default(),
2022-11-02 01:42:29 +00:00
shared_contexts: Default::default(),
2022-11-09 18:58:11 +00:00
tasks: self.scheduler.clone(),
2022-11-29 21:31:04 +00:00
}))
2022-11-02 01:42:29 +00:00
}
fn acquire_current_scope_raw(&mut self) -> Option<*mut ScopeState> {
self.scope_stack
.last()
.copied()
2022-11-29 21:31:04 +00:00
.and_then(|id| self.scopes.get_mut(id.0).map(|f| f.as_mut() as *mut _))
}
pub fn ensure_drop_safety(&self, scope: ScopeId) {
let scope = &self.scopes[scope.0];
let node = unsafe { scope.previous_frame().try_load_node() };
// And now we want to make sure the previous frame has dropped anything that borrows self
if let Some(RenderReturn::Sync(Ok(node))) = node {
self.ensure_drop_safety_inner(node);
}
}
fn ensure_drop_safety_inner(&self, node: &VNode) {
for attr in node.dynamic_attrs {
if let AttributeValue::Listener(l) = &attr.value {
l.borrow_mut().take();
}
}
for child in node.dynamic_nodes {
match child {
DynamicNode::Component(c) => {
// Only descend if the props are borrowed
if !c.static_props {
self.ensure_drop_safety(c.scope.get().unwrap());
c.props.set(None);
}
}
DynamicNode::Fragment(f) => {
2022-11-29 21:31:04 +00:00
for node in *f {
self.ensure_drop_safety_inner(node);
}
}
_ => {}
}
}
2022-11-02 01:42:29 +00:00
}
2022-11-09 18:58:11 +00:00
pub(crate) fn run_scope(&mut self, scope_id: ScopeId) -> &RenderReturn {
2022-11-29 21:31:04 +00:00
// Cycle to the next frame and then reset it
// This breaks any latent references, invalidating every pointer referencing into it.
// Remove all the outdated listeners
self.ensure_drop_safety(scope_id);
2022-11-08 06:55:22 +00:00
let mut new_nodes = unsafe {
2022-11-29 21:31:04 +00:00
let scope = self.scopes[scope_id.0].as_mut();
scope.previous_frame_mut().bump.reset();
// Make sure to reset the hook counter so we give out hooks in the right order
2022-11-08 06:55:22 +00:00
scope.hook_idx.set(0);
2022-11-09 18:58:11 +00:00
// safety: due to how we traverse the tree, we know that the scope is not currently aliased
2022-11-20 01:07:29 +00:00
let props: &dyn AnyProps = mem::transmute(&*scope.props);
2022-11-09 18:58:11 +00:00
props.render(scope).extend_lifetime()
2022-11-03 09:11:04 +00:00
};
2022-11-08 06:55:22 +00:00
// immediately resolve futures that can be resolved
if let RenderReturn::Async(task) = &mut new_nodes {
2022-11-09 18:58:11 +00:00
let mut leaves = self.scheduler.leaves.borrow_mut();
2022-11-08 06:55:22 +00:00
let entry = leaves.vacant_entry();
2022-11-09 18:58:11 +00:00
let suspense_id = SuspenseId(entry.key());
2022-11-08 06:55:22 +00:00
let leaf = Rc::new(SuspenseLeaf {
scope_id,
task: task.as_mut(),
2022-11-09 03:39:37 +00:00
id: suspense_id,
2022-11-09 18:58:11 +00:00
tx: self.scheduler.sender.clone(),
notified: Default::default(),
2022-11-08 06:55:22 +00:00
});
let waker = leaf.waker();
let mut cx = Context::from_waker(&waker);
2022-11-09 18:58:11 +00:00
// safety: the task is already pinned in the bump arena
2022-11-08 06:55:22 +00:00
let mut pinned = unsafe { Pin::new_unchecked(task.as_mut()) };
2022-11-09 18:58:11 +00:00
// Keep polling until either we get a value or the future is not ready
2022-11-08 06:55:22 +00:00
loop {
match pinned.poll_unpin(&mut cx) {
// If nodes are produced, then set it and we can break
Poll::Ready(nodes) => {
new_nodes = RenderReturn::Sync(nodes);
break;
}
// If no nodes are produced but the future woke up immediately, then try polling it again
// This circumvents things like yield_now, but is important is important when rendering
// components that are just a stream of immediately ready futures
2022-11-09 18:58:11 +00:00
_ if leaf.notified.get() => {
leaf.notified.set(false);
2022-11-08 06:55:22 +00:00
continue;
}
// If no nodes are produced, then we need to wait for the future to be woken up
// Insert the future into fiber leaves and break
_ => {
entry.insert(leaf);
2022-11-09 18:58:11 +00:00
self.collected_leaves.push(suspense_id);
2022-11-08 06:55:22 +00:00
break;
}
};
2022-11-04 05:30:26 +00:00
}
};
2022-11-20 01:07:29 +00:00
let scope = &self.scopes[scope_id.0];
2022-11-18 06:31:14 +00:00
2022-11-20 01:07:29 +00:00
// We write on top of the previous frame and then make it the current by pushing the generation forward
let frame = scope.previous_frame();
2022-11-20 01:07:29 +00:00
// set the new head of the bump frame
2022-11-29 21:31:04 +00:00
let alloced = &*frame.bump.alloc(new_nodes);
frame.node.set(alloced);
2022-11-18 06:31:14 +00:00
// And move the render generation forward by one
scope.render_cnt.set(scope.render_cnt.get() + 1);
2022-11-20 23:58:05 +00:00
// remove this scope from dirty scopes
self.dirty_scopes.remove(&DirtyScope {
height: scope.height,
id: scope.id,
});
// rebind the lifetime now that its stored internally
2022-11-08 06:55:22 +00:00
unsafe { mem::transmute(alloced) }
}
}