Skip to content

Commit

Permalink
use namespaced vuex modules
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 3, 2017
1 parent a195835 commit 0e11035
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/devtools/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export default {
events: EventsTab
},
computed: mapState({
message: state => state.app.message,
tab: state => state.app.tab,
message: state => state.message,
tab: state => state.tab,
newEventCount: state => state.events.newEventCount
}),
methods: {
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function initApp (shell) {
})

bridge.on('flush', payload => {
store.commit('FLUSH', parse(payload))
store.commit('components/FLUSH', parse(payload))
})

bridge.on('instance-details', details => {
store.commit('RECEIVE_INSTANCE_DETAILS', parse(details))
store.commit('components/RECEIVE_INSTANCE_DETAILS', parse(details))
})

bridge.on('vuex:init', state => {
Expand Down
21 changes: 0 additions & 21 deletions src/devtools/store/app-module.js

This file was deleted.

19 changes: 15 additions & 4 deletions src/devtools/store/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import Vue from 'vue'
import Vuex from 'vuex'
import app from './app-module'
import components from 'views/components/module'
import vuex from 'views/vuex/module'
import events from 'views/events/module'

Vue.use(Vuex)

const store = new Vuex.Store({
state: {
message: '',
tab: 'components'
},
mutations: {
SHOW_MESSAGE (state, message) {
state.message = message
},
SWITCH_TAB (state, tab) {
state.tab = tab
},
RECEIVE_INSTANCE_DETAILS (state, instance) {
state.message = 'Instance selected: ' + instance.name
}
},
modules: {
app,
components,
vuex,
events
Expand All @@ -20,15 +33,13 @@ export default store

if (module.hot) {
module.hot.accept([
'./app-module',
'views/components/module',
'views/vuex/module',
'views/events/module'
], () => {
try {
store.hotUpdate({
modules: {
app: require('./app-module').default,
components: require('views/components/module').default,
vuex: require('views/vuex/module').default,
events: require('views/events/module').default
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/views/components/ComponentInstance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default {
this.toggleWithValue(false)
},
toggleWithValue (val) {
this.$store.commit('TOGGLE_INSTANCE', {
this.$store.commit('components/TOGGLE_INSTANCE', {
id: this.instance.id,
expanded: val
})
Expand Down
6 changes: 3 additions & 3 deletions src/devtools/views/components/ComponentsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default {
ComponentInspector,
SplitPane
},
computed: mapState({
instances: state => state.components.instances,
inspectedInstance: state => state.components.inspectedInstance
computed: mapState('components', {
instances: state => state.instances,
inspectedInstance: state => state.inspectedInstance
}),
methods: {
filter (e) {
Expand Down
1 change: 1 addition & 0 deletions src/devtools/views/components/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const mutations = {
}

export default {
namespaced: true,
state,
mutations
}
2 changes: 1 addition & 1 deletion src/devtools/views/events/EventInspector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
}
},
computed: {
...mapGetters([
...mapGetters('events', [
'activeEvent'
]),
sortedEventData () {
Expand Down
6 changes: 3 additions & 3 deletions src/devtools/views/events/EventsHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export default {
ActionHeader
},
computed: {
...mapState({
events: state => state.events.filteredEvents,
activeEventIndex: state => state.events.activeFilteredEventIndex
...mapState('events', {
events: state => state.filteredEvents,
activeEventIndex: state => state.activeFilteredEventIndex
})
},
filters: {
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/views/events/EventsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import EventInspector from './EventInspector.vue'
import { mapGetters } from 'vuex'
export default {
computed: mapGetters([
computed: mapGetters('events', [
'hasEvents'
]),
components: {
Expand Down
13 changes: 7 additions & 6 deletions src/devtools/views/events/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ const state = {
}

const mutations = {
'events/EMIT' (state, payload) {
'EMIT' (state, payload) {
if (state.events.length === state.filteredEvents.length) {
state.filteredEvents.push(payload)
}
state.events.push(payload)
state.activeFilteredEventIndex = state.filteredEvents.length - 1
},
'events/RESET' (state) {
'RESET' (state) {
state.events = []
state.filteredEvents = []
},
'events/STEP' (state, n) {
'STEP' (state, n) {
state.activeFilteredEventIndex = n
},
'events/RESET_NEW_EVENT_COUNT' (state) {
'RESET_NEW_EVENT_COUNT' (state) {
state.newEventCount = 0
},
'events/INCREASE_NEW_EVENT_COUNT' (state) {
'INCREASE_NEW_EVENT_COUNT' (state) {
state.newEventCount++
},
'events/FILTER_EVENTS' (state, filter) {
'FILTER_EVENTS' (state, filter) {
state.filteredEvents = state.events.filter(event => {
return event.eventName.toLowerCase().includes(filter) || event.instanceName.toLowerCase().includes(filter)
})
Expand All @@ -40,6 +40,7 @@ const getters = {
}

export default {
namespaced: true,
state,
mutations,
getters
Expand Down
10 changes: 5 additions & 5 deletions src/devtools/views/vuex/VuexHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export default {
}
},
computed: {
...mapState({
history: state => state.vuex.history,
lastCommit: state => state.vuex.lastCommit,
activeIndex: state => state.vuex.activeIndex
...mapState('vuex', {
history: state => state.history,
lastCommit: state => state.lastCommit,
activeIndex: state => state.activeIndex
}),
compiledFilter () {
const regexParts = this.userInputFilter.match(REGEX_RE)
Expand Down Expand Up @@ -106,7 +106,7 @@ export default {
}
},
methods: {
...mapActions([
...mapActions('vuex', [
'commitAll',
'revertAll',
'commitSelected',
Expand Down
8 changes: 4 additions & 4 deletions src/devtools/views/vuex/VuexStateInspector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export default {
showImportStatePopup: false
}
},
computed: mapGetters({
activeState: 'vuexActiveState'
}),
computed: mapGetters('vuex', [
'activeState'
]),
watch: {
showImportStatePopup (val) {
if (val) {
Expand Down Expand Up @@ -98,7 +98,7 @@ export default {
} else {
try {
parse(importedStr) // Try to parse
this.$store.dispatch('importState', importedStr)
this.$store.dispatch('vuex/importState', importedStr)
this.showBadJSONMessage = false
} catch (e) {
this.showBadJSONMessage = true
Expand Down
4 changes: 2 additions & 2 deletions src/devtools/views/vuex/VuexTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import VuexStateInspector from './VuexStateInspector.vue'
import { mapState } from 'vuex'
export default {
computed: mapState({
hasVuex: state => state.vuex.hasVuex
computed: mapState('vuex', {
hasVuex: state => state.hasVuex
}),
components: {
SplitPane,
Expand Down
14 changes: 7 additions & 7 deletions src/devtools/views/vuex/actions.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
export function commitAll ({ commit, state }) {
if (state.history.length > 0) {
commit('vuex/COMMIT_ALL')
commit('COMMIT_ALL')
travelTo(state)
}
}

export function revertAll ({ commit, state }) {
if (state.history.length > 0) {
commit('vuex/REVERT_ALL')
commit('REVERT_ALL')
travelTo(state)
}
}

export function commitSelected ({ commit, state }) {
commit('vuex/COMMIT_SELECTED')
commit('COMMIT_SELECTED')
travelTo(state)
}

export function revertSelected ({ commit, state }) {
commit('vuex/REVERT_SELECTED')
commit('REVERT_SELECTED')
travelTo(state)
}

export function reset ({ commit, state }) {
commit('vuex/RESET')
commit('RESET')
travelTo(state)
}

export function step ({ commit, state }, index) {
commit('vuex/STEP', index)
commit('STEP', index)
travelTo(state)
}

export function importState (store, importedState) {
store.commit('vuex/INIT', importedState)
store.commit('INIT', importedState)
store.dispatch('reset')
}

Expand Down
19 changes: 10 additions & 9 deletions src/devtools/views/vuex/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ const state = {
}

const mutations = {
'vuex/INIT' (state, initialState) {
'INIT' (state, initialState) {
state.initial = state.base = initialState
state.hasVuex = true
reset(state)
},
'vuex/RECEIVE_MUTATION' (state, entry) {
'RECEIVE_MUTATION' (state, entry) {
state.history.push(entry)
state.activeIndex = state.history.length - 1
},
'vuex/COMMIT_ALL' (state) {
'COMMIT_ALL' (state) {
state.base = state.history[state.history.length - 1].state
state.lastCommit = Date.now()
reset(state)
},
'vuex/REVERT_ALL' (state) {
'REVERT_ALL' (state) {
reset(state)
},
'vuex/COMMIT_SELECTED' (state) {
'COMMIT_SELECTED' (state) {
state.base = state.history[state.activeIndex].state
state.lastCommit = Date.now()
state.history = state.history.slice(state.activeIndex + 1)
state.activeIndex = -1
},
'vuex/REVERT_SELECTED' (state) {
'REVERT_SELECTED' (state) {
state.history = state.history.slice(0, state.activeIndex)
state.activeIndex--
},
'vuex/RESET' (state) {
'RESET' (state) {
state.base = state.initial
state.lastCommit = state.initialCommit
reset(state)
},
'vuex/STEP' (state, n) {
'STEP' (state, n) {
state.activeIndex = n
}
}
Expand All @@ -55,7 +55,7 @@ function reset (state) {
}

const getters = {
vuexActiveState ({ base, history, activeIndex }) {
activeState ({ base, history, activeIndex }) {
const entry = history[activeIndex]
const res = {}
if (entry) {
Expand All @@ -70,6 +70,7 @@ const getters = {
}

export default {
namespaced: true,
state,
mutations,
actions,
Expand Down

0 comments on commit 0e11035

Please sign in to comment.