Skip to content

Commit 11ac753

Browse files
committed
fix(store): fixed issue with wrong getter value after state update
1 parent 7d53fc4 commit 11ac753

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/core/modules/store/create-store.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,44 @@ function createStore(storeParams = {}) {
1414
const gettersDependencies = {};
1515
const gettersCallbacks = {};
1616

17-
Object.keys(getters).forEach((key) => {
18-
gettersDependencies[key] = [];
19-
gettersCallbacks[key] = [];
17+
Object.keys(getters).forEach((getterKey) => {
18+
gettersDependencies[getterKey] = [];
19+
gettersCallbacks[getterKey] = [];
2020
});
21-
const addGetterDependencies = (key, deps) => {
22-
if (!gettersDependencies[key]) gettersDependencies[key] = [];
21+
22+
const getGetterValue = (getterKey) => {
23+
return getters[getterKey]({ state: store.state });
24+
};
25+
26+
const addGetterDependencies = (getterKey, deps) => {
27+
if (!gettersDependencies[getterKey]) gettersDependencies[getterKey] = [];
2328
deps.forEach((dep) => {
24-
if (gettersDependencies[key].indexOf(dep) < 0) {
25-
gettersDependencies[key].push(dep);
29+
if (gettersDependencies[getterKey].indexOf(dep) < 0) {
30+
gettersDependencies[getterKey].push(dep);
2631
}
2732
});
2833
};
2934

30-
const addGetterCallback = (key, callback) => {
31-
if (!gettersCallbacks[key]) gettersCallbacks[key] = [];
32-
gettersCallbacks[key].push(callback);
35+
const addGetterCallback = (getterKey, callback) => {
36+
if (!gettersCallbacks[getterKey]) gettersCallbacks[getterKey] = [];
37+
gettersCallbacks[getterKey].push(callback);
3338
};
3439

35-
const runGetterCallbacks = (stateKey, value) => {
40+
const runGetterCallbacks = (stateKey) => {
3641
const keys = Object.keys(gettersDependencies).filter((getterKey) => {
3742
return gettersDependencies[getterKey].indexOf(stateKey) >= 0;
3843
});
3944
keys.forEach((getterKey) => {
4045
if (!gettersCallbacks[getterKey] || !gettersCallbacks[getterKey].length) return;
4146
gettersCallbacks[getterKey].forEach((callback) => {
42-
callback(value);
47+
callback(getGetterValue(getterKey));
4348
});
4449
});
4550
};
4651

4752
const removeGetterCallback = (callback) => {
48-
Object.keys(gettersCallbacks).forEach((key) => {
49-
const callbacks = gettersCallbacks[key];
53+
Object.keys(gettersCallbacks).forEach((stateKey) => {
54+
const callbacks = gettersCallbacks[stateKey];
5055

5156
if (callbacks.indexOf(callback) >= 0) {
5257
callbacks.splice(callbacks.indexOf(callback), 1);
@@ -59,13 +64,13 @@ function createStore(storeParams = {}) {
5964
removeGetterCallback(callback);
6065
};
6166

62-
const getterValue = (key) => {
63-
if (key === 'constructor') return;
67+
const getterValue = (getterKey) => {
68+
if (getterKey === 'constructor') return;
6469
propsQueue = [];
65-
const value = getters[key]({ state: store.state });
66-
addGetterDependencies(key, propsQueue);
70+
const value = getGetterValue(getterKey);
71+
addGetterDependencies(getterKey, propsQueue);
6772
const onUpdated = (callback) => {
68-
addGetterCallback(key, callback);
73+
addGetterCallback(getterKey, callback);
6974
};
7075

7176
const obj = {
@@ -77,15 +82,15 @@ function createStore(storeParams = {}) {
7782
};
7883

7984
obj.__callback = callback;
80-
addGetterCallback(key, callback);
85+
addGetterCallback(getterKey, callback);
8186
// eslint-disable-next-line
8287
return obj;
8388
};
8489

8590
store.state = new Proxy(state, {
8691
set: (target, prop, value) => {
8792
target[prop] = value;
88-
runGetterCallbacks(prop, value);
93+
runGetterCallbacks(prop);
8994
return true;
9095
},
9196
get: (target, prop) => {

0 commit comments

Comments
 (0)