Skip to content

Commit 20b0c36

Browse files
committed
devtool middleware
1 parent aeb71ab commit 20b0c36

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/index.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ class Store {
8787
this._middlewares.forEach(m => {
8888
if (m.onMutation) {
8989
if (m.snapshot) {
90-
m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot)
90+
m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot, this)
9191
} else {
92-
m.onMutation({ type, payload }, state)
92+
m.onMutation({ type, payload }, state, this)
9393
}
9494
}
9595
})
@@ -129,18 +129,6 @@ class Store {
129129
this._setupModuleMutations(modules || this._modules)
130130
}
131131

132-
/**
133-
* Replace entire state tree.
134-
*/
135-
136-
___replaceState (newState) {
137-
const state = this._vm._data
138-
const clone = deepClone(newState)
139-
Object.keys(clone).forEach(key => {
140-
state[key] = clone[key]
141-
})
142-
}
143-
144132
/**
145133
* Attach sub state tree of each module to the root tree.
146134
*
@@ -151,7 +139,7 @@ class Store {
151139
_setupModuleState (state, modules) {
152140
const { setPath } = Vue.parsers.path
153141
Object.keys(modules).forEach(key => {
154-
setPath(state, key, modules[key].state)
142+
setPath(state, key, modules[key].state || {})
155143
})
156144
}
157145

@@ -168,6 +156,7 @@ class Store {
168156
const allMutations = [this._rootMutations]
169157
Object.keys(modules).forEach(key => {
170158
const module = modules[key]
159+
if (!module || !module.mutations) return
171160
// bind mutations to sub state tree
172161
const mutations = {}
173162
Object.keys(module.mutations).forEach(name => {
@@ -234,7 +223,7 @@ class Store {
234223
// call init hooks
235224
this._middlewares.forEach(m => {
236225
if (m.onInit) {
237-
m.onInit(m.snapshot ? initialSnapshot : state)
226+
m.onInit(m.snapshot ? initialSnapshot : state, this)
238227
}
239228
})
240229
}

src/middlewares/devtool.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
const hook =
2+
typeof window !== 'undefined' &&
3+
window.__VUE_DEVTOOLS_GLOBAL_HOOK__
4+
15
export default {
2-
onInit (state) {
3-
// TODO
6+
onInit (state, store) {
7+
if (!hook) return
8+
hook.emit('vuex:init', store)
9+
hook.on('vuex:travel-to-state', targetState => {
10+
const currentState = store._vm._data
11+
Object.keys(targetState).forEach(key => {
12+
currentState[key] = targetState[key]
13+
})
14+
})
415
},
516
onMutation (mutation, state) {
6-
// TODO
17+
if (!hook) return
18+
hook.emit('vuex:mutation', mutation, state)
719
}
820
}

0 commit comments

Comments
 (0)