Skip to content

Commit 30e7641

Browse files
committed
move vuex history logic into module
1 parent 5a1c39f commit 30e7641

File tree

3 files changed

+64
-51
lines changed

3 files changed

+64
-51
lines changed

src/devtools/views/vuex/VuexHistory.vue

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<action-header slot="header">
44
<div class="search">
55
<i class="material-icons">search</i>
6-
<input :class="{ invalid: invalidRegex }" placeholder="Filter mutations" v-model="userInputFilter">
6+
<input :class="{ invalid: filterRegexInvalid }" placeholder="Filter mutations" v-model="filter">
77
</div>
88
<a class="button commit-all" :class="{ disabled: !history.length }" @click="commitAll" title="Commit All">
99
<i class="material-icons">get_app</i>
@@ -68,57 +68,35 @@ import ScrollPane from 'components/ScrollPane.vue'
6868
import ActionHeader from 'components/ActionHeader.vue'
6969
7070
import keyNavMixin from '../../mixins/key-nav'
71-
import { mapState, mapActions } from 'vuex'
72-
73-
const REGEX_RE = /^\/(.*?)\/(\w*)/
71+
import { mapState, mapGetters, mapActions } from 'vuex'
7472
7573
export default {
7674
mixins: [keyNavMixin],
7775
components: {
7876
ActionHeader,
7977
ScrollPane
8078
},
81-
data () {
82-
return {
83-
userInputFilter: '',
84-
invalidRegex: false
85-
}
86-
},
8779
computed: {
80+
filter: {
81+
get () {
82+
return this.$store.state.vuex.filter
83+
},
84+
set (filter) {
85+
this.$store.dispatch('vuex/updateFilter', filter)
86+
}
87+
},
8888
...mapState('vuex', [
8989
'enabled',
9090
'history',
9191
'lastCommit',
9292
'inspectedIndex',
93-
'activeIndex'
93+
'activeIndex',
94+
'filterRegex',
95+
'filterRegexInvalid'
9496
]),
95-
compiledFilter () {
96-
const regexParts = this.userInputFilter.match(REGEX_RE)
97-
if (regexParts !== null) {
98-
// looks like it might be a regex -> try to compile it
99-
try {
100-
const re = new RegExp(regexParts[1], regexParts[2])
101-
this.invalidRegex = false
102-
return re
103-
} catch (e) {
104-
this.invalidRegex = true
105-
return new RegExp('.*', 'i')
106-
}
107-
}
108-
// simple case-insensitve search
109-
this.invalidRegex = false
110-
return new RegExp(this.escapeStringForRegExp(this.userInputFilter), 'i')
111-
},
112-
filteredHistory () {
113-
return this.history.filter((entry) => {
114-
return this.compiledFilter.test(entry.mutation.type)
115-
})
116-
}
117-
},
118-
filters: {
119-
formatTime (timestamp) {
120-
return (new Date(timestamp)).toString().match(/\d\d:\d\d:\d\d/)[0]
121-
}
97+
...mapGetters('vuex', [
98+
'filteredHistory'
99+
])
122100
},
123101
methods: {
124102
...mapActions('vuex', [
@@ -128,7 +106,8 @@ export default {
128106
'commitSelected',
129107
'revertSelected',
130108
'step',
131-
'timeTravelToSelected'
109+
'timeTravelToSelected',
110+
'updateFilter'
132111
]),
133112
isActive (entry) {
134113
return this.activeIndex === this.history.indexOf(entry)
@@ -142,9 +121,11 @@ export default {
142121
} else if (dir === 'down') {
143122
this.step(this.inspectedIndex + 1)
144123
}
145-
},
146-
escapeStringForRegExp (str) {
147-
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
124+
}
125+
},
126+
filters: {
127+
formatTime (timestamp) {
128+
return (new Date(timestamp)).toString().match(/\d\d:\d\d:\d\d/)[0]
148129
}
149130
}
150131
}

src/devtools/views/vuex/actions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export function importState ({ commit, dispatch }, importedState) {
4848
dispatch('reset')
4949
}
5050

51+
export function updateFilter ({ commit }, filter) {
52+
commit('UPDATE_FILTER', filter)
53+
}
54+
5155
function travelTo (state, commit) {
5256
const { history, inspectedIndex, base } = state
5357
const targetState = inspectedIndex > -1

src/devtools/views/vuex/module.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { parse } from 'src/util'
22
import * as actions from './actions'
33
import storage from 'storage'
44

5+
const REGEX_RE = /^\/(.*?)\/(\w*)/
6+
const ANY_RE = new RegExp('.*', 'i')
57
const ENABLED_KEY = 'VUEX_ENABLED'
68
const enabled = storage.get(ENABLED_KEY)
79

@@ -14,7 +16,10 @@ const state = {
1416
activeIndex: -1,
1517
history: [],
1618
initialCommit: Date.now(),
17-
lastCommit: Date.now()
19+
lastCommit: Date.now(),
20+
filter: '',
21+
filterRegex: ANY_RE,
22+
filterRegexInvalid: false
1823
}
1924

2025
const mutations = {
@@ -25,7 +30,9 @@ const mutations = {
2530
},
2631
'RECEIVE_MUTATION' (state, entry) {
2732
state.history.push(entry)
28-
state.inspectedIndex = state.activeIndex = state.history.length - 1
33+
if (!state.filter) {
34+
state.inspectedIndex = state.activeIndex = state.history.length - 1
35+
}
2936
},
3037
'COMMIT_ALL' (state) {
3138
state.base = state.history[state.history.length - 1].state
@@ -39,17 +46,12 @@ const mutations = {
3946
state.base = state.history[state.inspectedIndex].state
4047
state.lastCommit = Date.now()
4148
state.history = state.history.slice(state.inspectedIndex + 1)
42-
state.inspectedIndex = -1
49+
state.inspectedIndex = state.activeIndex = -1
4350
},
4451
'REVERT_SELECTED' (state) {
4552
state.history = state.history.slice(0, state.inspectedIndex)
4653
state.inspectedIndex--
4754
},
48-
'RESET' (state) {
49-
state.base = state.initial
50-
state.lastCommit = state.initialCommit
51-
reset(state)
52-
},
5355
'STEP' (state, index) {
5456
state.inspectedIndex = index
5557
},
@@ -58,12 +60,34 @@ const mutations = {
5860
},
5961
'TOGGLE' (state) {
6062
storage.set(ENABLED_KEY, state.enabled = !state.enabled)
63+
},
64+
'UPDATE_FILTER' (state, filter) {
65+
state.filter = filter
66+
const regexParts = filter.match(REGEX_RE)
67+
if (regexParts !== null) {
68+
// looks like it might be a regex -> try to compile it
69+
try {
70+
state.filterRegexInvalid = false
71+
state.filterRegex = new RegExp(regexParts[1], regexParts[2])
72+
} catch (e) {
73+
state.filterRegexInvalid = true
74+
state.filterRegex = ANY_RE
75+
}
76+
} else {
77+
// simple case-insensitve search
78+
state.filterRegexInvalid = false
79+
state.filterRegex = new RegExp(escapeStringForRegExp(filter), 'i')
80+
}
6181
}
6282
}
6383

6484
function reset (state) {
6585
state.history = []
66-
state.inspectedIndex = -1
86+
state.inspectedIndex = state.activeIndex = -1
87+
}
88+
89+
function escapeStringForRegExp (str) {
90+
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
6791
}
6892

6993
const getters = {
@@ -78,6 +102,10 @@ const getters = {
78102
}
79103
res.state = parse(entry ? entry.state : base)
80104
return res
105+
},
106+
107+
filteredHistory ({ history, filterRegex }) {
108+
return history.filter(entry => filterRegex.test(entry.mutation.type))
81109
}
82110
}
83111

0 commit comments

Comments
 (0)