dioxus/packages/core/src/scope_arena.rs

136 lines
4.5 KiB
Rust
Raw Normal View History

2022-11-08 06:55:22 +00:00
use crate::{innerlude::SuspenseContext, scheduler::RcWake};
use futures_util::{pin_mut, task::noop_waker_ref};
use std::{
mem,
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
2022-11-04 05:30:26 +00:00
use crate::{
any_props::AnyProps,
arena::ElementId,
bump_frame::BumpFrame,
2022-11-04 03:56:31 +00:00
factory::RenderReturn,
2022-11-08 06:55:22 +00:00
innerlude::{SuspenseId, SuspenseLeaf},
2022-11-02 01:42:29 +00:00
scopes::{ScopeId, ScopeState},
2022-11-08 06:55:22 +00:00
virtualdom::VirtualDom,
};
impl VirtualDom {
2022-11-03 09:11:04 +00:00
pub fn new_scope(&mut self, props: *mut dyn AnyProps<'static>) -> ScopeId {
2022-11-02 01:42:29 +00:00
let parent = self.acquire_current_scope_raw();
let container = self.acquire_current_container();
let entry = self.scopes.vacant_entry();
let height = unsafe { parent.map(|f| (*f).height).unwrap_or(0) + 1 };
2022-11-02 01:42:29 +00:00
let id = ScopeId(entry.key());
entry.insert(ScopeState {
parent,
container,
2022-11-02 01:42:29 +00:00
id,
height,
props,
2022-11-08 06:55:22 +00:00
placeholder: None.into(),
node_arena_1: BumpFrame::new(50),
node_arena_2: BumpFrame::new(50),
render_cnt: Default::default(),
hook_arena: Default::default(),
hook_vals: Default::default(),
hook_idx: Default::default(),
2022-11-02 01:42:29 +00:00
shared_contexts: Default::default(),
2022-11-06 08:48:34 +00:00
tasks: self.scheduler.handle.clone(),
});
2022-11-02 01:42:29 +00:00
id
}
pub fn acquire_current_container(&self) -> ElementId {
self.element_stack
.last()
.copied()
.expect("Always have a container")
}
2022-11-02 01:42:29 +00:00
fn acquire_current_scope_raw(&mut self) -> Option<*mut ScopeState> {
self.scope_stack
.last()
.copied()
.and_then(|id| self.scopes.get_mut(id.0).map(|f| f as *mut ScopeState))
}
2022-11-08 06:55:22 +00:00
pub fn run_scope(&mut self, scope_id: ScopeId) -> &mut RenderReturn {
let mut new_nodes = unsafe {
let scope = &mut self.scopes[scope_id.0];
scope.hook_idx.set(0);
2022-11-08 06:55:22 +00:00
let props: &mut dyn AnyProps = mem::transmute(&mut *scope.props);
2022-11-04 03:56:31 +00:00
let res: RenderReturn = props.render(scope);
2022-11-08 06:55:22 +00:00
let res: RenderReturn<'static> = mem::transmute(res);
2022-11-03 09:11:04 +00:00
res
};
2022-11-08 06:55:22 +00:00
// immediately resolve futures that can be resolved
if let RenderReturn::Async(task) = &mut new_nodes {
use futures_util::FutureExt;
let mut leaves = self.scheduler.handle.leaves.borrow_mut();
let entry = leaves.vacant_entry();
let key = entry.key();
let leaf = Rc::new(SuspenseLeaf {
scope_id,
task: task.as_mut(),
id: SuspenseId(key),
tx: self.scheduler.handle.sender.clone(),
boundary: ScopeId(0),
notified: false.into(),
});
let _leaf = leaf.clone();
let waker = leaf.waker();
let mut cx = Context::from_waker(&waker);
let mut pinned = unsafe { Pin::new_unchecked(task.as_mut()) };
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
_ if _leaf.notified.get() => {
_leaf.notified.set(false);
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);
break;
}
};
2022-11-04 05:30:26 +00:00
}
};
2022-11-08 06:55:22 +00:00
let scope = &mut self.scopes[scope_id.0];
let frame = match scope.render_cnt % 2 {
0 => &mut scope.node_arena_1,
1 => &mut scope.node_arena_2,
_ => unreachable!(),
};
// set the head of the bump frame
2022-11-08 06:55:22 +00:00
let alloced = frame.bump.alloc(new_nodes);
frame.node.set(alloced);
// rebind the lifetime now that its stored internally
2022-11-08 06:55:22 +00:00
unsafe { mem::transmute(alloced) }
}
}