浏览代码

fix: element stack not being updated properly

Jonathan Kelley 3 年之前
父节点
当前提交
06418f73db
共有 3 个文件被更改,包括 10 次插入44 次删除
  1. 2 0
      packages/core/src/diff.rs
  2. 7 8
      packages/core/src/scopes.rs
  3. 1 36
      packages/router/src/service.rs

+ 2 - 0
packages/core/src/diff.rs

@@ -166,6 +166,8 @@ impl<'b> DiffState<'b> {
             self.create_and_append_children(children);
         }
 
+        self.element_stack.pop();
+
         1
     }
 

+ 7 - 8
packages/core/src/scopes.rs

@@ -96,10 +96,9 @@ impl ScopeArena {
 
         // Get the height of the scope
         let height = parent_scope
-            .map(|id| self.get_scope(id).map(|scope| scope.height))
+            .map(|id| self.get_scope(id).map(|scope| scope.height + 1))
             .flatten()
-            .unwrap_or_default()
-            + 1;
+            .unwrap_or_default();
 
         let parent_scope = parent_scope.map(|f| self.get_scope_raw(f)).flatten();
 
@@ -202,11 +201,8 @@ impl ScopeArena {
     }
 
     pub fn update_node<'a>(&self, node: &'a VNode<'a>, id: ElementId) {
-        *self
-            .nodes
-            .borrow_mut()
-            .get_mut(id.0)
-            .expect("node to exist if it's being updated") = unsafe { extend_vnode(node) };
+        let node = unsafe { extend_vnode(node) };
+        *self.nodes.borrow_mut().get_mut(id.0).unwrap() = node;
     }
 
     pub fn collect_garbage(&self, id: ElementId) {
@@ -317,9 +313,12 @@ impl ScopeArena {
         while let Some(id) = cur_el.take() {
             if let Some(el) = nodes.get(id.0) {
                 let real_el = unsafe { &**el };
+                log::debug!("looking for listener on {:?}", real_el);
+
                 if let VNode::Element(real_el) = real_el {
                     for listener in real_el.listeners.borrow().iter() {
                         if listener.event == event.name {
+                            log::debug!("calling listener {:?}", listener.event);
                             if state.canceled.get() {
                                 // stop bubbling if canceled
                                 break;

+ 1 - 36
packages/router/src/service.rs

@@ -62,12 +62,10 @@ impl RouterService {
                 root_found.set(None);
                 // checking if the route is valid is cheap, so we do it
                 for (slot, root) in slots.borrow_mut().iter().rev() {
-                    // log::trace!("regenerating slot {:?} for root '{}'", slot, root);
                     regen_route(*slot);
                 }
 
                 for listener in onchange_listeners.borrow_mut().iter() {
-                    // log::trace!("regenerating listener {:?}", listener);
                     regen_route(*listener);
                 }
 
@@ -91,31 +89,24 @@ impl RouterService {
     }
 
     pub fn push_route(&self, route: &str) {
-        // log::trace!("Pushing route: {}", route);
         self.history.borrow_mut().push(route);
     }
 
     pub fn register_total_route(&self, route: String, scope: ScopeId, fallback: bool) {
         let clean = clean_route(route);
-        // log::trace!("Registered route '{}' with scope id {:?}", clean, scope);
         self.slots.borrow_mut().push((scope, clean));
     }
 
     pub fn should_render(&self, scope: ScopeId) -> bool {
-        // log::trace!("Should render scope id {:?}?", scope);
         if let Some(root_id) = self.root_found.get() {
-            // log::trace!("  we already found a root with scope id {:?}", root_id);
             if root_id == scope {
-                // log::trace!("    yes - it's a match");
                 return true;
             }
-            // log::trace!("    no - it's not a match");
             return false;
         }
 
         let location = self.history.borrow().location();
         let path = location.path();
-        // log::trace!("  current path is '{}'", path);
 
         let roots = self.slots.borrow();
 
@@ -124,23 +115,15 @@ impl RouterService {
         // fallback logic
         match root {
             Some((id, route)) => {
-                // log::trace!(
-                //     "  matched given scope id {:?} with route root '{}'",
-                //     scope,
-                //     route,
-                // );
                 if let Some(params) = route_matches_path(route, path) {
-                    // log::trace!("    and it matches the current path '{}'", path);
                     self.root_found.set(Some(*id));
                     *self.cur_path_params.borrow_mut() = params;
                     true
                 } else {
                     if route == "" {
-                        // log::trace!("    and the route is the root, so we will use that without a better match");
                         self.root_found.set(Some(*id));
                         true
                     } else {
-                        // log::trace!("    and the route '{}' is not the root nor does it match the current path", route);
                         false
                     }
                 }
@@ -158,12 +141,10 @@ impl RouterService {
     }
 
     pub fn subscribe_onchange(&self, id: ScopeId) {
-        // log::trace!("Subscribing onchange for scope id {:?}", id);
         self.onchange_listeners.borrow_mut().insert(id);
     }
 
     pub fn unsubscribe_onchange(&self, id: ScopeId) {
-        // log::trace!("Subscribing onchange for scope id {:?}", id);
         self.onchange_listeners.borrow_mut().remove(&id);
     }
 }
@@ -186,36 +167,20 @@ fn route_matches_path(route: &str, path: &str) -> Option<HashMap<String, String>
     let route_pieces = route.split('/').collect::<Vec<_>>();
     let path_pieces = clean_path(path).split('/').collect::<Vec<_>>();
 
-    // log::trace!(
-    //     "  checking route pieces {:?} vs path pieces {:?}",
-    //     route_pieces,
-    //     path_pieces,
-    // );
-
     if route_pieces.len() != path_pieces.len() {
-        // log::trace!("    the routes are different lengths");
         return None;
     }
 
     let mut matches = HashMap::new();
     for (i, r) in route_pieces.iter().enumerate() {
-        // log::trace!("    checking route piece '{}' vs path", r);
         // If this is a parameter then it matches as long as there's
         // _any_thing in that spot in the path.
         if r.starts_with(':') {
-            // log::trace!(
-            //     "      route piece '{}' starts with a colon so it matches anything",
-            //     r,
-            // );
             let param = &r[1..];
             matches.insert(param.to_string(), path_pieces[i].to_string());
             continue;
         }
-        // log::trace!(
-        //     "      route piece '{}' must be an exact match for path piece '{}'",
-        //     r,
-        //     path_pieces[i],
-        // );
+
         if path_pieces[i] != *r {
             return None;
         }