Skip to content

Commit 81c35d4

Browse files
ktsnyyx990803
authored andcommitted
Adjust mapState helper to refer to actual state (vuejs#528)
* ensure update state passed to mapState when entire state is replaced * simplify module asserts registration
1 parent 7a64daf commit 81c35d4

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const mapState = normalizeNamespace((namespace, states) => {
1010
warnNamespace('mapState', namespace)
1111
return
1212
}
13-
state = module.state
13+
state = module.context.state
1414
getters = module.context.getters
1515
}
1616
return typeof val === 'function'

src/index.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,21 @@ function installModule (store, rootState, path, module, hot) {
236236
})
237237
}
238238

239-
const local = module.context = makeLocalContext(store, namespace)
239+
const local = module.context = makeLocalContext(store, namespace, path)
240240

241241
module.forEachMutation((mutation, key) => {
242242
const namespacedType = namespace + key
243-
registerMutation(store, namespacedType, mutation, path)
243+
registerMutation(store, namespacedType, mutation, local)
244244
})
245245

246246
module.forEachAction((action, key) => {
247247
const namespacedType = namespace + key
248-
registerAction(store, namespacedType, action, local, path)
248+
registerAction(store, namespacedType, action, local)
249249
})
250250

251251
module.forEachGetter((getter, key) => {
252252
const namespacedType = namespace + key
253-
registerGetter(store, namespacedType, getter, local, path)
253+
registerGetter(store, namespacedType, getter, local)
254254
})
255255

256256
module.forEachChild((child, key) => {
@@ -259,10 +259,10 @@ function installModule (store, rootState, path, module, hot) {
259259
}
260260

261261
/**
262-
* make localized dispatch, commit and getters
262+
* make localized dispatch, commit, getters and state
263263
* if there is no namespace, just use root ones
264264
*/
265-
function makeLocalContext (store, namespace) {
265+
function makeLocalContext (store, namespace, path) {
266266
const noNamespace = namespace === ''
267267

268268
const local = {
@@ -299,10 +299,17 @@ function makeLocalContext (store, namespace) {
299299
}
300300
}
301301

302-
// getters object must be gotten lazily
303-
// because store.getters will be changed by vm update
304-
Object.defineProperty(local, 'getters', {
305-
get: noNamespace ? () => store.getters : () => makeLocalGetters(store, namespace)
302+
// getters and state object must be gotten lazily
303+
// because they will be changed by vm update
304+
Object.defineProperties(local, {
305+
getters: {
306+
get: noNamespace
307+
? () => store.getters
308+
: () => makeLocalGetters(store, namespace)
309+
},
310+
state: {
311+
get: () => getNestedState(store.state, path)
312+
}
306313
})
307314

308315
return local
@@ -331,21 +338,21 @@ function makeLocalGetters (store, namespace) {
331338
return gettersProxy
332339
}
333340

334-
function registerMutation (store, type, handler, path) {
341+
function registerMutation (store, type, handler, local) {
335342
const entry = store._mutations[type] || (store._mutations[type] = [])
336343
entry.push(function wrappedMutationHandler (payload) {
337-
handler(getNestedState(store.state, path), payload)
344+
handler(local.state, payload)
338345
})
339346
}
340347

341-
function registerAction (store, type, handler, local, path) {
348+
function registerAction (store, type, handler, local) {
342349
const entry = store._actions[type] || (store._actions[type] = [])
343350
entry.push(function wrappedActionHandler (payload, cb) {
344351
let res = handler({
345352
dispatch: local.dispatch,
346353
commit: local.commit,
347354
getters: local.getters,
348-
state: getNestedState(store.state, path),
355+
state: local.state,
349356
rootGetters: store.getters,
350357
rootState: store.state
351358
}, payload, cb)
@@ -363,14 +370,14 @@ function registerAction (store, type, handler, local, path) {
363370
})
364371
}
365372

366-
function registerGetter (store, type, rawGetter, local, path) {
373+
function registerGetter (store, type, rawGetter, local) {
367374
if (store._wrappedGetters[type]) {
368375
console.error(`[vuex] duplicate getter key: ${type}`)
369376
return
370377
}
371378
store._wrappedGetters[type] = function wrappedGetter (store) {
372379
return rawGetter(
373-
getNestedState(store.state, path), // local state
380+
local.state, // local state
374381
local.getters, // local getters
375382
store.state, // root state
376383
store.getters // root getters

test/unit/helpers.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ describe('Helpers', () => {
6262
expect(vm.a).toBe(3)
6363
store.state.foo.a++
6464
expect(vm.a).toBe(5)
65+
store.replaceState({
66+
foo: { a: 3 }
67+
})
68+
expect(vm.a).toBe(7)
6569
})
6670

6771
it('mapMutations (array)', () => {

0 commit comments

Comments
 (0)