1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _redux = require("redux");
|
9 |
|
10 | var _reduxActions = require("redux-actions");
|
11 |
|
12 | var _duraUtil = require("dura-util");
|
13 |
|
14 | var reduxSagaEffects = _interopRequireWildcard(require("redux-saga/effects"));
|
15 |
|
16 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
17 |
|
18 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
19 |
|
20 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
21 |
|
22 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
|
23 |
|
24 | function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
25 |
|
26 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
27 |
|
28 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
29 |
|
30 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
31 |
|
32 | 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); } }
|
33 |
|
34 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
35 |
|
36 | function _defineProperty(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; }
|
37 |
|
38 | var ModelHandler =
|
39 |
|
40 | function () {
|
41 | function ModelHandler(_ref) {
|
42 | var pluginHandler = _ref.pluginHandler;
|
43 |
|
44 | _classCallCheck(this, ModelHandler);
|
45 |
|
46 | _defineProperty(this, "models", []);
|
47 |
|
48 | _defineProperty(this, "pluginHandler", undefined);
|
49 |
|
50 | _defineProperty(this, "defaultModels", [{
|
51 | namespace: '@@duraCore',
|
52 | initialState: {
|
53 | count: 0
|
54 | },
|
55 | reducers: {
|
56 | onChangeState: function onChangeState(state) {
|
57 | return _objectSpread({}, state, {
|
58 | count: state.count + 1
|
59 | });
|
60 | }
|
61 | }
|
62 | }]);
|
63 |
|
64 | this.pluginHandler = pluginHandler;
|
65 | }
|
66 |
|
67 | _createClass(ModelHandler, [{
|
68 | key: "addModel",
|
69 | value: function addModel(model) {
|
70 | this.models.push(this._additionalNamespacePrefix(model));
|
71 | }
|
72 | }, {
|
73 | key: "delModel",
|
74 | value: function delModel(namespace) {
|
75 | this.models = this.models.filter(function (model) {
|
76 | return model.namespace !== namespace;
|
77 | });
|
78 | }
|
79 | }, {
|
80 | key: "getCombineReducers",
|
81 | value: function getCombineReducers() {
|
82 | var _this = this;
|
83 |
|
84 | return (0, _redux.combineReducers)(this._getModels().map(function (_ref2) {
|
85 | var namespace = _ref2.namespace,
|
86 | _ref2$reducers = _ref2.reducers,
|
87 | reducers = _ref2$reducers === void 0 ? {} : _ref2$reducers,
|
88 | _ref2$initialState = _ref2.initialState,
|
89 | initialState = _ref2$initialState === void 0 ? {} : _ref2$initialState;
|
90 | return _defineProperty({}, namespace, (0, _reduxActions.handleActions)(_this._applyOnReducerEvent(Object.keys(reducers), reducers, {}), initialState));
|
91 | }).reduce(this._reduce, {}));
|
92 | }
|
93 | }, {
|
94 | key: "getCombineEffects",
|
95 | value: function getCombineEffects() {
|
96 | var effects = this._getModels().map(function (_ref4) {
|
97 | var effects = _ref4.effects;
|
98 | return effects;
|
99 | }).reduce(function (prev, next) {
|
100 | return _objectSpread({}, prev, next);
|
101 | }, {});
|
102 |
|
103 | return this._getRootSaga.bind(this, effects);
|
104 | }
|
105 | }, {
|
106 | key: "_getModels",
|
107 | value: function _getModels() {
|
108 | var _this2 = this;
|
109 |
|
110 | var defaultModels = this.defaultModels.map(function (model) {
|
111 | return _this2._additionalNamespacePrefix(model);
|
112 | });
|
113 | var pluginModels = this.pluginHandler.plugins.filter(function (plugin) {
|
114 | return plugin.reducers;
|
115 | }).map(function (plugin) {
|
116 | return {
|
117 | namespace: plugin.namespace,
|
118 | initialState: plugin.initialState || {},
|
119 | reducers: plugin.reducers
|
120 | };
|
121 | }).map(function (pluginModel) {
|
122 | return _this2._additionalNamespacePrefix(pluginModel);
|
123 | });
|
124 | return defaultModels.concat(this.models).concat(pluginModels);
|
125 | }
|
126 | }, {
|
127 | key: "_mapGenerateSaga",
|
128 | value:
|
129 |
|
130 | regeneratorRuntime.mark(function _mapGenerateSaga(effects) {
|
131 | var effectKs, _i, name, effect, watcher;
|
132 |
|
133 | return regeneratorRuntime.wrap(function _mapGenerateSaga$(_context) {
|
134 | while (1) {
|
135 | switch (_context.prev = _context.next) {
|
136 | case 0:
|
137 | effectKs = Object.keys(effects);
|
138 | _i = 0;
|
139 |
|
140 | case 2:
|
141 | if (!(_i < effectKs.length)) {
|
142 | _context.next = 11;
|
143 | break;
|
144 | }
|
145 |
|
146 | name = effectKs[_i];
|
147 | effect = this._packEffect(name, effects[name]);
|
148 | watcher = this._getWatcher(effect);
|
149 | _context.next = 8;
|
150 | return reduxSagaEffects.fork(watcher);
|
151 |
|
152 | case 8:
|
153 | _i++;
|
154 | _context.next = 2;
|
155 | break;
|
156 |
|
157 | case 11:
|
158 | case "end":
|
159 | return _context.stop();
|
160 | }
|
161 | }
|
162 | }, _mapGenerateSaga, this);
|
163 | })
|
164 | }, {
|
165 | key: "_getRootSaga",
|
166 | value:
|
167 |
|
168 | regeneratorRuntime.mark(function _getRootSaga(effects) {
|
169 | var rootTask;
|
170 | return regeneratorRuntime.wrap(function _getRootSaga$(_context3) {
|
171 | while (1) {
|
172 | switch (_context3.prev = _context3.next) {
|
173 | case 0:
|
174 | _context3.next = 2;
|
175 | return reduxSagaEffects.fork(this._mapGenerateSaga.bind(this, effects));
|
176 |
|
177 | case 2:
|
178 | rootTask = _context3.sent;
|
179 | _context3.next = 5;
|
180 | return reduxSagaEffects.fork(
|
181 |
|
182 | regeneratorRuntime.mark(function _callee() {
|
183 | return regeneratorRuntime.wrap(function _callee$(_context2) {
|
184 | while (1) {
|
185 | switch (_context2.prev = _context2.next) {
|
186 | case 0:
|
187 | _context2.next = 2;
|
188 | return reduxSagaEffects.take("@@dura/cancel");
|
189 |
|
190 | case 2:
|
191 | _context2.next = 4;
|
192 | return reduxSagaEffects.cancel(rootTask);
|
193 |
|
194 | case 4:
|
195 | case "end":
|
196 | return _context2.stop();
|
197 | }
|
198 | }
|
199 | }, _callee, this);
|
200 | }));
|
201 |
|
202 | case 5:
|
203 | case "end":
|
204 | return _context3.stop();
|
205 | }
|
206 | }
|
207 | }, _getRootSaga, this);
|
208 | })
|
209 | }, {
|
210 | key: "_packEffect",
|
211 | value: function _packEffect(name, effect) {
|
212 | var defaultType = 'takeEvery';
|
213 | var newEffect = {
|
214 | name: name,
|
215 | saga: undefined,
|
216 | type: defaultType,
|
217 | ms: 100
|
218 | };
|
219 | var onEffectEventFuns = this.pluginHandler.getOnEffectEventFun();
|
220 |
|
221 | if (!Array.isArray(effect)) {
|
222 | newEffect.saga = (0, _duraUtil.recursiveEnhanceFun)(onEffectEventFuns, effect, name, reduxSagaEffects);
|
223 | } else {
|
224 | var _effect = _slicedToArray(effect, 2),
|
225 | saga = _effect[0],
|
226 | conf = _effect[1];
|
227 |
|
228 | if (!_typeof(conf)) {
|
229 | newEffect.type = defaultType;
|
230 | } else if (typeof conf === 'string') {
|
231 | newEffect.type = conf;
|
232 | } else if (_typeof(conf) === 'object') {
|
233 | newEffect.type = (conf === null || conf === void 0 ? void 0 : conf.type) || defaultType;
|
234 | } else {
|
235 | newEffect.type = defaultType;
|
236 | }
|
237 |
|
238 | newEffect.saga = (0, _duraUtil.recursiveEnhanceFun)(onEffectEventFuns, saga, name, reduxSagaEffects);
|
239 | }
|
240 |
|
241 | return newEffect;
|
242 | }
|
243 | }, {
|
244 | key: "_getWatcher",
|
245 | value: function _getWatcher(effect) {
|
246 | var name = effect.name,
|
247 | saga = effect.saga,
|
248 | type = effect.type,
|
249 | ms = effect.ms;
|
250 |
|
251 | switch (type) {
|
252 | case 'takeLatest':
|
253 | return (
|
254 |
|
255 | regeneratorRuntime.mark(function _callee2() {
|
256 | var _len,
|
257 | args,
|
258 | _key,
|
259 | _args4 = arguments;
|
260 |
|
261 | return regeneratorRuntime.wrap(function _callee2$(_context4) {
|
262 | while (1) {
|
263 | switch (_context4.prev = _context4.next) {
|
264 | case 0:
|
265 | for (_len = _args4.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
266 | args[_key] = _args4[_key];
|
267 | }
|
268 |
|
269 | _context4.next = 3;
|
270 | return reduxSagaEffects.takeLatest(name, saga, reduxSagaEffects, args);
|
271 |
|
272 | case 3:
|
273 | case "end":
|
274 | return _context4.stop();
|
275 | }
|
276 | }
|
277 | }, _callee2, this);
|
278 | })
|
279 | );
|
280 |
|
281 | case 'takeLeading':
|
282 | return (
|
283 |
|
284 | regeneratorRuntime.mark(function _callee3() {
|
285 | var _len2,
|
286 | args,
|
287 | _key2,
|
288 | _args5 = arguments;
|
289 |
|
290 | return regeneratorRuntime.wrap(function _callee3$(_context5) {
|
291 | while (1) {
|
292 | switch (_context5.prev = _context5.next) {
|
293 | case 0:
|
294 | for (_len2 = _args5.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
295 | args[_key2] = _args5[_key2];
|
296 | }
|
297 |
|
298 | _context5.next = 3;
|
299 | return reduxSagaEffects.takeLeading(name, saga, reduxSagaEffects, args);
|
300 |
|
301 | case 3:
|
302 | case "end":
|
303 | return _context5.stop();
|
304 | }
|
305 | }
|
306 | }, _callee3, this);
|
307 | })
|
308 | );
|
309 |
|
310 | case 'throttle':
|
311 | return (
|
312 |
|
313 | regeneratorRuntime.mark(function _callee4() {
|
314 | var _len3,
|
315 | args,
|
316 | _key3,
|
317 | _args6 = arguments;
|
318 |
|
319 | return regeneratorRuntime.wrap(function _callee4$(_context6) {
|
320 | while (1) {
|
321 | switch (_context6.prev = _context6.next) {
|
322 | case 0:
|
323 | for (_len3 = _args6.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
324 | args[_key3] = _args6[_key3];
|
325 | }
|
326 |
|
327 | _context6.next = 3;
|
328 | return reduxSagaEffects.throttle(ms, name, saga, reduxSagaEffects, args);
|
329 |
|
330 | case 3:
|
331 | case "end":
|
332 | return _context6.stop();
|
333 | }
|
334 | }
|
335 | }, _callee4, this);
|
336 | })
|
337 | );
|
338 |
|
339 | default:
|
340 | return (
|
341 |
|
342 | regeneratorRuntime.mark(function _callee5() {
|
343 | var _len4,
|
344 | args,
|
345 | _key4,
|
346 | _args7 = arguments;
|
347 |
|
348 | return regeneratorRuntime.wrap(function _callee5$(_context7) {
|
349 | while (1) {
|
350 | switch (_context7.prev = _context7.next) {
|
351 | case 0:
|
352 | for (_len4 = _args7.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
353 | args[_key4] = _args7[_key4];
|
354 | }
|
355 |
|
356 | _context7.next = 3;
|
357 | return reduxSagaEffects.takeEvery(name, saga, reduxSagaEffects, args);
|
358 |
|
359 | case 3:
|
360 | case "end":
|
361 | return _context7.stop();
|
362 | }
|
363 | }
|
364 | }, _callee5, this);
|
365 | })
|
366 | );
|
367 | }
|
368 | }
|
369 | }, {
|
370 | key: "_applyOnReducerEvent",
|
371 | value: function _applyOnReducerEvent(reducerKeys, reducers, nextReducers) {
|
372 | var first = reducerKeys.shift();
|
373 |
|
374 | if (first) {
|
375 | return this._applyOnReducerEvent(reducerKeys, reducers, _objectSpread({}, nextReducers, _defineProperty({}, first, (0, _duraUtil.recursiveEnhanceFun)(this.pluginHandler.getOnReducerEventFun(), reducers[first]))));
|
376 | }
|
377 |
|
378 | return nextReducers;
|
379 | }
|
380 | }, {
|
381 | key: "_additionalNamespacePrefix",
|
382 | value: function _additionalNamespacePrefix(model) {
|
383 | var namespace = model.namespace,
|
384 | _model$reducers = model.reducers,
|
385 | reducers = _model$reducers === void 0 ? {} : _model$reducers,
|
386 | _model$effects = model.effects,
|
387 | effects = _model$effects === void 0 ? {} : _model$effects,
|
388 | _model$initialState = model.initialState,
|
389 | initialState = _model$initialState === void 0 ? {} : _model$initialState;
|
390 | var _ref5 = [this._rename(namespace, reducers, 'reducers'), this._rename(namespace, effects, 'effects')],
|
391 | newReducers = _ref5[0],
|
392 | newEffects = _ref5[1];
|
393 | return {
|
394 | namespace: namespace,
|
395 | initialState: initialState,
|
396 | reducers: newReducers,
|
397 | effects: newEffects
|
398 | };
|
399 | }
|
400 | }, {
|
401 | key: "_reduce",
|
402 | value: function _reduce(prev, next) {
|
403 | return _objectSpread({}, prev, next);
|
404 | }
|
405 | }, {
|
406 | key: "_rename",
|
407 | value: function _rename(namespace, argObj, type) {
|
408 | return Object.keys(argObj).map(function (key) {
|
409 | return _defineProperty({}, "".concat(namespace, "/").concat(type, "/").concat(key), argObj[key]);
|
410 | }).reduce(this._reduce, {});
|
411 | }
|
412 | }]);
|
413 |
|
414 | return ModelHandler;
|
415 | }();
|
416 |
|
417 | var _default = ModelHandler;
|
418 | exports.default = _default; |
\ | No newline at end of file |