Skip to content

Commit c09fdfb

Browse files
author
Guillaume Chau
committed
fix: commit/revert
1 parent 53753b1 commit c09fdfb

File tree

6 files changed

+97
-33
lines changed

6 files changed

+97
-33
lines changed

src/backend/vuex.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ export function initVuexBackend (hook, bridge) {
99
getters: store.getters || {}
1010
})
1111

12-
let mutationIndex = 0
13-
const baseSnapshot = getSnapshot()
14-
const snapshots = [
15-
{ index: -1, state: baseSnapshot }
16-
]
17-
const mutations = []
12+
let baseSnapshot, snapshots, mutations, lastState
13+
14+
function reset () {
15+
baseSnapshot = getSnapshot()
16+
mutations = []
17+
resetSnapshotCache()
18+
}
19+
20+
function resetSnapshotCache () {
21+
snapshots = [
22+
{ index: -1, state: baseSnapshot }
23+
]
24+
}
25+
26+
reset()
1827

1928
bridge.send('vuex:init', baseSnapshot)
2029

@@ -25,7 +34,7 @@ export function initVuexBackend (hook, bridge) {
2534
hook.on('vuex:mutation', ({ type, payload }) => {
2635
if (!SharedData.recordVuex) return
2736

28-
const index = mutationIndex++
37+
const index = mutations.length
2938

3039
mutations.push({
3140
type,
@@ -45,14 +54,38 @@ export function initVuexBackend (hook, bridge) {
4554
})
4655

4756
// devtool -> application
48-
bridge.on('vuex:travel-to-state', index => {
57+
bridge.on('vuex:travel-to-state', ({ index, apply }) => {
4958
const snapshot = replayMutations(index)
5059
const { state } = parse(snapshot, true)
51-
hook.emit('vuex:travel-to-state', state)
5260
bridge.send('vuex:inspected-state', {
5361
index,
5462
snapshot
5563
})
64+
if (apply) {
65+
hook.emit('vuex:travel-to-state', state)
66+
}
67+
})
68+
69+
bridge.on('vuex:commit-all', () => {
70+
reset()
71+
})
72+
73+
bridge.on('vuex:revert-all', () => {
74+
reset()
75+
})
76+
77+
bridge.on('vuex:commit', index => {
78+
baseSnapshot = lastState
79+
resetSnapshotCache()
80+
mutations = mutations.slice(index + 1)
81+
mutations.forEach((mutation, index) => {
82+
mutation.index = index
83+
})
84+
})
85+
86+
bridge.on('vuex:revert', index => {
87+
resetSnapshotCache()
88+
mutations = mutations.slice(0, index)
5689
})
5790

5891
bridge.on('vuex:import-state', state => {
@@ -118,6 +151,8 @@ export function initVuexBackend (hook, bridge) {
118151
// Send final state after replay
119152
const resultState = getSnapshot()
120153

154+
lastState = resultState
155+
121156
// Restore user state
122157
store.replaceState(currentState)
123158

src/devtools/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isChrome, initEnv } from './env'
88
import SharedData, { init as initSharedData, destroy as destroySharedData } from 'src/shared-data'
99
import storage from './storage'
1010
import { snapshotsCache } from './views/vuex/cache'
11+
import VuexResolve from './views/vuex/resolve'
1112

1213
// UI
1314

@@ -140,6 +141,11 @@ function initApp (shell) {
140141
bridge.on('vuex:inspected-state', ({ index, snapshot }) => {
141142
snapshotsCache.set(index, snapshot)
142143
store.commit('vuex/UPDATE_INSPECTED_STATE', snapshot)
144+
145+
if (VuexResolve.travel) {
146+
VuexResolve.travel(snapshot)
147+
}
148+
143149
requestAnimationFrame(() => {
144150
SharedData.snapshotLoading = null
145151
})

src/devtools/views/vuex/VuexHistory.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
>
5959
<template slot-scope="{ item: entry, index }">
6060
<div
61-
v-if="index === 0"
61+
v-if="index <= 0"
6262
:class="{ active: activeIndex === -1, inspected: inspectedIndex === -1 }"
6363
class="entry list-item"
6464
@click="inspect(null)"

src/devtools/views/vuex/actions.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
import { snapshotsCache } from './cache'
2+
import Resolve from './resolve'
23
import SharedData from 'src/shared-data'
34

45
export function commitAll ({ commit, state }) {
56
if (state.history.length > 0) {
6-
commit('COMMIT_ALL')
7-
travelTo(state, commit, -1)
7+
travelTo(state, commit, state.history.length - 1).then(() => {
8+
snapshotsCache.reset()
9+
bridge.send('vuex:commit-all')
10+
commit('COMMIT_ALL')
11+
})
812
}
913
}
1014

1115
export function revertAll ({ commit, state }) {
1216
if (state.history.length > 0) {
13-
commit('REVERT_ALL')
14-
travelTo(state, commit, -1)
17+
travelTo(state, commit, -1).then(() => {
18+
snapshotsCache.reset()
19+
bridge.send('vuex:revert-all')
20+
commit('REVERT_ALL')
21+
})
1522
}
1623
}
1724

1825
export function commit ({ commit, state }, entry) {
1926
const index = state.history.indexOf(entry)
2027
if (index > -1) {
21-
commit('COMMIT', index)
22-
travelTo(state, commit, -1)
28+
travelTo(state, commit, index, false).then(() => {
29+
snapshotsCache.reset()
30+
bridge.send('vuex:commit', index)
31+
commit('COMMIT', index)
32+
travelTo(state, commit, state.history.length - 1)
33+
})
2334
}
2435
}
2536

2637
export function revert ({ commit, state }, entry) {
2738
const index = state.history.indexOf(entry)
2839
if (index > -1) {
29-
commit('REVERT', index)
30-
travelTo(state, commit, state.history.length - 1)
40+
travelTo(state, commit, index - 1).then(() => {
41+
snapshotsCache.reset()
42+
bridge.send('vuex:revert', index)
43+
commit('REVERT', index)
44+
})
3145
}
3246
}
3347

@@ -55,25 +69,28 @@ export function inspect ({ commit, getters }, entryOrIndex) {
5569
}
5670

5771
export function timeTravelTo ({ state, commit }, entry) {
58-
travelTo(state, commit, state.history.indexOf(entry))
72+
return travelTo(state, commit, state.history.indexOf(entry))
5973
}
6074

6175
export function updateFilter ({ commit }, filter) {
6276
commit('UPDATE_FILTER', filter)
6377
}
6478

65-
function travelTo (state, commit, index) {
66-
const { inspectedIndex } = state
79+
function travelTo (state, commit, index, apply = true) {
80+
return new Promise((resolve) => {
81+
Resolve.travel = resolve
82+
const { inspectedIndex } = state
6783

68-
commit('UPDATE_INSPECTED_STATE', null)
69-
SharedData.snapshotLoading = {
70-
current: 0,
71-
total: 1
72-
}
73-
bridge.send('vuex:travel-to-state', index)
84+
commit('UPDATE_INSPECTED_STATE', null)
85+
SharedData.snapshotLoading = {
86+
current: 0,
87+
total: 1
88+
}
89+
bridge.send('vuex:travel-to-state', { index, apply })
7490

75-
if (index !== inspectedIndex) {
76-
commit('INSPECT', index)
77-
}
78-
commit('TIME_TRAVEL', index)
91+
if (index !== inspectedIndex) {
92+
commit('INSPECT', index)
93+
}
94+
commit('TIME_TRAVEL', index)
95+
})
7996
}

src/devtools/views/vuex/module.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const mutations = {
3535
},
3636

3737
'COMMIT_ALL' (state) {
38-
state.base = state.history[state.history.length - 1].snapshot
38+
state.base = state.inspectedState
3939
state.lastCommit = Date.now()
4040
reset(state)
4141
},
@@ -45,9 +45,12 @@ const mutations = {
4545
},
4646

4747
'COMMIT' (state, index) {
48-
state.base = state.history[index].snapshot
48+
state.base = state.inspectedState
4949
state.lastCommit = Date.now()
5050
state.history = state.history.slice(index + 1)
51+
state.history.forEach(({ mutation }, index) => {
52+
mutation.index = index
53+
})
5154
state.inspectedIndex = -1
5255
},
5356

src/devtools/views/vuex/resolve.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
travel: null
3+
}

0 commit comments

Comments
 (0)