chore: more refactoring, docs
This commit is contained in:
1 parent
f7df6a9893
commit
c44bd11fe5
27 files changed
+657
-382
No files matched your search
@@ -14,16 +14,10 @@ pub use waker::RcWake;
|
||||
///
|
||||
/// These messages control how the scheduler will process updates to the UI.
|
||||
#[derive(Debug)]
|
||||
pub enum SchedulerMsg {
|
||||
/// Events from the Renderer
|
||||
Event,
|
||||
|
||||
pub(crate) enum SchedulerMsg {
|
||||
/// Immediate updates from Components that mark them as dirty
|
||||
Immediate(ScopeId),
|
||||
|
||||
/// Mark all components as dirty and update them
|
||||
DirtyAll,
|
||||
|
||||
/// A task has woken and needs to be progressed
|
||||
TaskNotified(TaskId),
|
||||
|
||||
|
||||
@@ -29,12 +29,11 @@ impl SuspenseBoundary {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SuspenseLeaf {
|
||||
pub(crate) struct SuspenseLeaf {
|
||||
pub id: SuspenseId,
|
||||
pub scope_id: ScopeId,
|
||||
pub tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||
pub notified: Cell<bool>,
|
||||
|
||||
pub task: *mut dyn Future<Output = Element<'static>>,
|
||||
}
|
||||
|
||||
|
||||
@@ -57,11 +57,6 @@ impl Scheduler {
|
||||
pub fn remove(&self, id: TaskId) {
|
||||
//
|
||||
}
|
||||
|
||||
// Aborts the future
|
||||
pub fn abort(&self, id: TaskId) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
impl RcWake for LocalTask {
|
||||
|
||||
@@ -1,110 +1,92 @@
|
||||
use futures_util::{FutureExt, StreamExt};
|
||||
use futures_util::FutureExt;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use crate::{
|
||||
diff::DirtyScope,
|
||||
factory::RenderReturn,
|
||||
innerlude::{Mutation, Mutations, SuspenseContext},
|
||||
VNode, VirtualDom,
|
||||
ScopeId, TaskId, VNode, VirtualDom,
|
||||
};
|
||||
|
||||
use super::{waker::RcWake, SchedulerMsg, SuspenseLeaf};
|
||||
use super::{waker::RcWake, SuspenseId, SuspenseLeaf};
|
||||
|
||||
impl VirtualDom {
|
||||
/// Wait for futures internal to the virtualdom
|
||||
/// Handle notifications by tasks inside the scheduler
|
||||
///
|
||||
/// This is cancel safe, so if the future is dropped, you can push events into the virtualdom
|
||||
pub async fn wait_for_work(&mut self) {
|
||||
// todo: make sure the scheduler queue is completely drained
|
||||
loop {
|
||||
match self.rx.next().await.unwrap() {
|
||||
SchedulerMsg::Event => break,
|
||||
/// This is precise, meaning we won't poll every task, just tasks that have woken up as notified to use by the
|
||||
/// queue
|
||||
pub fn handle_task_wakeup(&mut self, id: TaskId) {
|
||||
let mut tasks = self.scheduler.tasks.borrow_mut();
|
||||
let task = &tasks[id.0];
|
||||
|
||||
SchedulerMsg::Immediate(id) => {
|
||||
let height = self.scopes[id.0].height;
|
||||
self.dirty_scopes.insert(DirtyScope { height, id });
|
||||
break;
|
||||
}
|
||||
// If the task completes...
|
||||
if task.progress() {
|
||||
// Remove it from the scope so we dont try to double drop it when the scope dropes
|
||||
self.scopes[task.scope.0].spawned_tasks.remove(&id);
|
||||
|
||||
SchedulerMsg::DirtyAll => todo!(),
|
||||
// Remove it from the scheduler
|
||||
tasks.remove(id.0);
|
||||
}
|
||||
}
|
||||
|
||||
SchedulerMsg::TaskNotified(id) => {
|
||||
let mut tasks = self.scheduler.tasks.borrow_mut();
|
||||
let task = &tasks[id.0];
|
||||
pub fn handle_suspense_wakeup(&mut self, id: SuspenseId) {
|
||||
println!("suspense notified");
|
||||
|
||||
// If the task completes...
|
||||
if task.progress() {
|
||||
// Remove it from the scope so we dont try to double drop it when the scope dropes
|
||||
self.scopes[task.scope.0].spawned_tasks.remove(&id);
|
||||
let leaf = self
|
||||
.scheduler
|
||||
.leaves
|
||||
.borrow_mut()
|
||||
.get(id.0)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
// Remove it from the scheduler
|
||||
tasks.remove(id.0);
|
||||
}
|
||||
}
|
||||
let scope_id = leaf.scope_id;
|
||||
|
||||
SchedulerMsg::SuspenseNotified(id) => {
|
||||
println!("suspense notified");
|
||||
// todo: cache the waker
|
||||
let waker = leaf.waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
let leaf = self
|
||||
.scheduler
|
||||
.leaves
|
||||
.borrow_mut()
|
||||
.get(id.0)
|
||||
.unwrap()
|
||||
.clone();
|
||||
// Safety: the future is always pinned to the bump arena
|
||||
let mut pinned = unsafe { std::pin::Pin::new_unchecked(&mut *leaf.task) };
|
||||
let as_pinned_mut = &mut pinned;
|
||||
|
||||
let scope_id = leaf.scope_id;
|
||||
// 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 Poll::Ready(new_nodes) = as_pinned_mut.poll_unpin(&mut cx) {
|
||||
let boundary = &self.scopes[leaf.scope_id.0]
|
||||
.consume_context::<SuspenseContext>()
|
||||
.unwrap();
|
||||
|
||||
// todo: cache the waker
|
||||
let waker = leaf.waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
println!("ready pool");
|
||||
|
||||
// Safety: the future is always pinned to the bump arena
|
||||
let mut pinned = unsafe { std::pin::Pin::new_unchecked(&mut *leaf.task) };
|
||||
let as_pinned_mut = &mut pinned;
|
||||
let mut fiber = boundary.borrow_mut();
|
||||
|
||||
// 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 Poll::Ready(new_nodes) = as_pinned_mut.poll_unpin(&mut cx) {
|
||||
let boundary = &self.scopes[leaf.scope_id.0]
|
||||
.consume_context::<SuspenseContext>()
|
||||
.unwrap();
|
||||
println!(
|
||||
"Existing mutations {:?}, scope {:?}",
|
||||
fiber.mutations, fiber.id
|
||||
);
|
||||
|
||||
println!("ready pool");
|
||||
let scope = &mut self.scopes[scope_id.0];
|
||||
let arena = scope.current_frame();
|
||||
|
||||
let mut fiber = boundary.borrow_mut();
|
||||
let ret = arena.bump.alloc(RenderReturn::Sync(new_nodes));
|
||||
arena.node.set(ret);
|
||||
|
||||
println!(
|
||||
"Existing mutations {:?}, scope {:?}",
|
||||
fiber.mutations, fiber.id
|
||||
);
|
||||
if let RenderReturn::Sync(Some(template)) = ret {
|
||||
let mutations = &mut fiber.mutations;
|
||||
let template: &VNode = unsafe { std::mem::transmute(template) };
|
||||
let mutations: &mut Mutations = unsafe { std::mem::transmute(mutations) };
|
||||
|
||||
let scope = &mut self.scopes[scope_id.0];
|
||||
let arena = scope.current_arena();
|
||||
self.scope_stack.push(scope_id);
|
||||
self.create(mutations, template);
|
||||
self.scope_stack.pop();
|
||||
|
||||
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 Mutations =
|
||||
unsafe { std::mem::transmute(mutations) };
|
||||
|
||||
self.scope_stack.push(scope_id);
|
||||
self.create(mutations, template);
|
||||
self.scope_stack.pop();
|
||||
|
||||
println!("{:#?}", mutations);
|
||||
} else {
|
||||
println!("nodes arent right");
|
||||
}
|
||||
} else {
|
||||
println!("not ready");
|
||||
}
|
||||
}
|
||||
println!("{:#?}", mutations);
|
||||
} else {
|
||||
println!("nodes arent right");
|
||||
}
|
||||
} else {
|
||||
println!("not ready");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user