chore: finish tweaking context api

This commit is contained in:
Jonathan Kelley committed 2022-12-05 15:30:49 -08:00
1 parent 99a818996e
commit df4a1b6c7a
12 files changed
+76 -81

No files matched your search

+2 -1
View File
@@ -1,4 +1,5 @@
use std::cell::Cell;
use std::rc::Rc;
use crate::innerlude::{VComponent, VText};
use crate::mutations::Mutation;
@@ -340,7 +341,7 @@ impl<'b> VirtualDom {
}
// If running the scope has collected some leaves and *this* component is a boundary, then handle the suspense
let boundary = match self.scopes[scope.0].has_context::<SuspenseContext>() {
let boundary = match self.scopes[scope.0].has_context::<Rc<SuspenseContext>>() {
Some(boundary) => boundary,
_ => return created,
};
+2 -2
View File
@@ -34,9 +34,9 @@ impl VirtualDom {
}
}
pub(crate) fn acquire_suspense_boundary<'a>(&self, id: ScopeId) -> Rc<SuspenseContext> {
pub(crate) fn acquire_suspense_boundary<'a>(&'a self, id: ScopeId) -> Rc<SuspenseContext> {
self.scopes[id.0]
.consume_context::<SuspenseContext>()
.consume_context::<Rc<SuspenseContext>>()
.unwrap()
}
+16 -16
View File
@@ -81,7 +81,7 @@ pub struct ScopeState {
pub(crate) hook_list: RefCell<Vec<*mut dyn Any>>,
pub(crate) hook_idx: Cell<usize>,
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Box<dyn Any>>>,
pub(crate) tasks: Rc<Scheduler>,
pub(crate) spawned_tasks: HashSet<TaskId>,
@@ -250,17 +250,18 @@ impl<'src> ScopeState {
}
/// Return any context of type T if it exists on this scope
pub fn has_context<T: 'static>(&self) -> Option<Rc<T>> {
pub fn has_context<T: 'static + Clone>(&self) -> Option<T> {
self.shared_contexts
.borrow()
.get(&TypeId::of::<T>())?
.clone()
.downcast()
.ok()
.downcast_ref::<T>()
.cloned()
}
/// Try to retrieve a shared state with type `T` from any parent scope.
pub fn consume_context<T: 'static>(&self) -> Option<Rc<T>> {
///
/// Clones the state if it exists.
pub fn consume_context<T: 'static + Clone>(&self) -> Option<T> {
if let Some(this_ctx) = self.has_context() {
return Some(this_ctx);
}
@@ -270,7 +271,7 @@ impl<'src> ScopeState {
// safety: all parent pointers are valid thanks to the bump arena
let parent = unsafe { &*parent_ptr };
if let Some(shared) = parent.shared_contexts.borrow().get(&TypeId::of::<T>()) {
return shared.clone().downcast().ok();
return shared.downcast_ref::<T>().cloned();
}
search_parent = parent.parent;
}
@@ -303,15 +304,14 @@ impl<'src> ScopeState {
/// render!(div { "hello {state.0}" })
/// }
/// ```
pub fn provide_context<T: 'static>(&self, value: T) -> Rc<T> {
let mut contexts = self.shared_contexts.borrow_mut();
contexts.insert(TypeId::of::<T>(), Rc::new(value));
contexts
.get(&TypeId::of::<T>())
.unwrap()
.clone()
.downcast()
.unwrap()
pub fn provide_context<T: 'static + Clone>(&self, value: T) -> T {
let value2 = value.clone();
self.shared_contexts
.borrow_mut()
.insert(TypeId::of::<T>(), Box::new(value));
value2
}
/// Pushes the future onto the poll queue to be polled after the component renders.
+5 -5
View File
@@ -249,10 +249,10 @@ impl VirtualDom {
// This could be unexpected, so we might rethink this behavior later
//
// We *could* just panic if the suspense boundary is not found
root.provide_context(SuspenseContext::new(ScopeId(0)));
root.provide_context(Rc::new(SuspenseContext::new(ScopeId(0))));
// Unlike react, we provide a default error boundary that just renders the error as a string
root.provide_context(ErrorBoundary::new(ScopeId(0)));
root.provide_context(Rc::new(ErrorBoundary::new(ScopeId(0))));
// the root element is always given element ID 0 since it's the container for the entire tree
dom.elements.insert(ElementRef::null());
@@ -296,7 +296,7 @@ impl VirtualDom {
/// currently suspended.
pub fn is_scope_suspended(&self, id: ScopeId) -> bool {
!self.scopes[id.0]
.consume_context::<SuspenseContext>()
.consume_context::<Rc<SuspenseContext>>()
.unwrap()
.waiting_on
.borrow()
@@ -527,7 +527,7 @@ impl VirtualDom {
// first, unload any complete suspense trees
for finished_fiber in self.finished_fibers.drain(..) {
let scope = &mut self.scopes[finished_fiber.0];
let context = scope.has_context::<SuspenseContext>().unwrap();
let context = scope.has_context::<Rc<SuspenseContext>>().unwrap();
self.mutations
.templates
@@ -565,7 +565,7 @@ impl VirtualDom {
// No placeholder necessary since this is a diff
if !self.collected_leaves.is_empty() {
let mut boundary = self.scopes[dirty.id.0]
.consume_context::<SuspenseContext>()
.consume_context::<Rc<SuspenseContext>>()
.unwrap();
let boundary_mut = boundary.borrow_mut();