feat: update root props

This commit is contained in:
Jonathan Kelley committed 2021-09-01 15:45:53 -04:00
1 parent c321532a6c
commit fac2e56ed6
9 files changed
+96 -55

No files matched your search

+30
View File
@@ -23,6 +23,7 @@ use futures_util::{Future, FutureExt};
use std::{
any::{Any, TypeId},
pin::Pin,
rc::Rc,
};
/// An integrated virtual node system that progresses events and diffs UI trees.
@@ -39,6 +40,8 @@ pub struct VirtualDom {
base_scope: ScopeId,
root_fc: Box<dyn Any>,
root_prop_type: std::any::TypeId,
root_props: Pin<Box<dyn std::any::Any>>,
@@ -120,6 +123,7 @@ impl VirtualDom {
});
Self {
root_fc: Box::new(root),
base_scope,
scheduler,
root_props: _root_props,
@@ -135,6 +139,32 @@ impl VirtualDom {
self.scheduler.pool.get_scope(id)
}
/// Update the root props of this VirtualDOM.
///
/// This method retuns None if the old props could not be removed. The entire VirtualDOM will be rebuilt immediately,
/// so calling this method will block the main thread until computation is done.
pub fn update_root_props<'s, P: 'static>(&'s mut self, root_props: P) -> Option<Mutations<'s>> {
let root_scope = self.scheduler.pool.get_scope_mut(self.base_scope).unwrap();
root_scope.ensure_drop_safety(&self.scheduler.pool);
let mut _root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
let _root_prop_type = TypeId::of::<P>();
if let Some(props_ptr) = _root_props.downcast_ref::<P>().map(|p| p as *const P) {
std::mem::swap(&mut self.root_props, &mut _root_props);
let root = *self.root_fc.downcast_ref::<FC<P>>().unwrap();
let new_caller = NodeFactory::create_component_caller(root, props_ptr as *const _);
root_scope.update_scope_dependencies(new_caller, ScopeChildren(&[]));
Some(self.rebuild())
} else {
None
}
}
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
///
/// The diff machine expects the RealDom's stack to be the root of the application