Skip to content

Commit 7e59df4

Browse files
Guillaume Chauiksim
authored andcommitted
refactor(vuex): only take snapshots of the store state, closes vuejs#848
1 parent 302c7ef commit 7e59df4

File tree

5 files changed

+69
-41
lines changed

5 files changed

+69
-41
lines changed

shells/dev/target/store.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ export default new Vuex.Store({
5757
}
5858
},
5959
getters: {
60-
twoFoos: state => state.foo.repeat(2)
60+
twoFoos: state => state.foo.repeat(2),
61+
dummy: () => {
62+
console.log('dummy getter was computed')
63+
return 'dummy'
64+
}
6165
},
6266
mutations: {
6367
ADD_BAR: (state) => {

src/backend/hook.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export function installHook (target) {
9898

9999
hook.once('vuex:init', store => {
100100
hook.store = store
101-
hook.initialStore = clone(store)
101+
hook.initialStore = {
102+
state: clone(store.state),
103+
getters: store.getters
104+
}
102105
})
103106

104107
Object.defineProperty(target, '__VUE_DEVTOOLS_GLOBAL_HOOK__', {

src/backend/vuex.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,26 @@ export function initVuexBackend (hook, bridge, isLegacy) {
1414
computed: originalVm.$options.computed
1515
})
1616

17-
const getSnapshot = (_store = store) => stringify({
18-
state: _store.state,
19-
getters: _store.getters || {}
20-
})
17+
const getStateSnapshot = (_store = store) => stringify(_store.state)
2118

22-
let baseSnapshot, snapshots, mutations, lastState
19+
let baseStateSnapshot, stateSnapshots, mutations, lastState
2320

2421
function reset () {
25-
baseSnapshot = getSnapshot(hook.initialStore)
22+
baseStateSnapshot = getStateSnapshot(hook.initialStore)
2623
hook.initialStore = undefined
2724
mutations = []
2825
resetSnapshotCache()
2926
}
3027

3128
function resetSnapshotCache () {
32-
snapshots = [
33-
{ index: -1, state: baseSnapshot }
29+
stateSnapshots = [
30+
{ index: -1, state: baseStateSnapshot }
3431
]
3532
}
3633

3734
reset()
3835

39-
bridge.send('vuex:init', baseSnapshot)
36+
bridge.send('vuex:init')
4037

4138
// deal with multiple backend injections
4239
hook.off('vuex:mutation')
@@ -66,11 +63,11 @@ export function initVuexBackend (hook, bridge, isLegacy) {
6663

6764
// devtool -> application
6865
bridge.on('vuex:travel-to-state', ({ index, apply }) => {
69-
const snapshot = replayMutations(index)
70-
const { state } = parse(snapshot, true)
66+
const stateSnapshot = replayMutations(index)
67+
const state = parse(stateSnapshot, true)
7168
bridge.send('vuex:inspected-state', {
7269
index,
73-
snapshot
70+
snapshot: getSnapshot(stateSnapshot)
7471
})
7572
if (apply) {
7673
hook.emit('vuex:travel-to-state', state)
@@ -86,7 +83,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
8683
})
8784

8885
bridge.on('vuex:commit', index => {
89-
baseSnapshot = lastState
86+
baseStateSnapshot = lastState
9087
resetSnapshotCache()
9188
mutations = mutations.slice(index + 1)
9289
mutations.forEach((mutation, index) => {
@@ -101,45 +98,52 @@ export function initVuexBackend (hook, bridge, isLegacy) {
10198

10299
bridge.on('vuex:import-state', state => {
103100
hook.emit('vuex:travel-to-state', parse(state, true))
104-
bridge.send('vuex:init', getSnapshot())
101+
bridge.send('vuex:init')
105102
})
106103

107104
bridge.on('vuex:inspect-state', index => {
108-
const snapshot = replayMutations(index)
109-
bridge.send('vuex:inspected-state', {
110-
index,
111-
snapshot
112-
})
105+
if (index === -1) {
106+
bridge.send('vuex:inspected-state', {
107+
index,
108+
snapshot: getSnapshot(baseStateSnapshot)
109+
})
110+
} else {
111+
const stateSnapshot = replayMutations(index)
112+
bridge.send('vuex:inspected-state', {
113+
index,
114+
snapshot: getSnapshot(stateSnapshot)
115+
})
116+
}
113117
})
114118

115119
function replayMutations (index) {
116120
store._vm = snapshotsVm
117121

118122
// Get most recent snapshot for target index
119123
// for faster replay
120-
let snapshot
121-
for (let i = 0; i < snapshots.length; i++) {
122-
const s = snapshots[i]
124+
let stateSnapshot
125+
for (let i = 0; i < stateSnapshots.length; i++) {
126+
const s = stateSnapshots[i]
123127
if (s.index > index) {
124128
break
125129
} else {
126-
snapshot = s
130+
stateSnapshot = s
127131
}
128132
}
129133

130134
let resultState
131135

132136
// Snapshot was already replayed
133-
if (snapshot.index === index) {
134-
resultState = snapshot.state
137+
if (stateSnapshot.index === index) {
138+
resultState = stateSnapshot.state
135139
} else {
136-
const { state } = parse(snapshot.state, true)
140+
const state = parse(stateSnapshot.state, true)
137141
store.replaceState(state)
138142

139143
SharedData.snapshotLoading = true
140144

141145
// Replay mutations
142-
for (let i = snapshot.index + 1; i <= index; i++) {
146+
for (let i = stateSnapshot.index + 1; i <= index; i++) {
143147
const mutation = mutations[i]
144148
if (mutation.handlers) {
145149
if (Array.isArray(mutation.handlers)) {
@@ -155,12 +159,12 @@ export function initVuexBackend (hook, bridge, isLegacy) {
155159
}
156160

157161
if (i !== index && i % SharedData.cacheVuexSnapshotsEvery === 0) {
158-
takeSnapshot(i, state)
162+
takeStateSnapshot(i, state)
159163
}
160164
}
161165

162166
// Send final state after replay
163-
resultState = getSnapshot()
167+
resultState = getStateSnapshot()
164168
}
165169

166170
lastState = resultState
@@ -185,15 +189,34 @@ export function initVuexBackend (hook, bridge, isLegacy) {
185189
})
186190
})
187191

188-
function takeSnapshot (index) {
189-
snapshots.push({
192+
function takeStateSnapshot (index) {
193+
stateSnapshots.push({
190194
index,
191-
state: getSnapshot()
195+
state: getStateSnapshot()
192196
})
193197
// Delete old cached snapshots
194-
if (snapshots.length > SharedData.cacheVuexSnapshotsLimit) {
195-
snapshots.splice(1, 1)
198+
if (stateSnapshots.length > SharedData.cacheVuexSnapshotsLimit) {
199+
stateSnapshots.splice(1, 1)
200+
}
201+
}
202+
203+
function getSnapshot (stateSnapshot = null) {
204+
if (stateSnapshot) {
205+
store._vm = snapshotsVm
206+
store.replaceState(parse(stateSnapshot, true))
196207
}
208+
209+
const result = stringify({
210+
state: store.state,
211+
getters: store.getters || {}
212+
})
213+
214+
if (stateSnapshot) {
215+
// Restore user state
216+
store._vm = originalVm
217+
}
218+
219+
return result
197220
}
198221
}
199222

src/devtools/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ function initApp (shell) {
133133
store.commit('components/TOGGLE_INSTANCE', parse(payload))
134134
})
135135

136-
bridge.on('vuex:init', snapshot => {
137-
store.commit('vuex/INIT', snapshot)
136+
bridge.on('vuex:init', () => {
137+
store.commit('vuex/INIT')
138138
})
139139

140140
bridge.on('vuex:mutation', payload => {

src/devtools/views/vuex/module.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ let uid = 0
1010

1111
const state = {
1212
hasVuex: false,
13-
initial: null,
1413
base: null, // type Snapshot = { state: {}, getters: {} }
1514
inspectedIndex: -1,
1615
activeIndex: -1,
@@ -25,8 +24,7 @@ const state = {
2524
}
2625

2726
const mutations = {
28-
'INIT' (state, snapshot) {
29-
state.initial = state.base = snapshot
27+
'INIT' (state) {
3028
state.hasVuex = true
3129
reset(state)
3230
},

0 commit comments

Comments
 (0)