瀏覽代碼

wip: fix props update

Jonathan Kelley 3 年之前
父節點
當前提交
79e8df2
共有 4 個文件被更改,包括 14 次插入21 次删除
  1. 1 1
      packages/core/.vscode/settings.json
  2. 3 1
      packages/core/src/nodes.rs
  3. 0 8
      packages/core/src/scheduler.rs
  4. 10 11
      packages/core/src/virtual_dom.rs

+ 1 - 1
packages/core/.vscode/settings.json

@@ -1,3 +1,3 @@
 {
-  "rust-analyzer.inlayHints.enable": false
+  "rust-analyzer.inlayHints.enable": true
 }

+ 3 - 1
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<Option<ElementId>>,
 
     /// 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<Option<BumpBox<'bump, dyn FnMut(SyntheticEvent) + 'bump>>>,
 }
 

+ 0 - 8
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<O> {
-        todo!()
-    }
-
     // return a bumpframe with a lifetime attached to the arena borrow
     // this is useful for merging lifetimes
     pub fn with_scope_vnode<'b>(

+ 10 - 11
packages/core/src/virtual_dom.rs

@@ -61,7 +61,7 @@ pub struct VirtualDom {
 
     root_caller: Box<dyn for<'b> Fn(&'b Scope) -> DomTree<'b> + 'static>,
 
-    root_props: Pin<Box<dyn Any>>,
+    root_props: Box<dyn Any>,
 }
 
 impl VirtualDom {
@@ -122,10 +122,11 @@ impl VirtualDom {
     pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
         let root_fc = Box::new(root);
 
-        let root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
+        let root_props: Box<dyn Any> = Box::new(root_props);
 
         let props_ptr = root_props.downcast_ref::<P>().unwrap() as *const P;
 
+        // Safety: this callback is only valid for the lifetime of the root props
         let root_caller: Box<dyn Fn(&Scope) -> 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<dyn Any>> = Box::pin(root_props);
+        let mut root_props: Box<dyn Any> = Box::new(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();
 
-            todo!();
+            let root_caller: Box<dyn Fn(&Scope) -> 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 {}