From e6bfb266e3c4e3c239a549e477cbf5cda4c528ec Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Wed, 15 May 2019 11:18:36 +0200 Subject: [PATCH] fix memory leak #1507 --- src/store.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/store.js b/src/store.js index a79252b50..3782660c4 100644 --- a/src/store.js +++ b/src/store.js @@ -247,6 +247,13 @@ function resetStore (store, hot) { resetStoreVM(store, state, hot) } +// helper function for resetStoreVM to prevent closure-preserving memory leak +export function getKeyFromVM (store, key) { + return function () { + return store._vm[key] + } +} + function resetStoreVM (store, state, hot) { const oldVm = store._vm @@ -257,10 +264,10 @@ function resetStoreVM (store, state, hot) { 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 enviroment. + // using partial and getKeyFromVM to return functions with only arguments preserved in closure enviroment. computed[key] = partial(fn, store) Object.defineProperty(store.getters, key, { - get: () => store._vm[key], + get: getKeyFromVM(store, key), enumerable: true // for local getters }) })