Browse Source

devtool middleware

Evan You 9 năm trước cách đây
mục cha
commit
20b0c365de
2 tập tin đã thay đổi với 20 bổ sung19 xóa
  1. 5 16
      src/index.js
  2. 15 3
      src/middlewares/devtool.js

+ 5 - 16
src/index.js

@@ -87,9 +87,9 @@ class Store {
       this._middlewares.forEach(m => {
         if (m.onMutation) {
           if (m.snapshot) {
-            m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot)
+            m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot, this)
           } else {
-            m.onMutation({ type, payload }, state)
+            m.onMutation({ type, payload }, state, this)
           }
         }
       })
@@ -129,18 +129,6 @@ class Store {
     this._setupModuleMutations(modules || this._modules)
   }
 
-  /**
-   * Replace entire state tree.
-   */
-
-  replaceState (newState) {
-    const state = this._vm._data
-    const clone = deepClone(newState)
-    Object.keys(clone).forEach(key => {
-      state[key] = clone[key]
-    })
-  }
-
   /**
    * Attach sub state tree of each module to the root tree.
    *
@@ -151,7 +139,7 @@ class Store {
   _setupModuleState (state, modules) {
     const { setPath } = Vue.parsers.path
     Object.keys(modules).forEach(key => {
-      setPath(state, key, modules[key].state)
+      setPath(state, key, modules[key].state || {})
     })
   }
 
@@ -168,6 +156,7 @@ class Store {
     const allMutations = [this._rootMutations]
     Object.keys(modules).forEach(key => {
       const module = modules[key]
+      if (!module || !module.mutations) return
       // bind mutations to sub state tree
       const mutations = {}
       Object.keys(module.mutations).forEach(name => {
@@ -234,7 +223,7 @@ class Store {
     // call init hooks
     this._middlewares.forEach(m => {
       if (m.onInit) {
-        m.onInit(m.snapshot ? initialSnapshot : state)
+        m.onInit(m.snapshot ? initialSnapshot : state, this)
       }
     })
   }

+ 15 - 3
src/middlewares/devtool.js

@@ -1,8 +1,20 @@
+const hook =
+  typeof window !== 'undefined' &&
+  window.__VUE_DEVTOOLS_GLOBAL_HOOK__
+
 export default {
-  onInit (state) {
-    // TODO
+  onInit (state, store) {
+    if (!hook) return
+    hook.emit('vuex:init', store)
+    hook.on('vuex:travel-to-state', targetState => {
+      const currentState = store._vm._data
+      Object.keys(targetState).forEach(key => {
+        currentState[key] = targetState[key]
+      })
+    })
   },
   onMutation (mutation, state) {
-    // TODO
+    if (!hook) return
+    hook.emit('vuex:mutation', mutation, state)
   }
 }