Skip to content

Commit 0724381

Browse files
author
Guillaume Chau
committed
refactor(vuex): legacy backend mode, fix #967, fix #930, fix #971, fix #872, fix #929
1 parent fe067b6 commit 0724381

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

src/backend/vuex.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class VuexBackend {
3737
this.registeredModules = {}
3838
/** All dynamic modules ever registered, useful for mutation replaying */
3939
this.allTimeModules = {}
40+
/** Legacy base state */
41+
this.legacyBaseSnapshot = null
4042

4143
// First-time snapshot VM creation
4244
this.resetSnapshotsVm()
@@ -124,7 +126,11 @@ class VuexBackend {
124126
* ⚠️ State should be time-traveled to before executing this
125127
*/
126128
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+
}
128134
this.resetSnapshotCache()
129135
this.mutations = this.mutations.slice(index + 1)
130136
this.mutations.forEach((mutation, index) => {
@@ -149,8 +155,8 @@ class VuexBackend {
149155
onImportState (state) {
150156
const parsed = parse(state, true)
151157
this.initialState = parsed
152-
this.reset()
153158
this.hook.emit('vuex:travel-to-state', parsed)
159+
this.reset()
154160
this.bridge.send('vuex:init')
155161
this.onInspectState(-1)
156162
}
@@ -199,7 +205,11 @@ class VuexBackend {
199205
* Reset vuex backend
200206
*/
201207
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+
}
203213
this.mutations = []
204214
this.resetSnapshotCache()
205215
}
@@ -235,21 +245,23 @@ class VuexBackend {
235245
...module
236246
}
237247

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+
}
250262
}
263+
replaceNestedStates(fakeModule)
251264
}
252-
replaceNestedStates(fakeModule)
253265

254266
const key = path.join('/')
255267
const moduleInfo = this.registeredModules[key] = this.allTimeModules[key] = {
@@ -259,7 +271,7 @@ class VuexBackend {
259271
...options,
260272
preserveState: false
261273
},
262-
state: clone(state)
274+
state: SharedData.vuexNewBackend ? clone(state) : null
263275
}
264276

265277
if (SharedData.recordVuex) {
@@ -328,15 +340,26 @@ class VuexBackend {
328340
return !!this.store._modules.get(path)
329341
}
330342

343+
stringifyStore () {
344+
return stringify({
345+
state: this.store.state,
346+
getters: this.store.getters || {}
347+
})
348+
}
349+
331350
/**
332351
* Handle a new mutation commited to the store
333352
*/
334353
addMutation (type, payload, options = {}) {
335354
const index = this.mutations.length
336355

356+
if (!SharedData.vuexNewBackend) {
357+
options.snapshot = this.stringifyStore()
358+
}
359+
337360
this.mutations.push({
338361
type,
339-
payload: clone(payload),
362+
payload: SharedData.vuexNewBackend ? clone(payload) : null,
340363
index,
341364
handlers: this.store._mutations[type],
342365
registeredModules: Object.keys(this.registeredModules),
@@ -359,6 +382,12 @@ class VuexBackend {
359382
* to re-create what the vuex state should be at this point
360383
*/
361384
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+
362391
const originalVm = this.store._vm
363392
const originalState = clone(this.store.state)
364393
this.store._vm = this.snapshotsVm
@@ -481,10 +510,7 @@ class VuexBackend {
481510

482511
this.lastState = resultState
483512

484-
const result = stringify({
485-
state: this.store.state,
486-
getters: this.store.getters || {}
487-
})
513+
const result = this.stringifyStore()
488514

489515
// Restore user state
490516
tempAddedModules.sort((a, b) => b.length - a.length).forEach(m => {

src/devtools/views/settings/GlobalPreferences.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@
7777
</VueSwitch>
7878
</VueFormField>
7979

80+
<VueFormField
81+
title="New Vuex backend"
82+
>
83+
<VueSwitch v-model="$shared.vuexNewBackend">
84+
Enable
85+
</VueSwitch>
86+
<template #subtitle>
87+
Faster and less memory-intensive
88+
</template>
89+
</VueFormField>
90+
8091
<VueFormField
8192
title="Autoload Vuex state"
8293
>

src/shared-data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const internalSharedData = {
1313
recordPerf: false,
1414
editableProps: false,
1515
logDetected: true,
16+
vuexNewBackend: false,
1617
vuexAutoload: false
1718
}
1819

@@ -23,6 +24,7 @@ const persisted = [
2324
'recordVuex',
2425
'editableProps',
2526
'logDetected',
27+
'vuexNewBackend',
2628
'vuexAutoload'
2729
]
2830

0 commit comments

Comments
 (0)