2022-11-04 05:30:26 +00:00
|
|
|
use std::task::Context;
|
|
|
|
|
|
|
|
use futures_util::task::noop_waker_ref;
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
use crate::{
|
|
|
|
any_props::AnyProps,
|
|
|
|
arena::ElementId,
|
|
|
|
bump_frame::BumpFrame,
|
2022-11-04 03:56:31 +00:00
|
|
|
factory::RenderReturn,
|
2022-11-02 01:42:29 +00:00
|
|
|
scopes::{ScopeId, ScopeState},
|
2022-11-06 22:28:41 +00:00
|
|
|
virtualdom::VirtualDom, innerlude::SuspenseLeaf,
|
2022-10-28 04:58:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2022-10-28 04:58:47 +00:00
|
|
|
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());
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
entry.insert(ScopeState {
|
|
|
|
parent,
|
|
|
|
container,
|
2022-11-02 01:42:29 +00:00
|
|
|
id,
|
2022-10-28 04:58:47 +00:00
|
|
|
height,
|
|
|
|
props,
|
|
|
|
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-10-28 04:58:47 +00:00
|
|
|
});
|
|
|
|
|
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-10-28 04:58:47 +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()
|
|
|
|
.and_then(|id| self.scopes.get_mut(id.0).map(|f| f as *mut ScopeState))
|
|
|
|
}
|
|
|
|
|
2022-11-04 03:56:31 +00:00
|
|
|
pub fn run_scope(&mut self, id: ScopeId) -> &mut RenderReturn {
|
2022-11-02 01:42:29 +00:00
|
|
|
let scope = &mut self.scopes[id.0];
|
2022-10-28 04:58:47 +00:00
|
|
|
scope.hook_idx.set(0);
|
|
|
|
|
2022-11-04 05:30:26 +00:00
|
|
|
let mut new_nodes = {
|
2022-11-03 09:11:04 +00:00
|
|
|
let props = unsafe { &mut *scope.props };
|
|
|
|
let props: &mut dyn AnyProps = unsafe { std::mem::transmute(props) };
|
2022-11-04 03:56:31 +00:00
|
|
|
let res: RenderReturn = props.render(scope);
|
|
|
|
let res: RenderReturn<'static> = unsafe { std::mem::transmute(res) };
|
2022-11-03 09:11:04 +00:00
|
|
|
res
|
|
|
|
};
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-04 05:30:26 +00:00
|
|
|
// immediately resolve futures that can be resolved immediatelys
|
|
|
|
let res = match &mut new_nodes {
|
|
|
|
RenderReturn::Sync(_) => new_nodes,
|
|
|
|
RenderReturn::Async(fut) => {
|
2022-11-06 22:28:41 +00:00
|
|
|
// use futures_util::FutureExt;
|
|
|
|
|
|
|
|
let leaves = self.scheduler.handle.leaves.borrow_mut();
|
|
|
|
|
|
|
|
leaves.insert(Rc::new(SuspenseLeaf {
|
|
|
|
id: todo!(),
|
|
|
|
scope: todo!(),
|
|
|
|
boundary: todo!(),
|
|
|
|
tx: todo!(),
|
|
|
|
task: todo!(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
let waker = crate::scheduler::make_suspense_waker(task);
|
2022-11-04 05:30:26 +00:00
|
|
|
|
|
|
|
match fut.poll_unpin(&mut cx) {
|
2022-11-06 22:28:41 +00:00
|
|
|
std::task::Poll::Ready(nodes) => {
|
|
|
|
leaves.remove(key;)
|
|
|
|
RenderReturn::Sync(nodes)
|
|
|
|
},
|
2022-11-04 05:30:26 +00:00
|
|
|
std::task::Poll::Pending => new_nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
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
|
|
|
|
let alloced = frame.bump.alloc(res);
|
|
|
|
frame.node.set(alloced);
|
|
|
|
|
|
|
|
// rebind the lifetime now that its stored internally
|
|
|
|
unsafe { std::mem::transmute(alloced) }
|
|
|
|
}
|
|
|
|
}
|