feat: suspense!
This commit is contained in:
1 parent
e6c53803a6
commit
203935834d
13 files changed
+219
-205
No files matched your search
@@ -1,9 +1,13 @@
|
||||
use futures_task::Context;
|
||||
use futures_util::{FutureExt, StreamExt};
|
||||
|
||||
use crate::{innerlude::make_task_waker, VirtualDom};
|
||||
use crate::{
|
||||
factory::RenderReturn,
|
||||
innerlude::{Mutation, SuspenseContext},
|
||||
VNode, VirtualDom,
|
||||
};
|
||||
|
||||
use super::SchedulerMsg;
|
||||
use super::{waker::RcWake, SchedulerMsg, SuspenseLeaf};
|
||||
|
||||
impl VirtualDom {
|
||||
/// Wait for futures internal to the virtualdom
|
||||
@@ -22,7 +26,7 @@ impl VirtualDom {
|
||||
|
||||
// attach the waker to itself
|
||||
// todo: don't make a new waker every time, make it once and then just clone it
|
||||
let waker = make_task_waker(local_task.clone());
|
||||
let waker = local_task.waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
// safety: the waker owns its task and everythig is single threaded
|
||||
@@ -33,7 +37,57 @@ impl VirtualDom {
|
||||
}
|
||||
}
|
||||
|
||||
SchedulerMsg::SuspenseNotified(_) => todo!(),
|
||||
SchedulerMsg::SuspenseNotified(id) => {
|
||||
let leaf = self
|
||||
.scheduler
|
||||
.handle
|
||||
.leaves
|
||||
.borrow_mut()
|
||||
.get(id.0)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let scope_id = leaf.scope_id;
|
||||
|
||||
// todo: cache the waker
|
||||
let waker = leaf.waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
let fut = unsafe { &mut *leaf.task };
|
||||
|
||||
let mut pinned = unsafe { std::pin::Pin::new_unchecked(fut) };
|
||||
let as_pinned_mut = &mut pinned;
|
||||
|
||||
// the component finished rendering and gave us nodes
|
||||
// we should attach them to that component and then render its children
|
||||
// continue rendering the tree until we hit yet another suspended component
|
||||
if let futures_task::Poll::Ready(new_nodes) = as_pinned_mut.poll_unpin(&mut cx)
|
||||
{
|
||||
let boundary = &self.scopes[leaf.boundary.0]
|
||||
.consume_context::<SuspenseContext>()
|
||||
.unwrap();
|
||||
|
||||
let mut fiber = boundary.borrow_mut();
|
||||
let scope = &mut self.scopes[scope_id.0];
|
||||
let arena = scope.current_arena();
|
||||
|
||||
let ret = arena.bump.alloc(RenderReturn::Sync(new_nodes));
|
||||
arena.node.set(ret);
|
||||
|
||||
if let RenderReturn::Sync(Some(template)) = ret {
|
||||
let mutations = &mut fiber.mutations;
|
||||
let template: &VNode = unsafe { std::mem::transmute(template) };
|
||||
let mutations: &mut Vec<Mutation> =
|
||||
unsafe { std::mem::transmute(mutations) };
|
||||
|
||||
self.scope_stack.push(scope_id);
|
||||
self.create(mutations, template);
|
||||
self.scope_stack.pop();
|
||||
|
||||
println!("{:?}", mutations);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user