@@ -37,6 +37,8 @@ class VuexBackend {
37
37
this . registeredModules = { }
38
38
/** All dynamic modules ever registered, useful for mutation replaying */
39
39
this . allTimeModules = { }
40
+ /** Legacy base state */
41
+ this . legacyBaseSnapshot = null
40
42
41
43
// First-time snapshot VM creation
42
44
this . resetSnapshotsVm ( )
@@ -124,7 +126,11 @@ class VuexBackend {
124
126
* ⚠️ State should be time-traveled to before executing this
125
127
*/
126
128
onCommit ( index ) {
127
- this . baseStateSnapshot = this . lastState
129
+ if ( SharedData . vuexNewBackend ) {
130
+ this . baseStateSnapshot = this . lastState
131
+ } else {
132
+ this . legacyBaseSnapshot = this . mutations [ index ] . snapshot
133
+ }
128
134
this . resetSnapshotCache ( )
129
135
this . mutations = this . mutations . slice ( index + 1 )
130
136
this . mutations . forEach ( ( mutation , index ) => {
@@ -149,8 +155,8 @@ class VuexBackend {
149
155
onImportState ( state ) {
150
156
const parsed = parse ( state , true )
151
157
this . initialState = parsed
152
- this . reset ( )
153
158
this . hook . emit ( 'vuex:travel-to-state' , parsed )
159
+ this . reset ( )
154
160
this . bridge . send ( 'vuex:init' )
155
161
this . onInspectState ( - 1 )
156
162
}
@@ -199,7 +205,11 @@ class VuexBackend {
199
205
* Reset vuex backend
200
206
*/
201
207
reset ( stateSnapshot = null ) {
202
- this . baseStateSnapshot = stateSnapshot || clone ( this . initialState )
208
+ if ( SharedData . vuexNewBackend ) {
209
+ this . baseStateSnapshot = stateSnapshot || clone ( this . initialState )
210
+ } else {
211
+ this . legacyBaseSnapshot = this . stringifyStore ( )
212
+ }
203
213
this . mutations = [ ]
204
214
this . resetSnapshotCache ( )
205
215
}
@@ -235,21 +245,23 @@ class VuexBackend {
235
245
...module
236
246
}
237
247
238
- // Ensure all children state are cloned
239
- const replaceNestedStates = ( nestedModule ) => {
240
- if ( nestedModule . modules ) {
241
- Object . keys ( nestedModule . modules ) . forEach ( key => {
242
- const child = nestedModule . modules [ key ]
243
- let state = { }
244
- if ( child . state ) {
245
- state = typeof child . state === 'function' ? child . state ( ) : child . state
246
- }
247
- child . state = clone ( state )
248
- replaceNestedStates ( child )
249
- } )
248
+ if ( SharedData . vuexNewBackend ) {
249
+ // Ensure all children state are cloned
250
+ const replaceNestedStates = ( nestedModule ) => {
251
+ if ( nestedModule . modules ) {
252
+ Object . keys ( nestedModule . modules ) . forEach ( key => {
253
+ const child = nestedModule . modules [ key ]
254
+ let state = { }
255
+ if ( child . state ) {
256
+ state = typeof child . state === 'function' ? child . state ( ) : child . state
257
+ }
258
+ child . state = clone ( state )
259
+ replaceNestedStates ( child )
260
+ } )
261
+ }
250
262
}
263
+ replaceNestedStates ( fakeModule )
251
264
}
252
- replaceNestedStates ( fakeModule )
253
265
254
266
const key = path . join ( '/' )
255
267
const moduleInfo = this . registeredModules [ key ] = this . allTimeModules [ key ] = {
@@ -259,7 +271,7 @@ class VuexBackend {
259
271
...options ,
260
272
preserveState : false
261
273
} ,
262
- state : clone ( state )
274
+ state : SharedData . vuexNewBackend ? clone ( state ) : null
263
275
}
264
276
265
277
if ( SharedData . recordVuex ) {
@@ -328,15 +340,26 @@ class VuexBackend {
328
340
return ! ! this . store . _modules . get ( path )
329
341
}
330
342
343
+ stringifyStore ( ) {
344
+ return stringify ( {
345
+ state : this . store . state ,
346
+ getters : this . store . getters || { }
347
+ } )
348
+ }
349
+
331
350
/**
332
351
* Handle a new mutation commited to the store
333
352
*/
334
353
addMutation ( type , payload , options = { } ) {
335
354
const index = this . mutations . length
336
355
356
+ if ( ! SharedData . vuexNewBackend ) {
357
+ options . snapshot = this . stringifyStore ( )
358
+ }
359
+
337
360
this . mutations . push ( {
338
361
type,
339
- payload : clone ( payload ) ,
362
+ payload : SharedData . vuexNewBackend ? clone ( payload ) : null ,
340
363
index,
341
364
handlers : this . store . _mutations [ type ] ,
342
365
registeredModules : Object . keys ( this . registeredModules ) ,
@@ -359,6 +382,12 @@ class VuexBackend {
359
382
* to re-create what the vuex state should be at this point
360
383
*/
361
384
replayMutations ( index ) {
385
+ if ( ! SharedData . vuexNewBackend ) {
386
+ const snapshot = index === - 1 ? this . legacyBaseSnapshot : this . mutations [ index ] . snapshot
387
+ this . lastState = parse ( snapshot , true ) . state
388
+ return snapshot
389
+ }
390
+
362
391
const originalVm = this . store . _vm
363
392
const originalState = clone ( this . store . state )
364
393
this . store . _vm = this . snapshotsVm
@@ -481,10 +510,7 @@ class VuexBackend {
481
510
482
511
this . lastState = resultState
483
512
484
- const result = stringify ( {
485
- state : this . store . state ,
486
- getters : this . store . getters || { }
487
- } )
513
+ const result = this . stringifyStore ( )
488
514
489
515
// Restore user state
490
516
tempAddedModules . sort ( ( a , b ) => b . length - a . length ) . forEach ( m => {
0 commit comments