Ver Fonte

fix: Memory leak happening while using registerModule/u… (#1508)

* Fixed issue#1507 : Memory leak happening while using registerModule/unregisterModule.

* Adding comment for leak description. Simplyfy partial function to take only one argument.

* Removed oldVm.computed = null. This statement is not required
Parth há 6 anos atrás
pai
commit
cb9986ae5a
2 ficheiros alterados com 10 adições e 2 exclusões
  1. 4 2
      src/store.js
  2. 6 0
      src/util.js

+ 4 - 2
src/store.js

@@ -1,7 +1,7 @@
 import applyMixin from './mixin'
 import devtoolPlugin from './plugins/devtool'
 import ModuleCollection from './module/module-collection'
-import { forEachValue, isObject, isPromise, assert } from './util'
+import { forEachValue, isObject, isPromise, assert, partial } from './util'
 
 let Vue // bind on install
 
@@ -256,7 +256,9 @@ function resetStoreVM (store, state, hot) {
   const computed = {}
   forEachValue(wrappedGetters, (fn, key) => {
     // use computed to leverage its lazy-caching mechanism
-    computed[key] = () => fn(store)
+    // direct inline function use will lead to closure preserving oldVm.
+    // using partial to return function with only arguments preserved in closure enviroment.
+    computed[key] = partial(fn, store)
     Object.defineProperty(store.getters, key, {
       get: () => store._vm[key],
       enumerable: true // for local getters

+ 6 - 0
src/util.js

@@ -64,3 +64,9 @@ export function isPromise (val) {
 export function assert (condition, msg) {
   if (!condition) throw new Error(`[vuex] ${msg}`)
 }
+
+export function partial (fn, arg) {
+  return function () {
+    return fn(arg)
+  }
+}