Quellcode durchsuchen

fix: advance dynamic root

Jonathan Kelley vor 2 Jahren
Ursprung
Commit
e0390ff608
3 geänderte Dateien mit 27 neuen und 22 gelöschten Zeilen
  1. 14 15
      examples/flat_router.rs
  2. 9 3
      packages/core/src/create.rs
  3. 4 4
      packages/core/src/diff.rs

+ 14 - 15
examples/flat_router.rs

@@ -7,7 +7,6 @@ fn main() {
 
     let cfg = Config::new().with_window(
         WindowBuilder::new()
-            .with_title("Spinsense Client")
             .with_inner_size(LogicalSize::new(600, 1000))
             .with_resizable(false),
     );
@@ -17,21 +16,21 @@ fn main() {
 
 fn app(cx: Scope) -> Element {
     cx.render(rsx! {
-        Router {
-            Route { to: "/", "Home" }
-            Route { to: "/games", "Games" }
-            Route { to: "/play", "Play" }
-            Route { to: "/settings", "Settings" }
+        div {
+            Router {
+                Route { to: "/", "Home" }
+                Route { to: "/games", "Games" }
+                Route { to: "/play", "Play" }
+                Route { to: "/settings", "Settings" }
 
-            p {
-                "----"
-            }
-            nav {
-                ul {
-                    Link { to: "/", li { "Home" } }
-                    Link { to: "/games", li { "Games" } }
-                    Link { to: "/play", li { "Play" } }
-                    Link { to: "/settings", li { "Settings" } }
+                p { "----" }
+                nav {
+                    ul {
+                        Link { to: "/", li { "Home" } }
+                        Link { to: "/games", li { "Games" } }
+                        Link { to: "/play", li { "Play" } }
+                        Link { to: "/settings", li { "Settings" } }
+                    }
                 }
             }
         }

+ 9 - 3
packages/core/src/create.rs

@@ -44,7 +44,10 @@ impl<'b> VirtualDom {
             .iter()
             .enumerate()
             .map(|(idx, root)| match root {
-                DynamicText { id } | Dynamic { id } => self.write_dynamic_root(node, *id),
+                DynamicText { id } | Dynamic { id } => {
+                    nodes.next().unwrap();
+                    self.write_dynamic_root(node, *id)
+                }
                 Element { .. } => self.write_element_root(node, idx, &mut attrs, &mut nodes),
                 Text { .. } => self.write_static_text_root(node, idx),
             })
@@ -63,7 +66,7 @@ impl<'b> VirtualDom {
         use DynamicNode::*;
         match &template.dynamic_nodes[idx] {
             node @ Fragment(_) => self.create_dynamic_node(template, node, idx),
-            node @ Component { .. } => self.create_dynamic_node(template, node, idx),
+            node @ Component { .. } => dbg!(self.create_dynamic_node(template, node, idx)),
             Placeholder(VPlaceholder { id }) => {
                 let id = self.set_slot(template, id, idx);
                 self.mutations.push(CreatePlaceholder { id });
@@ -131,7 +134,10 @@ impl<'b> VirtualDom {
     ) {
         let (start, end) = match collect_dyn_node_range(dynamic_nodes, root_idx) {
             Some((a, b)) => (a, b),
-            None => return,
+            None => {
+                println!("No dynamic nodes found for root {}", root_idx);
+                return;
+            }
         };
 
         for idx in (start..=end).rev() {

+ 4 - 4
packages/core/src/diff.rs

@@ -711,8 +711,11 @@ impl<'b> VirtualDom {
 
     fn replace(&mut self, left: &'b VNode<'b>, right: impl IntoIterator<Item = &'b VNode<'b>>) {
         let m = self.create_children(right);
+
         let id = self.find_last_element(left);
+
         self.mutations.push(Mutation::InsertAfter { id, m });
+
         self.remove_node(left, true);
     }
 
@@ -735,10 +738,7 @@ impl<'b> VirtualDom {
     /// Remove these nodes from the dom
     /// Wont generate mutations for the inner nodes
     fn remove_nodes(&mut self, nodes: &'b [VNode<'b>]) {
-        nodes
-            .iter()
-            .rev()
-            .for_each(|node| self.remove_node(node, true));
+        nodes.iter().for_each(|node| self.remove_node(node, true));
     }
 
     fn remove_node(&mut self, node: &'b VNode<'b>, gen_muts: bool) {