From 34ba4ec036d37641293d4a6a18063c7ab7c8d4c7 Mon Sep 17 00:00:00 2001 From: liyi <846046245@qq.com> Date: Tue, 27 Oct 2020 10:05:47 +0800 Subject: [PATCH 1/2] [Repair] #1823 Getters are re-evaluated on every access --- src/store.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/store.js b/src/store.js index 549fd1a4f..557d54988 100644 --- a/src/store.js +++ b/src/store.js @@ -286,13 +286,17 @@ function resetStoreState (store, state, hot) { store._makeLocalGettersCache = Object.create(null) const wrappedGetters = store._wrappedGetters const computedObj = {} + const computedCache = {} forEachValue(wrappedGetters, (fn, key) => { // use computed to leverage its lazy-caching mechanism // direct inline function use will lead to closure preserving oldVm. // using partial to return function with only arguments preserved in closure environment. computedObj[key] = partial(fn, store) + computedCache[key] = computed(() => { + return computedObj[key]() + }) Object.defineProperty(store.getters, key, { - get: () => computed(() => computedObj[key]()).value, + get: () => computedCache[key].value, enumerable: true // for local getters }) }) From 30c7559b8bcd0a22700c2e3cd36850eeccfc3e3c Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Fri, 30 Oct 2020 19:25:44 +0900 Subject: [PATCH 2/2] style: adjust code style --- src/store.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/store.js b/src/store.js index 557d54988..8cbf13516 100644 --- a/src/store.js +++ b/src/store.js @@ -292,9 +292,7 @@ function resetStoreState (store, state, hot) { // direct inline function use will lead to closure preserving oldVm. // using partial to return function with only arguments preserved in closure environment. computedObj[key] = partial(fn, store) - computedCache[key] = computed(() => { - return computedObj[key]() - }) + computedCache[key] = computed(() => computedObj[key]()) Object.defineProperty(store.getters, key, { get: () => computedCache[key].value, enumerable: true // for local getters