From 79e8df26f5a84f5d765921ae307b33db83c14d4e Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 13 Sep 2021 12:42:38 -0400 Subject: [PATCH] wip: fix props update --- packages/core/.vscode/settings.json | 2 +- packages/core/src/nodes.rs | 4 +++- packages/core/src/scheduler.rs | 8 -------- packages/core/src/virtual_dom.rs | 21 ++++++++++----------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/core/.vscode/settings.json b/packages/core/.vscode/settings.json index 79505b018..1c68f97cc 100644 --- a/packages/core/.vscode/settings.json +++ b/packages/core/.vscode/settings.json @@ -1,3 +1,3 @@ { - "rust-analyzer.inlayHints.enable": false + "rust-analyzer.inlayHints.enable": true } diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index 3b8b8ecf9..e9854ebb4 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -12,7 +12,6 @@ use std::{ cell::{Cell, RefCell}, fmt::{Arguments, Debug, Formatter}, marker::PhantomData, - rc::Rc, }; /// A composable "VirtualNode" to declare a User Interface in the Dioxus VirtualDOM. @@ -245,6 +244,8 @@ pub struct Attribute<'a> { /// An event listener. /// IE onclick, onkeydown, etc pub struct Listener<'bump> { + /// The ID of the node that this listener is mounted to + /// Used to generate the event listener's ID on the DOM pub mounted_node: Cell>, /// The type of event to listen for. @@ -252,6 +253,7 @@ pub struct Listener<'bump> { /// IE "click" - whatever the renderer needs to attach the listener by name. pub event: &'static str, + /// The actual callback that the user specified pub(crate) callback: RefCell>>, } diff --git a/packages/core/src/scheduler.rs b/packages/core/src/scheduler.rs index e4d01800f..16dfbc869 100644 --- a/packages/core/src/scheduler.rs +++ b/packages/core/src/scheduler.rs @@ -739,14 +739,6 @@ impl ResourcePool { inner.get_mut(idx.0) } - pub fn with_scope<'b, O: 'static>( - &'b self, - _id: ScopeId, - _f: impl FnOnce(&'b mut Scope) -> O, - ) -> Option { - todo!() - } - // return a bumpframe with a lifetime attached to the arena borrow // this is useful for merging lifetimes pub fn with_scope_vnode<'b>( diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 7ce8f816d..8f1d63055 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -61,7 +61,7 @@ pub struct VirtualDom { root_caller: Box Fn(&'b Scope) -> DomTree<'b> + 'static>, - root_props: Pin>, + root_props: Box, } impl VirtualDom { @@ -122,10 +122,11 @@ impl VirtualDom { pub fn new_with_props(root: FC

, root_props: P) -> Self { let root_fc = Box::new(root); - let root_props: Pin> = Box::pin(root_props); + let root_props: Box = Box::new(root_props); let props_ptr = root_props.downcast_ref::

().unwrap() as *const P; + // Safety: this callback is only valid for the lifetime of the root props let root_caller: Box DomTree> = Box::new(move |scope: &Scope| unsafe { let props: &'_ P = &*(props_ptr as *const P); std::mem::transmute(root(Context { props, scope })) @@ -188,17 +189,20 @@ impl VirtualDom { 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::pin(root_props); + let mut root_props: Box = Box::new(root_props); if let Some(props_ptr) = root_props.downcast_ref::

().map(|p| p as *const P) { std::mem::swap(&mut self.root_props, &mut root_props); let root = *self.root_fc.downcast_ref::>().unwrap(); - todo!(); + let root_caller: Box DomTree> = + Box::new(move |scope: &Scope| unsafe { + let props: &'_ P = &*(props_ptr as *const P); + std::mem::transmute(root(Context { props, scope })) + }); - // let new_caller = NodeFactory::create_component_caller(root, props_ptr as *const _); - // root_scope.update_scope_dependencies(new_caller, ScopeChildren(&[])); + root_scope.update_scope_dependencies(&root_caller, ScopeChildren(&[])); Some(self.rebuild()) } else { @@ -386,8 +390,3 @@ impl VirtualDom { } } } - -// TODO! -// These impls are actually wrong. The DOM needs to have a mutex implemented. -unsafe impl Sync for VirtualDom {} -unsafe impl Send for VirtualDom {}