react-model
Advanced tools
Comparing version 4.0.0-rc.3 to 4.0.0-rc.4
@@ -1,1 +0,1 @@ | ||
import t from"immer";import{createContext as e,useState as n,useRef as r,useEffect as o,createElement as i,PureComponent as c}from"react";function s(){return(s=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var u={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},l={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},f=function(t,e){try{var n=t.next,r=l.tryCatch.enable?Promise.resolve(n(e).catch(function(t){return console.log(t)})).then(function(){}):Promise.resolve(n(e)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},m=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global;return Promise.resolve((0,t.action)(t.params,s({actions:(0,t.consumerActions)(o.Actions[n],{modelName:n}),state:o.State[n]},o.Context.__global||{},o.Context[n]||{}))).then(function(n){return t.newState=n||null,Promise.resolve(r(e)).then(function(){})})}catch(t){return Promise.reject(t)}},S=function(t,e){try{var n=t.modelName,r=t.newState,o=t.next,i=t.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(t){var e=i.Setter.functionSetter[n][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(i.State[n]))});var c=function(){if(r)return j(n,r),Promise.resolve(o(e)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},d=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global,i=t.__hash,c=o.Setter.functionSetter[n];return"function"===t.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(e)).then(function(){})}catch(t){return Promise.reject(t)}},v=function(t,e){try{var n=t.next,r=t.Global.subscriptions[t.modelName+"_"+t.actionName];return r&&r.forEach(function(t){t()}),Promise.resolve(n(e)).then(function(){})}catch(t){return Promise.reject(t)}},h=function(t,e){try{var n=t.Global,r=!0===l.logger.enable||"function"==typeof l.logger.enable&&l.logger.enable(t)?(console.group("%c "+t.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),Promise.resolve(t.next(e)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[t.modelName]),console.groupEnd()})):Promise.resolve(t.next(e)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},_=function(t,e){try{var n=t.Global;return Promise.resolve(t.next(e)).then(function(){n.withDevTools&&l.devtools.enable&&n.devTools.send(t.modelName+"_"+t.actionName,n.State)})}catch(t){return Promise.reject(t)}},p=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(t){var e=o.Setter.functionSetter[n][t];if(e)if(e.selector){var r=e.selector(o.State[n]);C(r,e.selectorRef)||(e.selectorRef=r,e.setState(o.State[n]))}else e.setState(o.State[n])}),Promise.resolve(r(e)).then(function(){})}catch(t){return Promise.reject(t)}},b=[f,h,_,m,S,d,p,v],g={communicator:p,consoleDebugger:h,devToolsListener:_,getNewState:m,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,n){try{var r=e.Global,o=e.modelName,i=e.next,c=e.actionName;return Promise.resolve(Promise.race([(0,e.action)(e.params,{actions:(0,e.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),A(t,x(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(i(n)).then(function(){})})}catch(t){return Promise.reject(t)}}},setNewState:S,stateUpdater:d,subscription:v,tryCatch:f,config:l},y=function(t,e){try{e.next=function(t){return t.length>0&&t[0](e,t.slice(1))};var n=function(){if(t.length>0)return Promise.resolve(t[0](e,t.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},P=e({}),N=P.Consumer;if(!console.group){var w=[],E="-".repeat(80);console.group=function(t){w.push(t),console.log("%c \nBEGIN GROUP: %c",E,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",w.pop(),E)}}}var O=function(t,e){var n={};return Object.entries(t).forEach(function(t){n[t[0]]=function(t,e){return function(n,r){try{return Promise.resolve(y(b,{Global:u,action:t,actionName:t.name,consumerActions:O,middlewareConfig:r,modelName:e.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(t){return Promise.reject(t)}}}(t[1],e)}),n},j=function(e,n){if("function"==typeof n){var r=u.State[e];r=t(r,n),u.State=t(u.State,function(t){t[e]=r})}else u.State=t(u.State,function(t){t[e]=s({},t[e],n)});return u.State},A=function(t,e){return new Promise(function(n){return setTimeout(function(){console.log(t),n(e)},t)})},R=function(e,n){try{var r={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(u.State).map(function(o){try{var i=function(){if(!e||!e.modelName||o===e.modelName||-1!==e.modelName.indexOf(o)){var i=function(e){n&&n.isServer?r[o]=e:u.State=t(u.State,function(t){t[o]=s({},t[o],e)})},c=u.AsyncState[o];return c?Promise.resolve(c(e)).then(i):i({})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return n&&n.isServer?r:u.State})}catch(t){return Promise.reject(t)}},x=function(t,e){var n=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return n?JSON.parse(n):null},C=function(t,e){if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(e,n[o])||t[n[o]]!==e[n[o]])return!1;return!0};function T(e,n,r){if(void 0!==e.state){u.uid+=1;var o="__"+u.uid;u.State=t(u.State,function(t){t[o]=e.state}),e.middlewares&&(u.Middlewares[o]=e.middlewares),u.Actions[o]=e.actions,u.AsyncState[o]=e.asyncState,n&&(u.Context[o]=n);var i=V(o);return{__id:o,actions:i,getState:function(){return M(o)},subscribe:function(t,e){return G(o,t,e)},unsubscribe:function(t){return D(o,t)},useStore:function(t){return k(o,t)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(t){return function(){return t}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}n&&!n.__FROM_SERVER__&&(u.State=t(u.State,function(t){Object.assign(t,n||{})})),r&&(u.Context.__global=r),Object.entries(e).forEach(function(e){var r=e[0],o=e[1];if(o.__ERROR__)return console.error(r+" model's schema is invalid"),u.State=t(u.State,function(t){t[r]={}}),void(u.Actions[r]={});!function(t){return void 0!==t.useStore}(o)?(n&&n.__FROM_SERVER__?u.State=t(u.State,function(t){t[r]=s({},o.state,n[r])}):u.State[r]||(u.State=t(u.State,function(t){t[r]=o.state})),o.middlewares&&(u.Middlewares[r]=o.middlewares),u.Actions[r]=o.actions,u.AsyncState[r]=o.asyncState):(u.State[r]&&n||(u.State=t(u.State,function(t){t[r]=t[o.__id]})),n&&n.__FROM_SERVER__&&(u.State=t(u.State,function(t){t[r]=s({},t[o.__id],n[r])})),u.Actions[r]=u.Actions[o.__id],u.AsyncState[r]=u.AsyncState[o.__id],u.Middlewares[r]=u.Middlewares[o.__id],u.Context[r]=u.Context[o.__id])});var a=Object.keys(e).reduce(function(t,e){var n;return s({},t,((n={})[e]=V(e),n))},{});return u.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,u.withDevTools&&(u.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,u.devTools.connect()),{actions:a,getActions:V,getInitialState:R,getState:M,subscribe:G,unsubscribe:D,useStore:k}}var D=function(t,e){G(t,e,void 0)},G=function(t,e,n){Array.isArray(e)?e.forEach(function(e){u.subscriptions[t+"_"+e]||(u.subscriptions[t+"_"+e]=[]),n?u.subscriptions[t+"_"+e].push(n):u.subscriptions[t+"_"+e]=[]}):(u.subscriptions[t+"_"+e]||(u.subscriptions[t+"_"+e]=[]),n?u.subscriptions[t+"_"+e].push(n):u.subscriptions[t+"_"+e]=[])},M=function(t){return u.State[t]},V=function(t,e){void 0===e&&(e={type:"outer"});var n={};return Object.entries(u.Actions[t]).forEach(function(r){var o=r[0],i=r[1];return n[o]=function(n,r){try{var c=s({action:i,actionName:o,consumerActions:O,middlewareConfig:r,modelName:t,newState:null,params:n},e,{Global:u}),a=u.Middlewares[t]?Promise.resolve(y(u.Middlewares[t],c)).then(function(){}):Promise.resolve(y(b,c)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}}),n},k=function(t,e){var i=n({})[1],c=r("");o(function(){u.uid+=1;var n=""+u.uid;return c.current=n,u.Setter.functionSetter[t]||(u.Setter.functionSetter[t]={}),u.Setter.functionSetter[t][n]={setState:i,selector:e},function(){delete u.Setter.functionSetter[t][n]}},[]);var s=V(t,{__hash:c.current,type:"function"});return[e?e(M(t)):M(t),s]},F=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=u.State,e}return a(e,t),e.prototype.render=function(){var t=this.props.children;return u.Setter.classSetter=this.setState.bind(this),i(P.Provider,{value:s({},this.state)},t)},e}(c),I=function(t,e,n){return function(r){return function(o){function c(){return o.apply(this,arguments)||this}return a(c,o),c.prototype.render=function(){var o=this,c=this.props,a=c.state,l=void 0===a?{}:a,f=c.actions,m=void 0===f?{}:f;return i(N,null,function(c){var a=c[""+t],f=u.Actions[t];return i(r,Object.assign({},o.props,{state:s({},l,e?e(a):a),actions:s({},m,n?n(O(f,{modelName:t})):O(f,{modelName:t}))}))})},c}(c)}};export{N as Consumer,T as Model,F as Provider,b as actionMiddlewares,I as connect,R as getInitialState,M as getState,g as middlewares}; | ||
import t from"immer";import{createContext as e,useState as n,useRef as r,useEffect as o,createElement as i,PureComponent as c}from"react";function s(){return(s=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var u={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},l={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},f=function(t,e){try{var n=t.next,r=l.tryCatch.enable?Promise.resolve(n(e).catch(function(t){return console.log(t)})).then(function(){}):Promise.resolve(n(e)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},m=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global;return Promise.resolve((0,t.action)(t.params,s({actions:(0,t.consumerActions)(o.Actions[n],{modelName:n}),state:o.State[n]},o.Context.__global||{},o.Context[n]||{}))).then(function(n){return t.newState=n||null,Promise.resolve(r(e)).then(function(){})})}catch(t){return Promise.reject(t)}},S=function(t,e){try{var n=t.modelName,r=t.newState,o=t.next,i=t.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(t){var e=i.Setter.functionSetter[n][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(i.State[n]))});var c=function(){if(r)return j(n,r),Promise.resolve(o(e)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},d=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global,i=t.__hash,c=o.Setter.functionSetter[n];return"function"===t.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(e)).then(function(){})}catch(t){return Promise.reject(t)}},v=function(t,e){try{var n=t.next,r=t.Global.subscriptions[t.modelName+"_"+t.actionName];return r&&r.forEach(function(t){t()}),Promise.resolve(n(e)).then(function(){})}catch(t){return Promise.reject(t)}},h=function(t,e){try{var n=t.Global,r=!0===l.logger.enable||"function"==typeof l.logger.enable&&l.logger.enable(t)?(console.group("%c "+t.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),Promise.resolve(t.next(e)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[t.modelName]),console.groupEnd()})):Promise.resolve(t.next(e)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},_=function(t,e){try{var n=t.Global;return Promise.resolve(t.next(e)).then(function(){n.withDevTools&&l.devtools.enable&&n.devTools.send(t.modelName+"_"+t.actionName,n.State)})}catch(t){return Promise.reject(t)}},p=function(t,e){try{var n=t.modelName,r=t.next,o=t.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(t){var e=o.Setter.functionSetter[n][t];if(e)if(e.selector){var r=e.selector(o.State[n]);C(r,e.selectorRef)||(e.selectorRef=r,e.setState(o.State[n]))}else e.setState(o.State[n])}),Promise.resolve(r(e)).then(function(){})}catch(t){return Promise.reject(t)}},b=[f,h,_,m,S,d,p,v],y={communicator:p,consoleDebugger:h,devToolsListener:_,getNewState:m,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,n){try{var r=e.Global,o=e.modelName,i=e.next,c=e.actionName;return Promise.resolve(Promise.race([(0,e.action)(e.params,{actions:(0,e.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),A(t,x(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(i(n)).then(function(){})})}catch(t){return Promise.reject(t)}}},setNewState:S,stateUpdater:d,subscription:v,tryCatch:f,config:l},g=function(t,e){try{e.next=function(t){return t.length>0&&t[0](e,t.slice(1))};var n=function(){if(t.length>0)return Promise.resolve(t[0](e,t.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},P=e({}),N=P.Consumer;if(!console.group){var w=[],E="-".repeat(80);console.group=function(t){w.push(t),console.log("%c \nBEGIN GROUP: %c",E,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",w.pop(),E)}}}var O=function(t,e){var n={};return Object.keys(t).forEach(function(r){n[r]=function(t,e){return function(n,r){try{return Promise.resolve(g(b,{Global:u,action:t,actionName:t.name,consumerActions:O,middlewareConfig:r,modelName:e.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(t){return Promise.reject(t)}}}(t[r],e)}),n},j=function(e,n){if("function"==typeof n){var r=u.State[e];r=t(r,n),u.State=t(u.State,function(t){t[e]=r})}else u.State=t(u.State,function(t){t[e]=s({},t[e],n)});return u.State},A=function(t,e){return new Promise(function(n){return setTimeout(function(){console.log(t),n(e)},t)})},R=function(e,n){try{var r={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(u.State).map(function(o){try{var i=function(){if(!e||!e.modelName||o===e.modelName||-1!==e.modelName.indexOf(o)){var i=function(e){n&&n.isServer?r[o]=e:u.State=t(u.State,function(t){t[o]=s({},t[o],e)})},c=u.AsyncState[o];return c?Promise.resolve(c(e)).then(i):i({})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return n&&n.isServer?r:u.State})}catch(t){return Promise.reject(t)}},x=function(t,e){var n=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return n?JSON.parse(n):null},C=function(t,e){if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(e,n[o])||t[n[o]]!==e[n[o]])return!1;return!0};function T(e,n,r){if(void 0!==e.state){u.uid+=1;var o="__"+u.uid;u.State=t(u.State,function(t){t[o]=e.state}),e.middlewares&&(u.Middlewares[o]=e.middlewares),u.Actions[o]=e.actions,u.AsyncState[o]=e.asyncState,n&&(u.Context[o]=n);var i=k(o);return{__id:o,actions:i,getState:function(){return M(o)},subscribe:function(t,e){return G(o,t,e)},unsubscribe:function(t){return D(o,t)},useStore:function(t){return V(o,t)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(t){return function(){return t}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}n&&!n.__FROM_SERVER__&&(u.State=t(u.State,function(t){Object.assign(t,n||{})})),r&&(u.Context.__global=r),Object.keys(e).forEach(function(r){var o=e[r];if(o.__ERROR__)return console.error(r+" model's schema is invalid"),u.State=t(u.State,function(t){t[r]={}}),void(u.Actions[r]={});!function(t){return void 0!==t.useStore}(o)?(n&&n.__FROM_SERVER__?u.State=t(u.State,function(t){t[r]=s({},o.state,n[r])}):u.State[r]||(u.State=t(u.State,function(t){t[r]=o.state})),o.middlewares&&(u.Middlewares[r]=o.middlewares),u.Actions[r]=o.actions,u.AsyncState[r]=o.asyncState):(u.State[r]&&n||(u.State=t(u.State,function(t){t[r]=t[o.__id]})),n&&n.__FROM_SERVER__&&(u.State=t(u.State,function(t){t[r]=s({},t[o.__id],n[r])})),u.Actions[r]=u.Actions[o.__id],u.AsyncState[r]=u.AsyncState[o.__id],u.Middlewares[r]=u.Middlewares[o.__id],u.Context[r]=u.Context[o.__id])});var a=Object.keys(e).reduce(function(t,e){var n;return s({},t,((n={})[e]=k(e),n))},{});return u.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,u.withDevTools&&(u.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,u.devTools.connect()),{actions:a,getActions:k,getInitialState:R,getState:M,subscribe:G,unsubscribe:D,useStore:V}}var D=function(t,e){G(t,e,void 0)},G=function(t,e,n){Array.isArray(e)?e.forEach(function(e){u.subscriptions[t+"_"+e]||(u.subscriptions[t+"_"+e]=[]),n?u.subscriptions[t+"_"+e].push(n):u.subscriptions[t+"_"+e]=[]}):(u.subscriptions[t+"_"+e]||(u.subscriptions[t+"_"+e]=[]),n?u.subscriptions[t+"_"+e].push(n):u.subscriptions[t+"_"+e]=[])},M=function(t){return u.State[t]},k=function(t,e){void 0===e&&(e={type:"outer"});var n={};return Object.keys(u.Actions[t]).forEach(function(r){return n[r]=function(n,o){try{var i=s({action:u.Actions[t][r],actionName:r,consumerActions:O,middlewareConfig:o,modelName:t,newState:null,params:n},e,{Global:u}),c=u.Middlewares[t]?Promise.resolve(g(u.Middlewares[t],i)).then(function(){}):Promise.resolve(g(b,i)).then(function(){});return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}}),n},V=function(t,e){var i=n({})[1],c=r("");o(function(){u.uid+=1;var n=""+u.uid;return c.current=n,u.Setter.functionSetter[t]||(u.Setter.functionSetter[t]={}),u.Setter.functionSetter[t][n]={setState:i,selector:e},function(){delete u.Setter.functionSetter[t][n]}},[]);var s=k(t,{__hash:c.current,type:"function"});return[e?e(M(t)):M(t),s]},F=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=u.State,e}return a(e,t),e.prototype.render=function(){var t=this.props.children;return u.Setter.classSetter=this.setState.bind(this),i(P.Provider,{value:s({},this.state)},t)},e}(c),I=function(t,e,n){return function(r){return function(o){function c(){return o.apply(this,arguments)||this}return a(c,o),c.prototype.render=function(){var o=this,c=this.props,a=c.state,l=void 0===a?{}:a,f=c.actions,m=void 0===f?{}:f;return i(N,null,function(c){var a=c[""+t],f=u.Actions[t];return i(r,Object.assign({},o.props,{state:s({},l,e?e(a):a),actions:s({},m,n?n(O(f,{modelName:t})):O(f,{modelName:t}))}))})},c}(c)}};export{N as Consumer,T as Model,F as Provider,b as actionMiddlewares,I as connect,R as getInitialState,M as getState,y as middlewares}; |
@@ -1,1 +0,1 @@ | ||
var e,t=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e,n=require("react");function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function o(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},c={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},s=function(e,t){try{var n=e.next,r=c.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,o=e.next,i=e.Global;return Promise.resolve((0,e.action)(e.params,r({actions:(0,e.consumerActions)(i.Actions[n],{modelName:n}),state:i.State[n]},i.Context.__global||{},i.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[n]))});var c=function(){if(r)return N(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.Global,r=!0===c.logger.enable||"function"==typeof c.logger.enable&&c.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&c.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);j(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},v=[s,m,S,a,u,l,d,f],h={communicator:d,consoleDebugger:m,devToolsListener:S,getNewState:a,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),w(e,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:u,stateUpdater:l,subscription:f,tryCatch:s,config:c},_=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},p=n.createContext({}),b=p.Consumer;if(!console.group){var g=[],y="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",y,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),y)}}}var P=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,r){try{return Promise.resolve(_(v,{Global:i,action:e,actionName:e.name,consumerActions:P,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},N=function(e,n){if("function"==typeof n){var o=i.State[e];o=t(o,n),i.State=t(i.State,function(t){t[e]=o})}else i.State=t(i.State,function(t){t[e]=r({},t[e],n)});return i.State},w=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},E=function(e,n){try{var o={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(i.State).map(function(c){try{var s=function(){if(!e||!e.modelName||c===e.modelName||-1!==e.modelName.indexOf(c)){var s=function(e){n&&n.isServer?o[c]=e:i.State=t(i.State,function(t){t[c]=r({},t[c],e)})},a=i.AsyncState[c];return a?Promise.resolve(a(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return n&&n.isServer?o:i.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},j=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||e[n[o]]!==t[n[o]])return!1;return!0},x=function(e,t){A(e,t,void 0)},A=function(e,t,n){Array.isArray(t)?t.forEach(function(t){i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[]}):(i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[])},R=function(e){return i.State[e]},C=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(i.Actions[e]).forEach(function(o){var c=o[0],s=o[1];return n[c]=function(n,o){try{var a=r({action:s,actionName:c,consumerActions:P,middlewareConfig:o,modelName:e,newState:null,params:n},t,{Global:i}),u=i.Middlewares[e]?Promise.resolve(_(i.Middlewares[e],a)).then(function(){}):Promise.resolve(_(v,a)).then(function(){});return Promise.resolve(u&&u.then?u.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},T=function(e,t){var r=n.useState({})[1],o=n.useRef("");n.useEffect(function(){i.uid+=1;var n=""+i.uid;return o.current=n,i.Setter.functionSetter[e]||(i.Setter.functionSetter[e]={}),i.Setter.functionSetter[e][n]={setState:r,selector:t},function(){delete i.Setter.functionSetter[e][n]}},[]);var c=C(e,{__hash:o.current,type:"function"});return[t?t(R(e)):R(e),c]},D=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=i.State,t}return o(t,e),t.prototype.render=function(){var e=this.props.children;return i.Setter.classSetter=this.setState.bind(this),n.createElement(p.Provider,{value:r({},this.state)},e)},t}(n.PureComponent);exports.Consumer=b,exports.Model=function(e,n,o){if(void 0!==e.state){i.uid+=1;var c="__"+i.uid;i.State=t(i.State,function(t){t[c]=e.state}),e.middlewares&&(i.Middlewares[c]=e.middlewares),i.Actions[c]=e.actions,i.AsyncState[c]=e.asyncState,n&&(i.Context[c]=n);var s=C(c);return{__id:c,actions:s,getState:function(){return R(c)},subscribe:function(e,t){return A(c,e,t)},unsubscribe:function(e){return x(c,e)},useStore:function(e){return T(c,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var a=function(e){return function(){return e}};return{__ERROR__:!0,actions:a({}),getActions:a({}),getInitialState:a({}),getState:a({}),subscribe:a(),unsubscribe:a(),useStore:a([{},{}])}}n&&!n.__FROM_SERVER__&&(i.State=t(i.State,function(e){Object.assign(e,n||{})})),o&&(i.Context.__global=o),Object.entries(e).forEach(function(e){var o=e[0],c=e[1];if(c.__ERROR__)return console.error(o+" model's schema is invalid"),i.State=t(i.State,function(e){e[o]={}}),void(i.Actions[o]={});void 0===c.useStore?(n&&n.__FROM_SERVER__?i.State=t(i.State,function(e){e[o]=r({},c.state,n[o])}):i.State[o]||(i.State=t(i.State,function(e){e[o]=c.state})),c.middlewares&&(i.Middlewares[o]=c.middlewares),i.Actions[o]=c.actions,i.AsyncState[o]=c.asyncState):(i.State[o]&&n||(i.State=t(i.State,function(e){e[o]=e[c.__id]})),n&&n.__FROM_SERVER__&&(i.State=t(i.State,function(e){e[o]=r({},e[c.__id],n[o])})),i.Actions[o]=i.Actions[c.__id],i.AsyncState[o]=i.AsyncState[c.__id],i.Middlewares[o]=i.Middlewares[c.__id],i.Context[o]=i.Context[c.__id])});var u=Object.keys(e).reduce(function(e,t){var n;return r({},e,((n={})[t]=C(t),n))},{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:u,getActions:C,getInitialState:E,getState:R,subscribe:A,unsubscribe:x,useStore:T}},exports.Provider=D,exports.actionMiddlewares=v,exports.connect=function(e,t,c){return function(s){return function(a){function u(){return a.apply(this,arguments)||this}return o(u,a),u.prototype.render=function(){var o=this,a=this.props,u=a.state,l=void 0===u?{}:u,f=a.actions,m=void 0===f?{}:f;return n.createElement(b,null,function(a){var u=a[""+e],f=i.Actions[e];return n.createElement(s,Object.assign({},o.props,{state:r({},l,t?t(u):u),actions:r({},m,c?c(P(f,{modelName:e})):P(f,{modelName:e}))}))})},u}(n.PureComponent)}},exports.getInitialState=E,exports.getState=R,exports.middlewares=h; | ||
var e,t=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e,n=require("react");function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function o(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},c={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},s=function(e,t){try{var n=e.next,r=c.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,o=e.next,i=e.Global;return Promise.resolve((0,e.action)(e.params,r({actions:(0,e.consumerActions)(i.Actions[n],{modelName:n}),state:i.State[n]},i.Context.__global||{},i.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[n]))});var c=function(){if(r)return N(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.Global,r=!0===c.logger.enable||"function"==typeof c.logger.enable&&c.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&c.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);j(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},v=[s,m,S,a,u,l,d,f],h={communicator:d,consoleDebugger:m,devToolsListener:S,getNewState:a,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),w(e,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:u,stateUpdater:l,subscription:f,tryCatch:s,config:c},_=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},p=n.createContext({}),b=p.Consumer;if(!console.group){var y=[],g="-".repeat(80);console.group=function(e){y.push(e),console.log("%c \nBEGIN GROUP: %c",g,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",y.pop(),g)}}}var P=function(e,t){var n={};return Object.keys(e).forEach(function(r){n[r]=function(e,t){return function(n,r){try{return Promise.resolve(_(v,{Global:i,action:e,actionName:e.name,consumerActions:P,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[r],t)}),n},N=function(e,n){if("function"==typeof n){var o=i.State[e];o=t(o,n),i.State=t(i.State,function(t){t[e]=o})}else i.State=t(i.State,function(t){t[e]=r({},t[e],n)});return i.State},w=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},E=function(e,n){try{var o={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(i.State).map(function(c){try{var s=function(){if(!e||!e.modelName||c===e.modelName||-1!==e.modelName.indexOf(c)){var s=function(e){n&&n.isServer?o[c]=e:i.State=t(i.State,function(t){t[c]=r({},t[c],e)})},a=i.AsyncState[c];return a?Promise.resolve(a(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return n&&n.isServer?o:i.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},j=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||e[n[o]]!==t[n[o]])return!1;return!0},x=function(e,t){A(e,t,void 0)},A=function(e,t,n){Array.isArray(t)?t.forEach(function(t){i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[]}):(i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[])},R=function(e){return i.State[e]},C=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.keys(i.Actions[e]).forEach(function(o){return n[o]=function(n,c){try{var s=r({action:i.Actions[e][o],actionName:o,consumerActions:P,middlewareConfig:c,modelName:e,newState:null,params:n},t,{Global:i}),a=i.Middlewares[e]?Promise.resolve(_(i.Middlewares[e],s)).then(function(){}):Promise.resolve(_(v,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},T=function(e,t){var r=n.useState({})[1],o=n.useRef("");n.useEffect(function(){i.uid+=1;var n=""+i.uid;return o.current=n,i.Setter.functionSetter[e]||(i.Setter.functionSetter[e]={}),i.Setter.functionSetter[e][n]={setState:r,selector:t},function(){delete i.Setter.functionSetter[e][n]}},[]);var c=C(e,{__hash:o.current,type:"function"});return[t?t(R(e)):R(e),c]},D=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=i.State,t}return o(t,e),t.prototype.render=function(){var e=this.props.children;return i.Setter.classSetter=this.setState.bind(this),n.createElement(p.Provider,{value:r({},this.state)},e)},t}(n.PureComponent);exports.Consumer=b,exports.Model=function(e,n,o){if(void 0!==e.state){i.uid+=1;var c="__"+i.uid;i.State=t(i.State,function(t){t[c]=e.state}),e.middlewares&&(i.Middlewares[c]=e.middlewares),i.Actions[c]=e.actions,i.AsyncState[c]=e.asyncState,n&&(i.Context[c]=n);var s=C(c);return{__id:c,actions:s,getState:function(){return R(c)},subscribe:function(e,t){return A(c,e,t)},unsubscribe:function(e){return x(c,e)},useStore:function(e){return T(c,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var a=function(e){return function(){return e}};return{__ERROR__:!0,actions:a({}),getActions:a({}),getInitialState:a({}),getState:a({}),subscribe:a(),unsubscribe:a(),useStore:a([{},{}])}}n&&!n.__FROM_SERVER__&&(i.State=t(i.State,function(e){Object.assign(e,n||{})})),o&&(i.Context.__global=o),Object.keys(e).forEach(function(o){var c=e[o];if(c.__ERROR__)return console.error(o+" model's schema is invalid"),i.State=t(i.State,function(e){e[o]={}}),void(i.Actions[o]={});void 0===c.useStore?(n&&n.__FROM_SERVER__?i.State=t(i.State,function(e){e[o]=r({},c.state,n[o])}):i.State[o]||(i.State=t(i.State,function(e){e[o]=c.state})),c.middlewares&&(i.Middlewares[o]=c.middlewares),i.Actions[o]=c.actions,i.AsyncState[o]=c.asyncState):(i.State[o]&&n||(i.State=t(i.State,function(e){e[o]=e[c.__id]})),n&&n.__FROM_SERVER__&&(i.State=t(i.State,function(e){e[o]=r({},e[c.__id],n[o])})),i.Actions[o]=i.Actions[c.__id],i.AsyncState[o]=i.AsyncState[c.__id],i.Middlewares[o]=i.Middlewares[c.__id],i.Context[o]=i.Context[c.__id])});var u=Object.keys(e).reduce(function(e,t){var n;return r({},e,((n={})[t]=C(t),n))},{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:u,getActions:C,getInitialState:E,getState:R,subscribe:A,unsubscribe:x,useStore:T}},exports.Provider=D,exports.actionMiddlewares=v,exports.connect=function(e,t,c){return function(s){return function(a){function u(){return a.apply(this,arguments)||this}return o(u,a),u.prototype.render=function(){var o=this,a=this.props,u=a.state,l=void 0===u?{}:u,f=a.actions,m=void 0===f?{}:f;return n.createElement(b,null,function(a){var u=a[""+e],f=i.Actions[e];return n.createElement(s,Object.assign({},o.props,{state:r({},l,t?t(u):u),actions:r({},m,c?c(P(f,{modelName:e})):P(f,{modelName:e}))}))})},u}(n.PureComponent)}},exports.getInitialState=E,exports.getState=R,exports.middlewares=h; |
@@ -1,1 +0,1 @@ | ||
import t from"immer";import{createContext as e,useState as o,useRef as n,useEffect as s,PureComponent as a,createElement as c}from"react";var r={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1};const i={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},l=async(t,e)=>{const{next:o}=t;i.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},S=async(t,e)=>{const{action:o,modelName:n,consumerActions:s,params:a,next:c,Global:r}=t;t.newState=await o(a,{actions:s(r.Actions[n],{modelName:n}),state:r.State[n],...r.Context.__global||{},...r.Context[n]||{}})||null,await c(e)},u=async(t,e)=>{const{modelName:o,newState:n,next:s,Global:a}=t;a.Setter.functionSetter[o]&&Object.keys(a.Setter.functionSetter[o]).map(t=>{const e=a.Setter.functionSetter[o][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(a.State[o]))}),n&&(E(o,n),await s(e))},d=async(t,e)=>{const{modelName:o,next:n,Global:s,__hash:a}=t,c=s.Setter.functionSetter[o];"function"===t.type&&a&&c&&c[a]&&c[a].setState&&c[a].setState(s.State[o]),await n(e)},_=async(t,e)=>{const{modelName:o,actionName:n,next:s,Global:a}=t,c=a.subscriptions[`${o}_${n}`];c&&c.forEach(t=>{t()}),await s(e)},m=async(t,e)=>{const{Global:o}=t;!0===i.logger.enable||"function"==typeof i.logger.enable&&i.logger.enable(t)?(console.group(`%c ${t.modelName} State Change %c ${(new Date).toLocaleTimeString()}`,"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",o.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),await t.next(e),console.log("%c Next","color: #4CAF50; font-weight: bold",o.State[t.modelName]),console.groupEnd()):await t.next(e)},b=async(t,e)=>{const{Global:o}=t;await t.next(e),o.withDevTools&&i.devtools.enable&&o.devTools.send(`${t.modelName}_${t.actionName}`,o.State)},p=async(t,e)=>{const{modelName:o,next:n,Global:s}=t;s.Setter.classSetter&&s.Setter.classSetter(s.State),s.Setter.functionSetter[o]&&Object.keys(s.Setter.functionSetter[o]).map(t=>{const e=s.Setter.functionSetter[o][t];if(e)if(e.selector){const t=e.selector(s.State[o]);R(t,e.selectorRef)||(e.selectorRef=t,e.setState(s.State[o]))}else e.setState(s.State[o])}),await n(e)},f=[l,m,b,S,u,d,p,_],w={communicator:p,consoleDebugger:m,devToolsListener:b,getNewState:S,getNewStateWithCache:(t=5e3)=>async(e,o)=>{const{action:n,Global:s,modelName:a,consumerActions:c,params:r,next:i,actionName:l}=e;e.newState=await Promise.race([n(r,{actions:c(s.Actions[a],{modelName:a}),state:s.State[a]}),O(t,A(a,l))])||null,await i(o)},setNewState:u,stateUpdater:d,subscription:_,tryCatch:l,config:i},g=async(t,e)=>{e.next=t=>t.length>0&&t[0](e,t.slice(1)),t.length>0&&await t[0](e,t.slice(1))},y=e({}),h=y.Consumer;if(!console.group){const t=[],e="-".repeat(80);console.group=function(o){t.push(o),console.log("%c \nBEGIN GROUP: %c",e,o),console.groupEnd=function(){console.log("END GROUP: %c\n%c",t.pop(),e)}}}const N=(t,e)=>{const o={};return Object.entries(t).forEach(([t,n])=>{o[t]=((t,e)=>async(o,n)=>{const s={Global:r,action:t,actionName:t.name,consumerActions:N,middlewareConfig:n,modelName:e.modelName,newState:null,params:o,type:"outer"};await g(f,s)})(n,e)}),o},E=(e,o)=>{if("function"==typeof o){let n=r.State[e];n=t(n,o),r.State=t(r.State,t=>{t[e]=n})}else r.State=t(r.State,t=>{t[e]={...t[e],...o}});return r.State},O=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),v=async(e,o)=>{const n={__FROM_SERVER__:!0};return await Promise.all(Object.keys(r.State).map(async s=>{if(!e||!e.modelName||s===e.modelName||-1!==e.modelName.indexOf(s)){const a=r.AsyncState[s],c=a?await a(e):{};o&&o.isServer?n[s]=c:r.State=t(r.State,t=>{t[s]={...t[s],...c}})}})),o&&o.isServer?n:r.State},A=(t,e)=>{const o=localStorage.getItem(`__REACT_MODELX__${t}_${e}`);return o?JSON.parse(o):null},R=(t,e)=>{if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;const o=Object.keys(t),n=Object.keys(e);if(o.length!==n.length)return!1;for(let n=0;n<o.length;n++)if(!Object.prototype.hasOwnProperty.call(e,o[n])||t[o[n]]!==e[o[n]])return!1;return!0};function $(e,o,n){if(void 0!==e.state){r.uid+=1;const n="__"+r.uid;r.State=t(r.State,t=>{t[n]=e.state}),e.middlewares&&(r.Middlewares[n]=e.middlewares),r.Actions[n]=e.actions,r.AsyncState[n]=e.asyncState,o&&(r.Context[n]=o);const s=D(n);return{__id:n,actions:s,getState:()=>T(n),subscribe:(t,e)=>C(n,t,e),unsubscribe:t=>x(n,t),useStore:t=>j(n,t)}}{if(e.actions){console.error("invalid model(s) schema: ",e);const t=t=>(...e)=>t;return{__ERROR__:!0,actions:t({}),getActions:t({}),getInitialState:t({}),getState:t({}),subscribe:t(),unsubscribe:t(),useStore:t([{},{}])}}o&&!o.__FROM_SERVER__&&(r.State=t(r.State,t=>{Object.assign(t,o||{})})),n&&(r.Context.__global=n),Object.entries(e).forEach(([e,n])=>{if(n.__ERROR__)return console.error(e+" model's schema is invalid"),r.State=t(r.State,t=>{t[e]={}}),void(r.Actions[e]={});(t=>void 0!==t.useStore)(n)?(r.State[e]&&o||(r.State=t(r.State,t=>{t[e]=t[n.__id]})),o&&o.__FROM_SERVER__&&(r.State=t(r.State,t=>{t[e]={...t[n.__id],...o[e]}})),r.Actions[e]=r.Actions[n.__id],r.AsyncState[e]=r.AsyncState[n.__id],r.Middlewares[e]=r.Middlewares[n.__id],r.Context[e]=r.Context[n.__id]):(o&&o.__FROM_SERVER__?r.State=t(r.State,t=>{t[e]={...n.state,...o[e]}}):r.State[e]||(r.State=t(r.State,t=>{t[e]=n.state})),n.middlewares&&(r.Middlewares[e]=n.middlewares),r.Actions[e]=n.actions,r.AsyncState[e]=n.asyncState)});const s=Object.keys(e).reduce((t,e)=>({...t,[e]:D(e)}),{});return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),{actions:s,getActions:D,getInitialState:v,getState:T,subscribe:C,unsubscribe:x,useStore:j}}}const x=(t,e)=>{C(t,e,void 0)},C=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{r.subscriptions[`${t}_${e}`]||(r.subscriptions[`${t}_${e}`]=[]),o?r.subscriptions[`${t}_${e}`].push(o):r.subscriptions[`${t}_${e}`]=[]}):(r.subscriptions[`${t}_${e}`]||(r.subscriptions[`${t}_${e}`]=[]),o?r.subscriptions[`${t}_${e}`].push(o):r.subscriptions[`${t}_${e}`]=[])},T=t=>r.State[t],D=(t,e={type:"outer"})=>{const o={};return Object.entries(r.Actions[t]).forEach(([n,s])=>o[n]=async(o,a)=>{const c={action:s,actionName:n,consumerActions:N,middlewareConfig:a,modelName:t,newState:null,params:o,...e,Global:r};r.Middlewares[t]?await g(r.Middlewares[t],c):await g(f,c)}),o},j=(t,e)=>{const a=o({})[1],c=n("");s(()=>{r.uid+=1;const o=""+r.uid;return c.current=o,r.Setter.functionSetter[t]||(r.Setter.functionSetter[t]={}),r.Setter.functionSetter[t][o]={setState:a,selector:e},function(){delete r.Setter.functionSetter[t][o]}},[]);const i=D(t,{__hash:c.current,type:"function"});return[e?e(T(t)):T(t),i]};class G extends a{constructor(){super(...arguments),this.state=r.State}render(){const{children:t}=this.props;return r.Setter.classSetter=this.setState.bind(this),c(y.Provider,{value:{...this.state}},t)}}const M=(t,e,o)=>n=>class extends a{render(){const{state:s={},actions:a={}}=this.props;return c(h,null,i=>{const{[""+t]:l}=i,S=r.Actions[t];return c(n,Object.assign({},this.props,{state:{...s,...e?e(l):l},actions:{...a,...o?o(N(S,{modelName:t})):N(S,{modelName:t})}}))})}};export{h as Consumer,$ as Model,G as Provider,f as actionMiddlewares,M as connect,v as getInitialState,T as getState,w as middlewares}; | ||
import t from"immer";import{createContext as e,useState as o,useRef as n,useEffect as s,PureComponent as a,createElement as c}from"react";var r={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1};const i={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},l=async(t,e)=>{const{next:o}=t;i.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},S=async(t,e)=>{const{action:o,modelName:n,consumerActions:s,params:a,next:c,Global:r}=t;t.newState=await o(a,{actions:s(r.Actions[n],{modelName:n}),state:r.State[n],...r.Context.__global||{},...r.Context[n]||{}})||null,await c(e)},u=async(t,e)=>{const{modelName:o,newState:n,next:s,Global:a}=t;a.Setter.functionSetter[o]&&Object.keys(a.Setter.functionSetter[o]).map(t=>{const e=a.Setter.functionSetter[o][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(a.State[o]))}),n&&(E(o,n),await s(e))},d=async(t,e)=>{const{modelName:o,next:n,Global:s,__hash:a}=t,c=s.Setter.functionSetter[o];"function"===t.type&&a&&c&&c[a]&&c[a].setState&&c[a].setState(s.State[o]),await n(e)},_=async(t,e)=>{const{modelName:o,actionName:n,next:s,Global:a}=t,c=a.subscriptions[`${o}_${n}`];c&&c.forEach(t=>{t()}),await s(e)},m=async(t,e)=>{const{Global:o}=t;!0===i.logger.enable||"function"==typeof i.logger.enable&&i.logger.enable(t)?(console.group(`%c ${t.modelName} State Change %c ${(new Date).toLocaleTimeString()}`,"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",o.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),await t.next(e),console.log("%c Next","color: #4CAF50; font-weight: bold",o.State[t.modelName]),console.groupEnd()):await t.next(e)},b=async(t,e)=>{const{Global:o}=t;await t.next(e),o.withDevTools&&i.devtools.enable&&o.devTools.send(`${t.modelName}_${t.actionName}`,o.State)},p=async(t,e)=>{const{modelName:o,next:n,Global:s}=t;s.Setter.classSetter&&s.Setter.classSetter(s.State),s.Setter.functionSetter[o]&&Object.keys(s.Setter.functionSetter[o]).map(t=>{const e=s.Setter.functionSetter[o][t];if(e)if(e.selector){const t=e.selector(s.State[o]);R(t,e.selectorRef)||(e.selectorRef=t,e.setState(s.State[o]))}else e.setState(s.State[o])}),await n(e)},f=[l,m,b,S,u,d,p,_],w={communicator:p,consoleDebugger:m,devToolsListener:b,getNewState:S,getNewStateWithCache:(t=5e3)=>async(e,o)=>{const{action:n,Global:s,modelName:a,consumerActions:c,params:r,next:i,actionName:l}=e;e.newState=await Promise.race([n(r,{actions:c(s.Actions[a],{modelName:a}),state:s.State[a]}),O(t,v(a,l))])||null,await i(o)},setNewState:u,stateUpdater:d,subscription:_,tryCatch:l,config:i},g=async(t,e)=>{e.next=t=>t.length>0&&t[0](e,t.slice(1)),t.length>0&&await t[0](e,t.slice(1))},y=e({}),h=y.Consumer;if(!console.group){const t=[],e="-".repeat(80);console.group=function(o){t.push(o),console.log("%c \nBEGIN GROUP: %c",e,o),console.groupEnd=function(){console.log("END GROUP: %c\n%c",t.pop(),e)}}}const N=(t,e)=>{const o={};return Object.keys(t).forEach(n=>{o[n]=((t,e)=>async(o,n)=>{const s={Global:r,action:t,actionName:t.name,consumerActions:N,middlewareConfig:n,modelName:e.modelName,newState:null,params:o,type:"outer"};await g(f,s)})(t[n],e)}),o},E=(e,o)=>{if("function"==typeof o){let n=r.State[e];n=t(n,o),r.State=t(r.State,t=>{t[e]=n})}else r.State=t(r.State,t=>{t[e]={...t[e],...o}});return r.State},O=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),A=async(e,o)=>{const n={__FROM_SERVER__:!0};return await Promise.all(Object.keys(r.State).map(async s=>{if(!e||!e.modelName||s===e.modelName||-1!==e.modelName.indexOf(s)){const a=r.AsyncState[s],c=a?await a(e):{};o&&o.isServer?n[s]=c:r.State=t(r.State,t=>{t[s]={...t[s],...c}})}})),o&&o.isServer?n:r.State},v=(t,e)=>{const o=localStorage.getItem(`__REACT_MODELX__${t}_${e}`);return o?JSON.parse(o):null},R=(t,e)=>{if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;const o=Object.keys(t),n=Object.keys(e);if(o.length!==n.length)return!1;for(let n=0;n<o.length;n++)if(!Object.prototype.hasOwnProperty.call(e,o[n])||t[o[n]]!==e[o[n]])return!1;return!0};function $(e,o,n){if(void 0!==e.state){r.uid+=1;const n="__"+r.uid;r.State=t(r.State,t=>{t[n]=e.state}),e.middlewares&&(r.Middlewares[n]=e.middlewares),r.Actions[n]=e.actions,r.AsyncState[n]=e.asyncState,o&&(r.Context[n]=o);const s=D(n);return{__id:n,actions:s,getState:()=>T(n),subscribe:(t,e)=>C(n,t,e),unsubscribe:t=>x(n,t),useStore:t=>j(n,t)}}{if(e.actions){console.error("invalid model(s) schema: ",e);const t=t=>(...e)=>t;return{__ERROR__:!0,actions:t({}),getActions:t({}),getInitialState:t({}),getState:t({}),subscribe:t(),unsubscribe:t(),useStore:t([{},{}])}}o&&!o.__FROM_SERVER__&&(r.State=t(r.State,t=>{Object.assign(t,o||{})})),n&&(r.Context.__global=n),Object.keys(e).forEach(n=>{const s=e[n];if(s.__ERROR__)return console.error(n+" model's schema is invalid"),r.State=t(r.State,t=>{t[n]={}}),void(r.Actions[n]={});(t=>void 0!==t.useStore)(s)?(r.State[n]&&o||(r.State=t(r.State,t=>{t[n]=t[s.__id]})),o&&o.__FROM_SERVER__&&(r.State=t(r.State,t=>{t[n]={...t[s.__id],...o[n]}})),r.Actions[n]=r.Actions[s.__id],r.AsyncState[n]=r.AsyncState[s.__id],r.Middlewares[n]=r.Middlewares[s.__id],r.Context[n]=r.Context[s.__id]):(o&&o.__FROM_SERVER__?r.State=t(r.State,t=>{t[n]={...s.state,...o[n]}}):r.State[n]||(r.State=t(r.State,t=>{t[n]=s.state})),s.middlewares&&(r.Middlewares[n]=s.middlewares),r.Actions[n]=s.actions,r.AsyncState[n]=s.asyncState)});const s=Object.keys(e).reduce((t,e)=>({...t,[e]:D(e)}),{});return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),{actions:s,getActions:D,getInitialState:A,getState:T,subscribe:C,unsubscribe:x,useStore:j}}}const x=(t,e)=>{C(t,e,void 0)},C=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{r.subscriptions[`${t}_${e}`]||(r.subscriptions[`${t}_${e}`]=[]),o?r.subscriptions[`${t}_${e}`].push(o):r.subscriptions[`${t}_${e}`]=[]}):(r.subscriptions[`${t}_${e}`]||(r.subscriptions[`${t}_${e}`]=[]),o?r.subscriptions[`${t}_${e}`].push(o):r.subscriptions[`${t}_${e}`]=[])},T=t=>r.State[t],D=(t,e={type:"outer"})=>{const o={};return Object.keys(r.Actions[t]).forEach(n=>o[n]=async(o,s)=>{const a={action:r.Actions[t][n],actionName:n,consumerActions:N,middlewareConfig:s,modelName:t,newState:null,params:o,...e,Global:r};r.Middlewares[t]?await g(r.Middlewares[t],a):await g(f,a)}),o},j=(t,e)=>{const a=o({})[1],c=n("");s(()=>{r.uid+=1;const o=""+r.uid;return c.current=o,r.Setter.functionSetter[t]||(r.Setter.functionSetter[t]={}),r.Setter.functionSetter[t][o]={setState:a,selector:e},function(){delete r.Setter.functionSetter[t][o]}},[]);const i=D(t,{__hash:c.current,type:"function"});return[e?e(T(t)):T(t),i]};class G extends a{constructor(){super(...arguments),this.state=r.State}render(){const{children:t}=this.props;return r.Setter.classSetter=this.setState.bind(this),c(y.Provider,{value:{...this.state}},t)}}const M=(t,e,o)=>n=>class extends a{render(){const{state:s={},actions:a={}}=this.props;return c(h,null,i=>{const{[""+t]:l}=i,S=r.Actions[t];return c(n,Object.assign({},this.props,{state:{...s,...e?e(l):l},actions:{...a,...o?o(N(S,{modelName:t})):N(S,{modelName:t})}}))})}};export{h as Consumer,$ as Model,G as Provider,f as actionMiddlewares,M as connect,A as getInitialState,T as getState,w as middlewares}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("immer"),require("react")):"function"==typeof define&&define.amd?define(["exports","immer","react"],t):t((e=e||self).reactModel={},e.immer,e.react)}(this,function(e,t,n){function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function o(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t;var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},c={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},a=function(e,t){try{var n=e.next,r=c.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},s=function(e,t){try{var n=e.modelName,o=e.next,i=e.Global;return Promise.resolve((0,e.action)(e.params,r({actions:(0,e.consumerActions)(i.Actions[n],{modelName:n}),state:i.State[n]},i.Context.__global||{},i.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[n]))});var c=function(){if(r)return w(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.Global,r=!0===c.logger.enable||"function"==typeof c.logger.enable&&c.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&c.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);j(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},v=[a,m,d,s,u,l,S,f],h={communicator:S,consoleDebugger:m,devToolsListener:d,getNewState:s,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),N(e,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:u,stateUpdater:l,subscription:f,tryCatch:a,config:c},_=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},p=n.createContext({}),b=p.Consumer;if(!console.group){var y=[],g="-".repeat(80);console.group=function(e){y.push(e),console.log("%c \nBEGIN GROUP: %c",g,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",y.pop(),g)}}}var P=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,r){try{return Promise.resolve(_(v,{Global:i,action:e,actionName:e.name,consumerActions:P,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},w=function(e,n){if("function"==typeof n){var o=i.State[e];o=t(o,n),i.State=t(i.State,function(t){t[e]=o})}else i.State=t(i.State,function(t){t[e]=r({},t[e],n)});return i.State},N=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},E=function(e,n){try{var o={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(i.State).map(function(c){try{var a=function(){if(!e||!e.modelName||c===e.modelName||-1!==e.modelName.indexOf(c)){var a=function(e){n&&n.isServer?o[c]=e:i.State=t(i.State,function(t){t[c]=r({},t[c],e)})},s=i.AsyncState[c];return s?Promise.resolve(s(e)).then(a):a({})}}();return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return n&&n.isServer?o:i.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},j=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||e[n[o]]!==t[n[o]])return!1;return!0},A=function(e,t){R(e,t,void 0)},R=function(e,t,n){Array.isArray(t)?t.forEach(function(t){i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[]}):(i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[])},x=function(e){return i.State[e]},C=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(i.Actions[e]).forEach(function(o){var c=o[0],a=o[1];return n[c]=function(n,o){try{var s=r({action:a,actionName:c,consumerActions:P,middlewareConfig:o,modelName:e,newState:null,params:n},t,{Global:i}),u=i.Middlewares[e]?Promise.resolve(_(i.Middlewares[e],s)).then(function(){}):Promise.resolve(_(v,s)).then(function(){});return Promise.resolve(u&&u.then?u.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},T=function(e,t){var r=n.useState({})[1],o=n.useRef("");n.useEffect(function(){i.uid+=1;var n=""+i.uid;return o.current=n,i.Setter.functionSetter[e]||(i.Setter.functionSetter[e]={}),i.Setter.functionSetter[e][n]={setState:r,selector:t},function(){delete i.Setter.functionSetter[e][n]}},[]);var c=C(e,{__hash:o.current,type:"function"});return[t?t(x(e)):x(e),c]},D=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=i.State,t}return o(t,e),t.prototype.render=function(){var e=this.props.children;return i.Setter.classSetter=this.setState.bind(this),n.createElement(p.Provider,{value:r({},this.state)},e)},t}(n.PureComponent);e.Consumer=b,e.Model=function(e,n,o){if(void 0!==e.state){i.uid+=1;var c="__"+i.uid;i.State=t(i.State,function(t){t[c]=e.state}),e.middlewares&&(i.Middlewares[c]=e.middlewares),i.Actions[c]=e.actions,i.AsyncState[c]=e.asyncState,n&&(i.Context[c]=n);var a=C(c);return{__id:c,actions:a,getState:function(){return x(c)},subscribe:function(e,t){return R(c,e,t)},unsubscribe:function(e){return A(c,e)},useStore:function(e){return T(c,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var s=function(e){return function(){return e}};return{__ERROR__:!0,actions:s({}),getActions:s({}),getInitialState:s({}),getState:s({}),subscribe:s(),unsubscribe:s(),useStore:s([{},{}])}}n&&!n.__FROM_SERVER__&&(i.State=t(i.State,function(e){Object.assign(e,n||{})})),o&&(i.Context.__global=o),Object.entries(e).forEach(function(e){var o=e[0],c=e[1];if(c.__ERROR__)return console.error(o+" model's schema is invalid"),i.State=t(i.State,function(e){e[o]={}}),void(i.Actions[o]={});void 0===c.useStore?(n&&n.__FROM_SERVER__?i.State=t(i.State,function(e){e[o]=r({},c.state,n[o])}):i.State[o]||(i.State=t(i.State,function(e){e[o]=c.state})),c.middlewares&&(i.Middlewares[o]=c.middlewares),i.Actions[o]=c.actions,i.AsyncState[o]=c.asyncState):(i.State[o]&&n||(i.State=t(i.State,function(e){e[o]=e[c.__id]})),n&&n.__FROM_SERVER__&&(i.State=t(i.State,function(e){e[o]=r({},e[c.__id],n[o])})),i.Actions[o]=i.Actions[c.__id],i.AsyncState[o]=i.AsyncState[c.__id],i.Middlewares[o]=i.Middlewares[c.__id],i.Context[o]=i.Context[c.__id])});var u=Object.keys(e).reduce(function(e,t){var n;return r({},e,((n={})[t]=C(t),n))},{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:u,getActions:C,getInitialState:E,getState:x,subscribe:R,unsubscribe:A,useStore:T}},e.Provider=D,e.actionMiddlewares=v,e.connect=function(e,t,c){return function(a){return function(s){function u(){return s.apply(this,arguments)||this}return o(u,s),u.prototype.render=function(){var o=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(b,null,function(s){var u=s[""+e],f=i.Actions[e];return n.createElement(a,Object.assign({},o.props,{state:r({},l,t?t(u):u),actions:r({},m,c?c(P(f,{modelName:e})):P(f,{modelName:e}))}))})},u}(n.PureComponent)}},e.getInitialState=E,e.getState=x,e.middlewares=h}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("immer"),require("react")):"function"==typeof define&&define.amd?define(["exports","immer","react"],t):t((e=e||self).reactModel={},e.immer,e.react)}(this,function(e,t,n){function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function o(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t;var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},c={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},s=function(e,t){try{var n=e.next,r=c.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,o=e.next,i=e.Global;return Promise.resolve((0,e.action)(e.params,r({actions:(0,e.consumerActions)(i.Actions[n],{modelName:n}),state:i.State[n]},i.Context.__global||{},i.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global;i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[n]))});var c=function(){if(r)return w(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.Global,r=!0===c.logger.enable||"function"==typeof c.logger.enable&&c.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&c.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);j(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},v=[s,m,d,a,u,l,S,f],h={communicator:S,consoleDebugger:m,devToolsListener:d,getNewState:a,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),N(e,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:u,stateUpdater:l,subscription:f,tryCatch:s,config:c},_=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},p=n.createContext({}),b=p.Consumer;if(!console.group){var y=[],g="-".repeat(80);console.group=function(e){y.push(e),console.log("%c \nBEGIN GROUP: %c",g,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",y.pop(),g)}}}var P=function(e,t){var n={};return Object.keys(e).forEach(function(r){n[r]=function(e,t){return function(n,r){try{return Promise.resolve(_(v,{Global:i,action:e,actionName:e.name,consumerActions:P,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[r],t)}),n},w=function(e,n){if("function"==typeof n){var o=i.State[e];o=t(o,n),i.State=t(i.State,function(t){t[e]=o})}else i.State=t(i.State,function(t){t[e]=r({},t[e],n)});return i.State},N=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},E=function(e,n){try{var o={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(i.State).map(function(c){try{var s=function(){if(!e||!e.modelName||c===e.modelName||-1!==e.modelName.indexOf(c)){var s=function(e){n&&n.isServer?o[c]=e:i.State=t(i.State,function(t){t[c]=r({},t[c],e)})},a=i.AsyncState[c];return a?Promise.resolve(a(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return n&&n.isServer?o:i.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},j=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||e[n[o]]!==t[n[o]])return!1;return!0},A=function(e,t){R(e,t,void 0)},R=function(e,t,n){Array.isArray(t)?t.forEach(function(t){i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[]}):(i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),n?i.subscriptions[e+"_"+t].push(n):i.subscriptions[e+"_"+t]=[])},x=function(e){return i.State[e]},C=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.keys(i.Actions[e]).forEach(function(o){return n[o]=function(n,c){try{var s=r({action:i.Actions[e][o],actionName:o,consumerActions:P,middlewareConfig:c,modelName:e,newState:null,params:n},t,{Global:i}),a=i.Middlewares[e]?Promise.resolve(_(i.Middlewares[e],s)).then(function(){}):Promise.resolve(_(v,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},T=function(e,t){var r=n.useState({})[1],o=n.useRef("");n.useEffect(function(){i.uid+=1;var n=""+i.uid;return o.current=n,i.Setter.functionSetter[e]||(i.Setter.functionSetter[e]={}),i.Setter.functionSetter[e][n]={setState:r,selector:t},function(){delete i.Setter.functionSetter[e][n]}},[]);var c=C(e,{__hash:o.current,type:"function"});return[t?t(x(e)):x(e),c]},D=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=i.State,t}return o(t,e),t.prototype.render=function(){var e=this.props.children;return i.Setter.classSetter=this.setState.bind(this),n.createElement(p.Provider,{value:r({},this.state)},e)},t}(n.PureComponent);e.Consumer=b,e.Model=function(e,n,o){if(void 0!==e.state){i.uid+=1;var c="__"+i.uid;i.State=t(i.State,function(t){t[c]=e.state}),e.middlewares&&(i.Middlewares[c]=e.middlewares),i.Actions[c]=e.actions,i.AsyncState[c]=e.asyncState,n&&(i.Context[c]=n);var s=C(c);return{__id:c,actions:s,getState:function(){return x(c)},subscribe:function(e,t){return R(c,e,t)},unsubscribe:function(e){return A(c,e)},useStore:function(e){return T(c,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var a=function(e){return function(){return e}};return{__ERROR__:!0,actions:a({}),getActions:a({}),getInitialState:a({}),getState:a({}),subscribe:a(),unsubscribe:a(),useStore:a([{},{}])}}n&&!n.__FROM_SERVER__&&(i.State=t(i.State,function(e){Object.assign(e,n||{})})),o&&(i.Context.__global=o),Object.keys(e).forEach(function(o){var c=e[o];if(c.__ERROR__)return console.error(o+" model's schema is invalid"),i.State=t(i.State,function(e){e[o]={}}),void(i.Actions[o]={});void 0===c.useStore?(n&&n.__FROM_SERVER__?i.State=t(i.State,function(e){e[o]=r({},c.state,n[o])}):i.State[o]||(i.State=t(i.State,function(e){e[o]=c.state})),c.middlewares&&(i.Middlewares[o]=c.middlewares),i.Actions[o]=c.actions,i.AsyncState[o]=c.asyncState):(i.State[o]&&n||(i.State=t(i.State,function(e){e[o]=e[c.__id]})),n&&n.__FROM_SERVER__&&(i.State=t(i.State,function(e){e[o]=r({},e[c.__id],n[o])})),i.Actions[o]=i.Actions[c.__id],i.AsyncState[o]=i.AsyncState[c.__id],i.Middlewares[o]=i.Middlewares[c.__id],i.Context[o]=i.Context[c.__id])});var u=Object.keys(e).reduce(function(e,t){var n;return r({},e,((n={})[t]=C(t),n))},{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:u,getActions:C,getInitialState:E,getState:x,subscribe:R,unsubscribe:A,useStore:T}},e.Provider=D,e.actionMiddlewares=v,e.connect=function(e,t,c){return function(s){return function(a){function u(){return a.apply(this,arguments)||this}return o(u,a),u.prototype.render=function(){var o=this,a=this.props,u=a.state,l=void 0===u?{}:u,f=a.actions,m=void 0===f?{}:f;return n.createElement(b,null,function(a){var u=a[""+e],f=i.Actions[e];return n.createElement(s,Object.assign({},o.props,{state:r({},l,t?t(u):u),actions:r({},m,c?c(P(f,{modelName:e})):P(f,{modelName:e}))}))})},u}(n.PureComponent)}},e.getInitialState=E,e.getState=x,e.middlewares=h}); |
{ | ||
"name": "react-model", | ||
"version": "4.0.0-rc.3", | ||
"version": "4.0.0-rc.4", | ||
"description": "The State management library for React", | ||
@@ -70,3 +70,11 @@ "main": "./dist/react-model.js", | ||
} | ||
} | ||
}, | ||
"browserslist": [ | ||
"edge 17", | ||
"firefox 70", | ||
"chrome 48", | ||
"safari 12.1", | ||
"android 4.0", | ||
"samsung 9.2" | ||
] | ||
} |
@@ -46,4 +46,5 @@ import produce from 'immer' | ||
const ret: any = {} | ||
Object.entries<Action>(actions).forEach(([key, action]) => { | ||
ret[key] = consumerAction(action, modelContext) | ||
Object.keys(actions).forEach((key) => { | ||
// @ts-ignore | ||
ret[key] = consumerAction(actions[key], modelContext) | ||
}) | ||
@@ -50,0 +51,0 @@ return ret |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95803
1203