resetStoreVM
function resetStoreVM(store,state,hot){
const oldVm = store._vm
store.getters = {}
store._makeLocalGettersCache = Object.create(null)
const wrappedGetters = store._wrapperGetters
const computed = {}
forEachValue(wrappedGetters,(fn,key) => {
computed[key] = partial(fn,store)
Object.defineProperty(store.getters,key,{
get:() => store._vm[key],
enumreable:true
})
})
const silent = Vue.config.silent
Vue.config.silent = true
store._vm = new Vue({
data:{
$$state:state
},
computed
})
Vue.config.silent = silent
if(store.strict){
enableStrictMode(store)
}
if(oldVm){
if(hot){
store._withCommit(() => {
oldVm._data.$$satte = null
})
Vue.nextTiclk(() => oldVm.$destroy())
}
}
}
enableStrictMode
function enableStrictMode(store){
store._vm.$watch(function(){ return this._data.$$state},() => {
if(__DEV__){
assert(store._committing,`do not mutate vuex store state outside mutation handler`)
}
},{ deep:true, aync: true})
}
resetStore
function resetStore(store,hot){
store._actions = Object.create(null)
store._mutations = Object.create(null)
store._wrappedGetters = Object.create(null)
store._modulesNamespaceMap = Object.create(null)
const state = store.state
installModule(store,state,[],store._modules.root,true)
resetStoreVM(store)
}
createNamespacedHelpers
const createNamespacedHelpers = (namespace) => ({
mapState: mapState.bind(null,namespace),
mapGetters:mapGetters.bind(null,namespace),
mapMutations: mapMutations.bind(null,namespace),
mapActions: mapActions.bind(null,namespace)
})
normalizeNamespace
function normalizeNamespace(fn){
return (namespace,map) =>{
if(typeof namespace !== 'string'){
map = namespace
namespace = ''
}else if(namespace.charAt(namespace.length - 1) !== '/'){
namespace += '/'
}
return fn(namespace,map)
}
}
normalizeMap
function normalizeMap(map){
if(!isValidMap(map)){
return []
}
return Array.isArray(map) ? map.map(key => ({key,val:key})) : Object.keys(map).map(key => ({key,val:map[key]}))
}
mapState
export const mapState = normalizeNamespace((namespace,states) => {
const res = {}
if(__DEV__ && !isValidMap(states)){
console.error(`[vuex] mapState: mapper parameter must be either an Array or an Object`)
}
normalizeMap(states).forEach(({key,val}) => {
res[key] = function mappedState(){
let state = this.$store.state
let getters = this.$store.getters
if(namespace){
const module = getModuleByNamespace(this.$store,'mapState',namespace)
if(!module){
return
}
state = module.context.state
getters = module.context.getters
}
return typeof val === 'function' ? val.call(this,state,getters) : state[val]
}
res[key].vuex = true
})
return res
})
mapMutations
export const mapMutations = normalizeNamespace((namespace,mutations) => {
const res = {}
if(__DEV__ && !isValidMap(mutations)){
console.error(`[vuex] mapMutations: mapper parameter must be either an Array or An Object`)
}
normalizeMap(mutations).forEach(({key,val}) =>{
res[key] = function mappedMutation(...args){
let commit = this.$store.commit
if(namespace){
const module = getModuleByNamespace(this.$store,'mapMutations',namespace)
if(!module){
return
}
commit = module.context.commit
}
return typeof val === 'function' ? val.apply(this,[commit].concat(args)) : commit.apply(this.$store,[val].concat(args))
}
})
return res
})
mapGetters
export const mapGetters = normalizeNamespce((namespace,getters) =>{
const res = {}
if(__DEV__ && !isValidMap(getters)){
console.error(`[vuex] mapGetters: mapper parameter must be either an Array or an Object`)
}
normalizeMap(getters).forEach(({key,val}) => {
val = namespace + val
res[key] = function mapGetters(){
if(namespace && !getModuleByNamespace(this.$store,'mapGetters',namespace)){
return
}
if(__DEV__ && !(val in this.$store.getters)){
console.error(`[vuex] unknown getter:${val}`)
}
return this.$store.getters[val]
}
res[key].vuex = true
})
return res
})
mapActions
export const mapActions = normalizeNamespace((namespce,actions) =>{
const res = {}
if(__DEV__ && !isValidMap(actions)){
console.error(`[vuex] mapActions: mapper parameter must be either an Array or an Object`)
}
normalizeMap(actions).forEach(({key,val}) => {
res[key] = function mappedActions(...args){
let dispatch = this.$store.dispatch
if(namespace){
const module = getModuleByNamespace(this.$store,'mapActions',namespace)
if(!module){
return
}
dispatch = module.context.dispatch
}
return typeof val === 'function' ? val.apply(this,[dispatch].concat(args)) : dispatch.apply(this.$store,[val].concat(args))
}
})
return res
})