-
Notifications
You must be signed in to change notification settings - Fork 4
/
connect.js
164 lines (125 loc) · 6.71 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; };
var _redux = require('redux');
var _isEqualShallow = require('is-equal-shallow');
var _isEqualShallow2 = _interopRequireDefault(_isEqualShallow);
var _isPlainObject = require('is-plain-object');
var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var defaultMapStateToProps = function defaultMapStateToProps() {
return {};
};
var defaultMapDispatchToProps = function defaultMapDispatchToProps(dispatch) {
return { dispatch: dispatch };
};
var defaultMergeProps = function defaultMergeProps(stateProps, dispatchProps, parentProps) {
return _extends({}, parentProps, stateProps, dispatchProps);
};
var wrapActionCreators = function wrapActionCreators(actionCreators) {
return function (dispatch) {
return (0, _redux.bindActionCreators)(actionCreators, dispatch);
};
};
function connect() {
var mapStateToProps = arguments.length <= 0 || arguments[0] === undefined ? defaultMapStateToProps : arguments[0];
var mapDispatchToProps = arguments.length <= 1 || arguments[1] === undefined ? defaultMapDispatchToProps : arguments[1];
var mergeProps = arguments.length <= 2 || arguments[2] === undefined ? defaultMergeProps : arguments[2];
if ((0, _isPlainObject2.default)(mapDispatchToProps)) {
mapDispatchToProps = wrapActionCreators(mapDispatchToProps);
}
var mappedStateUseProps = mapStateToProps.length > 1;
var mappedDispatchUseProps = mapDispatchToProps.length > 1;
var computeStateProps = function computeStateProps(props) {
var state = props.store.getState();
var stateProps = mapStateToProps(state, props);
(0, _invariant2.default)((0, _isPlainObject2.default)(stateProps), '[deku-redux][connect] mapStateToProps must return an object. Instead received %s', stateProps);
return stateProps;
};
var computeDispatchProps = function computeDispatchProps(props) {
var dispatch = props.store.dispatch;
var dispatchProps = mapDispatchToProps(dispatch, props);
(0, _invariant2.default)((0, _isPlainObject2.default)(dispatchProps), '[deku-redux][connect] mapDispatchToProps must return an object. Instead received %s', dispatchProps);
return dispatchProps;
};
return function connectWrapper(Component) {
var connectRegistry = {};
var getStateProps = function getStateProps(id) {
return connectRegistry[id] ? connectRegistry[id].stateProps : null;
};
var setStateProps = function setStateProps(id, stateProps) {
return connectRegistry[id].stateProps = stateProps;
};
var getDispatchProps = function getDispatchProps(id) {
return connectRegistry[id] ? connectRegistry[id].dispatchProps : null;
};
var setDispatchProps = function setDispatchProps(id, dispatchProps) {
return connectRegistry[id].dispatchProps = dispatchProps;
};
var ConnectedComponent = {
propTypes: {
store: { source: 'store' },
storeState: { source: 'storeState' }
},
beforeMount: function beforeMount(_ref, elm, setState) {
var id = _ref.id;
var props = _ref.props;
(0, _invariant2.default)(props.store, '[deku-redux][connect] Could not find store. Did you use `storePlugin` on your deku tree?');
(0, _invariant2.default)(props.store.getState && props.store.subscribe && props.store.subscribe, '[deku-redux][connect] Could not recognise store. Did you use `storePlugin` with a valid redux store?');
connectRegistry[id] = {};
setStateProps(id, computeStateProps(props));
setDispatchProps(id, computeDispatchProps(props));
},
shouldUpdate: function shouldUpdate(_ref2, nextProps) {
var id = _ref2.id;
var props = _ref2.props;
var storeChanged = nextProps.storeState !== props.storeState;
var propsChanged = !(0, _isEqualShallow2.default)(nextProps, props);
var computeNewStateProps = storeChanged || propsChanged && mappedStateUseProps;
var computeNewDispatchProps = propsChanged && mappedDispatchUseProps;
var statePropsChanged = false;
var dispatchPropsChanged = false;
if (computeNewStateProps) {
var _stateProps = computeStateProps(props);
statePropsChanged = !(0, _isEqualShallow2.default)(_stateProps, getStateProps(id));
if (statePropsChanged) {
setStateProps(id, _stateProps);
}
}
if (computeNewDispatchProps) {
var dispatchProps = computeDispatchProps(props);
dispatchPropsChanged = !(0, _isEqualShallow2.default)(dispatchProps, getDispatchProps(id));
if (dispatchPropsChanged) {
setDispatchProps(id, stateProps);
}
}
return propsChanged || statePropsChanged || dispatchPropsChanged;
},
beforeUnmount: function beforeUnmount(_ref3) {
var id = _ref3.id;
connectRegistry[id] = undefined;
},
render: function render(_ref4) {
var id = _ref4.id;
var props = _ref4.props;
// TODO: use _.omit or similar
var parentProps = Object.keys(props).filter(function (prop) {
return prop !== 'store' || prop !== 'storeState';
}).reduce(function (acc, prop) {
acc[prop] = props[prop];
return acc;
}, {});
var componentProps = mergeProps(parentProps, getStateProps(id), getDispatchProps(id));
(0, _invariant2.default)((0, _isPlainObject2.default)(componentProps), '[deku-redux][connect] `mergeProps` function didn\'t return a plain object.');
// <Component {...componentProps} />
return { type: Component, children: props.children, attributes: componentProps };
}
};
return ConnectedComponent;
};
}
exports.default = connect;