UNPKG

47.3 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory();
4 else if(typeof define === 'function' && define.amd)
5 define("redux-app", [], factory);
6 else if(typeof exports === 'object')
7 exports["redux-app"] = factory();
8 else
9 root["redux-app"] = factory();
10})(typeof self !== 'undefined' ? self : this, function() {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14/******/
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17/******/
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId]) {
20/******/ return installedModules[moduleId].exports;
21/******/ }
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ i: moduleId,
25/******/ l: false,
26/******/ exports: {}
27/******/ };
28/******/
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31/******/
32/******/ // Flag the module as loaded
33/******/ module.l = true;
34/******/
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38/******/
39/******/
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42/******/
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45/******/
46/******/ // define getter function for harmony exports
47/******/ __webpack_require__.d = function(exports, name, getter) {
48/******/ if(!__webpack_require__.o(exports, name)) {
49/******/ Object.defineProperty(exports, name, {
50/******/ configurable: false,
51/******/ enumerable: true,
52/******/ get: getter
53/******/ });
54/******/ }
55/******/ };
56/******/
57/******/ // getDefaultExport function for compatibility with non-harmony modules
58/******/ __webpack_require__.n = function(module) {
59/******/ var getter = module && module.__esModule ?
60/******/ function getDefault() { return module['default']; } :
61/******/ function getModuleExports() { return module; };
62/******/ __webpack_require__.d(getter, 'a', getter);
63/******/ return getter;
64/******/ };
65/******/
66/******/ // Object.prototype.hasOwnProperty.call
67/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
68/******/
69/******/ // __webpack_public_path__
70/******/ __webpack_require__.p = "";
71/******/
72/******/ // Load entry module and return exports
73/******/ return __webpack_require__(__webpack_require__.s = 0);
74/******/ })
75/************************************************************************/
76/******/ ([
77/* 0 */
78/***/ (function(module, exports, __webpack_require__) {
79
80module.exports = __webpack_require__(1);
81
82
83/***/ }),
84/* 1 */
85/***/ (function(module, __webpack_exports__, __webpack_require__) {
86
87"use strict";
88Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
89
90// CONCATENATED MODULE: ./src/symbols.ts
91function isSymbol(obj) {
92 return typeof obj === 'symbol' || obj instanceof Symbol;
93}
94function setSymbol(obj, symbol, value) {
95 return obj[symbol] = value;
96}
97function getSymbol(obj, symbol) {
98 return obj[symbol];
99}
100function getOwnSymbol(obj, symbol) {
101 return Object.getOwnPropertySymbols(obj).includes(symbol) && getSymbol(obj, symbol);
102}
103var COMPONENT_INFO = Symbol('REDUX-APP.COMPONENT_INFO');
104var COMPONENT_TEMPLATE_INFO = Symbol('REDUX-APP.COMPONENT_TEMPLATE_INFO');
105var CLASS_INFO = Symbol('REDUX-APP.CLASS_INFO');
106var AUTO_ID = Symbol('REDUX-APP.AUTO_ID');
107
108// CONCATENATED MODULE: ./src/info/classInfo.ts
109
110var classInfo_ClassInfo = (function () {
111 function ClassInfo() {
112 this.ignoreState = {};
113 }
114 ClassInfo.getInfo = function (obj) {
115 if (!obj)
116 return undefined;
117 return getSymbol(obj, CLASS_INFO);
118 };
119 ClassInfo.getOrInitInfo = function (obj) {
120 var info = ClassInfo.getInfo(obj);
121 if (!info) {
122 info = setSymbol(obj, CLASS_INFO, new ClassInfo());
123 }
124 return info;
125 };
126 return ClassInfo;
127}());
128
129
130// CONCATENATED MODULE: ./src/info/componentInfo.ts
131
132var componentInfo_ComponentInfo = (function () {
133 function ComponentInfo(template, dispatch, id) {
134 this.originalClass = template.constructor;
135 this.dispatch = dispatch;
136 this.id = id;
137 }
138 ComponentInfo.getInfo = function (component) {
139 if (!component)
140 return undefined;
141 return getSymbol(component, COMPONENT_INFO);
142 };
143 ComponentInfo.initInfo = function (component, template, dispatch, id) {
144 var info = new ComponentInfo(template, dispatch, id);
145 return setSymbol(component, COMPONENT_INFO, info);
146 };
147 return ComponentInfo;
148}());
149
150
151// CONCATENATED MODULE: ./src/options.ts
152var ActionOptions = (function () {
153 function ActionOptions() {
154 this.actionNamespace = true;
155 this.actionNamespaceSeparator = '.';
156 this.uppercaseActions = false;
157 }
158 return ActionOptions;
159}());
160
161var AppOptions = (function () {
162 function AppOptions() {
163 this.updateState = true;
164 }
165 return AppOptions;
166}());
167
168var LogLevel;
169(function (LogLevel) {
170 LogLevel[LogLevel["None"] = 0] = "None";
171 LogLevel[LogLevel["Verbose"] = 1] = "Verbose";
172 LogLevel[LogLevel["Debug"] = 2] = "Debug";
173 LogLevel[LogLevel["Warn"] = 5] = "Warn";
174 LogLevel[LogLevel["Silent"] = 10] = "Silent";
175})(LogLevel || (LogLevel = {}));
176var GlobalOptions = (function () {
177 function GlobalOptions() {
178 this.logLevel = LogLevel.Warn;
179 this.action = new ActionOptions();
180 }
181 return GlobalOptions;
182}());
183
184var globalOptions = new GlobalOptions();
185
186// CONCATENATED MODULE: ./src/utils/log.ts
187
188var log_Log = (function () {
189 function Log() {
190 }
191 Log.prototype.verbose = function (message) {
192 var optionalParams = [];
193 for (var _i = 1; _i < arguments.length; _i++) {
194 optionalParams[_i - 1] = arguments[_i];
195 }
196 if (!this.shouldLog(LogLevel.Verbose))
197 return;
198 console.debug.apply(console, ['VERBOSE [redux-app] ' + message].concat(optionalParams));
199 };
200 Log.prototype.debug = function (message) {
201 var optionalParams = [];
202 for (var _i = 1; _i < arguments.length; _i++) {
203 optionalParams[_i - 1] = arguments[_i];
204 }
205 if (!this.shouldLog(LogLevel.Debug))
206 return;
207 console.log.apply(console, ['DEBUG [redux-app] ' + message].concat(optionalParams));
208 };
209 Log.prototype.warn = function (message) {
210 var optionalParams = [];
211 for (var _i = 1; _i < arguments.length; _i++) {
212 optionalParams[_i - 1] = arguments[_i];
213 }
214 if (!this.shouldLog(LogLevel.Warn))
215 return;
216 console.warn.apply(console, ['WARN [redux-app] ' + message].concat(optionalParams));
217 };
218 Log.prototype.shouldLog = function (level) {
219 if (globalOptions.logLevel === LogLevel.None)
220 return false;
221 if (globalOptions.logLevel > level)
222 return false;
223 return true;
224 };
225 return Log;
226}());
227var log = new log_Log();
228
229// CONCATENATED MODULE: ./src/utils/simpleCombineReducers.ts
230function simpleCombineReducers(reducers) {
231 var reducerKeys = Object.keys(reducers);
232 return function combination(state, action) {
233 if (state === void 0) { state = {}; }
234 var hasChanged = false;
235 var nextState = {};
236 for (var _i = 0, reducerKeys_1 = reducerKeys; _i < reducerKeys_1.length; _i++) {
237 var key = reducerKeys_1[_i];
238 var reducer = reducers[key];
239 var previousStateForKey = state[key];
240 var nextStateForKey = reducer(previousStateForKey, action);
241 nextState[key] = nextStateForKey;
242 hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
243 }
244 return hasChanged ? nextState : state;
245 };
246}
247
248// CONCATENATED MODULE: ./src/utils/utils.ts
249
250function clearProperties(obj) {
251 var keys = Object.keys(obj);
252 for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
253 var key = keys_1[_i];
254 delete obj[key];
255 }
256}
257var DescriptorType;
258(function (DescriptorType) {
259 DescriptorType["None"] = "None";
260 DescriptorType["Field"] = "Field";
261 DescriptorType["Property"] = "Property";
262 DescriptorType["Method"] = "Method";
263})(DescriptorType || (DescriptorType = {}));
264function defineProperties(target, source, descriptorTypes) {
265 var descriptors = getAllPropertyDescriptors(source, descriptorTypes);
266 for (var _i = 0, _a = Object.keys(descriptors); _i < _a.length; _i++) {
267 var key = _a[_i];
268 Object.defineProperty(target, key, descriptors[key]);
269 }
270 return target;
271}
272function getAllPropertyDescriptors(obj, descriptorTypes) {
273 var result = {};
274 while (obj.constructor !== Object) {
275 var descriptors = Object.getOwnPropertyDescriptors(obj);
276 if (descriptorTypes && descriptorTypes.length) {
277 var filteredDescriptors = {};
278 for (var _i = 0, _a = Object.keys(descriptors); _i < _a.length; _i++) {
279 var key = _a[_i];
280 for (var _b = 0, descriptorTypes_1 = descriptorTypes; _b < descriptorTypes_1.length; _b++) {
281 var flag = descriptorTypes_1[_b];
282 var shouldAdd = false;
283 switch (flag) {
284 case DescriptorType.None:
285 break;
286 case DescriptorType.Field:
287 shouldAdd = (typeof descriptors[key].value !== 'function' && typeof descriptors[key].get !== 'function');
288 break;
289 case DescriptorType.Property:
290 shouldAdd = (typeof descriptors[key].get === 'function');
291 break;
292 case DescriptorType.Method:
293 shouldAdd = (typeof descriptors[key].value === 'function' && typeof descriptors[key].get !== 'function');
294 break;
295 default:
296 throw new Error("Property flag not supported: " + flag);
297 }
298 if (shouldAdd)
299 filteredDescriptors[key] = descriptors[key];
300 }
301 }
302 descriptors = filteredDescriptors;
303 }
304 result = Object.assign(descriptors, result);
305 obj = getPrototype(obj);
306 }
307 if (result.constructor)
308 delete result.constructor;
309 return result;
310}
311function getConstructorProp(obj, key) {
312 if (!obj || !obj.constructor)
313 return undefined;
314 var ctor = obj.constructor;
315 return ctor[key];
316}
317function getConstructorOwnProp(obj, key) {
318 if (!obj || !obj.constructor)
319 return undefined;
320 var ctor = obj.constructor;
321 if (isSymbol(key) && Object.getOwnPropertySymbols(ctor).includes(key)) {
322 return ctor[key];
323 }
324 else if (typeof key === 'string' && Object.getOwnPropertyNames(ctor).includes(key)) {
325 return ctor[key];
326 }
327 return undefined;
328}
329function getMethods(obj, bind) {
330 if (bind === void 0) { bind = false; }
331 var methodDescriptors = getAllPropertyDescriptors(obj, [DescriptorType.Method]);
332 var methods = {};
333 for (var _i = 0, _a = Object.keys(methodDescriptors); _i < _a.length; _i++) {
334 var key = _a[_i];
335 methods[key] = methodDescriptors[key].value;
336 if (bind) {
337 methods[key] = methods[key].bind(obj);
338 }
339 }
340 return methods;
341}
342function getPrototype(obj) {
343 if (typeof obj === 'object') {
344 return Object.getPrototypeOf(obj);
345 }
346 else if (typeof obj === 'function') {
347 return obj.prototype;
348 }
349 else {
350 throw new Error("Expected an object or a function. Got: " + obj);
351 }
352}
353function isPrimitive(val) {
354 if (!val)
355 return true;
356 var type = typeof val;
357 return type !== 'object' && type !== 'function';
358}
359
360// CONCATENATED MODULE: ./src/utils/index.ts
361
362
363
364
365// CONCATENATED MODULE: ./src/info/componentTemplateInfo.ts
366
367
368var componentTemplateInfo_ComponentTemplateInfo = (function () {
369 function ComponentTemplateInfo() {
370 this.actions = {};
371 this.sequences = {};
372 this.childIds = {};
373 }
374 ComponentTemplateInfo.getInfo = function (obj) {
375 if (!obj)
376 return undefined;
377 var ownInfo = ComponentTemplateInfo.getOwnInfo(obj);
378 if (ownInfo)
379 return ownInfo;
380 var baseInfo = ComponentTemplateInfo.getBaseInfo(obj);
381 if (baseInfo)
382 return ComponentTemplateInfo.initInfo(obj);
383 return undefined;
384 };
385 ComponentTemplateInfo.getOrInitInfo = function (obj) {
386 var info = ComponentTemplateInfo.getInfo(obj);
387 if (info)
388 return info;
389 return ComponentTemplateInfo.initInfo(obj);
390 };
391 ComponentTemplateInfo.getOwnInfo = function (obj) {
392 if (typeof obj === 'object') {
393 return getConstructorOwnProp(obj, COMPONENT_TEMPLATE_INFO);
394 }
395 else {
396 return getOwnSymbol(obj, COMPONENT_TEMPLATE_INFO);
397 }
398 };
399 ComponentTemplateInfo.getBaseInfo = function (obj) {
400 if (typeof obj === 'object') {
401 return getConstructorProp(obj, COMPONENT_TEMPLATE_INFO);
402 }
403 else {
404 return getSymbol(obj, COMPONENT_TEMPLATE_INFO);
405 }
406 };
407 ComponentTemplateInfo.initInfo = function (obj) {
408 var isConstructor = (typeof obj === 'function' ? true : false);
409 var target = (isConstructor ? obj : obj.constructor);
410 var baseInfo = getSymbol(target, COMPONENT_TEMPLATE_INFO);
411 var selfInfo = Object.assign(new ComponentTemplateInfo(), baseInfo);
412 return setSymbol(target, COMPONENT_TEMPLATE_INFO, selfInfo);
413 };
414 return ComponentTemplateInfo;
415}());
416
417
418// CONCATENATED MODULE: ./src/info/index.ts
419
420
421
422
423// CONCATENATED MODULE: ./src/decorators/action.ts
424
425function action_action(target, propertyKey) {
426 var info = componentTemplateInfo_ComponentTemplateInfo.getOrInitInfo(target);
427 info.actions[propertyKey] = true;
428}
429
430// CONCATENATED MODULE: ./src/decorators/ignoreState.ts
431
432function ignoreState(target, propertyKey) {
433 var info = classInfo_ClassInfo.getOrInitInfo(target);
434 info.ignoreState[propertyKey] = true;
435}
436var ignoreState_IgnoreState = (function () {
437 function IgnoreState() {
438 }
439 IgnoreState.isIgnoredProperty = function (propHolder, propKey) {
440 var info = classInfo_ClassInfo.getInfo(propHolder);
441 return info && info.ignoreState[propKey];
442 };
443 IgnoreState.removeIgnoredProps = function (state, obj) {
444 var info = classInfo_ClassInfo.getInfo(obj);
445 if (!info)
446 return state;
447 for (var _i = 0, _a = Object.keys(info.ignoreState); _i < _a.length; _i++) {
448 var propKey = _a[_i];
449 delete state[propKey];
450 }
451 return state;
452 };
453 return IgnoreState;
454}());
455
456
457// CONCATENATED MODULE: ./src/decorators/sequence.ts
458
459function sequence(target, propertyKey) {
460 var info = componentTemplateInfo_ComponentTemplateInfo.getOrInitInfo(target);
461 info.sequences[propertyKey] = true;
462}
463
464// CONCATENATED MODULE: ./src/decorators/withId.ts
465
466
467
468function withId(targetOrId, propertyKeyOrNothing) {
469 if (propertyKeyOrNothing) {
470 withIdDecorator.call(undefined, targetOrId, propertyKeyOrNothing);
471 }
472 else {
473 return function (target, propertyKey) { return withIdDecorator(target, propertyKey, targetOrId); };
474 }
475}
476function withIdDecorator(target, propertyKey, id) {
477 var info = componentTemplateInfo_ComponentTemplateInfo.getOrInitInfo(target);
478 info.childIds[propertyKey] = id || AUTO_ID;
479}
480var withId_ComponentId = (function () {
481 function ComponentId() {
482 }
483 ComponentId.nextAvailableId = function () {
484 return --ComponentId.autoComponentId;
485 };
486 ComponentId.getComponentId = function (parentTemplate, path) {
487 var pathArray = path.split('.');
488 if (!parentTemplate || !pathArray.length)
489 return undefined;
490 var info = componentTemplateInfo_ComponentTemplateInfo.getInfo(parentTemplate);
491 if (!info)
492 return;
493 var selfKey = pathArray[pathArray.length - 1];
494 var id = info.childIds[selfKey];
495 if (!id)
496 return undefined;
497 if (id === AUTO_ID) {
498 var generatedId = ComponentId.nextAvailableId();
499 log.verbose('[getComponentId] new component id generated: ' + generatedId);
500 info.childIds[selfKey] = generatedId;
501 return generatedId;
502 }
503 return id;
504 };
505 ComponentId.autoComponentId = 0;
506 return ComponentId;
507}());
508
509
510// CONCATENATED MODULE: ./src/decorators/index.ts
511
512
513
514
515
516// EXTERNAL MODULE: external "redux"
517var external__redux_ = __webpack_require__(2);
518var external__redux__default = /*#__PURE__*/__webpack_require__.n(external__redux_);
519
520// CONCATENATED MODULE: ./src/reduxApp.ts
521var __assign = (this && this.__assign) || Object.assign || function(t) {
522 for (var s, i = 1, n = arguments.length; i < n; i++) {
523 s = arguments[i];
524 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
525 t[p] = s[p];
526 }
527 return t;
528};
529
530
531
532
533
534
535var getProp = __webpack_require__(3);
536var ROOT_COMPONENT_PATH = 'root';
537var DEFAULT_APP_NAME = 'default';
538var appsRepository = {};
539var appsCount = 0;
540var UpdateContext = (function () {
541 function UpdateContext(initial) {
542 this.visited = new Set();
543 this.path = ROOT_COMPONENT_PATH;
544 this.forceRecursion = false;
545 Object.assign(this, initial);
546 }
547 return UpdateContext;
548}());
549var reduxApp_ReduxApp = (function () {
550 function ReduxApp(appTemplate) {
551 var params = [];
552 for (var _i = 1; _i < arguments.length; _i++) {
553 params[_i - 1] = arguments[_i];
554 }
555 this.warehouse = new Map();
556 this.initialStateUpdated = false;
557 var _a = this.resolveParameters(appTemplate, params), options = _a.options, preLoadedState = _a.preLoadedState, enhancer = _a.enhancer;
558 this.name = this.getAppName(options.name);
559 if (appsRepository[this.name])
560 throw new Error("An app with name '" + this.name + "' already exists.");
561 appsRepository[this.name] = this;
562 var initialReducer = function (state) { return state; };
563 this.store = Object(external__redux_["createStore"])(initialReducer, preLoadedState, enhancer);
564 var creationContext = new component_ComponentCreationContext({ appName: this.name });
565 var rootComponent = component_Component.create(this.store, appTemplate, creationContext);
566 this.root = rootComponent;
567 this.registerComponents(creationContext.createdComponents);
568 var reducersContext = new reducer_CombineReducersContext({
569 componentPaths: Object.keys(creationContext.createdComponents)
570 });
571 var rootReducer = reducer_ComponentReducer.combineReducersTree(this.root, reducersContext);
572 if (options.updateState) {
573 var stateListener = this.updateState(reducersContext);
574 this.subscriptionDisposer = this.store.subscribe(stateListener);
575 }
576 this.store.replaceReducer(rootReducer);
577 }
578 ReduxApp.createApp = function (appTemplate) {
579 var params = [];
580 for (var _i = 1; _i < arguments.length; _i++) {
581 params[_i - 1] = arguments[_i];
582 }
583 return new (ReduxApp.bind.apply(ReduxApp, [void 0, appTemplate].concat(params)))();
584 };
585 ReduxApp.getComponent = function (type, componentId, appId) {
586 var app = ReduxApp.getApp(appId);
587 if (!app)
588 throw new Error("App not found (id: '" + (appId || DEFAULT_APP_NAME) + "')");
589 var warehouse = app.getTypeWarehouse(type);
590 if (componentId) {
591 var comp = warehouse.get(componentId);
592 if (!comp)
593 throw new Error("Component not found. Type: " + type.name + ". Id: '" + componentId + "'.");
594 return comp;
595 }
596 else {
597 var comp = warehouse.values().next().value;
598 if (!comp)
599 throw new Error("Component not found. Type: " + type.name + ".");
600 return comp;
601 }
602 };
603 ReduxApp.getApp = function (appId) {
604 var applicationId = appId || DEFAULT_APP_NAME;
605 var app = appsRepository[applicationId];
606 if (!app)
607 log.debug("[ReduxApp] Application '" + applicationId + "' does not exist.");
608 return app;
609 };
610 ReduxApp.prototype.dispose = function () {
611 if (this.subscriptionDisposer) {
612 this.subscriptionDisposer();
613 this.subscriptionDisposer = null;
614 }
615 if (appsRepository[this.name]) {
616 delete appsRepository[this.name];
617 }
618 };
619 ReduxApp.prototype.resolveParameters = function (appTemplate, params) {
620 var result = {};
621 if (params.length === 0) {
622 result.options = new AppOptions();
623 result.preLoadedState = appTemplate;
624 }
625 else if (params.length === 1) {
626 if (typeof params[0] === 'function') {
627 result.options = new AppOptions();
628 result.enhancer = params[0];
629 result.preLoadedState = appTemplate;
630 }
631 else {
632 result.options = Object.assign(new AppOptions(), params[0]);
633 result.preLoadedState = appTemplate;
634 }
635 }
636 else if (params.length === 2) {
637 result.options = Object.assign(new AppOptions(), params[0]);
638 result.preLoadedState = params[1];
639 }
640 else {
641 result.options = Object.assign(new AppOptions(), params[0]);
642 result.preLoadedState = params[1];
643 result.enhancer = params[2];
644 }
645 return result;
646 };
647 ReduxApp.prototype.getAppName = function (name) {
648 if (name)
649 return name;
650 if (!Object.keys(appsRepository).length) {
651 return DEFAULT_APP_NAME;
652 }
653 else {
654 return DEFAULT_APP_NAME + '_' + (++appsCount);
655 }
656 };
657 ReduxApp.prototype.registerComponents = function (components) {
658 for (var _i = 0, _a = Object.values(components); _i < _a.length; _i++) {
659 var comp = _a[_i];
660 var compInfo = componentInfo_ComponentInfo.getInfo(comp);
661 var warehouse = this.getTypeWarehouse(compInfo.originalClass);
662 var key = compInfo.id || withId_ComponentId.nextAvailableId();
663 warehouse.set(key, comp);
664 }
665 };
666 ReduxApp.prototype.getTypeWarehouse = function (type) {
667 if (!this.warehouse.has(type))
668 this.warehouse.set(type, new Map());
669 return this.warehouse.get(type);
670 };
671 ReduxApp.prototype.updateState = function (reducersContext) {
672 var _this = this;
673 return function () {
674 var start = Date.now();
675 var newState = _this.store.getState();
676 if (!_this.initialStateUpdated || !reducersContext.invoked) {
677 _this.initialStateUpdated = true;
678 _this.updateStateRecursion(_this.root, newState, new UpdateContext({ forceRecursion: true }));
679 }
680 else {
681 _this.updateChangedComponents((_a = {}, _a[ROOT_COMPONENT_PATH] = newState, _a), reducersContext.changedComponents);
682 }
683 reducersContext.reset();
684 var end = Date.now();
685 log.debug("[updateState] Component tree updated in " + (end - start) + "ms.");
686 var _a;
687 };
688 };
689 ReduxApp.prototype.updateChangedComponents = function (newState, changedComponents) {
690 var changedPaths = Object.keys(changedComponents);
691 var updateContext = new UpdateContext();
692 for (var _i = 0, changedPaths_1 = changedPaths; _i < changedPaths_1.length; _i++) {
693 var path = changedPaths_1[_i];
694 var curComponent = changedComponents[path];
695 var newSubState = getProp(newState, path);
696 this.updateStateRecursion(curComponent, newSubState, __assign({}, updateContext, { path: path }));
697 }
698 };
699 ReduxApp.prototype.updateStateRecursion = function (obj, newState, context) {
700 if (obj === newState)
701 return newState;
702 if (isPrimitive(obj) || isPrimitive(newState))
703 return newState;
704 if (context.visited.has(obj))
705 return obj;
706 context.visited.add(obj);
707 if (context.forceRecursion || (obj instanceof component_Component)) {
708 var changeMessage;
709 if (Array.isArray(obj) && Array.isArray(newState)) {
710 changeMessage = this.updateArray(obj, newState, context);
711 }
712 else {
713 changeMessage = this.updateObject(obj, newState, context);
714 }
715 }
716 else {
717 obj = newState;
718 changeMessage = 'Object overwritten.';
719 }
720 if (changeMessage && changeMessage.length) {
721 log.debug("[updateState] Change in '" + context.path + "'. " + changeMessage);
722 log.verbose("[updateState] New state: ", obj);
723 }
724 return obj;
725 };
726 ReduxApp.prototype.updateObject = function (obj, newState, context) {
727 var propsDeleted = [];
728 for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
729 var key = _a[_i];
730 if (ignoreState_IgnoreState.isIgnoredProperty(obj, key))
731 continue;
732 if (!newState.hasOwnProperty(key)) {
733 var desc = Object.getOwnPropertyDescriptor(obj, key);
734 if (desc && typeof desc.get === 'function')
735 continue;
736 if (typeof obj[key] === 'function')
737 log.warn("[updateState] Function property removed in path: " + context.path + "." + key + ". Consider using a method instead.");
738 delete obj[key];
739 propsDeleted.push(key);
740 }
741 }
742 var propsAssigned = [];
743 for (var _b = 0, _c = Object.keys(newState); _b < _c.length; _b++) {
744 var key = _c[_b];
745 if (ignoreState_IgnoreState.isIgnoredProperty(obj, key))
746 continue;
747 var desc = Object.getOwnPropertyDescriptor(obj, key);
748 if (desc && typeof desc.get === 'function' && typeof desc.set !== 'function')
749 continue;
750 var subState = newState[key];
751 var subObj = obj[key];
752 var newSubObj = this.updateStateRecursion(subObj, subState, __assign({}, context, { path: context.path + '.' + key }));
753 if (newSubObj !== subObj) {
754 obj[key] = newSubObj;
755 propsAssigned.push(key);
756 }
757 }
758 if (propsAssigned.length || propsDeleted.length) {
759 var propsAssignedMessage = propsAssigned.length ? "Props assigned: " + propsAssigned.join(', ') + "." : '';
760 var propsDeleteMessage = propsDeleted.length ? "Props deleted: " + propsDeleted.join(', ') + "." : '';
761 var space = (propsAssigned.length && propsDeleted.length) ? ' ' : '';
762 return propsAssignedMessage + space + propsDeleteMessage;
763 }
764 else {
765 return null;
766 }
767 };
768 ReduxApp.prototype.updateArray = function (arr, newState, context) {
769 var changeMessage = [];
770 var prevLength = arr.length;
771 var newLength = newState.length;
772 var itemsAssigned = [];
773 for (var i = 0; i < Math.min(prevLength, newLength); i++) {
774 var subState = newState[i];
775 var subObj = arr[i];
776 var newSubObj = this.updateStateRecursion(subObj, subState, __assign({}, context, { path: context.path + '.' + i }));
777 if (newSubObj !== subObj) {
778 arr[i] = newSubObj;
779 itemsAssigned.push(i);
780 }
781 }
782 if (itemsAssigned.length)
783 changeMessage.push("Assigned item(s) at indexes " + itemsAssigned.join(', ') + ".");
784 if (newLength > prevLength) {
785 var newItems = newState.slice(prevLength);
786 Array.prototype.push.apply(arr, newItems);
787 changeMessage.push("Added " + (newLength - prevLength) + " item(s) at index " + prevLength + ".");
788 }
789 else if (prevLength > newLength) {
790 arr.splice(newLength);
791 changeMessage.push("Removed " + (prevLength - newLength) + " item(s) at index " + newLength + ".");
792 }
793 return changeMessage.join(' ');
794 };
795 ReduxApp.options = globalOptions;
796 return ReduxApp;
797}());
798
799
800// CONCATENATED MODULE: ./src/components/reducer.ts
801var reducer___assign = (this && this.__assign) || Object.assign || function(t) {
802 for (var s, i = 1, n = arguments.length; i < n; i++) {
803 s = arguments[i];
804 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
805 t[p] = s[p];
806 }
807 return t;
808};
809
810
811
812
813
814var reducer_CombineReducersContext = (function () {
815 function CombineReducersContext(initial) {
816 this.visited = new Set();
817 this.path = ROOT_COMPONENT_PATH;
818 this.componentPaths = [];
819 this.changedComponents = {};
820 this.invoked = false;
821 Object.assign(this, initial);
822 }
823 CombineReducersContext.prototype.reset = function () {
824 clearProperties(this.changedComponents);
825 this.invoked = false;
826 };
827 return CombineReducersContext;
828}());
829
830var reducer_ComponentReducer = (function () {
831 function ComponentReducer() {
832 }
833 ComponentReducer.createReducer = function (component, componentTemplate) {
834 var templateInfo = componentTemplateInfo_ComponentTemplateInfo.getInfo(componentTemplate);
835 if (!templateInfo)
836 throw new Error("Inconsistent component '" + componentTemplate.constructor.name + "'. The 'component' class decorator is missing.");
837 var methods = ComponentReducer.createMethodsLookup(componentTemplate, templateInfo);
838 var stateProto = ComponentReducer.createStateObjectPrototype(component, templateInfo);
839 var componentId = componentInfo_ComponentInfo.getInfo(component).id;
840 return function (changeListener) {
841 return function (state, action) {
842 log.verbose("[reducer] Reducer of: " + componentTemplate.constructor.name + ", action: " + action.type + ".");
843 if (state === undefined) {
844 log.verbose('[reducer] State is undefined, returning initial value.');
845 return ComponentReducer.finalizeStateObject(component, component);
846 }
847 if (state === componentTemplate) {
848 log.verbose("[reducer] State equals to component's template, returning initial value.");
849 return ComponentReducer.finalizeStateObject(component, component);
850 }
851 if (componentId !== action.id) {
852 log.verbose("[reducer] Component id and action.id don't match (" + componentId + " !== " + action.id + ").");
853 return state;
854 }
855 var actionReducer = methods[action.type];
856 if (!actionReducer) {
857 log.verbose('[reducer] No matching action in this reducer, returning previous state.');
858 return state;
859 }
860 var newState = ComponentReducer.createStateObject(state, stateProto);
861 actionReducer.call.apply(actionReducer, [newState].concat(action.payload));
862 changeListener(component);
863 log.verbose('[reducer] Reducer invoked, returning new state.');
864 return ComponentReducer.finalizeStateObject(newState, component);
865 };
866 };
867 };
868 ComponentReducer.combineReducersTree = function (root, context) {
869 var reducer = ComponentReducer.combineReducersRecursion(root, context);
870 return function (state, action) {
871 var start = Date.now();
872 context.invoked = true;
873 log.debug("[rootReducer] Reducing action: " + action.type + ".");
874 var newState = reducer(state, action);
875 var end = Date.now();
876 log.debug("[rootReducer] Reducer tree processed in " + (end - start) + "ms.");
877 return newState;
878 };
879 };
880 ComponentReducer.createMethodsLookup = function (componentTemplate, templateInfo) {
881 var allMethods = getMethods(componentTemplate);
882 var actionMethods = {};
883 Object.keys(templateInfo.actions).forEach(function (originalActionName) {
884 var normalizedActionName = actions_ComponentActions.getActionName(componentTemplate, originalActionName);
885 actionMethods[normalizedActionName] = allMethods[originalActionName];
886 });
887 return actionMethods;
888 };
889 ComponentReducer.createStateObjectPrototype = function (component, templateInfo) {
890 var stateProto = defineProperties({}, component, [DescriptorType.Property]);
891 var componentMethods = getMethods(component);
892 for (var _i = 0, _a = Object.keys(componentMethods); _i < _a.length; _i++) {
893 var key = _a[_i];
894 if (!templateInfo.actions[key]) {
895 stateProto[key] = componentMethods[key].bind(component);
896 }
897 else {
898 stateProto[key] = ComponentReducer.actionInvokedError;
899 }
900 }
901 return stateProto;
902 };
903 ComponentReducer.actionInvokedError = function () {
904 throw new Error("Actions should not be invoked from within other actions.");
905 };
906 ComponentReducer.createStateObject = function (state, stateProto) {
907 var stateObj = Object.create(stateProto);
908 for (var _i = 0, _a = Object.keys(state); _i < _a.length; _i++) {
909 var key = _a[_i];
910 var desc = Object.getOwnPropertyDescriptor(stateProto, key);
911 if (desc && typeof desc.get === 'function' && typeof desc.set !== 'function')
912 continue;
913 stateObj[key] = state[key];
914 }
915 return stateObj;
916 };
917 ComponentReducer.finalizeStateObject = function (state, component) {
918 log.verbose('[finalizeStateObject] finalizing state.');
919 var finalizedState = Object.assign({}, state);
920 finalizedState = ignoreState_IgnoreState.removeIgnoredProps(finalizedState, component);
921 log.verbose('[finalizeStateObject] state finalized.');
922 return finalizedState;
923 };
924 ComponentReducer.combineReducersRecursion = function (obj, context) {
925 if (isPrimitive(obj))
926 return undefined;
927 if (context.visited.has(obj))
928 return undefined;
929 context.visited.add(obj);
930 if (!context.componentPaths.some(function (path) { return path.startsWith(context.path); }))
931 return ComponentReducer.identityReducer;
932 var rootReducer;
933 var info = componentInfo_ComponentInfo.getInfo(obj);
934 if (info) {
935 rootReducer = info.reducerCreator(function (comp) {
936 context.changedComponents[context.path] = comp;
937 });
938 }
939 else {
940 rootReducer = ComponentReducer.identityReducer;
941 }
942 var subReducers = {};
943 for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
944 var key = _a[_i];
945 var newSubReducer = ComponentReducer.combineReducersRecursion(obj[key], new reducer_CombineReducersContext(reducer___assign({}, context, { path: (context.path === '' ? key : context.path + '.' + key) })));
946 if (typeof newSubReducer === 'function')
947 subReducers[key] = newSubReducer;
948 }
949 var resultReducer = rootReducer;
950 if (Object.keys(subReducers).length) {
951 var combinedSubReducer_1 = simpleCombineReducers(subReducers);
952 resultReducer = function (state, action) {
953 var thisState = rootReducer(state, action);
954 var subStates = combinedSubReducer_1(thisState, action);
955 var combinedState = ComponentReducer.mergeState(thisState, subStates);
956 return combinedState;
957 };
958 }
959 return resultReducer;
960 };
961 ComponentReducer.mergeState = function (state, subStates) {
962 if (Array.isArray(state) && Array.isArray(subStates)) {
963 for (var i = 0; i < subStates.length; i++)
964 state[i] = subStates[i];
965 return state;
966 }
967 else {
968 return reducer___assign({}, state, subStates);
969 }
970 };
971 ComponentReducer.identityReducer = function (state) { return state; };
972 return ComponentReducer;
973}());
974
975
976// CONCATENATED MODULE: ./src/components/component.ts
977var component___assign = (this && this.__assign) || Object.assign || function(t) {
978 for (var s, i = 1, n = arguments.length; i < n; i++) {
979 s = arguments[i];
980 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
981 t[p] = s[p];
982 }
983 return t;
984};
985
986
987
988
989
990
991var component_ComponentCreationContext = (function () {
992 function ComponentCreationContext(initial) {
993 this.visitedNodes = new Set();
994 this.visitedTemplates = new Map();
995 this.path = ROOT_COMPONENT_PATH;
996 this.createdComponents = {};
997 Object.assign(this, initial);
998 }
999 return ComponentCreationContext;
1000}());
1001
1002var component_Component = (function () {
1003 function Component(store, template, context) {
1004 if (!componentTemplateInfo_ComponentTemplateInfo.getInfo(template))
1005 throw new Error("Argument '" + "template" + "' is not a component template. Did you forget to use the decorator?");
1006 Component.createSelf(this, store, template, context);
1007 context.createdComponents[context.path] = this;
1008 context.visitedTemplates.set(template, this);
1009 log.verbose("[Component] New " + template.constructor.name + " component created. Path: " + context.path);
1010 Component.createSubComponents(this, store, template, context);
1011 }
1012 Component.create = function (store, template, context) {
1013 context = Object.assign(new component_ComponentCreationContext(), context);
1014 componentTemplateInfo_ComponentTemplateInfo.getOrInitInfo(template);
1015 var ComponentClass = Component.getComponentClass(template);
1016 var component = new ComponentClass(store, template, context);
1017 return component;
1018 };
1019 Component.getComponentClass = function (template) {
1020 var info = componentTemplateInfo_ComponentTemplateInfo.getInfo(template);
1021 if (!info.componentClass) {
1022 info.componentClass = Component.createComponentClass(template);
1023 }
1024 return info.componentClass;
1025 };
1026 Component.createComponentClass = function (template) {
1027 var componentClassFactory = new Function('initCallback', "\"use strict\";return function " + template.constructor.name + "_ReduxAppComponent() { initCallback(this, arguments); }");
1028 var ComponentClass = componentClassFactory(function (self, args) { return Component.apply(self, args); });
1029 ComponentClass.prototype = Object.create(Component.prototype);
1030 ComponentClass.prototype.constructor = ComponentClass;
1031 var actions = actions_ComponentActions.createActions(template);
1032 Object.assign(ComponentClass.prototype, actions);
1033 return ComponentClass;
1034 };
1035 Component.createSelf = function (component, store, template, context) {
1036 defineProperties(component, template, [DescriptorType.Field, DescriptorType.Property]);
1037 var id = withId_ComponentId.getComponentId(context.parentTemplate, context.path);
1038 var selfInfo = componentInfo_ComponentInfo.initInfo(component, template, store.dispatch, id);
1039 var selfClassInfo = classInfo_ClassInfo.getOrInitInfo(component);
1040 var templateClassInfo = classInfo_ClassInfo.getInfo(template) || new classInfo_ClassInfo();
1041 selfClassInfo.ignoreState = templateClassInfo.ignoreState;
1042 selfInfo.reducerCreator = reducer_ComponentReducer.createReducer(component, template);
1043 };
1044 Component.createSubComponents = function (treeNode, store, template, context) {
1045 if (isPrimitive(treeNode))
1046 return;
1047 if (context.visitedNodes.has(treeNode))
1048 return;
1049 context.visitedNodes.add(treeNode);
1050 var searchIn = template || treeNode;
1051 for (var _i = 0, _a = Object.keys(searchIn); _i < _a.length; _i++) {
1052 var key = _a[_i];
1053 var subPath = context.path + '.' + key;
1054 var subTemplate = searchIn[key];
1055 if (componentTemplateInfo_ComponentTemplateInfo.getInfo(subTemplate)) {
1056 if (context.visitedTemplates.has(subTemplate)) {
1057 treeNode[key] = context.visitedTemplates.get(subTemplate);
1058 }
1059 else {
1060 treeNode[key] = Component.create(store, subTemplate, component___assign({}, context, { parentTemplate: template, path: subPath }));
1061 }
1062 }
1063 else {
1064 Component.createSubComponents(treeNode[key], store, null, component___assign({}, context, { parentTemplate: null, path: subPath }));
1065 }
1066 }
1067 };
1068 return Component;
1069}());
1070
1071
1072// CONCATENATED MODULE: ./src/components/actions.ts
1073
1074
1075
1076
1077var snakecase = __webpack_require__(4);
1078var actions_ComponentActions = (function () {
1079 function ComponentActions() {
1080 }
1081 ComponentActions.createActions = function (template) {
1082 var methods = getMethods(template);
1083 if (!methods)
1084 return undefined;
1085 var templateInfo = componentTemplateInfo_ComponentTemplateInfo.getInfo(template);
1086 var componentActions = {};
1087 Object.keys(methods).forEach(function (key) {
1088 componentActions[key] = function () {
1089 var payload = [];
1090 for (var _i = 0; _i < arguments.length; _i++) {
1091 payload[_i] = arguments[_i];
1092 }
1093 if (!(this instanceof component_Component))
1094 throw new Error("Component method invoked with non-Component as 'this'. Bound 'this' argument is: " + this);
1095 var oldMethod = methods[key];
1096 if (templateInfo.actions[key] || templateInfo.sequences[key]) {
1097 var compInfo = componentInfo_ComponentInfo.getInfo(this);
1098 var action = {
1099 type: ComponentActions.getActionName(template, key),
1100 id: (compInfo ? compInfo.id : undefined),
1101 payload: payload
1102 };
1103 compInfo.dispatch(action);
1104 }
1105 if (!templateInfo.actions[key]) {
1106 return oldMethod.call.apply(oldMethod, [this].concat(payload));
1107 }
1108 };
1109 });
1110 return componentActions;
1111 };
1112 ComponentActions.getActionName = function (template, methodName) {
1113 var options = Object.assign(new ActionOptions(), globalOptions.action);
1114 var actionName = methodName;
1115 var actionNamespace = template.constructor.name;
1116 if (options.uppercaseActions) {
1117 actionName = snakecase(actionName).toUpperCase();
1118 actionNamespace = snakecase(actionNamespace).toUpperCase();
1119 }
1120 if (options.actionNamespace) {
1121 actionName = actionNamespace + options.actionNamespaceSeparator + actionName;
1122 }
1123 return actionName;
1124 };
1125 return ComponentActions;
1126}());
1127
1128
1129// CONCATENATED MODULE: ./src/components/utils.ts
1130
1131function isInstanceOf(obj, type) {
1132 if (obj instanceof type)
1133 return true;
1134 var info = componentInfo_ComponentInfo.getInfo(obj);
1135 return !!(info && info.originalClass === type);
1136}
1137
1138// CONCATENATED MODULE: ./src/components/index.ts
1139
1140
1141
1142
1143
1144// CONCATENATED MODULE: ./src/index.ts
1145/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "isInstanceOf", function() { return isInstanceOf; });
1146/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "action", function() { return action_action; });
1147/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "ignoreState", function() { return ignoreState; });
1148/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "sequence", function() { return sequence; });
1149/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "withId", function() { return withId; });
1150/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "ActionOptions", function() { return ActionOptions; });
1151/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "AppOptions", function() { return AppOptions; });
1152/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "GlobalOptions", function() { return GlobalOptions; });
1153/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "LogLevel", function() { return LogLevel; });
1154/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "getMethods", function() { return getMethods; });
1155/* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "ReduxApp", function() { return reduxApp_ReduxApp; });
1156
1157
1158
1159
1160
1161
1162
1163/***/ }),
1164/* 2 */
1165/***/ (function(module, exports) {
1166
1167module.exports = require("redux");
1168
1169/***/ }),
1170/* 3 */
1171/***/ (function(module, exports) {
1172
1173module.exports = require("lodash.get");
1174
1175/***/ }),
1176/* 4 */
1177/***/ (function(module, exports) {
1178
1179module.exports = require("lodash.snakecase");
1180
1181/***/ })
1182/******/ ]);
1183});
1184//# sourceMappingURL=redux-app.js.map
\No newline at end of file