Merge branch 'upstream' into fix-non-str-attributes

This commit is contained in:
Evan Almloff committed 2022-12-30 11:52:55 -06:00
commit 5b1d9c5c8d
44 files changed
+1335 -2346

No files matched your search

+3 -5
View File
@@ -4,11 +4,9 @@ use slab::Slab;
mod suspense;
mod task;
mod wait;
mod waker;
pub use suspense::*;
pub use task::*;
pub use waker::ArcWake;
/// The type of message that can be sent to the scheduler.
///
@@ -25,16 +23,16 @@ pub(crate) enum SchedulerMsg {
SuspenseNotified(SuspenseId),
}
use std::{cell::RefCell, rc::Rc, sync::Arc};
use std::{cell::RefCell, rc::Rc};
pub(crate) struct Scheduler {
pub sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
/// Tasks created with cx.spawn
pub tasks: RefCell<Slab<Arc<LocalTask>>>,
pub tasks: RefCell<Slab<LocalTask>>,
/// Async components
pub leaves: RefCell<Slab<Arc<SuspenseLeaf>>>,
pub leaves: RefCell<Slab<SuspenseLeaf>>,
}
impl Scheduler {
+11 -5
View File
@@ -1,8 +1,11 @@
use super::{waker::ArcWake, SchedulerMsg};
use futures_util::task::ArcWake;
use super::SchedulerMsg;
use crate::ElementId;
use crate::{innerlude::Mutations, Element, ScopeId};
use std::future::Future;
use std::sync::Arc;
use std::task::Waker;
use std::{
cell::{Cell, RefCell},
collections::HashSet,
@@ -35,16 +38,19 @@ impl SuspenseContext {
}
pub(crate) struct SuspenseLeaf {
pub(crate) id: SuspenseId,
pub(crate) scope_id: ScopeId,
pub(crate) tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
pub(crate) notified: Cell<bool>,
pub(crate) task: *mut dyn Future<Output = Element<'static>>,
pub(crate) waker: Waker,
}
impl ArcWake for SuspenseLeaf {
pub struct SuspenseHandle {
pub(crate) id: SuspenseId,
pub(crate) tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
}
impl ArcWake for SuspenseHandle {
fn wake_by_ref(arc_self: &Arc<Self>) {
arc_self.notified.set(true);
_ = arc_self
.tx
.unbounded_send(SchedulerMsg::SuspenseNotified(arc_self.id));
+23 -10
View File
@@ -1,9 +1,12 @@
use super::{waker::ArcWake, Scheduler, SchedulerMsg};
use futures_util::task::ArcWake;
use super::{Scheduler, SchedulerMsg};
use crate::ScopeId;
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Waker;
/// A task's unique identifier.
///
@@ -17,8 +20,7 @@ pub struct TaskId(pub usize);
pub(crate) struct LocalTask {
pub scope: ScopeId,
pub(super) task: RefCell<Pin<Box<dyn Future<Output = ()> + 'static>>>,
id: TaskId,
tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
pub waker: Waker,
}
impl Scheduler {
@@ -33,15 +35,20 @@ impl Scheduler {
/// will only occur when the VirtuaalDom itself has been dropped.
pub fn spawn(&self, scope: ScopeId, task: impl Future<Output = ()> + 'static) -> TaskId {
let mut tasks = self.tasks.borrow_mut();
let entry = tasks.vacant_entry();
let task_id = TaskId(entry.key());
entry.insert(Arc::new(LocalTask {
id: task_id,
tx: self.sender.clone(),
let task = LocalTask {
task: RefCell::new(Box::pin(task)),
scope,
}));
waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
id: task_id,
tx: self.sender.clone(),
})),
};
entry.insert(task);
self.sender
.unbounded_send(SchedulerMsg::TaskNotified(task_id))
@@ -58,10 +65,16 @@ impl Scheduler {
}
}
impl ArcWake for LocalTask {
pub struct LocalTaskHandle {
id: TaskId,
tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
}
impl ArcWake for LocalTaskHandle {
fn wake_by_ref(arc_self: &Arc<Self>) {
_ = arc_self
arc_self
.tx
.unbounded_send(SchedulerMsg::TaskNotified(arc_self.id));
.unbounded_send(SchedulerMsg::TaskNotified(arc_self.id))
.unwrap();
}
}
+9 -12
View File
@@ -10,7 +10,7 @@ use crate::{
ScopeId, TaskId, VNode, VirtualDom,
};
use super::{waker::ArcWake, SuspenseId};
use super::SuspenseId;
impl VirtualDom {
/// Handle notifications by tasks inside the scheduler
@@ -22,11 +22,11 @@ impl VirtualDom {
let task = match tasks.get(id.0) {
Some(task) => task,
// The task was removed from the scheduler, so we can just ignore it
None => return,
};
let waker = task.waker();
let mut cx = Context::from_waker(&waker);
let mut cx = Context::from_waker(&task.waker);
// If the task completes...
if task.task.borrow_mut().as_mut().poll(&mut cx).is_ready() {
@@ -45,19 +45,13 @@ impl VirtualDom {
}
pub(crate) fn handle_suspense_wakeup(&mut self, id: SuspenseId) {
let leaf = self
.scheduler
.leaves
.borrow_mut()
.get(id.0)
.unwrap()
.clone();
let leaves = self.scheduler.leaves.borrow_mut();
let leaf = leaves.get(id.0).unwrap();
let scope_id = leaf.scope_id;
// todo: cache the waker
let waker = leaf.waker();
let mut cx = Context::from_waker(&waker);
let mut cx = Context::from_waker(&leaf.waker);
// Safety: the future is always pinned to the bump arena
let mut pinned = unsafe { std::pin::Pin::new_unchecked(&mut *leaf.task) };
@@ -91,6 +85,9 @@ impl VirtualDom {
let place_holder_id = scope.placeholder.get().unwrap();
self.scope_stack.push(scope_id);
drop(leaves);
let created = self.create(template);
self.scope_stack.pop();
mutations.push(Mutation::ReplaceWith {
-37
View File
@@ -1,37 +0,0 @@
use std::mem;
use std::sync::Arc;
use std::task::{RawWaker, RawWakerVTable, Waker};
pub trait ArcWake: Sized {
/// Create a waker from this self-wakening object
fn waker(self: &Arc<Self>) -> Waker {
unsafe fn rc_vtable<T: ArcWake>() -> &'static RawWakerVTable {
&RawWakerVTable::new(
|data| {
let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data.cast::<T>()));
let _rc_clone: mem::ManuallyDrop<_> = arc.clone();
RawWaker::new(data, rc_vtable::<T>())
},
|data| Arc::from_raw(data.cast::<T>()).wake(),
|data| {
let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data.cast::<T>()));
ArcWake::wake_by_ref(&arc);
},
|data| drop(Arc::<T>::from_raw(data.cast::<T>())),
)
}
unsafe {
Waker::from_raw(RawWaker::new(
Arc::into_raw(self.clone()).cast(),
rc_vtable::<Self>(),
))
}
}
fn wake_by_ref(arc_self: &Arc<Self>);
fn wake(self: Arc<Self>) {
Self::wake_by_ref(&self)
}
}
+8 -8
View File
@@ -2,9 +2,8 @@ use crate::{
any_props::AnyProps,
bump_frame::BumpFrame,
innerlude::DirtyScope,
innerlude::{SuspenseId, SuspenseLeaf},
innerlude::{SuspenseHandle, SuspenseId, SuspenseLeaf},
nodes::RenderReturn,
scheduler::ArcWake,
scopes::{ScopeId, ScopeState},
virtual_dom::VirtualDom,
};
@@ -90,16 +89,17 @@ impl VirtualDom {
let entry = leaves.vacant_entry();
let suspense_id = SuspenseId(entry.key());
let leaf = Arc::new(SuspenseLeaf {
let leaf = SuspenseLeaf {
scope_id,
task: task.as_mut(),
id: suspense_id,
tx: self.scheduler.sender.clone(),
notified: Default::default(),
});
waker: futures_util::task::waker(Arc::new(SuspenseHandle {
id: suspense_id,
tx: self.scheduler.sender.clone(),
})),
};
let waker = leaf.waker();
let mut cx = Context::from_waker(&waker);
let mut cx = Context::from_waker(&leaf.waker);
// safety: the task is already pinned in the bump arena
let mut pinned = unsafe { Pin::new_unchecked(task.as_mut()) };