Browse Source

Adjust mapState helper to refer to actual state (#528)

* ensure update state passed to mapState when entire state is replaced

* simplify module asserts registration
katashin 8 years ago
parent
commit
81c35d48f0
3 changed files with 28 additions and 17 deletions
  1. 1 1
      src/helpers.js
  2. 23 16
      src/index.js
  3. 4 0
      test/unit/helpers.spec.js

+ 1 - 1
src/helpers.js

@@ -10,7 +10,7 @@ export const mapState = normalizeNamespace((namespace, states) => {
           warnNamespace('mapState', namespace)
           warnNamespace('mapState', namespace)
           return
           return
         }
         }
-        state = module.state
+        state = module.context.state
         getters = module.context.getters
         getters = module.context.getters
       }
       }
       return typeof val === 'function'
       return typeof val === 'function'

+ 23 - 16
src/index.js

@@ -236,21 +236,21 @@ function installModule (store, rootState, path, module, hot) {
     })
     })
   }
   }
 
 
-  const local = module.context = makeLocalContext(store, namespace)
+  const local = module.context = makeLocalContext(store, namespace, path)
 
 
   module.forEachMutation((mutation, key) => {
   module.forEachMutation((mutation, key) => {
     const namespacedType = namespace + key
     const namespacedType = namespace + key
-    registerMutation(store, namespacedType, mutation, path)
+    registerMutation(store, namespacedType, mutation, local)
   })
   })
 
 
   module.forEachAction((action, key) => {
   module.forEachAction((action, key) => {
     const namespacedType = namespace + key
     const namespacedType = namespace + key
-    registerAction(store, namespacedType, action, local, path)
+    registerAction(store, namespacedType, action, local)
   })
   })
 
 
   module.forEachGetter((getter, key) => {
   module.forEachGetter((getter, key) => {
     const namespacedType = namespace + key
     const namespacedType = namespace + key
-    registerGetter(store, namespacedType, getter, local, path)
+    registerGetter(store, namespacedType, getter, local)
   })
   })
 
 
   module.forEachChild((child, key) => {
   module.forEachChild((child, key) => {
@@ -259,10 +259,10 @@ function installModule (store, rootState, path, module, hot) {
 }
 }
 
 
 /**
 /**
- * make localized dispatch, commit and getters
+ * make localized dispatch, commit, getters and state
  * if there is no namespace, just use root ones
  * if there is no namespace, just use root ones
  */
  */
-function makeLocalContext (store, namespace) {
+function makeLocalContext (store, namespace, path) {
   const noNamespace = namespace === ''
   const noNamespace = namespace === ''
 
 
   const local = {
   const local = {
@@ -299,10 +299,17 @@ function makeLocalContext (store, namespace) {
     }
     }
   }
   }
 
 
-  // getters object must be gotten lazily
-  // because store.getters will be changed by vm update
-  Object.defineProperty(local, 'getters', {
-    get: noNamespace ? () => store.getters : () => makeLocalGetters(store, namespace)
+  // getters and state object must be gotten lazily
+  // because they will be changed by vm update
+  Object.defineProperties(local, {
+    getters: {
+      get: noNamespace
+        ? () => store.getters
+        : () => makeLocalGetters(store, namespace)
+    },
+    state: {
+      get: () => getNestedState(store.state, path)
+    }
   })
   })
 
 
   return local
   return local
@@ -331,21 +338,21 @@ function makeLocalGetters (store, namespace) {
   return gettersProxy
   return gettersProxy
 }
 }
 
 
-function registerMutation (store, type, handler, path) {
+function registerMutation (store, type, handler, local) {
   const entry = store._mutations[type] || (store._mutations[type] = [])
   const entry = store._mutations[type] || (store._mutations[type] = [])
   entry.push(function wrappedMutationHandler (payload) {
   entry.push(function wrappedMutationHandler (payload) {
-    handler(getNestedState(store.state, path), payload)
+    handler(local.state, payload)
   })
   })
 }
 }
 
 
-function registerAction (store, type, handler, local, path) {
+function registerAction (store, type, handler, local) {
   const entry = store._actions[type] || (store._actions[type] = [])
   const entry = store._actions[type] || (store._actions[type] = [])
   entry.push(function wrappedActionHandler (payload, cb) {
   entry.push(function wrappedActionHandler (payload, cb) {
     let res = handler({
     let res = handler({
       dispatch: local.dispatch,
       dispatch: local.dispatch,
       commit: local.commit,
       commit: local.commit,
       getters: local.getters,
       getters: local.getters,
-      state: getNestedState(store.state, path),
+      state: local.state,
       rootGetters: store.getters,
       rootGetters: store.getters,
       rootState: store.state
       rootState: store.state
     }, payload, cb)
     }, payload, cb)
@@ -363,14 +370,14 @@ function registerAction (store, type, handler, local, path) {
   })
   })
 }
 }
 
 
-function registerGetter (store, type, rawGetter, local, path) {
+function registerGetter (store, type, rawGetter, local) {
   if (store._wrappedGetters[type]) {
   if (store._wrappedGetters[type]) {
     console.error(`[vuex] duplicate getter key: ${type}`)
     console.error(`[vuex] duplicate getter key: ${type}`)
     return
     return
   }
   }
   store._wrappedGetters[type] = function wrappedGetter (store) {
   store._wrappedGetters[type] = function wrappedGetter (store) {
     return rawGetter(
     return rawGetter(
-      getNestedState(store.state, path), // local state
+      local.state, // local state
       local.getters, // local getters
       local.getters, // local getters
       store.state, // root state
       store.state, // root state
       store.getters // root getters
       store.getters // root getters

+ 4 - 0
test/unit/helpers.spec.js

@@ -62,6 +62,10 @@ describe('Helpers', () => {
     expect(vm.a).toBe(3)
     expect(vm.a).toBe(3)
     store.state.foo.a++
     store.state.foo.a++
     expect(vm.a).toBe(5)
     expect(vm.a).toBe(5)
+    store.replaceState({
+      foo: { a: 3 }
+    })
+    expect(vm.a).toBe(7)
   })
   })
 
 
   it('mapMutations (array)', () => {
   it('mapMutations (array)', () => {