Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const mapState = normalizeNamespace((namespace, states) => {
warnNamespace('mapState', namespace)
return
}
state = module.state
state = module.context.state
getters = module.context.getters
}
return typeof val === 'function'
Expand Down
39 changes: 23 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
const namespacedType = namespace + key
registerMutation(store, namespacedType, mutation, path)
registerMutation(store, namespacedType, mutation, local)
})

module.forEachAction((action, key) => {
const namespacedType = namespace + key
registerAction(store, namespacedType, action, local, path)
registerAction(store, namespacedType, action, local)
})

module.forEachGetter((getter, key) => {
const namespacedType = namespace + key
registerGetter(store, namespacedType, getter, local, path)
registerGetter(store, namespacedType, getter, local)
})

module.forEachChild((child, key) => {
Expand All @@ -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
*/
function makeLocalContext (store, namespace) {
function makeLocalContext (store, namespace, path) {
const noNamespace = namespace === ''

const local = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -331,21 +338,21 @@ function makeLocalGetters (store, namespace) {
return gettersProxy
}

function registerMutation (store, type, handler, path) {
function registerMutation (store, type, handler, local) {
const entry = store._mutations[type] || (store._mutations[type] = [])
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] = [])
entry.push(function wrappedActionHandler (payload, cb) {
let res = handler({
dispatch: local.dispatch,
commit: local.commit,
getters: local.getters,
state: getNestedState(store.state, path),
state: local.state,
rootGetters: store.getters,
rootState: store.state
}, payload, cb)
Expand All @@ -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]) {
console.error(`[vuex] duplicate getter key: ${type}`)
return
}
store._wrappedGetters[type] = function wrappedGetter (store) {
return rawGetter(
getNestedState(store.state, path), // local state
local.state, // local state
local.getters, // local getters
store.state, // root state
store.getters // root getters
Expand Down
4 changes: 4 additions & 0 deletions test/unit/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('Helpers', () => {
expect(vm.a).toBe(3)
store.state.foo.a++
expect(vm.a).toBe(5)
store.replaceState({
foo: { a: 3 }
})
expect(vm.a).toBe(7)
})

it('mapMutations (array)', () => {
Expand Down