@@ -14,29 +14,26 @@ export function initVuexBackend (hook, bridge, isLegacy) {
14
14
computed : originalVm . $options . computed
15
15
} )
16
16
17
- const getSnapshot = ( _store = store ) => stringify ( {
18
- state : _store . state ,
19
- getters : _store . getters || { }
20
- } )
17
+ const getStateSnapshot = ( _store = store ) => stringify ( _store . state )
21
18
22
- let baseSnapshot , snapshots , mutations , lastState
19
+ let baseStateSnapshot , stateSnapshots , mutations , lastState
23
20
24
21
function reset ( ) {
25
- baseSnapshot = getSnapshot ( hook . initialStore )
22
+ baseStateSnapshot = getStateSnapshot ( hook . initialStore )
26
23
hook . initialStore = undefined
27
24
mutations = [ ]
28
25
resetSnapshotCache ( )
29
26
}
30
27
31
28
function resetSnapshotCache ( ) {
32
- snapshots = [
33
- { index : - 1 , state : baseSnapshot }
29
+ stateSnapshots = [
30
+ { index : - 1 , state : baseStateSnapshot }
34
31
]
35
32
}
36
33
37
34
reset ( )
38
35
39
- bridge . send ( 'vuex:init' , baseSnapshot )
36
+ bridge . send ( 'vuex:init' )
40
37
41
38
// deal with multiple backend injections
42
39
hook . off ( 'vuex:mutation' )
@@ -66,11 +63,11 @@ export function initVuexBackend (hook, bridge, isLegacy) {
66
63
67
64
// devtool -> application
68
65
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 )
71
68
bridge . send ( 'vuex:inspected-state' , {
72
69
index,
73
- snapshot
70
+ snapshot : getSnapshot ( stateSnapshot )
74
71
} )
75
72
if ( apply ) {
76
73
hook . emit ( 'vuex:travel-to-state' , state )
@@ -86,7 +83,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
86
83
} )
87
84
88
85
bridge . on ( 'vuex:commit' , index => {
89
- baseSnapshot = lastState
86
+ baseStateSnapshot = lastState
90
87
resetSnapshotCache ( )
91
88
mutations = mutations . slice ( index + 1 )
92
89
mutations . forEach ( ( mutation , index ) => {
@@ -101,45 +98,52 @@ export function initVuexBackend (hook, bridge, isLegacy) {
101
98
102
99
bridge . on ( 'vuex:import-state' , state => {
103
100
hook . emit ( 'vuex:travel-to-state' , parse ( state , true ) )
104
- bridge . send ( 'vuex:init' , getSnapshot ( ) )
101
+ bridge . send ( 'vuex:init' )
105
102
} )
106
103
107
104
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
+ }
113
117
} )
114
118
115
119
function replayMutations ( index ) {
116
120
store . _vm = snapshotsVm
117
121
118
122
// Get most recent snapshot for target index
119
123
// 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 ]
123
127
if ( s . index > index ) {
124
128
break
125
129
} else {
126
- snapshot = s
130
+ stateSnapshot = s
127
131
}
128
132
}
129
133
130
134
let resultState
131
135
132
136
// Snapshot was already replayed
133
- if ( snapshot . index === index ) {
134
- resultState = snapshot . state
137
+ if ( stateSnapshot . index === index ) {
138
+ resultState = stateSnapshot . state
135
139
} else {
136
- const { state } = parse ( snapshot . state , true )
140
+ const state = parse ( stateSnapshot . state , true )
137
141
store . replaceState ( state )
138
142
139
143
SharedData . snapshotLoading = true
140
144
141
145
// Replay mutations
142
- for ( let i = snapshot . index + 1 ; i <= index ; i ++ ) {
146
+ for ( let i = stateSnapshot . index + 1 ; i <= index ; i ++ ) {
143
147
const mutation = mutations [ i ]
144
148
if ( mutation . handlers ) {
145
149
if ( Array . isArray ( mutation . handlers ) ) {
@@ -155,12 +159,12 @@ export function initVuexBackend (hook, bridge, isLegacy) {
155
159
}
156
160
157
161
if ( i !== index && i % SharedData . cacheVuexSnapshotsEvery === 0 ) {
158
- takeSnapshot ( i , state )
162
+ takeStateSnapshot ( i , state )
159
163
}
160
164
}
161
165
162
166
// Send final state after replay
163
- resultState = getSnapshot ( )
167
+ resultState = getStateSnapshot ( )
164
168
}
165
169
166
170
lastState = resultState
@@ -185,15 +189,34 @@ export function initVuexBackend (hook, bridge, isLegacy) {
185
189
} )
186
190
} )
187
191
188
- function takeSnapshot ( index ) {
189
- snapshots . push ( {
192
+ function takeStateSnapshot ( index ) {
193
+ stateSnapshots . push ( {
190
194
index,
191
- state : getSnapshot ( )
195
+ state : getStateSnapshot ( )
192
196
} )
193
197
// 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 ) )
196
207
}
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
197
220
}
198
221
}
199
222
0 commit comments