Skip to content

Commit a59d5cd

Browse files
fix: Remove stale reactive effect from modules after unregistering the module
1 parent 649854b commit a59d5cd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/store-util.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export function resetStore (store, hot) {
3030
export function resetStoreState (store, state, hot) {
3131
const oldState = store._state
3232
const oldScope = store._scope
33+
const oldCache = store._computedCache
34+
const oldGettersKeySet = new Set(store.getters ? Object.keys(store.getters) : [])
3335

3436
// bind store public getters
3537
store.getters = {}
@@ -45,6 +47,10 @@ export function resetStoreState (store, state, hot) {
4547

4648
scope.run(() => {
4749
forEachValue(wrappedGetters, (fn, key) => {
50+
// Filter stale getters' key by comparing oldGetters and wrappedGetters,
51+
// the key does not be removed from oldGettersKeySet are the key of stale computed cache.
52+
// Stale computed cache: the computed cache should be removed as the corresponding module is removed.
53+
oldGettersKeySet.delete(key)
4854
// use computed to leverage its lazy-caching mechanism
4955
// direct inline function use will lead to closure preserving oldState.
5056
// using partial to return function with only arguments preserved in closure environment.
@@ -64,6 +70,7 @@ export function resetStoreState (store, state, hot) {
6470
// register the newly created effect scope to the store so that we can
6571
// dispose the effects when this method runs again in the future.
6672
store._scope = scope
73+
store._computedCache = computedCache
6774

6875
// enable strict mode for new state
6976
if (store.strict) {
@@ -83,8 +90,14 @@ export function resetStoreState (store, state, hot) {
8390
// dispose previously registered effect scope if there is one.
8491
if (oldScope) {
8592
const deadEffects = []
93+
const staleComputedCache = new Set()
94+
oldGettersKeySet.forEach((staleKey) => {
95+
staleComputedCache.add(oldCache[staleKey])
96+
})
8697
oldScope.effects.forEach(effect => {
87-
if (effect.deps.length) {
98+
// Use the staleComputedCache match the computed property of reactiveEffect,
99+
// to specify the stale cache
100+
if (effect.deps.length && !staleComputedCache.has(effect.computed)) {
88101
// Merge the effect that already have dependencies and prevent from being killed.
89102
scope.effects.push(effect)
90103
} else {

0 commit comments

Comments
 (0)