Skip to content

Commit

Permalink
fix: Memory leak happening while using registerModule/u… (#1508)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
parth67 authored and ktsn committed Mar 25, 2019
1 parent b58d3d6 commit cb9986a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions 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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/util.js
Expand Up @@ -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)
}
}

0 comments on commit cb9986a

Please sign in to comment.