Skip to content

Commit

Permalink
fix: fix getters getting re-evaluated on every access (#1823) (#1860)
Browse files Browse the repository at this point in the history
Co-authored-by: Kia Ishii <kia.king.08@gmail.com>
  • Loading branch information
jnxey and kiaking authored Oct 30, 2020
1 parent 4ace559 commit 0006765
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ 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(() => computedObj[key]())
Object.defineProperty(store.getters, key, {
get: () => computed(() => computedObj[key]()).value,
get: () => computedCache[key].value,
enumerable: true // for local getters
})
})
Expand Down

0 comments on commit 0006765

Please sign in to comment.