Skip to content

Commit 7b87997

Browse files
author
Guillaume Chau
committed
fix(vuex): nested dynamic module support, closes #919
1 parent 39b21be commit 7b87997

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

shells/dev/target/Counter.vue

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@
5858
>
5959
Remove dynamic module
6060
</button>
61+
<button
62+
:disabled="!$store.state.dynamic || $store.state.dynamic.nested"
63+
@click="addDynamicNestedModule()"
64+
>
65+
Add dynamic nested module
66+
</button>
67+
<button
68+
:disabled="!$store.state.dynamic || !$store.state.dynamic.nested"
69+
@click="toggleDynamicNested()"
70+
>
71+
Toggle dynamic nested state
72+
</button>
73+
<button
74+
:disabled="!$store.state.dynamic || !$store.state.dynamic.nested"
75+
@click="removeDynamicNestedModule()"
76+
>
77+
Remove dynamic nested module
78+
</button>
6179
</div>
6280

6381
<pre>{{ $store.state.instant }}</pre>
@@ -68,7 +86,7 @@
6886

6987
<script>
7088
import { mapState, mapGetters, mapMutations } from 'vuex'
71-
import DynamicModule from './dynamic-module'
89+
import { dynamic, nested } from './dynamic-module'
7290
import NoProp from './NoProp.vue'
7391
7492
export default {
@@ -137,7 +155,7 @@ export default {
137155
}),
138156
139157
addDynamicModule () {
140-
this.$store.registerModule('dynamic', DynamicModule)
158+
this.$store.registerModule('dynamic', dynamic)
141159
},
142160
143161
removeDynamicModule () {
@@ -146,6 +164,18 @@ export default {
146164
147165
toggleDynamic () {
148166
this.$store.commit('dynamic/TOGGLE')
167+
},
168+
169+
addDynamicNestedModule () {
170+
this.$store.registerModule(['dynamic', 'nested'], nested)
171+
},
172+
173+
removeDynamicNestedModule () {
174+
this.$store.unregisterModule(['dynamic', 'nested'])
175+
},
176+
177+
toggleDynamicNested () {
178+
this.$store.commit('dynamic/TOGGLE')
149179
}
150180
}
151181
}

shells/dev/target/dynamic-module.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
export const dynamic = {
22
namespaced: true,
33
state () {
44
return {
@@ -7,7 +7,6 @@ export default {
77
},
88
getters: {
99
notDynamic: state => {
10-
console.log('notDynamic', JSON.stringify(state, null, 2))
1110
if (state) return !state.dynamic
1211
}
1312
},
@@ -17,3 +16,22 @@ export default {
1716
}
1817
}
1918
}
19+
20+
export const nested = {
21+
namespaced: true,
22+
state () {
23+
return {
24+
nested: true
25+
}
26+
},
27+
getters: {
28+
notNested: state => {
29+
if (state) return !state.nested
30+
}
31+
},
32+
mutations: {
33+
TOGGLE_NESTED: state => {
34+
state.nested = !state.nested
35+
}
36+
}
37+
}

src/backend/vuex.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
236236
} else {
237237
tempRemovedModules = Object.keys(registeredModules)
238238
}
239-
tempRemovedModules.forEach(m => {
239+
tempRemovedModules.sort((a, b) => b.length - a.length).forEach(m => {
240240
origUnregisterModule(m.split('/'))
241241
})
242242

@@ -254,7 +254,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
254254
tempAddedModules.push(path.join('/'))
255255
origRegisterModule(path, module, options)
256256
updateSnapshotsVm(store.state)
257-
} else if (mutation.unregisterModule && get(store.state, mutation.payload.path.join('.')) != null) {
257+
} else if (mutation.unregisterModule && get(store.state, mutation.payload.path) != null) {
258258
const path = mutation.payload.path
259259
const index = tempAddedModules.indexOf(path.join('/'))
260260
if (index !== -1) tempAddedModules.splice(index, 1)
@@ -292,10 +292,10 @@ export function initVuexBackend (hook, bridge, isLegacy) {
292292
})
293293

294294
// Restore user state
295-
tempAddedModules.forEach(m => {
295+
tempAddedModules.sort((a, b) => b.length - a.length).forEach(m => {
296296
origUnregisterModule(m.split('/'))
297297
})
298-
tempRemovedModules.forEach(m => {
298+
tempRemovedModules.sort((a, b) => a.length - b.length).forEach(m => {
299299
const { path, module, options } = registeredModules[m]
300300
origRegisterModule(path, module, options)
301301
})
@@ -353,7 +353,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
353353
function ensureRegisteredModules (mutation) {
354354
if (mutation) {
355355
mutation.registeredModules.forEach(m => {
356-
if (!Object.keys(registeredModules).includes(m)) {
356+
if (!Object.keys(registeredModules).sort((a, b) => a.length - b.length).includes(m)) {
357357
const data = allTimeModules[m]
358358
if (data) {
359359
const { path, module, options } = data
@@ -362,7 +362,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
362362
}
363363
}
364364
})
365-
Object.keys(registeredModules).forEach(m => {
365+
Object.keys(registeredModules).sort((a, b) => b.length - a.length).forEach(m => {
366366
if (!mutation.registeredModules.includes(m)) {
367367
origUnregisterModule(m.split('/'))
368368
delete registeredModules[m]

src/util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ export function sortByKey (state) {
516516
}
517517

518518
export function set (object, path, value, cb = null) {
519-
const sections = path.split('.')
519+
const sections = Array.isArray(path) ? path : path.split('.')
520520
while (sections.length > 1) {
521521
object = object[sections.shift()]
522522
}
@@ -529,7 +529,7 @@ export function set (object, path, value, cb = null) {
529529
}
530530

531531
export function get (object, path) {
532-
const sections = path.split('.')
532+
const sections = Array.isArray(path) ? path : path.split('.')
533533
for (let i = 0; i < sections.length; i++) {
534534
object = object[sections[i]]
535535
if (!object) {
@@ -544,7 +544,7 @@ export function has (object, path, parent = false) {
544544
return false
545545
}
546546

547-
const sections = path.split('.')
547+
const sections = Array.isArray(path) ? path : path.split('.')
548548
const size = !parent ? 1 : 2
549549
while (sections.length > size) {
550550
object = object[sections.shift()]

0 commit comments

Comments
 (0)