1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
8+
9+ exports . default = connect ;
10+
11+ var _normalizeProps = require ( './normalizeProps' ) ;
12+
13+ var _normalizeProps2 = _interopRequireDefault ( _normalizeProps ) ;
14+
15+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
16+
17+ function noop ( ) { }
18+
19+ function getStore ( component ) {
20+ return component . $store ;
21+ }
22+
23+ function getAttrs ( component ) {
24+ return component . _self . $options . _parentVnode . data . attrs ;
25+ }
26+
27+ function getStates ( component , mapStateToProps ) {
28+ var store = getStore ( component ) ;
29+ var attrs = getAttrs ( component ) ;
30+
31+ return mapStateToProps ( store . getState ( ) , attrs ) || { } ;
32+ }
33+
34+ function getActions ( component , mapActionsToProps ) {
35+ var store = getStore ( component ) ;
36+
37+ return mapActionsToProps ( store . dispatch , getAttrs . bind ( null , component ) ) || { } ;
38+ }
39+
40+ function getProps ( component ) {
41+ var props = { } ;
42+ var attrs = getAttrs ( component ) ;
43+ var stateNames = component . vuaReduxStateNames ;
44+ var actionNames = component . vuaReduxActionNames ;
45+
46+ for ( var ii = 0 ; ii < stateNames . length ; ii ++ ) {
47+ props [ stateNames [ ii ] ] = component [ stateNames [ ii ] ] ;
48+ }
49+
50+ for ( var _ii = 0 ; _ii < actionNames . length ; _ii ++ ) {
51+ props [ actionNames [ _ii ] ] = component [ actionNames [ _ii ] ] ;
52+ }
53+
54+ return _extends ( { } , props , attrs ) ;
55+ }
56+
57+ function defaultMergeProps ( stateProps , actionsProps ) {
58+ return _extends ( { } , stateProps , actionsProps ) ;
59+ }
60+
61+ /**
62+ * 1. utilities are moved above because vue stores methods, states and props
63+ * in the same namespace
64+ * 2. actions are set while created
65+ */
66+
67+ /**
68+ * @param mapStateToProps
69+ * @param mapActionsToProps
70+ * @param mergeProps
71+ * @returns Object
72+ */
73+ function connect ( mapStateToProps , mapActionsToProps , mergeProps ) {
74+ mapStateToProps = mapStateToProps || noop ;
75+ mapActionsToProps = mapActionsToProps || noop ;
76+ mergeProps = mergeProps || defaultMergeProps ;
77+
78+ return function ( children ) {
79+
80+ /** @namespace children.collect */
81+ if ( children . collect ) {
82+ children . props = _extends ( { } , ( 0 , _normalizeProps2 . default ) ( children . props || { } ) , ( 0 , _normalizeProps2 . default ) ( children . collect || { } ) ) ;
83+
84+ var msg = 'vua-redux: collect is deprecated, use props ' + ( 'in ' + ( children . name || 'anonymous' ) + ' component' ) ;
85+
86+ console . warn ( msg ) ;
87+ }
88+
89+ return {
90+ name : 'ConnectVuaRedux-' + ( children . name || 'children' ) ,
91+
92+ render : function render ( h ) {
93+ var props = getProps ( this ) ;
94+
95+ return h ( children , { props : props } ) ;
96+ } ,
97+ data : function data ( ) {
98+ var state = getStates ( this , mapStateToProps ) ;
99+ var actions = getActions ( this , mapActionsToProps ) ;
100+ var stateNames = Object . keys ( state ) ;
101+ var actionNames = Object . keys ( actions ) ;
102+
103+ return _extends ( { } , mergeProps ( state , actions ) , {
104+ vuaReduxStateNames : stateNames ,
105+ vuaReduxActionNames : actionNames
106+ } ) ;
107+ } ,
108+ created : function created ( ) {
109+ var _this = this ;
110+
111+ var store = getStore ( this ) ;
112+
113+ this . vuaReduxUnsubscribe = store . subscribe ( function ( ) {
114+ var state = getStates ( _this , mapStateToProps ) ;
115+ var stateNames = Object . keys ( state ) ;
116+ _this . vuaReduxStateNames = stateNames ;
117+
118+ for ( var ii = 0 ; ii < stateNames . length ; ii ++ ) {
119+ _this [ stateNames [ ii ] ] = state [ stateNames [ ii ] ] ;
120+ }
121+ } ) ;
122+ } ,
123+ beforeDestroy : function beforeDestroy ( ) {
124+ this . vuaReduxUnsubscribe ( ) ;
125+ }
126+ } ;
127+ } ;
128+ }
0 commit comments