-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
238 lines (188 loc) · 7.23 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
function onDispatchStoreEnhancer(createStore) {
return function (reducer, initialState, enhancer) {
var store = createStore(reducer, initialState, enhancer);
var dispatch = store.dispatch;
var onDispatchHandlers = [];
store.onDispatch = function (callback) {
onDispatchHandlers.push(callback);
return function () {
onDispatchHandlers = onDispatchHandlers.filter(function (cb) {
return cb !== callback;
});
};
};
store.dispatch = function (action) {
var result = dispatch(action);
onDispatchHandlers.forEach(function (handler) {
return handler(action);
});
return result;
};
return store;
};
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var defineProperty = function (obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
};
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var isObject = function isObject(value) {
return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && !Array.isArray(value);
};
var keysAreEqual = function keysAreEqual(left, right) {
return Object.keys(left).length === Object.keys(right).length && Object.keys(left).every(function (leftKey) {
return left[leftKey] === right[leftKey];
});
};
var shallowEquals = function shallowEquals(left, right) {
return left === right || keysAreEqual(left, right);
};
var bindActionCreatorsObject = function bindActionCreatorsObject(mapDispatchToProps, dispatchAction) {
return Object.keys(mapDispatchToProps).reduce(function (actionCreators, actionName) {
return Object.assign({}, actionCreators, defineProperty({}, actionName, function () {
var actionCreator = mapDispatchToProps[actionName];
var action = actionCreator.apply(undefined, arguments);
dispatchAction(action);
}));
}, {});
};
var bindActionCreators = function bindActionCreators(mapDispatchToProps, dispatchAction) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (typeof mapDispatchToProps === 'function') {
return mapDispatchToProps(dispatchAction, props);
} else if ((typeof mapDispatchToProps === 'undefined' ? 'undefined' : _typeof(mapDispatchToProps)) === 'object') {
return bindActionCreatorsObject(mapDispatchToProps, dispatchAction);
}
return {};
};
var contextType = {};
var getContextType = function getContextType() {
return contextType;
};
var setContextType = function setContextType(type) {
return contextType = type;
};
var initAction = {
type: '@@localReducer/INIT'
};
var defaultMergeProps = function defaultMergeProps() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return Object.assign.apply(Object, [{}].concat(args));
};
var withReducer = function withReducer(createReducer, mapDispatchToProps, mergeProps, contextType) {
return function (WrappedComponent) {
var WithReducer = function (_PureComponent) {
inherits(WithReducer, _PureComponent);
function WithReducer(props, context) {
classCallCheck(this, WithReducer);
var _this = possibleConstructorReturn(this, (WithReducer.__proto__ || Object.getPrototypeOf(WithReducer)).call(this, props, context));
_this.dispatchAction = function (action) {
if (!isObject(action) || !action.type) {
console.error('[' + componentName + '][dispatch] Expected an action object with type, got:', action);
return;
}
var newState = _this.reducer(_this.state, action);
if (!isObject(newState)) {
console.error('[' + componentName + '][reducer] Expected an object to be returned, got:', newState);
}
if (!shallowEquals(_this.state, newState)) {
_this.setState(newState);
}
};
_this.mergeProps = mergeProps || defaultMergeProps;
_this.reducer = createReducer(props, context);
_this.state = _this.reducer(undefined, initAction);
_this.actionCreators = bindActionCreators(mapDispatchToProps, _this.dispatchAction, _this.props);
var _ref = context || {},
store = _ref.store;
if (store && store.onDispatch) {
_this.unsubscribe = store.onDispatch(_this.dispatchAction);
}
return _this;
}
createClass(WithReducer, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
}, {
key: 'render',
value: function render() {
return React__default.createElement(WrappedComponent, this.mergeProps(this.props, this.state, this.actionCreators));
}
}]);
return WithReducer;
}(React.PureComponent);
var finalContextType = contextType || getContextType();
if (finalContextType) {
WithReducer.contextType = finalContextType;
}
return WithReducer;
};
};
exports.withReducer = withReducer;
exports.onDispatchStoreEnhancer = onDispatchStoreEnhancer;
exports.setContextType = setContextType;