Quellcode durchsuchen

Feat: cleanup edit module

Jonathan Kelley vor 4 Jahren
Ursprung
Commit
c70652a
1 geänderte Dateien mit 65 neuen und 79 gelöschten Zeilen
  1. 65 79
      packages/core/src/patch.rs

+ 65 - 79
packages/core/src/patch.rs

@@ -16,101 +16,112 @@
 //! Because the change list references data internal to the vdom, it needs to be consumed by the renderer before the vdom
 //! can continue to work. This means once a change list is generated, it should be consumed as fast as possible, otherwise the
 //! dom will be blocked from progressing. This is enforced by lifetimes on the returend changelist object.
-//!
-//!
 
-use bumpalo::Bump;
+use crate::innerlude::ScopeIdx;
+
+pub type EditList<'src> = Vec<Edit<'src>>;
 
-use crate::innerlude::{Listener, ScopeIdx};
-use serde::{Deserialize, Serialize};
 /// The `Edit` represents a single modifcation of the renderer tree.
 /// todo@ jon: allow serde to be optional
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Debug, serde::Serialize, serde::Deserialize)]
 #[serde(tag = "type")]
 pub enum Edit<'d> {
+    // ========================================================
+    // Common Ops: The most common operation types
+    // ========================================================
     SetText {
         text: &'d str,
     },
-    RemoveSelfAndNextSiblings {},
-    ReplaceWith,
-    SetAttribute {
-        name: &'d str,
-        value: &'d str,
-    },
-    RemoveAttribute {
-        name: &'d str,
-    },
-    PushReverseChild {
-        n: u32,
-    },
-    PopPushChild {
-        n: u32,
+    SetClass {
+        class_name: &'d str,
     },
-    Pop,
-    AppendChild,
     CreateTextNode {
         text: &'d str,
     },
     CreateElement {
         tag_name: &'d str,
     },
-
     CreateElementNs {
         tag_name: &'d str,
         ns: &'d str,
     },
-    SaveChildrenToTemporaries {
-        temp: u32,
-        start: u32,
-        end: u32,
-    },
-    PushChild {
-        n: u32,
-    },
-    PushTemporary {
-        temp: u32,
+
+    // ========================================================
+    // Attributes
+    // ========================================================
+    SetAttribute {
+        name: &'d str,
+        value: &'d str,
     },
-    InsertBefore,
-    PopPushReverseChild {
-        n: u32,
+    RemoveAttribute {
+        name: &'d str,
     },
     RemoveChild {
         n: u32,
     },
-    SetClass {
-        class_name: &'d str,
+
+    // ============================================================
+    // Event Listeners: Event types and IDs used to update the VDOM
+    // ============================================================
+    NewListener {
+        event: &'d str,
+        scope: ScopeIdx,
+        id: usize,
+    },
+    UpdateListener {
+        event: &'d str,
+        scope: ScopeIdx,
+        id: usize,
+    },
+    RemoveListener {
+        event: &'d str,
     },
 
+    // ========================================================
+    // Cached Roots: The mount point for individual components
+    //               Allows quick traversal to cached entrypoints
+    // ========================================================
     // push a known node on to the stack
     TraverseToKnown {
         node: ScopeIdx,
     },
-
     // Add the current top of the stack to the known nodes
     MakeKnown {
         node: ScopeIdx,
     },
-
     // Remove the current top of the stack from the known nodes
     RemoveKnown,
 
-    NewListener {
-        event: &'d str,
-        scope: ScopeIdx,
-        id: usize,
+    // ========================================================
+    // Stack OPs: Operations for manipulating the stack machine
+    // ========================================================
+    PushReverseChild {
+        n: u32,
     },
-    UpdateListener {
-        event: &'d str,
-        scope: ScopeIdx,
-        id: usize,
+    PopPushChild {
+        n: u32,
     },
-    RemoveListener {
-        event: &'d str,
+    Pop,
+    AppendChild,
+    RemoveSelfAndNextSiblings {},
+    ReplaceWith,
+    SaveChildrenToTemporaries {
+        temp: u32,
+        start: u32,
+        end: u32,
+    },
+    PushChild {
+        n: u32,
+    },
+    PushTemporary {
+        temp: u32,
+    },
+    InsertBefore,
+    PopPushReverseChild {
+        n: u32,
     },
 }
 
-pub type EditList<'src> = Vec<Edit<'src>>;
-
 pub struct EditMachine<'src> {
     pub traversal: Traversal,
     next_temporary: u32,
@@ -119,7 +130,8 @@ pub struct EditMachine<'src> {
 }
 
 impl<'b> EditMachine<'b> {
-    pub fn new(_bump: &'b Bump) -> Self {
+    pub fn new(_bump: &'b bumpalo::Bump) -> Self {
+        // todo: see if bumpalo is needed for edit list
         Self {
             traversal: Traversal::new(),
             next_temporary: 0,
@@ -373,7 +385,6 @@ impl<'a> EditMachine<'a> {
 
 // Keeps track of where we are moving in a DOM tree, and shortens traversal
 // paths between mutations to their minimal number of operations.
-
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub enum MoveTo {
     /// Move from the current node up to its parent.
@@ -503,9 +514,7 @@ impl Traversal {
     /// that have *not* been committed yet?
     #[inline]
     pub fn is_committed(&self) -> bool {
-        // is_empty is not inlined?
         self.uncommitted.is_empty()
-        // self.uncommitted.len() == 0
     }
 
     /// Commit this traversals moves and return the optimized path from the last
@@ -521,19 +530,6 @@ impl Traversal {
     }
 }
 
-// pub struct Moves<'a> {
-//     inner: std::vec::Drain<'a, MoveTo>,
-// }
-
-// impl Iterator for Moves<'_> {
-//     type Item = MoveTo;
-
-//     #[inline]
-//     fn next(&mut self) -> Option<MoveTo> {
-//         self.inner.next()
-//     }
-// }
-
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -715,13 +711,3 @@ mod tests {
         }
     }
 }
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct StringKey(u32);
-
-impl From<StringKey> for u32 {
-    #[inline]
-    fn from(key: StringKey) -> u32 {
-        key.0
-    }
-}