1
1
/**
2
- * vuex v3.1.1
2
+ * vuex v3.1.2
3
3
* (c) 2019 Evan You
4
4
* @license MIT
5
5
*/
@@ -316,6 +316,7 @@ class Store {
316
316
this . _modulesNamespaceMap = Object . create ( null ) ;
317
317
this . _subscribers = [ ] ;
318
318
this . _watcherVM = new Vue ( ) ;
319
+ this . _makeLocalGettersCache = Object . create ( null ) ;
319
320
320
321
// bind commit and dispatch to self
321
322
const store = this ;
@@ -532,12 +533,14 @@ function resetStoreVM (store, state, hot) {
532
533
533
534
// bind store public getters
534
535
store . getters = { } ;
536
+ // reset local getters cache
537
+ store . _makeLocalGettersCache = Object . create ( null ) ;
535
538
const wrappedGetters = store . _wrappedGetters ;
536
539
const computed = { } ;
537
540
forEachValue ( wrappedGetters , ( fn , key ) => {
538
541
// use computed to leverage its lazy-caching mechanism
539
542
// direct inline function use will lead to closure preserving oldVm.
540
- // using partial to return function with only arguments preserved in closure enviroment .
543
+ // using partial to return function with only arguments preserved in closure environment .
541
544
computed [ key ] = partial ( fn , store ) ;
542
545
Object . defineProperty ( store . getters , key , {
543
546
get : ( ) => store . _vm [ key ] ,
@@ -581,6 +584,9 @@ function installModule (store, rootState, path, module, hot) {
581
584
582
585
// register in namespace map
583
586
if ( module . namespaced ) {
587
+ if ( store . _modulesNamespaceMap [ namespace ] && "development" !== 'production' ) {
588
+ console . error ( `[vuex] duplicate namespace ${ namespace } for the namespaced module ${ path . join ( '/' ) } ` ) ;
589
+ }
584
590
store . _modulesNamespaceMap [ namespace ] = module ;
585
591
}
586
592
@@ -589,6 +595,13 @@ function installModule (store, rootState, path, module, hot) {
589
595
const parentState = getNestedState ( rootState , path . slice ( 0 , - 1 ) ) ;
590
596
const moduleName = path [ path . length - 1 ] ;
591
597
store . _withCommit ( ( ) => {
598
+ {
599
+ if ( moduleName in parentState ) {
600
+ console . warn (
601
+ `[vuex] state field "${ moduleName } " was overridden by a module with the same name at "${ path . join ( '.' ) } "`
602
+ ) ;
603
+ }
604
+ }
592
605
Vue . set ( parentState , moduleName , module . state ) ;
593
606
} ) ;
594
607
}
@@ -674,26 +687,28 @@ function makeLocalContext (store, namespace, path) {
674
687
}
675
688
676
689
function makeLocalGetters ( store , namespace ) {
677
- const gettersProxy = { } ;
678
-
679
- const splitPos = namespace . length ;
680
- Object . keys ( store . getters ) . forEach ( type => {
681
- // skip if the target getter is not match this namespace
682
- if ( type . slice ( 0 , splitPos ) !== namespace ) return
683
-
684
- // extract local getter type
685
- const localType = type . slice ( splitPos ) ;
686
-
687
- // Add a port to the getters proxy.
688
- // Define as getter property because
689
- // we do not want to evaluate the getters in this time.
690
- Object . defineProperty ( gettersProxy , localType , {
691
- get : ( ) => store . getters [ type ] ,
692
- enumerable : true
690
+ if ( ! store . _makeLocalGettersCache [ namespace ] ) {
691
+ const gettersProxy = { } ;
692
+ const splitPos = namespace . length ;
693
+ Object . keys ( store . getters ) . forEach ( type => {
694
+ // skip if the target getter is not match this namespace
695
+ if ( type . slice ( 0 , splitPos ) !== namespace ) return
696
+
697
+ // extract local getter type
698
+ const localType = type . slice ( splitPos ) ;
699
+
700
+ // Add a port to the getters proxy.
701
+ // Define as getter property because
702
+ // we do not want to evaluate the getters in this time.
703
+ Object . defineProperty ( gettersProxy , localType , {
704
+ get : ( ) => store . getters [ type ] ,
705
+ enumerable : true
706
+ } ) ;
693
707
} ) ;
694
- } ) ;
708
+ store . _makeLocalGettersCache [ namespace ] = gettersProxy ;
709
+ }
695
710
696
- return gettersProxy
711
+ return store . _makeLocalGettersCache [ namespace ]
697
712
}
698
713
699
714
function registerMutation ( store , type , handler , local ) {
@@ -705,15 +720,15 @@ function registerMutation (store, type, handler, local) {
705
720
706
721
function registerAction ( store , type , handler , local ) {
707
722
const entry = store . _actions [ type ] || ( store . _actions [ type ] = [ ] ) ;
708
- entry . push ( function wrappedActionHandler ( payload , cb ) {
723
+ entry . push ( function wrappedActionHandler ( payload ) {
709
724
let res = handler . call ( store , {
710
725
dispatch : local . dispatch ,
711
726
commit : local . commit ,
712
727
getters : local . getters ,
713
728
state : local . state ,
714
729
rootGetters : store . getters ,
715
730
rootState : store . state
716
- } , payload , cb ) ;
731
+ } , payload ) ;
717
732
if ( ! isPromise ( res ) ) {
718
733
res = Promise . resolve ( res ) ;
719
734
}
@@ -794,6 +809,9 @@ function install (_Vue) {
794
809
*/
795
810
const mapState = normalizeNamespace ( ( namespace , states ) => {
796
811
const res = { } ;
812
+ if ( ! isValidMap ( states ) ) {
813
+ console . error ( '[vuex] mapState: mapper parameter must be either an Array or an Object' ) ;
814
+ }
797
815
normalizeMap ( states ) . forEach ( ( { key, val } ) => {
798
816
res [ key ] = function mappedState ( ) {
799
817
let state = this . $store . state ;
@@ -824,6 +842,9 @@ const mapState = normalizeNamespace((namespace, states) => {
824
842
*/
825
843
const mapMutations = normalizeNamespace ( ( namespace , mutations ) => {
826
844
const res = { } ;
845
+ if ( ! isValidMap ( mutations ) ) {
846
+ console . error ( '[vuex] mapMutations: mapper parameter must be either an Array or an Object' ) ;
847
+ }
827
848
normalizeMap ( mutations ) . forEach ( ( { key, val } ) => {
828
849
res [ key ] = function mappedMutation ( ...args ) {
829
850
// Get the commit method from store
@@ -851,6 +872,9 @@ const mapMutations = normalizeNamespace((namespace, mutations) => {
851
872
*/
852
873
const mapGetters = normalizeNamespace ( ( namespace , getters ) => {
853
874
const res = { } ;
875
+ if ( ! isValidMap ( getters ) ) {
876
+ console . error ( '[vuex] mapGetters: mapper parameter must be either an Array or an Object' ) ;
877
+ }
854
878
normalizeMap ( getters ) . forEach ( ( { key, val } ) => {
855
879
// The namespace has been mutated by normalizeNamespace
856
880
val = namespace + val ;
@@ -878,6 +902,9 @@ const mapGetters = normalizeNamespace((namespace, getters) => {
878
902
*/
879
903
const mapActions = normalizeNamespace ( ( namespace , actions ) => {
880
904
const res = { } ;
905
+ if ( ! isValidMap ( actions ) ) {
906
+ console . error ( '[vuex] mapActions: mapper parameter must be either an Array or an Object' ) ;
907
+ }
881
908
normalizeMap ( actions ) . forEach ( ( { key, val } ) => {
882
909
res [ key ] = function mappedAction ( ...args ) {
883
910
// get dispatch function from store
@@ -917,11 +944,23 @@ const createNamespacedHelpers = (namespace) => ({
917
944
* @return {Object }
918
945
*/
919
946
function normalizeMap ( map ) {
947
+ if ( ! isValidMap ( map ) ) {
948
+ return [ ]
949
+ }
920
950
return Array . isArray ( map )
921
951
? map . map ( key => ( { key, val : key } ) )
922
952
: Object . keys ( map ) . map ( key => ( { key, val : map [ key ] } ) )
923
953
}
924
954
955
+ /**
956
+ * Validate whether given map is valid or not
957
+ * @param {* } map
958
+ * @return {Boolean }
959
+ */
960
+ function isValidMap ( map ) {
961
+ return Array . isArray ( map ) || isObject ( map )
962
+ }
963
+
925
964
/**
926
965
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
927
966
* @param {Function } fn
@@ -957,7 +996,7 @@ function getModuleByNamespace (store, helper, namespace) {
957
996
var index_esm = {
958
997
Store,
959
998
install,
960
- version : '3.1.1 ' ,
999
+ version : '3.1.2 ' ,
961
1000
mapState,
962
1001
mapMutations,
963
1002
mapGetters,
0 commit comments