UNPKG

17.7 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("redux-app",[],e):"object"==typeof exports?exports["redux-app"]=e():t["redux-app"]=e()}("undefined"!=typeof self?self:this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){t.exports=n(1)},function(t,e,n){"use strict";function r(t){return"symbol"==typeof t||t instanceof Symbol}function o(t,e,n){return t[e]=n}function i(t,e){return t[e]}function a(t,e){return Object.getOwnPropertySymbols(t).includes(e)&&i(t,e)}function s(t){var e=Object.keys(t);return function(n,r){void 0===n&&(n={});for(var o=!1,i={},a=0,s=e;a<s.length;a++){var c=s[a],u=t[c],p=n[c],f=u(p,r);i[c]=f,o=o||f!==p}return o?i:n}}function c(t){for(var e=Object.keys(t),n=0,r=e;n<r.length;n++){delete t[r[n]]}}function u(t,e,n){for(var r=p(e,n),o=0,i=Object.keys(r);o<i.length;o++){var a=i[o];Object.defineProperty(t,a,r[a])}return t}function p(t,e){for(var n={};t.constructor!==Object;){var r=Object.getOwnPropertyDescriptors(t);if(e&&e.length){for(var o={},i=0,a=Object.keys(r);i<a.length;i++)for(var s=a[i],c=0,u=e;c<u.length;c++){var p=u[c],f=!1;switch(p){case N.None:break;case N.Field:f="function"!=typeof r[s].value&&"function"!=typeof r[s].get;break;case N.Property:f="function"==typeof r[s].get;break;case N.Method:f="function"==typeof r[s].value&&"function"!=typeof r[s].get;break;default:throw new Error("Property flag not supported: "+p)}f&&(o[s]=r[s])}r=o}n=Object.assign(r,n),t=l(t)}return n.constructor&&delete n.constructor,n}function f(t,e){if(t&&t.constructor){return t.constructor[e]}}function d(t,e){if(t&&t.constructor){var n=t.constructor;return r(e)&&Object.getOwnPropertySymbols(n).includes(e)?n[e]:"string"==typeof e&&Object.getOwnPropertyNames(n).includes(e)?n[e]:void 0}}function h(t,e){void 0===e&&(e=!1);for(var n=p(t,[N.Method]),r={},o=0,i=Object.keys(n);o<i.length;o++){var a=i[o];r[a]=n[a].value,e&&(r[a]=r[a].bind(t))}return r}function l(t){if("object"==typeof t)return Object.getPrototypeOf(t);if("function"==typeof t)return t.prototype;throw new Error("Expected an object or a function. Got: "+t)}function g(t){if(!t)return!0;var e=typeof t;return"object"!==e&&"function"!==e}function v(t,e){_.getOrInitInfo(t).actions[e]=!0}function y(t,e){A.getOrInitInfo(t).ignoreState[e]=!0}function b(t,e){_.getOrInitInfo(t).sequences[e]=!0}function m(t,e){if(!e)return function(e,n){return O(e,n,t)};O.call(void 0,t,e)}function O(t,e,n){_.getOrInitInfo(t).childIds[e]=n||P}function w(t,e){if(t instanceof e)return!0;var n=R.getInfo(t);return!(!n||n.originalClass!==e)}Object.defineProperty(e,"__esModule",{value:!0});var j,I=Symbol("REDUX-APP.COMPONENT_INFO"),S=Symbol("REDUX-APP.COMPONENT_TEMPLATE_INFO"),C=Symbol("REDUX-APP.CLASS_INFO"),P=Symbol("REDUX-APP.AUTO_ID"),A=function(){function t(){this.ignoreState={}}return t.getInfo=function(t){if(t)return i(t,C)},t.getOrInitInfo=function(e){var n=t.getInfo(e);return n||(n=o(e,C,new t)),n},t}(),R=function(){function t(t,e,n){this.originalClass=t.constructor,this.dispatch=e,this.id=n}return t.getInfo=function(t){if(t)return i(t,I)},t.initInfo=function(e,n,r,i){var a=new t(n,r,i);return o(e,I,a)},t}(),k=function(){function t(){this.actionNamespace=!0,this.actionNamespaceSeparator=".",this.uppercaseActions=!1}return t}(),x=function(){function t(){this.updateState=!0}return t}();!function(t){t[t.None=0]="None",t[t.Verbose=1]="Verbose",t[t.Debug=2]="Debug",t[t.Warn=5]="Warn",t[t.Silent=10]="Silent"}(j||(j={}));var N,E=function(){function t(){this.logLevel=j.Warn,this.action=new k}return t}(),D=new E,T=function(){function t(){}return t.prototype.verbose=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this.shouldLog(j.Verbose)&&console.debug.apply(console,["VERBOSE [redux-app] "+t].concat(e))},t.prototype.debug=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this.shouldLog(j.Debug)&&console.log.apply(console,["DEBUG [redux-app] "+t].concat(e))},t.prototype.warn=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this.shouldLog(j.Warn)&&console.warn.apply(console,["WARN [redux-app] "+t].concat(e))},t.prototype.shouldLog=function(t){return D.logLevel!==j.None&&!(D.logLevel>t)},t}(),L=new T;!function(t){t.None="None",t.Field="Field",t.Property="Property",t.Method="Method"}(N||(N={}));var _=function(){function t(){this.actions={},this.sequences={},this.childIds={}}return t.getInfo=function(e){if(e){var n=t.getOwnInfo(e);if(n)return n;return t.getBaseInfo(e)?t.initInfo(e):void 0}},t.getOrInitInfo=function(e){var n=t.getInfo(e);return n||t.initInfo(e)},t.getOwnInfo=function(t){return"object"==typeof t?d(t,S):a(t,S)},t.getBaseInfo=function(t){return"object"==typeof t?f(t,S):i(t,S)},t.initInfo=function(e){var n="function"==typeof e,r=n?e:e.constructor,a=i(r,S),s=Object.assign(new t,a);return o(r,S,s)},t}(),M=function(){function t(){}return t.isIgnoredProperty=function(t,e){var n=A.getInfo(t);return n&&n.ignoreState[e]},t.removeIgnoredProps=function(t,e){var n=A.getInfo(e);if(!n)return t;for(var r=0,o=Object.keys(n.ignoreState);r<o.length;r++){delete t[o[r]]}return t},t}(),U=function(){function t(){}return t.nextAvailableId=function(){return--t.autoComponentId},t.getComponentId=function(e,n){var r=n.split(".");if(e&&r.length){var o=_.getInfo(e);if(o){var i=r[r.length-1],a=o.childIds[i];if(a){if(a===P){var s=t.nextAvailableId();return L.verbose("[getComponentId] new component id generated: "+s),o.childIds[i]=s,s}return a}}}},t.autoComponentId=0,t}(),F=n(2),W=this&&this.__assign||Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++){e=arguments[n];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},q=n(3),z="root",B={},V=0,X=function(){function t(t){this.visited=new Set,this.path=z,this.forceRecursion=!1,Object.assign(this,t)}return t}(),G=function(){function t(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this.warehouse=new Map,this.initialStateUpdated=!1;var r=this.resolveParameters(t,e),o=r.options,i=r.preLoadedState,a=r.enhancer;if(this.name=this.getAppName(o.name),B[this.name])throw new Error("An app with name '"+this.name+"' already exists.");B[this.name]=this;var s=function(t){return t};this.store=Object(F.createStore)(s,i,a);var c=new Y({appName:this.name}),u=Z.create(this.store,t,c);this.root=u,this.registerComponents(c.createdComponents);var p=new J({componentPaths:Object.keys(c.createdComponents)}),f=K.combineReducersTree(this.root,p);if(o.updateState){var d=this.updateState(p);this.subscriptionDisposer=this.store.subscribe(d)}this.store.replaceReducer(f)}return t.createApp=function(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return new(t.bind.apply(t,[void 0,e].concat(n)))},t.getComponent=function(e,n,r){var o=t.getApp(r);if(!o)throw new Error("App not found (id: '"+(r||"default")+"')");var i=o.getTypeWarehouse(e);if(n){var a=i.get(n);if(!a)throw new Error("Component not found. Type: "+e.name+". Id: '"+n+"'.");return a}var a=i.values().next().value;if(!a)throw new Error("Component not found. Type: "+e.name+".");return a},t.getApp=function(t){var e=t||"default",n=B[e];return n||L.debug("[ReduxApp] Application '"+e+"' does not exist."),n},t.prototype.dispose=function(){this.subscriptionDisposer&&(this.subscriptionDisposer(),this.subscriptionDisposer=null),B[this.name]&&delete B[this.name]},t.prototype.resolveParameters=function(t,e){var n={};return 0===e.length?(n.options=new x,n.preLoadedState=t):1===e.length?"function"==typeof e[0]?(n.options=new x,n.enhancer=e[0],n.preLoadedState=t):(n.options=Object.assign(new x,e[0]),n.preLoadedState=t):2===e.length?(n.options=Object.assign(new x,e[0]),n.preLoadedState=e[1]):(n.options=Object.assign(new x,e[0]),n.preLoadedState=e[1],n.enhancer=e[2]),n},t.prototype.getAppName=function(t){return t||(Object.keys(B).length?"default_"+ ++V:"default")},t.prototype.registerComponents=function(t){for(var e=0,n=Object.values(t);e<n.length;e++){var r=n[e],o=R.getInfo(r),i=this.getTypeWarehouse(o.originalClass),a=o.id||U.nextAvailableId();i.set(a,r)}},t.prototype.getTypeWarehouse=function(t){return this.warehouse.has(t)||this.warehouse.set(t,new Map),this.warehouse.get(t)},t.prototype.updateState=function(t){var e=this;return function(){var n=Date.now(),r=e.store.getState();e.initialStateUpdated&&t.invoked?e.updateChangedComponents((i={},i[z]=r,i),t.changedComponents):(e.initialStateUpdated=!0,e.updateStateRecursion(e.root,r,new X({forceRecursion:!0}))),t.reset();var o=Date.now();L.debug("[updateState] Component tree updated in "+(o-n)+"ms.");var i}},t.prototype.updateChangedComponents=function(t,e){for(var n=Object.keys(e),r=new X,o=0,i=n;o<i.length;o++){var a=i[o],s=e[a],c=q(t,a);this.updateStateRecursion(s,c,W({},r,{path:a}))}},t.prototype.updateStateRecursion=function(t,e,n){if(t===e)return e;if(g(t)||g(e))return e;if(n.visited.has(t))return t;if(n.visited.add(t),n.forceRecursion||t instanceof Z){var r;r=Array.isArray(t)&&Array.isArray(e)?this.updateArray(t,e,n):this.updateObject(t,e,n)}else t=e,r="Object overwritten.";return r&&r.length&&(L.debug("[updateState] Change in '"+n.path+"'. "+r),L.verbose("[updateState] New state: ",t)),t},t.prototype.updateObject=function(t,e,n){for(var r=[],o=0,i=Object.keys(t);o<i.length;o++){var a=i[o];if(!M.isIgnoredProperty(t,a)&&!e.hasOwnProperty(a)){var s=Object.getOwnPropertyDescriptor(t,a);if(s&&"function"==typeof s.get)continue;"function"==typeof t[a]&&L.warn("[updateState] Function property removed in path: "+n.path+"."+a+". Consider using a method instead."),delete t[a],r.push(a)}}for(var c=[],u=0,p=Object.keys(e);u<p.length;u++){var a=p[u];if(!M.isIgnoredProperty(t,a)){var s=Object.getOwnPropertyDescriptor(t,a);if(!s||"function"!=typeof s.get||"function"==typeof s.set){var f=e[a],d=t[a],h=this.updateStateRecursion(d,f,W({},n,{path:n.path+"."+a}));h!==d&&(t[a]=h,c.push(a))}}}if(c.length||r.length){var l=c.length?"Props assigned: "+c.join(", ")+".":"",g=r.length?"Props deleted: "+r.join(", ")+".":"";return l+(c.length&&r.length?" ":"")+g}return null},t.prototype.updateArray=function(t,e,n){for(var r=[],o=t.length,i=e.length,a=[],s=0;s<Math.min(o,i);s++){var c=e[s],u=t[s],p=this.updateStateRecursion(u,c,W({},n,{path:n.path+"."+s}));p!==u&&(t[s]=p,a.push(s))}if(a.length&&r.push("Assigned item(s) at indexes "+a.join(", ")+"."),i>o){var f=e.slice(o);Array.prototype.push.apply(t,f),r.push("Added "+(i-o)+" item(s) at index "+o+".")}else o>i&&(t.splice(i),r.push("Removed "+(o-i)+" item(s) at index "+i+"."));return r.join(" ")},t.options=D,t}(),H=this&&this.__assign||Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++){e=arguments[n];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},J=function(){function t(t){this.visited=new Set,this.path=z,this.componentPaths=[],this.changedComponents={},this.invoked=!1,Object.assign(this,t)}return t.prototype.reset=function(){c(this.changedComponents),this.invoked=!1},t}(),K=function(){function t(){}return t.createReducer=function(e,n){var r=_.getInfo(n);if(!r)throw new Error("Inconsistent component '"+n.constructor.name+"'. The 'component' class decorator is missing.");var o=t.createMethodsLookup(n,r),i=t.createStateObjectPrototype(e,r),a=R.getInfo(e).id;return function(r){return function(s,c){if(L.verbose("[reducer] Reducer of: "+n.constructor.name+", action: "+c.type+"."),void 0===s)return L.verbose("[reducer] State is undefined, returning initial value."),t.finalizeStateObject(e,e);if(s===n)return L.verbose("[reducer] State equals to component's template, returning initial value."),t.finalizeStateObject(e,e);if(a!==c.id)return L.verbose("[reducer] Component id and action.id don't match ("+a+" !== "+c.id+")."),s;var u=o[c.type];if(!u)return L.verbose("[reducer] No matching action in this reducer, returning previous state."),s;var p=t.createStateObject(s,i);return u.call.apply(u,[p].concat(c.payload)),r(e),L.verbose("[reducer] Reducer invoked, returning new state."),t.finalizeStateObject(p,e)}}},t.combineReducersTree=function(e,n){var r=t.combineReducersRecursion(e,n);return function(t,e){var o=Date.now();n.invoked=!0,L.debug("[rootReducer] Reducing action: "+e.type+".");var i=r(t,e),a=Date.now();return L.debug("[rootReducer] Reducer tree processed in "+(a-o)+"ms."),i}},t.createMethodsLookup=function(t,e){var n=h(t),r={};return Object.keys(e.actions).forEach(function(e){var o=tt.getActionName(t,e);r[o]=n[e]}),r},t.createStateObjectPrototype=function(e,n){for(var r=u({},e,[N.Property]),o=h(e),i=0,a=Object.keys(o);i<a.length;i++){var s=a[i];n.actions[s]?r[s]=t.actionInvokedError:r[s]=o[s].bind(e)}return r},t.actionInvokedError=function(){throw new Error("Actions should not be invoked from within other actions.")},t.createStateObject=function(t,e){for(var n=Object.create(e),r=0,o=Object.keys(t);r<o.length;r++){var i=o[r],a=Object.getOwnPropertyDescriptor(e,i);a&&"function"==typeof a.get&&"function"!=typeof a.set||(n[i]=t[i])}return n},t.finalizeStateObject=function(t,e){L.verbose("[finalizeStateObject] finalizing state.");var n=Object.assign({},t);return n=M.removeIgnoredProps(n,e),L.verbose("[finalizeStateObject] state finalized."),n},t.combineReducersRecursion=function(e,n){if(!g(e)&&!n.visited.has(e)){if(n.visited.add(e),!n.componentPaths.some(function(t){return t.startsWith(n.path)}))return t.identityReducer;var r,o=R.getInfo(e);r=o?o.reducerCreator(function(t){n.changedComponents[n.path]=t}):t.identityReducer;for(var i={},a=0,c=Object.keys(e);a<c.length;a++){var u=c[a],p=t.combineReducersRecursion(e[u],new J(H({},n,{path:""===n.path?u:n.path+"."+u})));"function"==typeof p&&(i[u]=p)}var f=r;if(Object.keys(i).length){var d=s(i);f=function(e,n){var o=r(e,n),i=d(o,n);return t.mergeState(o,i)}}return f}},t.mergeState=function(t,e){if(Array.isArray(t)&&Array.isArray(e)){for(var n=0;n<e.length;n++)t[n]=e[n];return t}return H({},t,e)},t.identityReducer=function(t){return t},t}(),Q=this&&this.__assign||Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++){e=arguments[n];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},Y=function(){function t(t){this.visitedNodes=new Set,this.visitedTemplates=new Map,this.path=z,this.createdComponents={},Object.assign(this,t)}return t}(),Z=function(){function t(e,n,r){if(!_.getInfo(n))throw new Error("Argument 'template' is not a component template. Did you forget to use the decorator?");t.createSelf(this,e,n,r),r.createdComponents[r.path]=this,r.visitedTemplates.set(n,this),L.verbose("[Component] New "+n.constructor.name+" component created. Path: "+r.path),t.createSubComponents(this,e,n,r)}return t.create=function(e,n,r){return r=Object.assign(new Y,r),_.getOrInitInfo(n),new(t.getComponentClass(n))(e,n,r)},t.getComponentClass=function(e){var n=_.getInfo(e);return n.componentClass||(n.componentClass=t.createComponentClass(e)),n.componentClass},t.createComponentClass=function(e){var n=new Function("initCallback",'"use strict";return function '+e.constructor.name+"_ReduxAppComponent() { initCallback(this, arguments); }"),r=n(function(e,n){return t.apply(e,n)});r.prototype=Object.create(t.prototype),r.prototype.constructor=r;var o=tt.createActions(e);return Object.assign(r.prototype,o),r},t.createSelf=function(t,e,n,r){u(t,n,[N.Field,N.Property]);var o=U.getComponentId(r.parentTemplate,r.path),i=R.initInfo(t,n,e.dispatch,o),a=A.getOrInitInfo(t),s=A.getInfo(n)||new A;a.ignoreState=s.ignoreState,i.reducerCreator=K.createReducer(t,n)},t.createSubComponents=function(e,n,r,o){if(!g(e)&&!o.visitedNodes.has(e)){o.visitedNodes.add(e);for(var i=r||e,a=0,s=Object.keys(i);a<s.length;a++){var c=s[a],u=o.path+"."+c,p=i[c];_.getInfo(p)?o.visitedTemplates.has(p)?e[c]=o.visitedTemplates.get(p):e[c]=t.create(n,p,Q({},o,{parentTemplate:r,path:u})):t.createSubComponents(e[c],n,null,Q({},o,{parentTemplate:null,path:u}))}}},t}(),$=n(4),tt=function(){function t(){}return t.createActions=function(e){var n=h(e);if(n){var r=_.getInfo(e),o={};return Object.keys(n).forEach(function(i){o[i]=function(){for(var o=[],a=0;a<arguments.length;a++)o[a]=arguments[a];if(!(this instanceof Z))throw new Error("Component method invoked with non-Component as 'this'. Bound 'this' argument is: "+this);var s=n[i];if(r.actions[i]||r.sequences[i]){var c=R.getInfo(this),u={type:t.getActionName(e,i),id:c?c.id:void 0,payload:o};c.dispatch(u)}if(!r.actions[i])return s.call.apply(s,[this].concat(o))}}),o}},t.getActionName=function(t,e){var n=Object.assign(new k,D.action),r=e,o=t.constructor.name;return n.uppercaseActions&&(r=$(r).toUpperCase(),o=$(o).toUpperCase()),n.actionNamespace&&(r=o+n.actionNamespaceSeparator+r),r},t}();n.d(e,"isInstanceOf",function(){return w}),n.d(e,"action",function(){return v}),n.d(e,"ignoreState",function(){return y}),n.d(e,"sequence",function(){return b}),n.d(e,"withId",function(){return m}),n.d(e,"ActionOptions",function(){return k}),n.d(e,"AppOptions",function(){return x}),n.d(e,"GlobalOptions",function(){return E}),n.d(e,"LogLevel",function(){return j}),n.d(e,"getMethods",function(){return h}),n.d(e,"ReduxApp",function(){return G})},function(t,e){t.exports=require("redux")},function(t,e){t.exports=require("lodash.get")},function(t,e){t.exports=require("lodash.snakecase")}])});
\No newline at end of file