wip: more work on rebuild

This commit is contained in:
Jonathan Kelley committed 2021-09-01 15:52:38 -04:00
1 parent fac2e56ed6
commit 5c3dc0a874
2 files changed
+26 -13

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
{
"rust-analyzer.inlayHints.enable": true
"rust-analyzer.inlayHints.enable": false
}
+25 -12
View File
@@ -42,8 +42,6 @@ pub struct VirtualDom {
root_fc: Box<dyn Any>,
root_prop_type: std::any::TypeId,
root_props: Pin<Box<dyn std::any::Any>>,
}
@@ -105,10 +103,8 @@ impl VirtualDom {
pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
let scheduler = Scheduler::new();
let _root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
let _root_prop_type = TypeId::of::<P>();
let props_ptr = _root_props.downcast_ref::<P>().unwrap() as *const P;
let root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
let props_ptr = root_props.downcast_ref::<P>().unwrap() as *const P;
let base_scope = scheduler.pool.insert_scope_with_key(|myidx| {
let caller = NodeFactory::create_component_caller(root, props_ptr as *const _);
@@ -126,15 +122,19 @@ impl VirtualDom {
root_fc: Box::new(root),
base_scope,
scheduler,
root_props: _root_props,
root_prop_type: _root_prop_type,
root_props,
}
}
/// Get the [`Scope`] for the root component.
///
/// This is useful for traversing the tree from the root for heuristics or altnerative renderers that use Dioxus
/// directly.
pub fn base_scope(&self) -> &Scope {
self.scheduler.pool.get_scope(self.base_scope).unwrap()
}
/// Get the [`Scope`] for a component given its [`ScopeId`]
pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> {
self.scheduler.pool.get_scope(id)
}
@@ -143,15 +143,28 @@ impl 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.
///
/// ## Example
///
/// ```rust
/// #[derive(Props, PartialEq)]
/// struct AppProps {
/// route: &'static str
/// }
/// static App: FC<AppProps> = |cx| cx.render(rsx!{ "route is {cx.route}" });
///
/// let mut dom = VirtualDom::new_with_props(App, AppProps { route: "start" });
///
/// let mutations = dom.update_root_props(AppProps { route: "end" }).unwrap();
/// ```
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>();
let mut root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
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);
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();