@@ -41,9 +41,12 @@ function applyMixin (Vue) {
41
41
}
42
42
}
43
43
44
- var devtoolHook =
45
- typeof window !== 'undefined' &&
46
- window . __VUE_DEVTOOLS_GLOBAL_HOOK__ ;
44
+ var target = typeof window !== 'undefined'
45
+ ? window
46
+ : typeof global !== 'undefined'
47
+ ? global
48
+ : { } ;
49
+ var devtoolHook = target . __VUE_DEVTOOLS_GLOBAL_HOOK__ ;
47
50
48
51
function devtoolPlugin ( store ) {
49
52
if ( ! devtoolHook ) { return }
@@ -89,6 +92,12 @@ function assert (condition, msg) {
89
92
if ( ! condition ) { throw new Error ( ( "[vuex] " + msg ) ) }
90
93
}
91
94
95
+ function partial ( fn , arg ) {
96
+ return function ( ) {
97
+ return fn ( arg )
98
+ }
99
+ }
100
+
92
101
// Base data struct for store's module, package with some attribute and method
93
102
var Module = function Module ( rawModule , runtime ) {
94
103
this . runtime = runtime ;
@@ -550,7 +559,9 @@ function resetStoreVM (store, state, hot) {
550
559
var computed = { } ;
551
560
forEachValue ( wrappedGetters , function ( fn , key ) {
552
561
// use computed to leverage its lazy-caching mechanism
553
- computed [ key ] = function ( ) { return fn ( store ) ; } ;
562
+ // direct inline function use will lead to closure preserving oldVm.
563
+ // using partial to return function with only arguments preserved in closure enviroment.
564
+ computed [ key ] = partial ( fn , store ) ;
554
565
Object . defineProperty ( store . getters , key , {
555
566
get : function ( ) { return store . _vm [ key ] ; } ,
556
567
enumerable : true // for local getters
@@ -687,27 +698,37 @@ function makeLocalContext (store, namespace, path) {
687
698
return local
688
699
}
689
700
701
+ var makeLocalGettersCache = { } ;
702
+ var cacheStore = { } ;
703
+
690
704
function makeLocalGetters ( store , namespace ) {
691
- var gettersProxy = { } ;
692
-
693
- var splitPos = namespace . length ;
694
- Object . keys ( store . getters ) . forEach ( function ( type ) {
695
- // skip if the target getter is not match this namespace
696
- if ( type . slice ( 0 , splitPos ) !== namespace ) { return }
697
-
698
- // extract local getter type
699
- var localType = type . slice ( splitPos ) ;
700
-
701
- // Add a port to the getters proxy.
702
- // Define as getter property because
703
- // we do not want to evaluate the getters in this time.
704
- Object . defineProperty ( gettersProxy , localType , {
705
- get : function ( ) { return store . getters [ type ] ; } ,
706
- enumerable : true
705
+ if ( cacheStore !== store ) {
706
+ makeLocalGettersCache = { } ;
707
+ cacheStore = store ;
708
+ }
709
+
710
+ if ( ! makeLocalGettersCache [ namespace ] ) {
711
+ var gettersProxy = { } ;
712
+ var splitPos = namespace . length ;
713
+ Object . keys ( store . getters ) . forEach ( function ( type ) {
714
+ // skip if the target getter is not match this namespace
715
+ if ( type . slice ( 0 , splitPos ) !== namespace ) { return }
716
+
717
+ // extract local getter type
718
+ var localType = type . slice ( splitPos ) ;
719
+
720
+ // Add a port to the getters proxy.
721
+ // Define as getter property because
722
+ // we do not want to evaluate the getters in this time.
723
+ Object . defineProperty ( gettersProxy , localType , {
724
+ get : function ( ) { return store . getters [ type ] ; } ,
725
+ enumerable : true
726
+ } ) ;
707
727
} ) ;
708
- } ) ;
728
+ makeLocalGettersCache [ namespace ] = gettersProxy ;
729
+ }
709
730
710
- return gettersProxy
731
+ return makeLocalGettersCache [ namespace ]
711
732
}
712
733
713
734
function registerMutation ( store , type , handler , local ) {
0 commit comments