Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid resetting store state when registering a dynamic module #2234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/store-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reactive, computed, watch, effectScope } from 'vue'
import { forEachValue, isObject, isPromise, assert, partial } from './util'
import { isObject, isPromise, assert, partial } from './util'

export function genericSubscribe (fn, subs, options) {
if (subs.indexOf(fn) < 0) {
Expand Down Expand Up @@ -35,36 +35,19 @@ export function resetStoreState (store, state, hot) {
store.getters = {}
// reset local getters cache
store._makeLocalGettersCache = Object.create(null)
const wrappedGetters = store._wrappedGetters
const computedObj = {}
const computedCache = {}

// create a new effect scope and create computed object inside it to avoid
// getters (computed) getting destroyed on component unmount.
const scope = effectScope(true)

scope.run(() => {
forEachValue(wrappedGetters, (fn, key) => {
// use computed to leverage its lazy-caching mechanism
// direct inline function use will lead to closure preserving oldState.
// using partial to return function with only arguments preserved in closure environment.
computedObj[key] = partial(fn, store)
computedCache[key] = computed(() => computedObj[key]())
Object.defineProperty(store.getters, key, {
get: () => computedCache[key].value,
enumerable: true // for local getters
})
})
})
// register the newly created effect scope to the store so that we can
// dispose the effects when this method runs again in the future.
store._scope = scope
registerGetters(store, Object.keys(store._wrappedGetters))

store._state = reactive({
data: state
})

// register the newly created effect scope to the store so that we can
// dispose the effects when this method runs again in the future.
store._scope = scope

// enable strict mode for new state
if (store.strict) {
enableStrictMode(store)
Expand All @@ -86,6 +69,26 @@ export function resetStoreState (store, state, hot) {
}
}

export function registerGetters (store, getterKeys) {
const computedObj = {}
const computedCache = {}

store._scope.run(() => {
getterKeys.forEach((key) => {
const fn = store._wrappedGetters[key]
// use computed to leverage its lazy-caching mechanism
// direct inline function use will lead to closure preserving oldState.
// using partial to return function with only arguments preserved in closure environment.
computedObj[key] = partial(fn, store)
computedCache[key] = computed(() => computedObj[key]())
Object.defineProperty(store.getters, key, {
get: () => computedCache[key].value,
enumerable: true // for local getters
})
})
})
}

export function installModule (store, rootState, path, module, hot) {
const isRoot = !path.length
const namespace = store._modules.getNamespace(path)
Expand Down
9 changes: 6 additions & 3 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
installModule,
resetStore,
resetStoreState,
unifyObjectStyle
unifyObjectStyle,
registerGetters
} from './store-util'

export function createStore (options) {
Expand Down Expand Up @@ -228,8 +229,10 @@ export class Store {

this._modules.register(path, rawModule)
installModule(this, this.state, path, this._modules.get(path), options.preserveState)
// reset store to update getters...
resetStoreState(this, this.state)

const namespace = this._modules.getNamespace(path)
const getterKeys = Object.keys(rawModule.getters || {}).map((key) => namespace + key)
registerGetters(this, getterKeys)
}

unregisterModule (path) {
Expand Down
29 changes: 28 additions & 1 deletion test/unit/modules.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, nextTick } from 'vue'
import { computed, h, nextTick } from 'vue'
import { mount } from 'test/helpers'
import Vuex from '@/index'

Expand Down Expand Up @@ -925,4 +925,31 @@ describe('Modules', () => {
/getters should be function but "getters\.test" in module "foo\.bar" is true/
)
})

it('module: computed getter should be reactive after module registration', () => {
const store = new Vuex.Store({
state: {
foo: 0
},
getters: {
getFoo: state => state.foo
},
mutations: {
incrementFoo: state => state.foo++
}
})

const computedFoo = computed(() => store.getters.getFoo)
store.commit('incrementFoo')
expect(computedFoo.value).toBe(1)

store.registerModule('bar', {
state: {
bar: 0
}
})

store.commit('incrementFoo')
expect(computedFoo.value).toBe(2)
})
})