react-model
Advanced tools
Comparing version 4.3.0-alpha.1 to 4.3.0-rc.0
@@ -1,1 +0,1 @@ | ||
import t,{enableES5 as e}from"immer";import{createContext as r,useState as n,useRef as o,createElement as i,useEffect as c,useLayoutEffect as a,PureComponent as s}from"react";function u(){return(u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function l(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var f={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},m={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},S=function(t,e){try{var r=t.next;return Promise.resolve(m.tryCatch.enable?r(e).catch(function(t){return console.log(t)}):r(e))}catch(t){return Promise.reject(t)}},d=function(t,e){try{var r=function(){return Promise.resolve(a(e))},n=t.action,o=t.modelName,i=t.consumerActions,c=t.params,a=t.next,s=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(c,u({actions:i(s.Actions[o],{modelName:o}),state:s.State[o]},s.Context.__global||{},s.Context[o]||{}))).then(function(e){t.newState=e||null});t.newState=n()}();return Promise.resolve(f&&f.then?f.then(r):r())}catch(t){return Promise.reject(t)}},b=function(t,e){try{var r=t.modelName,n=t.newState,o=t.next,i=t.Global,c=t.type;return i.Setter.functionSetter[r]&&!t.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[r]).map(function(t){var e=i.Setter.functionSetter[r][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(i.State[r]))}),Promise.resolve(function(){if(n||"u"===c)return R(r,n||{}),Promise.resolve(o(e))}())}catch(t){return Promise.reject(t)}},v=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i=t.__hash,c=o.Setter.functionSetter[r];return"f"===t.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[r]),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},p=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i="u"===t.type?o.subscriptions[r]:o.subscriptions[r+"_"+t.actionName];return i&&i.forEach(function(e){e(t)}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},_=function(t,e){try{var r=t.Global;return!0===m.logger.enable||"function"==typeof m.logger.enable&&m.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",r.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),Promise.resolve(t.next(e)).then(function(e){return console.log("%c Next","color: #4CAF50; font-weight: bold",r.State[t.modelName]),console.groupEnd(),e})):Promise.resolve(t.next(e))}catch(t){return Promise.reject(t)}},h=function(t,e){try{var r=t.Global;return Promise.resolve(t.next(e)).then(function(e){return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&P.config.devtools.enable&&!r.devTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),r.withDevTools&&m.devtools.enable&&r.devTools.send("u"===t.type&&t.disableSelectorUpdate?"store["+t.modelName+"].update":t.modelName+"_"+t.actionName,r.mutableState[t.modelName],void 0,t.modelName),e})}catch(t){return Promise.reject(t)}},y=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i=t.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[r]&&Object.keys(o.Setter.functionSetter[r]).map(function(t){var e=o.Setter.functionSetter[r][t];if(e)if(!e.selector||i)e.setState(o.State[r]);else{var n=e.selector(o.State[r]);G(n,e.selectorRef)||(e.selectorRef=n,e.setState(o.State[r]))}}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},g=[S,_,h,d,b,v,y,p],P={communicator:y,consoleDebugger:_,devToolsListener:h,getNewState:d,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,r){try{var n=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)(n.Actions[o],{modelName:o}),state:n.State[o]}),x(t,T(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(i(r))})}catch(t){return Promise.reject(t)}}},setNewState:b,stateUpdater:v,subscription:p,tryCatch:S,config:m},w=function(t,e){try{return e.next=function(t){return t.length>0?t[0](e,t.slice(1)):e.newState},Promise.resolve(t[0](e,t.slice(1)))}catch(t){return Promise.reject(t)}},N=r({}),O=N.Consumer;if(!console.group){var j=[],A="-".repeat(80);console.group=function(t){j.push(t),console.log("%c \nBEGIN GROUP: %c",A,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",j.pop(),A)}}}var E=function(t,e){var r={};return Object.keys(t).forEach(function(n){r[n]=function(t,e){return function(r,n){try{return Promise.resolve(w(g,{Global:f,action:t,actionName:t.name,consumerActions:E,middlewareConfig:n,modelName:e.modelName,newState:null,params:r,type:"o"}))}catch(t){return Promise.reject(t)}}}(t[n],e)}),r},R=function(e,r){if("function"==typeof r){var n=f.State[e];n=t(n,r),f.State=t(f.State,function(t){t[e]=n})}else f.State=t(f.State,function(t){t[e]=u({},t[e],r)});return f.State},x=function(t,e){return new Promise(function(r){return setTimeout(function(){console.log(t),r(e)},t)})},C=function(e,r){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(f.State).map(function(o){try{var i=r&&r.prefix||"",c=function(){if(!e||!e.modelName||o===i+e.modelName||-1!==e.modelName.indexOf(i+o)){var c=function(e){r&&r.isServer?n[o]=e:f.State=t(f.State,function(t){t[o]=u({},t[o],e)})},a=f.AsyncState[o];return a?Promise.resolve(a(e)).then(c):c({})}}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return r&&r.isServer?n:f.State})}catch(t){return Promise.reject(t)}},T=function(t,e){var r=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return r?JSON.parse(r):null},G=function(t,e){if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(var o=0;o<r.length;o++)if(!Object.prototype.hasOwnProperty.call(e,r[o])||t[r[o]]!==e[r[o]])return!1;return!0};e();var I="undefined"==typeof window?c:a;function D(e){var r=f.currentStoreId,n=f.mutableState[r].count;return f.mutableState[r].count+=1,f.mutableState[r].hasOwnProperty(n)||(f.mutableState[r][n]="function"==typeof e?e():e),[f.mutableState[r][n],function(e){try{return f.mutableState[r][n]="function"==typeof e?t(f.mutableState[r][n],e):f.mutableState[r][n]&&e&&"Object"===f.mutableState[r][n].constructor.name&&"Object"===e.constructor.name?u({},f.mutableState[r][n],e):e,Promise.resolve(w(g,{Global:f,action:function(){return"function"==typeof e?f.mutableState[r][n]:e},actionName:"setter",consumerActions:E,disableSelectorUpdate:!0,middlewareConfig:{},modelName:r,newState:{},params:void 0,type:"u"}))}catch(t){return Promise.reject(t)}}]}function M(t,e){var r="string"==typeof t;f.storeId+=r?0:1;var n=r?t:f.storeId.toString();f.Actions[n]||(f.Actions[n]={}),f.mutableState[n]||(f.mutableState[n]={count:0,cachedResult:void 0});var o=function(){return f.mutableState[n].count=0,f.currentStoreId=n,f.mutableState[n].cachedResult=e?e():t(),f.mutableState[n].cachedResult};return f.mutableState[n].selector=o,{useStore:function(){return X(n,o)},getState:function(){return o()},getStore:function(){return f.mutableState[n].cachedResult},subscribe:function(t){f.subscriptions[n]||(f.subscriptions[n]=[]),f.subscriptions[n].push(t)},unsubscribe:function(t){if(f.subscriptions[n]&&t){var e=f.subscriptions[n].indexOf(t);e>=0&&f.subscriptions[n].splice(e,1)}}}}function k(e,r,n){if(void 0!==e.state){f.storeId+=1;var o="__"+f.storeId;f.State=t(f.State,function(t){t[o]=e.state}),e.middlewares&&(f.Middlewares[o]=e.middlewares),f.Actions[o]=e.actions,f.AsyncState[o]=e.asyncState,r&&(f.Context[o]=r);var i=L(o);return{__id:o,actions:i,getState:function(){return V(o)},subscribe:function(t,e){return F(o,t,e)},unsubscribe:function(t){return U(o,t)},useStore:function(t){return X(o,t)}}}r?f.gid=1:f.gid+=1;var c="";if(f.gid>1&&(c=f.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var a=function(t){return function(){return t}};return{__ERROR__:!0,actions:a({}),getActions:a({}),getInitialState:a({}),getState:a({}),subscribe:a(),unsubscribe:a(),useStore:a([{},{}])}}r&&!r.__FROM_SERVER__&&(f.State=t(f.State,function(t){Object.assign(t,r||{})})),n&&(f.Context.__global=n);var s={};return Object.keys(e).forEach(function(n){var o=c+n,i=e[n];if(i.__ERROR__)return console.error(o+" model's schema is invalid"),f.State=t(f.State,function(t){t[o]={}}),void(f.Actions[o]={});!function(t){return void 0!==t.useStore}(i)?(r&&r.__FROM_SERVER__?f.State=t(f.State,function(t){t[o]=u({},i.state,r[o])}):f.State[o]||(f.State=t(f.State,function(t){t[o]=i.state})),i.middlewares&&(f.Middlewares[o]=i.middlewares),f.Actions[o]=i.actions,f.AsyncState[o]=i.asyncState):(f.State[o]&&r||(f.State=t(f.State,function(t){t[o]=t[i.__id]})),r&&r.__FROM_SERVER__&&(f.State=t(f.State,function(t){t[o]=u({},t[i.__id],r[o])})),f.Actions[o]=f.Actions[i.__id],f.AsyncState[o]=f.AsyncState[i.__id],f.Middlewares[o]=f.Middlewares[i.__id],f.Context[o]=f.Context[i.__id]),s[n]=L(o)}),{actions:s,getActions:function(t){return L(c+t)},getInitialState:function(t,e){try{return Promise.resolve(C(t,u({},e,{prefix:c})))}catch(t){return Promise.reject(t)}},getState:function(t){return V(c+t)},subscribe:function(t,e,r){return F(c+t,e,r)},unsubscribe:function(t,e){return U(c+t,e)},useStore:function(t,e){return X(c+t,e)}}}var U=function(t,e){F(t,e,void 0)},F=function(t,e,r){Array.isArray(e)?e.forEach(function(e){f.subscriptions[t+"_"+e]||(f.subscriptions[t+"_"+e]=[]),r?f.subscriptions[t+"_"+e].push(r):f.subscriptions[t+"_"+e]=[]}):(f.subscriptions[t+"_"+e]||(f.subscriptions[t+"_"+e]=[]),r?f.subscriptions[t+"_"+e].push(r):f.subscriptions[t+"_"+e]=[])},V=function(t){return f.State[t]},L=function(t,e){void 0===e&&(e={type:"o"});var r={};return Object.keys(f.Actions[t]).forEach(function(n){return r[n]=function(r,o){try{var i=u({action:f.Actions[t][n],actionName:n,consumerActions:E,middlewareConfig:o,modelName:t,newState:null,params:r},e,{Global:f});return Promise.resolve(w(f.Middlewares[t]?f.Middlewares[t]:g,i))}catch(t){return Promise.reject(t)}}}),r},X=function(t,e){var r,i=n({})[1],c=o(""),a=(r=[t],function(t){return r.reduce(function(t,e){return t&&t[e]?t[e]:null},t)})(f.mutableState),s=!!a,u=s?a.selector:e,l=s?a:V(t);if(I(function(){f.uid+=1;var e=""+f.uid;return c.current=e,f.Setter.functionSetter[t]||(f.Setter.functionSetter[t]={}),f.Setter.functionSetter[t][e]={setState:i,selector:u},function(){delete f.Setter.functionSetter[t][e]}},[]),s)return u(l);var m=L(t,{__hash:c.current,type:"f"});return[u?u(l):l,m]},B=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=f.State,e}return l(e,t),e.prototype.render=function(){var t=this.props.children;return f.Setter.classSetter=this.setState.bind(this),i(N.Provider,{value:u({},this.state)},t)},e}(s),J=function(t,e,r){return function(n){return function(o){function c(){return o.apply(this,arguments)||this}return l(c,o),c.prototype.render=function(){var o=this,c=this.props,a=c.state,s=void 0===a?{}:a,l=c.actions,m=void 0===l?{}:l;return i(O,null,function(c){var a=c[""+t],l=f.Actions[t];return i(n,Object.assign({},o.props,{state:u({},s,e?e(a):a),actions:u({},m,r?r(E(l,{modelName:t})):E(l,{modelName:t}))}))})},c}(s)}};export{O as Consumer,k as Model,B as Provider,g as actionMiddlewares,J as connect,M as createStore,C as getInitialState,V as getState,P as middlewares,D as useModel}; | ||
import t,{enableES5 as e}from"immer";import{createContext as r,useState as n,useRef as o,createElement as i,useEffect as c,useLayoutEffect as a,PureComponent as s}from"react";function u(){return(u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function l(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var f={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},m={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},S=function(t,e){try{var r=t.next;return Promise.resolve(m.tryCatch.enable?r(e).catch(function(t){return console.log(t)}):r(e))}catch(t){return Promise.reject(t)}},d=function(t,e){try{var r=function(){return Promise.resolve(a(e))},n=t.action,o=t.modelName,i=t.consumerActions,c=t.params,a=t.next,s=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(c,u({actions:i(s.Actions[o],{modelName:o}),state:s.State[o]},s.Context.__global||{},s.Context[o]||{}))).then(function(e){t.newState=e||null});t.newState=n()}();return Promise.resolve(f&&f.then?f.then(r):r())}catch(t){return Promise.reject(t)}},b=function(t,e){try{var r=t.modelName,n=t.newState,o=t.next,i=t.Global,c=t.type;return i.Setter.functionSetter[r]&&!t.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[r]).map(function(t){var e=i.Setter.functionSetter[r][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(i.State[r]))}),Promise.resolve(function(){if(n||"u"===c)return R(r,n||{}),Promise.resolve(o(e))}())}catch(t){return Promise.reject(t)}},v=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i=t.__hash,c=o.Setter.functionSetter[r];return"f"===t.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[r]),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},p=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i="u"===t.type?o.subscriptions[r]:o.subscriptions[r+"_"+t.actionName];return i&&i.forEach(function(e){e(t)}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},_=function(t,e){try{var r=t.Global;return!0===m.logger.enable||"function"==typeof m.logger.enable&&m.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",r.State[t.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",t.actionName,"payload: "+t.params),Promise.resolve(t.next(e)).then(function(e){return console.log("%c Next","color: #4CAF50; font-weight: bold",r.State[t.modelName]),console.groupEnd(),e})):Promise.resolve(t.next(e))}catch(t){return Promise.reject(t)}},h=function(t,e){try{var r=t.Global;return Promise.resolve(t.next(e)).then(function(e){return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&P.config.devtools.enable&&!r.devTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),r.withDevTools&&m.devtools.enable&&r.devTools.send("u"===t.type&&t.disableSelectorUpdate?"store["+t.modelName+"].update":t.modelName+"_"+t.actionName,r.mutableState[t.modelName],void 0,t.modelName),e})}catch(t){return Promise.reject(t)}},y=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i=t.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[r]&&Object.keys(o.Setter.functionSetter[r]).map(function(t){var e=o.Setter.functionSetter[r][t];if(e)if(!e.selector||i)e.setState(o.State[r]);else{var n=e.selector(o.State[r]);G(n,e.selectorRef)||(e.selectorRef=n,e.setState(o.State[r]))}}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},g=[S,_,h,d,b,v,y,p],P={communicator:y,consoleDebugger:_,devToolsListener:h,getNewState:d,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,r){try{var n=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)(n.Actions[o],{modelName:o}),state:n.State[o]}),x(t,T(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(i(r))})}catch(t){return Promise.reject(t)}}},setNewState:b,stateUpdater:v,subscription:p,tryCatch:S,config:m},w=function(t,e){try{return e.next=function(t){return t.length>0?t[0](e,t.slice(1)):e.newState},Promise.resolve(t[0](e,t.slice(1)))}catch(t){return Promise.reject(t)}},N=r({}),O=N.Consumer;if(!console.group){var j=[],A="-".repeat(80);console.group=function(t){j.push(t),console.log("%c \nBEGIN GROUP: %c",A,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",j.pop(),A)}}}var E=function(t,e){var r={};return Object.keys(t).forEach(function(n){r[n]=function(t,e){return function(r,n){try{return Promise.resolve(w(g,{Global:f,action:t,actionName:t.name,consumerActions:E,middlewareConfig:n,modelName:e.modelName,newState:null,params:r,type:"o"}))}catch(t){return Promise.reject(t)}}}(t[n],e)}),r},R=function(e,r){if("function"==typeof r){var n=f.State[e];n=t(n,r),f.State=t(f.State,function(t){t[e]=n})}else f.State=t(f.State,function(t){t[e]=u({},t[e],r)});return f.State},x=function(t,e){return new Promise(function(r){return setTimeout(function(){console.log(t),r(e)},t)})},C=function(e,r){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(f.State).map(function(o){try{var i=r&&r.prefix||"",c=function(){if(!e||!e.modelName||o===i+e.modelName||-1!==e.modelName.indexOf(i+o)){var c=function(e){r&&r.isServer?n[o]=e:f.State=t(f.State,function(t){t[o]=u({},t[o],e)})},a=f.AsyncState[o];return a?Promise.resolve(a(e)).then(c):c({})}}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return r&&r.isServer?n:f.State})}catch(t){return Promise.reject(t)}},T=function(t,e){var r=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return r?JSON.parse(r):null},G=function(t,e){if(t===e)return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(var o=0;o<r.length;o++)if(!Object.prototype.hasOwnProperty.call(e,r[o])||t[r[o]]!==e[r[o]])return!1;return!0};e();var I="undefined"==typeof window?c:a;function D(e){var r=f.currentStoreId,n=f.mutableState[r].count;return f.mutableState[r].count+=1,f.mutableState[r].hasOwnProperty(n)||(f.mutableState[r][n]="function"==typeof e?e():e),[f.mutableState[r][n],function(e){try{return f.mutableState[r][n]="function"==typeof e?t(f.mutableState[r][n],e):f.mutableState[r][n]&&e&&"Object"===f.mutableState[r][n].constructor.name&&"Object"===e.constructor.name?u({},f.mutableState[r][n],e):e,Promise.resolve(w(g,{Global:f,action:function(){return"function"==typeof e?f.mutableState[r][n]:e},actionName:"setter",consumerActions:E,disableSelectorUpdate:!0,middlewareConfig:{},modelName:r,newState:{},params:void 0,type:"u"}))}catch(t){return Promise.reject(t)}}]}function M(t,e){var r="string"==typeof t;f.storeId+=r?0:1;var n=r?t:f.storeId.toString();f.Actions[n]||(f.Actions[n]={}),f.mutableState[n]||(f.mutableState[n]={count:0});var o=function(){return f.mutableState[n].count=0,f.currentStoreId=n,f.mutableState[n].cachedResult=e?e():t(),f.mutableState[n].cachedResult};return f.mutableState[n].selector=o,{useStore:function(){return X(n,o)},getState:function(){return o()},getStore:function(){return f.mutableState[n].cachedResult},subscribe:function(t){f.subscriptions[n]||(f.subscriptions[n]=[]),f.subscriptions[n].push(t)},unsubscribe:function(t){if(f.subscriptions[n]&&t){var e=f.subscriptions[n].indexOf(t);e>=0&&f.subscriptions[n].splice(e,1)}}}}function k(e,r,n){if(void 0!==e.state){f.storeId+=1;var o="__"+f.storeId;f.State=t(f.State,function(t){t[o]=e.state}),e.middlewares&&(f.Middlewares[o]=e.middlewares),f.Actions[o]=e.actions,f.AsyncState[o]=e.asyncState,r&&(f.Context[o]=r);var i=L(o);return{__id:o,actions:i,getState:function(){return V(o)},subscribe:function(t,e){return F(o,t,e)},unsubscribe:function(t){return U(o,t)},useStore:function(t){return X(o,t)}}}r?f.gid=1:f.gid+=1;var c="";if(f.gid>1&&(c=f.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var a=function(t){return function(){return t}};return{__ERROR__:!0,actions:a({}),getActions:a({}),getInitialState:a({}),getState:a({}),subscribe:a(),unsubscribe:a(),useStore:a([{},{}])}}r&&!r.__FROM_SERVER__&&(f.State=t(f.State,function(t){Object.assign(t,r||{})})),n&&(f.Context.__global=n);var s={};return Object.keys(e).forEach(function(n){var o=c+n,i=e[n];if(i.__ERROR__)return console.error(o+" model's schema is invalid"),f.State=t(f.State,function(t){t[o]={}}),void(f.Actions[o]={});!function(t){return void 0!==t.useStore}(i)?(r&&r.__FROM_SERVER__?f.State=t(f.State,function(t){t[o]=u({},i.state,r[o])}):f.State[o]||(f.State=t(f.State,function(t){t[o]=i.state})),i.middlewares&&(f.Middlewares[o]=i.middlewares),f.Actions[o]=i.actions,f.AsyncState[o]=i.asyncState):(f.State[o]&&r||(f.State=t(f.State,function(t){t[o]=t[i.__id]})),r&&r.__FROM_SERVER__&&(f.State=t(f.State,function(t){t[o]=u({},t[i.__id],r[o])})),f.Actions[o]=f.Actions[i.__id],f.AsyncState[o]=f.AsyncState[i.__id],f.Middlewares[o]=f.Middlewares[i.__id],f.Context[o]=f.Context[i.__id]),s[n]=L(o)}),{actions:s,getActions:function(t){return L(c+t)},getInitialState:function(t,e){try{return Promise.resolve(C(t,u({},e,{prefix:c})))}catch(t){return Promise.reject(t)}},getState:function(t){return V(c+t)},subscribe:function(t,e,r){return F(c+t,e,r)},unsubscribe:function(t,e){return U(c+t,e)},useStore:function(t,e){return X(c+t,e)}}}var U=function(t,e){F(t,e,void 0)},F=function(t,e,r){Array.isArray(e)?e.forEach(function(e){f.subscriptions[t+"_"+e]||(f.subscriptions[t+"_"+e]=[]),r?f.subscriptions[t+"_"+e].push(r):f.subscriptions[t+"_"+e]=[]}):(f.subscriptions[t+"_"+e]||(f.subscriptions[t+"_"+e]=[]),r?f.subscriptions[t+"_"+e].push(r):f.subscriptions[t+"_"+e]=[])},V=function(t){return f.State[t]},L=function(t,e){void 0===e&&(e={type:"o"});var r={};return Object.keys(f.Actions[t]).forEach(function(n){return r[n]=function(r,o){try{var i=u({action:f.Actions[t][n],actionName:n,consumerActions:E,middlewareConfig:o,modelName:t,newState:null,params:r},e,{Global:f});return Promise.resolve(w(f.Middlewares[t]?f.Middlewares[t]:g,i))}catch(t){return Promise.reject(t)}}}),r},X=function(t,e){var r,i=n({})[1],c=o(""),a=(r=[t],function(t){return r.reduce(function(t,e){return t&&t[e]?t[e]:null},t)})(f.mutableState),s=!!a,u=s?a.selector:e,l=s?a:V(t);if(I(function(){f.uid+=1;var e=""+f.uid;return c.current=e,f.Setter.functionSetter[t]||(f.Setter.functionSetter[t]={}),f.Setter.functionSetter[t][e]={setState:i,selector:u},function(){delete f.Setter.functionSetter[t][e]}},[]),s)return u(l);var m=L(t,{__hash:c.current,type:"f"});return[u?u(l):l,m]},B=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=f.State,e}return l(e,t),e.prototype.render=function(){var t=this.props.children;return f.Setter.classSetter=this.setState.bind(this),i(N.Provider,{value:u({},this.state)},t)},e}(s),J=function(t,e,r){return function(n){return function(o){function c(){return o.apply(this,arguments)||this}return l(c,o),c.prototype.render=function(){var o=this,c=this.props,a=c.state,s=void 0===a?{}:a,l=c.actions,m=void 0===l?{}:l;return i(O,null,function(c){var a=c[""+t],l=f.Actions[t];return i(n,Object.assign({},o.props,{state:u({},s,e?e(a):a),actions:u({},m,r?r(E(l,{modelName:t})):E(l,{modelName:t}))}))})},c}(s)}};export{O as Consumer,k as Model,B as Provider,g as actionMiddlewares,J as connect,M as createStore,C as getInitialState,V as getState,P as middlewares,D as useModel}; |
@@ -1,1 +0,1 @@ | ||
var e,t=require("immer"),r=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,n=require("react");function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function i(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var a={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},c={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var r=e.next;return Promise.resolve(c.tryCatch.enable?r(t).catch(function(e){return console.log(e)}):r(t))}catch(e){return Promise.reject(e)}},u=function(e,t){try{var r=function(){return Promise.resolve(s(t))},n=e.action,i=e.modelName,a=e.consumerActions,c=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(n(c,o({actions:a(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).then(function(t){e.newState=t||null});e.newState=n()}();return Promise.resolve(f&&f.then?f.then(r):r())}catch(e){return Promise.reject(e)}},l=function(e,t){try{var r=e.modelName,n=e.newState,o=e.next,i=e.Global,a=e.type;return i.Setter.functionSetter[r]&&!e.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[r]).map(function(e){var t=i.Setter.functionSetter[r][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[r]))}),Promise.resolve(function(){if(n||"u"===a)return N(r,n||{}),Promise.resolve(o(t))}())}catch(e){return Promise.reject(e)}},f=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i=e.__hash,a=o.Setter.functionSetter[r];return"f"===e.type&&i&&a&&a[i]&&a[i].setState&&a[i].setState(o.State[r]),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i="u"===e.type?o.subscriptions[r]:o.subscriptions[r+"_"+e.actionName];return i&&i.forEach(function(t){t(e)}),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var r=e.Global;return!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",r.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(t){return console.log("%c Next","color: #4CAF50; font-weight: bold",r.State[e.modelName]),console.groupEnd(),t})):Promise.resolve(e.next(t))}catch(e){return Promise.reject(e)}},d=function(e,t){try{var r=e.Global;return Promise.resolve(e.next(t)).then(function(t){return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&v.config.devtools.enable&&!r.devTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),r.withDevTools&&c.devtools.enable&&r.devTools.send("u"===e.type&&e.disableSelectorUpdate?"store["+e.modelName+"].update":e.modelName+"_"+e.actionName,r.mutableState[e.modelName],void 0,e.modelName),t})}catch(e){return Promise.reject(e)}},p=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i=e.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[r]&&Object.keys(o.Setter.functionSetter[r]).map(function(e){var t=o.Setter.functionSetter[r][e];if(t)if(!t.selector||i)t.setState(o.State[r]);else{var n=t.selector(o.State[r]);x(n,t.selectorRef)||(t.selectorRef=n,t.setState(o.State[r]))}}),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},b=[s,S,d,u,l,f,p,m],v={communicator:p,consoleDebugger:S,devToolsListener:d,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,r){try{var n=t.Global,o=t.modelName,i=t.next,a=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(n.Actions[o],{modelName:o}),state:n.State[o]}),E(e,j(o,a))])).then(function(e){return t.newState=e||null,Promise.resolve(i(r))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:c},_=function(e,t){try{return t.next=function(e){return e.length>0?e[0](t,e.slice(1)):t.newState},Promise.resolve(e[0](t,e.slice(1)))}catch(e){return Promise.reject(e)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",P,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=function(e,t){var r={};return Object.keys(e).forEach(function(n){r[n]=function(e,t){return function(r,n){try{return Promise.resolve(_(b,{Global:a,action:e,actionName:e.name,consumerActions:w,middlewareConfig:n,modelName:t.modelName,newState:null,params:r,type:"o"}))}catch(e){return Promise.reject(e)}}}(e[n],t)}),r},N=function(e,t){if("function"==typeof t){var n=a.State[e];n=r(n,t),a.State=r(a.State,function(t){t[e]=n})}else a.State=r(a.State,function(r){r[e]=o({},r[e],t)});return a.State},E=function(e,t){return new Promise(function(r){return setTimeout(function(){console.log(e),r(t)},e)})},O=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(a.State).map(function(i){try{var c=t&&t.prefix||"",s=function(){if(!e||!e.modelName||i===c+e.modelName||-1!==e.modelName.indexOf(c+i)){var s=function(e){t&&t.isServer?n[i]=e:a.State=r(a.State,function(t){t[i]=o({},t[i],e)})},u=a.AsyncState[i];return u?Promise.resolve(u(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:a.State})}catch(e){return Promise.reject(e)}},j=function(e,t){var r=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return r?JSON.parse(r):null},x=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var r=Object.keys(e),n=Object.keys(t);if(r.length!==n.length)return!1;for(var o=0;o<r.length;o++)if(!Object.prototype.hasOwnProperty.call(t,r[o])||e[r[o]]!==t[r[o]])return!1;return!0};t.enableES5();var A="undefined"==typeof window?n.useEffect:n.useLayoutEffect,R=function(e,t){C(e,t,void 0)},C=function(e,t,r){Array.isArray(t)?t.forEach(function(t){a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),r?a.subscriptions[e+"_"+t].push(r):a.subscriptions[e+"_"+t]=[]}):(a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),r?a.subscriptions[e+"_"+t].push(r):a.subscriptions[e+"_"+t]=[])},T=function(e){return a.State[e]},I=function(e,t){void 0===t&&(t={type:"o"});var r={};return Object.keys(a.Actions[e]).forEach(function(n){return r[n]=function(r,i){try{var c=o({action:a.Actions[e][n],actionName:n,consumerActions:w,middlewareConfig:i,modelName:e,newState:null,params:r},t,{Global:a});return Promise.resolve(_(a.Middlewares[e]?a.Middlewares[e]:b,c))}catch(e){return Promise.reject(e)}}}),r},M=function(e,t){var r,o=n.useState({})[1],i=n.useRef(""),c=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(a.mutableState),s=!!c,u=s?c.selector:t,l=s?c:T(e);if(A(function(){a.uid+=1;var t=""+a.uid;return i.current=t,a.Setter.functionSetter[e]||(a.Setter.functionSetter[e]={}),a.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete a.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=I(e,{__hash:i.current,type:"f"});return[u?u(l):l,f]},G=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=a.State,t}return i(t,e),t.prototype.render=function(){var e=this.props.children;return a.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:o({},this.state)},e)},t}(n.PureComponent);exports.Consumer=y,exports.Model=function(e,t,n){if(void 0!==e.state){a.storeId+=1;var i="__"+a.storeId;a.State=r(a.State,function(t){t[i]=e.state}),e.middlewares&&(a.Middlewares[i]=e.middlewares),a.Actions[i]=e.actions,a.AsyncState[i]=e.asyncState,t&&(a.Context[i]=t);var c=I(i);return{__id:i,actions:c,getState:function(){return T(i)},subscribe:function(e,t){return C(i,e,t)},unsubscribe:function(e){return R(i,e)},useStore:function(e){return M(i,e)}}}t?a.gid=1:a.gid+=1;var s="";if(a.gid>1&&(s=a.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var u=function(e){return function(){return e}};return{__ERROR__:!0,actions:u({}),getActions:u({}),getInitialState:u({}),getState:u({}),subscribe:u(),unsubscribe:u(),useStore:u([{},{}])}}t&&!t.__FROM_SERVER__&&(a.State=r(a.State,function(e){Object.assign(e,t||{})})),n&&(a.Context.__global=n);var l={};return Object.keys(e).forEach(function(n){var i=s+n,c=e[n];if(c.__ERROR__)return console.error(i+" model's schema is invalid"),a.State=r(a.State,function(e){e[i]={}}),void(a.Actions[i]={});void 0===c.useStore?(t&&t.__FROM_SERVER__?a.State=r(a.State,function(e){e[i]=o({},c.state,t[i])}):a.State[i]||(a.State=r(a.State,function(e){e[i]=c.state})),c.middlewares&&(a.Middlewares[i]=c.middlewares),a.Actions[i]=c.actions,a.AsyncState[i]=c.asyncState):(a.State[i]&&t||(a.State=r(a.State,function(e){e[i]=e[c.__id]})),t&&t.__FROM_SERVER__&&(a.State=r(a.State,function(e){e[i]=o({},e[c.__id],t[i])})),a.Actions[i]=a.Actions[c.__id],a.AsyncState[i]=a.AsyncState[c.__id],a.Middlewares[i]=a.Middlewares[c.__id],a.Context[i]=a.Context[c.__id]),l[n]=I(i)}),{actions:l,getActions:function(e){return I(s+e)},getInitialState:function(e,t){try{return Promise.resolve(O(e,o({},t,{prefix:s})))}catch(e){return Promise.reject(e)}},getState:function(e){return T(s+e)},subscribe:function(e,t,r){return C(s+e,t,r)},unsubscribe:function(e,t){return R(s+e,t)},useStore:function(e,t){return M(s+e,t)}}},exports.Provider=G,exports.actionMiddlewares=b,exports.connect=function(e,t,r){return function(c){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=a.Actions[e];return n.createElement(c,Object.assign({},i.props,{state:o({},l,t?t(u):u),actions:o({},m,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},exports.createStore=function(e,t){var r="string"==typeof e;a.storeId+=r?0:1;var n=r?e:a.storeId.toString();a.Actions[n]||(a.Actions[n]={}),a.mutableState[n]||(a.mutableState[n]={count:0,cachedResult:void 0});var o=function(){return a.mutableState[n].count=0,a.currentStoreId=n,a.mutableState[n].cachedResult=t?t():e(),a.mutableState[n].cachedResult};return a.mutableState[n].selector=o,{useStore:function(){return M(n,o)},getState:function(){return o()},getStore:function(){return a.mutableState[n].cachedResult},subscribe:function(e){a.subscriptions[n]||(a.subscriptions[n]=[]),a.subscriptions[n].push(e)},unsubscribe:function(e){if(a.subscriptions[n]&&e){var t=a.subscriptions[n].indexOf(e);t>=0&&a.subscriptions[n].splice(t,1)}}}},exports.getInitialState=O,exports.getState=T,exports.middlewares=v,exports.useModel=function(e){var t=a.currentStoreId,n=a.mutableState[t].count;return a.mutableState[t].count+=1,a.mutableState[t].hasOwnProperty(n)||(a.mutableState[t][n]="function"==typeof e?e():e),[a.mutableState[t][n],function(e){try{return a.mutableState[t][n]="function"==typeof e?r(a.mutableState[t][n],e):a.mutableState[t][n]&&e&&"Object"===a.mutableState[t][n].constructor.name&&"Object"===e.constructor.name?o({},a.mutableState[t][n],e):e,Promise.resolve(_(b,{Global:a,action:function(){return"function"==typeof e?a.mutableState[t][n]:e},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:t,newState:{},params:void 0,type:"u"}))}catch(e){return Promise.reject(e)}}]}; | ||
var e,t=require("immer"),r=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,n=require("react");function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function i(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var a={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},c={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var r=e.next;return Promise.resolve(c.tryCatch.enable?r(t).catch(function(e){return console.log(e)}):r(t))}catch(e){return Promise.reject(e)}},u=function(e,t){try{var r=function(){return Promise.resolve(s(t))},n=e.action,i=e.modelName,a=e.consumerActions,c=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(n(c,o({actions:a(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).then(function(t){e.newState=t||null});e.newState=n()}();return Promise.resolve(f&&f.then?f.then(r):r())}catch(e){return Promise.reject(e)}},l=function(e,t){try{var r=e.modelName,n=e.newState,o=e.next,i=e.Global,a=e.type;return i.Setter.functionSetter[r]&&!e.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[r]).map(function(e){var t=i.Setter.functionSetter[r][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[r]))}),Promise.resolve(function(){if(n||"u"===a)return N(r,n||{}),Promise.resolve(o(t))}())}catch(e){return Promise.reject(e)}},f=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i=e.__hash,a=o.Setter.functionSetter[r];return"f"===e.type&&i&&a&&a[i]&&a[i].setState&&a[i].setState(o.State[r]),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i="u"===e.type?o.subscriptions[r]:o.subscriptions[r+"_"+e.actionName];return i&&i.forEach(function(t){t(e)}),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var r=e.Global;return!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",r.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(t){return console.log("%c Next","color: #4CAF50; font-weight: bold",r.State[e.modelName]),console.groupEnd(),t})):Promise.resolve(e.next(t))}catch(e){return Promise.reject(e)}},d=function(e,t){try{var r=e.Global;return Promise.resolve(e.next(t)).then(function(t){return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&v.config.devtools.enable&&!r.devTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),r.withDevTools&&c.devtools.enable&&r.devTools.send("u"===e.type&&e.disableSelectorUpdate?"store["+e.modelName+"].update":e.modelName+"_"+e.actionName,r.mutableState[e.modelName],void 0,e.modelName),t})}catch(e){return Promise.reject(e)}},p=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,i=e.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[r]&&Object.keys(o.Setter.functionSetter[r]).map(function(e){var t=o.Setter.functionSetter[r][e];if(t)if(!t.selector||i)t.setState(o.State[r]);else{var n=t.selector(o.State[r]);x(n,t.selectorRef)||(t.selectorRef=n,t.setState(o.State[r]))}}),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},b=[s,S,d,u,l,f,p,m],v={communicator:p,consoleDebugger:S,devToolsListener:d,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,r){try{var n=t.Global,o=t.modelName,i=t.next,a=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(n.Actions[o],{modelName:o}),state:n.State[o]}),E(e,j(o,a))])).then(function(e){return t.newState=e||null,Promise.resolve(i(r))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:c},_=function(e,t){try{return t.next=function(e){return e.length>0?e[0](t,e.slice(1)):t.newState},Promise.resolve(e[0](t,e.slice(1)))}catch(e){return Promise.reject(e)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",P,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=function(e,t){var r={};return Object.keys(e).forEach(function(n){r[n]=function(e,t){return function(r,n){try{return Promise.resolve(_(b,{Global:a,action:e,actionName:e.name,consumerActions:w,middlewareConfig:n,modelName:t.modelName,newState:null,params:r,type:"o"}))}catch(e){return Promise.reject(e)}}}(e[n],t)}),r},N=function(e,t){if("function"==typeof t){var n=a.State[e];n=r(n,t),a.State=r(a.State,function(t){t[e]=n})}else a.State=r(a.State,function(r){r[e]=o({},r[e],t)});return a.State},E=function(e,t){return new Promise(function(r){return setTimeout(function(){console.log(e),r(t)},e)})},O=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(a.State).map(function(i){try{var c=t&&t.prefix||"",s=function(){if(!e||!e.modelName||i===c+e.modelName||-1!==e.modelName.indexOf(c+i)){var s=function(e){t&&t.isServer?n[i]=e:a.State=r(a.State,function(t){t[i]=o({},t[i],e)})},u=a.AsyncState[i];return u?Promise.resolve(u(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:a.State})}catch(e){return Promise.reject(e)}},j=function(e,t){var r=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return r?JSON.parse(r):null},x=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var r=Object.keys(e),n=Object.keys(t);if(r.length!==n.length)return!1;for(var o=0;o<r.length;o++)if(!Object.prototype.hasOwnProperty.call(t,r[o])||e[r[o]]!==t[r[o]])return!1;return!0};t.enableES5();var A="undefined"==typeof window?n.useEffect:n.useLayoutEffect,R=function(e,t){C(e,t,void 0)},C=function(e,t,r){Array.isArray(t)?t.forEach(function(t){a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),r?a.subscriptions[e+"_"+t].push(r):a.subscriptions[e+"_"+t]=[]}):(a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),r?a.subscriptions[e+"_"+t].push(r):a.subscriptions[e+"_"+t]=[])},T=function(e){return a.State[e]},I=function(e,t){void 0===t&&(t={type:"o"});var r={};return Object.keys(a.Actions[e]).forEach(function(n){return r[n]=function(r,i){try{var c=o({action:a.Actions[e][n],actionName:n,consumerActions:w,middlewareConfig:i,modelName:e,newState:null,params:r},t,{Global:a});return Promise.resolve(_(a.Middlewares[e]?a.Middlewares[e]:b,c))}catch(e){return Promise.reject(e)}}}),r},M=function(e,t){var r,o=n.useState({})[1],i=n.useRef(""),c=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(a.mutableState),s=!!c,u=s?c.selector:t,l=s?c:T(e);if(A(function(){a.uid+=1;var t=""+a.uid;return i.current=t,a.Setter.functionSetter[e]||(a.Setter.functionSetter[e]={}),a.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete a.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=I(e,{__hash:i.current,type:"f"});return[u?u(l):l,f]},G=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=a.State,t}return i(t,e),t.prototype.render=function(){var e=this.props.children;return a.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:o({},this.state)},e)},t}(n.PureComponent);exports.Consumer=y,exports.Model=function(e,t,n){if(void 0!==e.state){a.storeId+=1;var i="__"+a.storeId;a.State=r(a.State,function(t){t[i]=e.state}),e.middlewares&&(a.Middlewares[i]=e.middlewares),a.Actions[i]=e.actions,a.AsyncState[i]=e.asyncState,t&&(a.Context[i]=t);var c=I(i);return{__id:i,actions:c,getState:function(){return T(i)},subscribe:function(e,t){return C(i,e,t)},unsubscribe:function(e){return R(i,e)},useStore:function(e){return M(i,e)}}}t?a.gid=1:a.gid+=1;var s="";if(a.gid>1&&(s=a.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var u=function(e){return function(){return e}};return{__ERROR__:!0,actions:u({}),getActions:u({}),getInitialState:u({}),getState:u({}),subscribe:u(),unsubscribe:u(),useStore:u([{},{}])}}t&&!t.__FROM_SERVER__&&(a.State=r(a.State,function(e){Object.assign(e,t||{})})),n&&(a.Context.__global=n);var l={};return Object.keys(e).forEach(function(n){var i=s+n,c=e[n];if(c.__ERROR__)return console.error(i+" model's schema is invalid"),a.State=r(a.State,function(e){e[i]={}}),void(a.Actions[i]={});void 0===c.useStore?(t&&t.__FROM_SERVER__?a.State=r(a.State,function(e){e[i]=o({},c.state,t[i])}):a.State[i]||(a.State=r(a.State,function(e){e[i]=c.state})),c.middlewares&&(a.Middlewares[i]=c.middlewares),a.Actions[i]=c.actions,a.AsyncState[i]=c.asyncState):(a.State[i]&&t||(a.State=r(a.State,function(e){e[i]=e[c.__id]})),t&&t.__FROM_SERVER__&&(a.State=r(a.State,function(e){e[i]=o({},e[c.__id],t[i])})),a.Actions[i]=a.Actions[c.__id],a.AsyncState[i]=a.AsyncState[c.__id],a.Middlewares[i]=a.Middlewares[c.__id],a.Context[i]=a.Context[c.__id]),l[n]=I(i)}),{actions:l,getActions:function(e){return I(s+e)},getInitialState:function(e,t){try{return Promise.resolve(O(e,o({},t,{prefix:s})))}catch(e){return Promise.reject(e)}},getState:function(e){return T(s+e)},subscribe:function(e,t,r){return C(s+e,t,r)},unsubscribe:function(e,t){return R(s+e,t)},useStore:function(e,t){return M(s+e,t)}}},exports.Provider=G,exports.actionMiddlewares=b,exports.connect=function(e,t,r){return function(c){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=a.Actions[e];return n.createElement(c,Object.assign({},i.props,{state:o({},l,t?t(u):u),actions:o({},m,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},exports.createStore=function(e,t){var r="string"==typeof e;a.storeId+=r?0:1;var n=r?e:a.storeId.toString();a.Actions[n]||(a.Actions[n]={}),a.mutableState[n]||(a.mutableState[n]={count:0});var o=function(){return a.mutableState[n].count=0,a.currentStoreId=n,a.mutableState[n].cachedResult=t?t():e(),a.mutableState[n].cachedResult};return a.mutableState[n].selector=o,{useStore:function(){return M(n,o)},getState:function(){return o()},getStore:function(){return a.mutableState[n].cachedResult},subscribe:function(e){a.subscriptions[n]||(a.subscriptions[n]=[]),a.subscriptions[n].push(e)},unsubscribe:function(e){if(a.subscriptions[n]&&e){var t=a.subscriptions[n].indexOf(e);t>=0&&a.subscriptions[n].splice(t,1)}}}},exports.getInitialState=O,exports.getState=T,exports.middlewares=v,exports.useModel=function(e){var t=a.currentStoreId,n=a.mutableState[t].count;return a.mutableState[t].count+=1,a.mutableState[t].hasOwnProperty(n)||(a.mutableState[t][n]="function"==typeof e?e():e),[a.mutableState[t][n],function(e){try{return a.mutableState[t][n]="function"==typeof e?r(a.mutableState[t][n],e):a.mutableState[t][n]&&e&&"Object"===a.mutableState[t][n].constructor.name&&"Object"===e.constructor.name?o({},a.mutableState[t][n],e):e,Promise.resolve(_(b,{Global:a,action:function(){return"function"==typeof e?a.mutableState[t][n]:e},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:t,newState:{},params:void 0,type:"u"}))}catch(e){return Promise.reject(e)}}]}; |
@@ -1,1 +0,1 @@ | ||
import t,{enableES5 as e}from"immer";import{createContext as o,useState as a,useRef as n,PureComponent as s,createElement as c,useEffect as r,useLayoutEffect as i}from"react";var l={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1};const u={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},S=async(t,e)=>{const{next:o}=t;return u.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},d=async(t,e)=>{const{action:o,modelName:a,consumerActions:n,params:s,next:c,Global:r,type:i}=t;return t.newState="u"===i?o():await o(s,{actions:n(r.Actions[a],{modelName:a}),state:r.State[a],...r.Context.__global||{},...r.Context[a]||{}})||null,await c(e)},m=async(t,e)=>{const{modelName:o,newState:a,next:n,Global:s,disableSelectorUpdate:c,type:r}=t;if(s.Setter.functionSetter[o]&&!c&&Object.keys(s.Setter.functionSetter[o]).map(t=>{const e=s.Setter.functionSetter[o][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(s.State[o]))}),a||"u"===r)return E(o,a||{}),await n(e)},b=async(t,e)=>{const{modelName:o,next:a,Global:n,__hash:s}=t,c=n.Setter.functionSetter[o];return"f"===t.type&&s&&c&&c[s]&&c[s].setState&&c[s].setState(n.State[o]),await a(e)},p=async(t,e)=>{const{modelName:o,actionName:a,next:n,Global:s}=t,c="u"===t.type?s.subscriptions[o]:s.subscriptions[`${o}_${a}`];return c&&c.forEach(e=>{e(t)}),await n(e)},_=async(t,e)=>{const{Global:o}=t;if(!0===u.logger.enable||"function"==typeof u.logger.enable&&u.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);const a=await t.next(e);return console.log("%c Next","color: #4CAF50; font-weight: bold",o.State[t.modelName]),console.groupEnd(),a}return await t.next(e)},f=async(t,e)=>{const{Global:o}=t,a=await t.next(e);return o.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,o.withDevTools&&y.config.devtools.enable&&!o.devTools&&(o.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,o.devTools.connect()),o.withDevTools&&u.devtools.enable&&o.devTools.send("u"===t.type&&t.disableSelectorUpdate?`store[${t.modelName}].update`:`${t.modelName}_${t.actionName}`,o.mutableState[t.modelName],void 0,t.modelName),a},g=async(t,e)=>{const{modelName:o,next:a,Global:n,disableSelectorUpdate:s}=t;return n.Setter.classSetter&&n.Setter.classSetter(n.State),n.Setter.functionSetter[o]&&Object.keys(n.Setter.functionSetter[o]).map(t=>{const e=n.Setter.functionSetter[o][t];if(e)if(!e.selector||s)e.setState(n.State[o]);else{const t=e.selector(n.State[o]);$(t,e.selectorRef)||(e.selectorRef=t,e.setState(n.State[o]))}}),await a(e)},w=[S,_,f,d,m,b,g,p],y={communicator:g,consoleDebugger:_,devToolsListener:f,getNewState:d,getNewStateWithCache:(t=5e3)=>async(e,o)=>{const{action:a,Global:n,modelName:s,consumerActions:c,params:r,next:i,actionName:l}=e;return e.newState=await Promise.race([a(r,{actions:c(n.Actions[s],{modelName:s}),state:n.State[s]}),v(t,x(s,l))])||null,await i(o)},setNewState:m,stateUpdater:b,subscription:p,tryCatch:S,config:u},h=async(t,e)=>(e.next=t=>t.length>0?t[0](e,t.slice(1)):e.newState,await t[0](e,t.slice(1))),N=o({}),O=N.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 A=(t,e)=>{const o={};return Object.keys(t).forEach(a=>{o[a]=((t,e)=>async(o,a)=>{const n={Global:l,action:t,actionName:t.name,consumerActions:A,middlewareConfig:a,modelName:e.modelName,newState:null,params:o,type:"o"};return await h(w,n)})(t[a],e)}),o},E=(e,o)=>{if("function"==typeof o){let a=l.State[e];a=t(a,o),l.State=t(l.State,t=>{t[e]=a})}else l.State=t(l.State,t=>{t[e]={...t[e],...o}});return l.State},v=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),R=async(e,o)=>{const a={__FROM_SERVER__:!0};return await Promise.all(Object.keys(l.State).map(async n=>{let s=o&&o.prefix||"";if(!e||!e.modelName||n===s+e.modelName||-1!==e.modelName.indexOf(s+n)){const s=l.AsyncState[n],c=s?await s(e):{};o&&o.isServer?a[n]=c:l.State=t(l.State,t=>{t[n]={...t[n],...c}})}})),o&&o.isServer?a:l.State},x=(t,e)=>{const o=localStorage.getItem(`__REACT_MODELX__${t}_${e}`);return o?JSON.parse(o):null},$=(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),a=Object.keys(e);if(o.length!==a.length)return!1;for(let a=0;a<o.length;a++)if(!Object.prototype.hasOwnProperty.call(e,o[a])||t[o[a]]!==e[o[a]])return!1;return!0};e();const C="undefined"==typeof window?r:i;function T(e){const o=l.currentStoreId,a=l.mutableState[o].count;return l.mutableState[o].count+=1,l.mutableState[o].hasOwnProperty(a)||(l.mutableState[o][a]="function"==typeof e?e():e),[l.mutableState[o][a],async e=>{l.mutableState[o][a]="function"==typeof e?t(l.mutableState[o][a],e):l.mutableState[o][a]&&e&&"Object"===l.mutableState[o][a].constructor.name&&"Object"===e.constructor.name?{...l.mutableState[o][a],...e}:e;const n={Global:l,action:()=>"function"==typeof e?l.mutableState[o][a]:e,actionName:"setter",consumerActions:A,disableSelectorUpdate:!0,middlewareConfig:{},modelName:o,newState:{},params:void 0,type:"u"};return await h(w,n)}]}function j(t,e){const o="string"==typeof t;l.storeId+=o?0:1;const a=o?t:l.storeId.toString();l.Actions[a]||(l.Actions[a]={}),l.mutableState[a]||(l.mutableState[a]={count:0,cachedResult:void 0});const n=()=>(l.mutableState[a].count=0,l.currentStoreId=a,l.mutableState[a].cachedResult=e?e():t(),l.mutableState[a].cachedResult);return l.mutableState[a].selector=n,{useStore:()=>P(a,n),getState:()=>n(),getStore:()=>l.mutableState[a].cachedResult,subscribe:t=>{l.subscriptions[a]||(l.subscriptions[a]=[]),l.subscriptions[a].push(t)},unsubscribe:t=>{if(l.subscriptions[a]&&t){const e=l.subscriptions[a].indexOf(t);e>=0&&l.subscriptions[a].splice(e,1)}}}}function G(e,o,a){if(void 0!==e.state){l.storeId+=1;const a="__"+l.storeId;l.State=t(l.State,t=>{t[a]=e.state}),e.middlewares&&(l.Middlewares[a]=e.middlewares),l.Actions[a]=e.actions,l.AsyncState[a]=e.asyncState,o&&(l.Context[a]=o);const n=k(a);return{__id:a,actions:n,getState:()=>M(a),subscribe:(t,e)=>D(a,t,e),unsubscribe:t=>I(a,t),useStore:t=>P(a,t)}}{o?l.gid=1:l.gid+=1;let n="";if(l.gid>1&&(n=l.gid+"_"),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__&&(l.State=t(l.State,t=>{Object.assign(t,o||{})})),a&&(l.Context.__global=a);let s={};return Object.keys(e).forEach(a=>{let c=n+a;const r=e[a];if(r.__ERROR__)return console.error(c+" model's schema is invalid"),l.State=t(l.State,t=>{t[c]={}}),void(l.Actions[c]={});(t=>void 0!==t.useStore)(r)?(l.State[c]&&o||(l.State=t(l.State,t=>{t[c]=t[r.__id]})),o&&o.__FROM_SERVER__&&(l.State=t(l.State,t=>{t[c]={...t[r.__id],...o[c]}})),l.Actions[c]=l.Actions[r.__id],l.AsyncState[c]=l.AsyncState[r.__id],l.Middlewares[c]=l.Middlewares[r.__id],l.Context[c]=l.Context[r.__id]):(o&&o.__FROM_SERVER__?l.State=t(l.State,t=>{t[c]={...r.state,...o[c]}}):l.State[c]||(l.State=t(l.State,t=>{t[c]=r.state})),r.middlewares&&(l.Middlewares[c]=r.middlewares),l.Actions[c]=r.actions,l.AsyncState[c]=r.asyncState),s[a]=k(c)}),{actions:s,getActions:t=>k(n+t),getInitialState:async(t,e)=>R(t,{...e,prefix:n}),getState:t=>M(n+t),subscribe:(t,e,o)=>D(n+t,e,o),unsubscribe:(t,e)=>I(n+t,e),useStore:(t,e)=>P(n+t,e)}}}const I=(t,e)=>{D(t,e,void 0)},D=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{l.subscriptions[`${t}_${e}`]||(l.subscriptions[`${t}_${e}`]=[]),o?l.subscriptions[`${t}_${e}`].push(o):l.subscriptions[`${t}_${e}`]=[]}):(l.subscriptions[`${t}_${e}`]||(l.subscriptions[`${t}_${e}`]=[]),o?l.subscriptions[`${t}_${e}`].push(o):l.subscriptions[`${t}_${e}`]=[])},M=t=>l.State[t],k=(t,e={type:"o"})=>{const o={};return Object.keys(l.Actions[t]).forEach(a=>o[a]=async(o,n)=>{const s={action:l.Actions[t][a],actionName:a,consumerActions:A,middlewareConfig:n,modelName:t,newState:null,params:o,...e,Global:l};return l.Middlewares[t]?await h(l.Middlewares[t],s):await h(w,s)}),o},P=(t,e)=>{const o=a({})[1],s=n(""),c=(r=[t],t=>r.reduce((t,e)=>t&&t[e]?t[e]:null,t))(l.mutableState);var r;const i=!!c,u=i?c.selector:e,S=i?c:M(t);if(C(()=>{l.uid+=1;const e=""+l.uid;return s.current=e,l.Setter.functionSetter[t]||(l.Setter.functionSetter[t]={}),l.Setter.functionSetter[t][e]={setState:o,selector:u},function(){delete l.Setter.functionSetter[t][e]}},[]),i)return u(S);{const e=k(t,{__hash:s.current,type:"f"});return[u?u(S):S,e]}};class U extends s{constructor(){super(...arguments),this.state=l.State}render(){const{children:t}=this.props;return l.Setter.classSetter=this.setState.bind(this),c(N.Provider,{value:{...this.state}},t)}}const F=(t,e,o)=>a=>class extends s{render(){const{state:n={},actions:s={}}=this.props;return c(O,null,r=>{const{[""+t]:i}=r,u=l.Actions[t];return c(a,Object.assign({},this.props,{state:{...n,...e?e(i):i},actions:{...s,...o?o(A(u,{modelName:t})):A(u,{modelName:t})}}))})}};export{O as Consumer,G as Model,U as Provider,w as actionMiddlewares,F as connect,j as createStore,R as getInitialState,M as getState,y as middlewares,T as useModel}; | ||
import t,{enableES5 as e}from"immer";import{createContext as o,useState as a,useRef as n,PureComponent as s,createElement as r,useEffect as c,useLayoutEffect as i}from"react";var l={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1};const u={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},S=async(t,e)=>{const{next:o}=t;return u.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},d=async(t,e)=>{const{action:o,modelName:a,consumerActions:n,params:s,next:r,Global:c,type:i}=t;return t.newState="u"===i?o():await o(s,{actions:n(c.Actions[a],{modelName:a}),state:c.State[a],...c.Context.__global||{},...c.Context[a]||{}})||null,await r(e)},m=async(t,e)=>{const{modelName:o,newState:a,next:n,Global:s,disableSelectorUpdate:r,type:c}=t;if(s.Setter.functionSetter[o]&&!r&&Object.keys(s.Setter.functionSetter[o]).map(t=>{const e=s.Setter.functionSetter[o][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(s.State[o]))}),a||"u"===c)return E(o,a||{}),await n(e)},b=async(t,e)=>{const{modelName:o,next:a,Global:n,__hash:s}=t,r=n.Setter.functionSetter[o];return"f"===t.type&&s&&r&&r[s]&&r[s].setState&&r[s].setState(n.State[o]),await a(e)},p=async(t,e)=>{const{modelName:o,actionName:a,next:n,Global:s}=t,r="u"===t.type?s.subscriptions[o]:s.subscriptions[`${o}_${a}`];return r&&r.forEach(e=>{e(t)}),await n(e)},_=async(t,e)=>{const{Global:o}=t;if(!0===u.logger.enable||"function"==typeof u.logger.enable&&u.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);const a=await t.next(e);return console.log("%c Next","color: #4CAF50; font-weight: bold",o.State[t.modelName]),console.groupEnd(),a}return await t.next(e)},f=async(t,e)=>{const{Global:o}=t,a=await t.next(e);return o.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,o.withDevTools&&y.config.devtools.enable&&!o.devTools&&(o.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,o.devTools.connect()),o.withDevTools&&u.devtools.enable&&o.devTools.send("u"===t.type&&t.disableSelectorUpdate?`store[${t.modelName}].update`:`${t.modelName}_${t.actionName}`,o.mutableState[t.modelName],void 0,t.modelName),a},g=async(t,e)=>{const{modelName:o,next:a,Global:n,disableSelectorUpdate:s}=t;return n.Setter.classSetter&&n.Setter.classSetter(n.State),n.Setter.functionSetter[o]&&Object.keys(n.Setter.functionSetter[o]).map(t=>{const e=n.Setter.functionSetter[o][t];if(e)if(!e.selector||s)e.setState(n.State[o]);else{const t=e.selector(n.State[o]);$(t,e.selectorRef)||(e.selectorRef=t,e.setState(n.State[o]))}}),await a(e)},w=[S,_,f,d,m,b,g,p],y={communicator:g,consoleDebugger:_,devToolsListener:f,getNewState:d,getNewStateWithCache:(t=5e3)=>async(e,o)=>{const{action:a,Global:n,modelName:s,consumerActions:r,params:c,next:i,actionName:l}=e;return e.newState=await Promise.race([a(c,{actions:r(n.Actions[s],{modelName:s}),state:n.State[s]}),v(t,x(s,l))])||null,await i(o)},setNewState:m,stateUpdater:b,subscription:p,tryCatch:S,config:u},h=async(t,e)=>(e.next=t=>t.length>0?t[0](e,t.slice(1)):e.newState,await t[0](e,t.slice(1))),N=o({}),O=N.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 A=(t,e)=>{const o={};return Object.keys(t).forEach(a=>{o[a]=((t,e)=>async(o,a)=>{const n={Global:l,action:t,actionName:t.name,consumerActions:A,middlewareConfig:a,modelName:e.modelName,newState:null,params:o,type:"o"};return await h(w,n)})(t[a],e)}),o},E=(e,o)=>{if("function"==typeof o){let a=l.State[e];a=t(a,o),l.State=t(l.State,t=>{t[e]=a})}else l.State=t(l.State,t=>{t[e]={...t[e],...o}});return l.State},v=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),R=async(e,o)=>{const a={__FROM_SERVER__:!0};return await Promise.all(Object.keys(l.State).map(async n=>{let s=o&&o.prefix||"";if(!e||!e.modelName||n===s+e.modelName||-1!==e.modelName.indexOf(s+n)){const s=l.AsyncState[n],r=s?await s(e):{};o&&o.isServer?a[n]=r:l.State=t(l.State,t=>{t[n]={...t[n],...r}})}})),o&&o.isServer?a:l.State},x=(t,e)=>{const o=localStorage.getItem(`__REACT_MODELX__${t}_${e}`);return o?JSON.parse(o):null},$=(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),a=Object.keys(e);if(o.length!==a.length)return!1;for(let a=0;a<o.length;a++)if(!Object.prototype.hasOwnProperty.call(e,o[a])||t[o[a]]!==e[o[a]])return!1;return!0};e();const C="undefined"==typeof window?c:i;function T(e){const o=l.currentStoreId,a=l.mutableState[o].count;return l.mutableState[o].count+=1,l.mutableState[o].hasOwnProperty(a)||(l.mutableState[o][a]="function"==typeof e?e():e),[l.mutableState[o][a],async e=>{l.mutableState[o][a]="function"==typeof e?t(l.mutableState[o][a],e):l.mutableState[o][a]&&e&&"Object"===l.mutableState[o][a].constructor.name&&"Object"===e.constructor.name?{...l.mutableState[o][a],...e}:e;const n={Global:l,action:()=>"function"==typeof e?l.mutableState[o][a]:e,actionName:"setter",consumerActions:A,disableSelectorUpdate:!0,middlewareConfig:{},modelName:o,newState:{},params:void 0,type:"u"};return await h(w,n)}]}function j(t,e){const o="string"==typeof t;l.storeId+=o?0:1;const a=o?t:l.storeId.toString();l.Actions[a]||(l.Actions[a]={}),l.mutableState[a]||(l.mutableState[a]={count:0});const n=()=>(l.mutableState[a].count=0,l.currentStoreId=a,l.mutableState[a].cachedResult=e?e():t(),l.mutableState[a].cachedResult);return l.mutableState[a].selector=n,{useStore:()=>P(a,n),getState:()=>n(),getStore:()=>l.mutableState[a].cachedResult,subscribe:t=>{l.subscriptions[a]||(l.subscriptions[a]=[]),l.subscriptions[a].push(t)},unsubscribe:t=>{if(l.subscriptions[a]&&t){const e=l.subscriptions[a].indexOf(t);e>=0&&l.subscriptions[a].splice(e,1)}}}}function G(e,o,a){if(void 0!==e.state){l.storeId+=1;const a="__"+l.storeId;l.State=t(l.State,t=>{t[a]=e.state}),e.middlewares&&(l.Middlewares[a]=e.middlewares),l.Actions[a]=e.actions,l.AsyncState[a]=e.asyncState,o&&(l.Context[a]=o);const n=k(a);return{__id:a,actions:n,getState:()=>M(a),subscribe:(t,e)=>D(a,t,e),unsubscribe:t=>I(a,t),useStore:t=>P(a,t)}}{o?l.gid=1:l.gid+=1;let n="";if(l.gid>1&&(n=l.gid+"_"),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__&&(l.State=t(l.State,t=>{Object.assign(t,o||{})})),a&&(l.Context.__global=a);let s={};return Object.keys(e).forEach(a=>{let r=n+a;const c=e[a];if(c.__ERROR__)return console.error(r+" model's schema is invalid"),l.State=t(l.State,t=>{t[r]={}}),void(l.Actions[r]={});(t=>void 0!==t.useStore)(c)?(l.State[r]&&o||(l.State=t(l.State,t=>{t[r]=t[c.__id]})),o&&o.__FROM_SERVER__&&(l.State=t(l.State,t=>{t[r]={...t[c.__id],...o[r]}})),l.Actions[r]=l.Actions[c.__id],l.AsyncState[r]=l.AsyncState[c.__id],l.Middlewares[r]=l.Middlewares[c.__id],l.Context[r]=l.Context[c.__id]):(o&&o.__FROM_SERVER__?l.State=t(l.State,t=>{t[r]={...c.state,...o[r]}}):l.State[r]||(l.State=t(l.State,t=>{t[r]=c.state})),c.middlewares&&(l.Middlewares[r]=c.middlewares),l.Actions[r]=c.actions,l.AsyncState[r]=c.asyncState),s[a]=k(r)}),{actions:s,getActions:t=>k(n+t),getInitialState:async(t,e)=>R(t,{...e,prefix:n}),getState:t=>M(n+t),subscribe:(t,e,o)=>D(n+t,e,o),unsubscribe:(t,e)=>I(n+t,e),useStore:(t,e)=>P(n+t,e)}}}const I=(t,e)=>{D(t,e,void 0)},D=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{l.subscriptions[`${t}_${e}`]||(l.subscriptions[`${t}_${e}`]=[]),o?l.subscriptions[`${t}_${e}`].push(o):l.subscriptions[`${t}_${e}`]=[]}):(l.subscriptions[`${t}_${e}`]||(l.subscriptions[`${t}_${e}`]=[]),o?l.subscriptions[`${t}_${e}`].push(o):l.subscriptions[`${t}_${e}`]=[])},M=t=>l.State[t],k=(t,e={type:"o"})=>{const o={};return Object.keys(l.Actions[t]).forEach(a=>o[a]=async(o,n)=>{const s={action:l.Actions[t][a],actionName:a,consumerActions:A,middlewareConfig:n,modelName:t,newState:null,params:o,...e,Global:l};return l.Middlewares[t]?await h(l.Middlewares[t],s):await h(w,s)}),o},P=(t,e)=>{const o=a({})[1],s=n(""),r=(c=[t],t=>c.reduce((t,e)=>t&&t[e]?t[e]:null,t))(l.mutableState);var c;const i=!!r,u=i?r.selector:e,S=i?r:M(t);if(C(()=>{l.uid+=1;const e=""+l.uid;return s.current=e,l.Setter.functionSetter[t]||(l.Setter.functionSetter[t]={}),l.Setter.functionSetter[t][e]={setState:o,selector:u},function(){delete l.Setter.functionSetter[t][e]}},[]),i)return u(S);{const e=k(t,{__hash:s.current,type:"f"});return[u?u(S):S,e]}};class U extends s{constructor(){super(...arguments),this.state=l.State}render(){const{children:t}=this.props;return l.Setter.classSetter=this.setState.bind(this),r(N.Provider,{value:{...this.state}},t)}}const F=(t,e,o)=>a=>class extends s{render(){const{state:n={},actions:s={}}=this.props;return r(O,null,c=>{const{[""+t]:i}=c,u=l.Actions[t];return r(a,Object.assign({},this.props,{state:{...n,...e?e(i):i},actions:{...s,...o?o(A(u,{modelName:t})):A(u,{modelName:t})}}))})}};export{O as Consumer,G as Model,U as Provider,w as actionMiddlewares,F as connect,j as createStore,R as getInitialState,M as getState,y as middlewares,T as useModel}; |
@@ -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){var r="default"in t?t.default:t;function o(){return(o=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 i(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var a={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},c={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var n=e.next;return Promise.resolve(c.tryCatch.enable?n(t).catch(function(e){return console.log(e)}):n(t))}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=function(){return Promise.resolve(s(t))},r=e.action,i=e.modelName,a=e.consumerActions,c=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(r(c,o({actions:a(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).then(function(t){e.newState=t||null});e.newState=r()}();return Promise.resolve(f&&f.then?f.then(n):n())}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global,a=e.type;return i.Setter.functionSetter[n]&&!e.disableSelectorUpdate&&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]))}),Promise.resolve(function(){if(r||"u"===a)return N(n,r||{}),Promise.resolve(o(t))}())}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,a=o.Setter.functionSetter[n];return"f"===e.type&&i&&a&&a[i]&&a[i].setState&&a[i].setState(o.State[n]),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i="u"===e.type?o.subscriptions[n]:o.subscriptions[n+"_"+e.actionName];return i&&i.forEach(function(t){t(e)}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.Global;return!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(t){return console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd(),t})):Promise.resolve(e.next(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(t){return n.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,n.withDevTools&&v.config.devtools.enable&&!n.devTools&&(n.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,n.devTools.connect()),n.withDevTools&&c.devtools.enable&&n.devTools.send("u"===e.type&&e.disableSelectorUpdate?"store["+e.modelName+"].update":e.modelName+"_"+e.actionName,n.mutableState[e.modelName],void 0,e.modelName),t})}catch(e){return Promise.reject(e)}},b=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.disableSelectorUpdate;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||i)t.setState(o.State[n]);else{var r=t.selector(o.State[n]);A(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},p=[s,d,S,u,l,f,b,m],v={communicator:b,consoleDebugger:d,devToolsListener:S,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,a=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]}),E(e,j(o,a))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:c},_=function(e,t){try{return t.next=function(e){return e.length>0?e[0](t,e.slice(1)):t.newState},Promise.resolve(e[0](t,e.slice(1)))}catch(e){return Promise.reject(e)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",P,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=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(_(p,{Global:a,action:e,actionName:e.name,consumerActions:w,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"o"}))}catch(e){return Promise.reject(e)}}}(e[r],t)}),n},N=function(e,t){if("function"==typeof t){var n=a.State[e];n=r(n,t),a.State=r(a.State,function(t){t[e]=n})}else a.State=r(a.State,function(n){n[e]=o({},n[e],t)});return a.State},E=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},O=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(a.State).map(function(i){try{var c=t&&t.prefix||"",s=function(){if(!e||!e.modelName||i===c+e.modelName||-1!==e.modelName.indexOf(c+i)){var s=function(e){t&&t.isServer?n[i]=e:a.State=r(a.State,function(t){t[i]=o({},t[i],e)})},u=a.AsyncState[i];return u?Promise.resolve(u(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:a.State})}catch(e){return Promise.reject(e)}},j=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},A=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};t.enableES5();var R="undefined"==typeof window?n.useEffect:n.useLayoutEffect,x=function(e,t){C(e,t,void 0)},C=function(e,t,n){Array.isArray(t)?t.forEach(function(t){a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),n?a.subscriptions[e+"_"+t].push(n):a.subscriptions[e+"_"+t]=[]}):(a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),n?a.subscriptions[e+"_"+t].push(n):a.subscriptions[e+"_"+t]=[])},T=function(e){return a.State[e]},M=function(e,t){void 0===t&&(t={type:"o"});var n={};return Object.keys(a.Actions[e]).forEach(function(r){return n[r]=function(n,i){try{var c=o({action:a.Actions[e][r],actionName:r,consumerActions:w,middlewareConfig:i,modelName:e,newState:null,params:n},t,{Global:a});return Promise.resolve(_(a.Middlewares[e]?a.Middlewares[e]:p,c))}catch(e){return Promise.reject(e)}}}),n},I=function(e,t){var r,o=n.useState({})[1],i=n.useRef(""),c=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(a.mutableState),s=!!c,u=s?c.selector:t,l=s?c:T(e);if(R(function(){a.uid+=1;var t=""+a.uid;return i.current=t,a.Setter.functionSetter[e]||(a.Setter.functionSetter[e]={}),a.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete a.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=M(e,{__hash:i.current,type:"f"});return[u?u(l):l,f]},G=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=a.State,t}return i(t,e),t.prototype.render=function(){var e=this.props.children;return a.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:o({},this.state)},e)},t}(n.PureComponent);e.Consumer=y,e.Model=function(e,t,n){if(void 0!==e.state){a.storeId+=1;var i="__"+a.storeId;a.State=r(a.State,function(t){t[i]=e.state}),e.middlewares&&(a.Middlewares[i]=e.middlewares),a.Actions[i]=e.actions,a.AsyncState[i]=e.asyncState,t&&(a.Context[i]=t);var c=M(i);return{__id:i,actions:c,getState:function(){return T(i)},subscribe:function(e,t){return C(i,e,t)},unsubscribe:function(e){return x(i,e)},useStore:function(e){return I(i,e)}}}t?a.gid=1:a.gid+=1;var s="";if(a.gid>1&&(s=a.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var u=function(e){return function(){return e}};return{__ERROR__:!0,actions:u({}),getActions:u({}),getInitialState:u({}),getState:u({}),subscribe:u(),unsubscribe:u(),useStore:u([{},{}])}}t&&!t.__FROM_SERVER__&&(a.State=r(a.State,function(e){Object.assign(e,t||{})})),n&&(a.Context.__global=n);var l={};return Object.keys(e).forEach(function(n){var i=s+n,c=e[n];if(c.__ERROR__)return console.error(i+" model's schema is invalid"),a.State=r(a.State,function(e){e[i]={}}),void(a.Actions[i]={});void 0===c.useStore?(t&&t.__FROM_SERVER__?a.State=r(a.State,function(e){e[i]=o({},c.state,t[i])}):a.State[i]||(a.State=r(a.State,function(e){e[i]=c.state})),c.middlewares&&(a.Middlewares[i]=c.middlewares),a.Actions[i]=c.actions,a.AsyncState[i]=c.asyncState):(a.State[i]&&t||(a.State=r(a.State,function(e){e[i]=e[c.__id]})),t&&t.__FROM_SERVER__&&(a.State=r(a.State,function(e){e[i]=o({},e[c.__id],t[i])})),a.Actions[i]=a.Actions[c.__id],a.AsyncState[i]=a.AsyncState[c.__id],a.Middlewares[i]=a.Middlewares[c.__id],a.Context[i]=a.Context[c.__id]),l[n]=M(i)}),{actions:l,getActions:function(e){return M(s+e)},getInitialState:function(e,t){try{return Promise.resolve(O(e,o({},t,{prefix:s})))}catch(e){return Promise.reject(e)}},getState:function(e){return T(s+e)},subscribe:function(e,t,n){return C(s+e,t,n)},unsubscribe:function(e,t){return x(s+e,t)},useStore:function(e,t){return I(s+e,t)}}},e.Provider=G,e.actionMiddlewares=p,e.connect=function(e,t,r){return function(c){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=a.Actions[e];return n.createElement(c,Object.assign({},i.props,{state:o({},l,t?t(u):u),actions:o({},m,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},e.createStore=function(e,t){var n="string"==typeof e;a.storeId+=n?0:1;var r=n?e:a.storeId.toString();a.Actions[r]||(a.Actions[r]={}),a.mutableState[r]||(a.mutableState[r]={count:0,cachedResult:void 0});var o=function(){return a.mutableState[r].count=0,a.currentStoreId=r,a.mutableState[r].cachedResult=t?t():e(),a.mutableState[r].cachedResult};return a.mutableState[r].selector=o,{useStore:function(){return I(r,o)},getState:function(){return o()},getStore:function(){return a.mutableState[r].cachedResult},subscribe:function(e){a.subscriptions[r]||(a.subscriptions[r]=[]),a.subscriptions[r].push(e)},unsubscribe:function(e){if(a.subscriptions[r]&&e){var t=a.subscriptions[r].indexOf(e);t>=0&&a.subscriptions[r].splice(t,1)}}}},e.getInitialState=O,e.getState=T,e.middlewares=v,e.useModel=function(e){var t=a.currentStoreId,n=a.mutableState[t].count;return a.mutableState[t].count+=1,a.mutableState[t].hasOwnProperty(n)||(a.mutableState[t][n]="function"==typeof e?e():e),[a.mutableState[t][n],function(e){try{return a.mutableState[t][n]="function"==typeof e?r(a.mutableState[t][n],e):a.mutableState[t][n]&&e&&"Object"===a.mutableState[t][n].constructor.name&&"Object"===e.constructor.name?o({},a.mutableState[t][n],e):e,Promise.resolve(_(p,{Global:a,action:function(){return"function"==typeof e?a.mutableState[t][n]:e},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:t,newState:{},params:void 0,type:"u"}))}catch(e){return Promise.reject(e)}}]}}); | ||
!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){var r="default"in t?t.default:t;function o(){return(o=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 i(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var a={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},c={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var n=e.next;return Promise.resolve(c.tryCatch.enable?n(t).catch(function(e){return console.log(e)}):n(t))}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=function(){return Promise.resolve(s(t))},r=e.action,i=e.modelName,a=e.consumerActions,c=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(r(c,o({actions:a(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).then(function(t){e.newState=t||null});e.newState=r()}();return Promise.resolve(f&&f.then?f.then(n):n())}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global,a=e.type;return i.Setter.functionSetter[n]&&!e.disableSelectorUpdate&&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]))}),Promise.resolve(function(){if(r||"u"===a)return N(n,r||{}),Promise.resolve(o(t))}())}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,a=o.Setter.functionSetter[n];return"f"===e.type&&i&&a&&a[i]&&a[i].setState&&a[i].setState(o.State[n]),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i="u"===e.type?o.subscriptions[n]:o.subscriptions[n+"_"+e.actionName];return i&&i.forEach(function(t){t(e)}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.Global;return!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(t){return console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd(),t})):Promise.resolve(e.next(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(t){return n.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,n.withDevTools&&v.config.devtools.enable&&!n.devTools&&(n.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,n.devTools.connect()),n.withDevTools&&c.devtools.enable&&n.devTools.send("u"===e.type&&e.disableSelectorUpdate?"store["+e.modelName+"].update":e.modelName+"_"+e.actionName,n.mutableState[e.modelName],void 0,e.modelName),t})}catch(e){return Promise.reject(e)}},b=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.disableSelectorUpdate;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||i)t.setState(o.State[n]);else{var r=t.selector(o.State[n]);A(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},p=[s,d,S,u,l,f,b,m],v={communicator:b,consoleDebugger:d,devToolsListener:S,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,a=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]}),E(e,j(o,a))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:c},_=function(e,t){try{return t.next=function(e){return e.length>0?e[0](t,e.slice(1)):t.newState},Promise.resolve(e[0](t,e.slice(1)))}catch(e){return Promise.reject(e)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",P,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=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(_(p,{Global:a,action:e,actionName:e.name,consumerActions:w,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"o"}))}catch(e){return Promise.reject(e)}}}(e[r],t)}),n},N=function(e,t){if("function"==typeof t){var n=a.State[e];n=r(n,t),a.State=r(a.State,function(t){t[e]=n})}else a.State=r(a.State,function(n){n[e]=o({},n[e],t)});return a.State},E=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},O=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(a.State).map(function(i){try{var c=t&&t.prefix||"",s=function(){if(!e||!e.modelName||i===c+e.modelName||-1!==e.modelName.indexOf(c+i)){var s=function(e){t&&t.isServer?n[i]=e:a.State=r(a.State,function(t){t[i]=o({},t[i],e)})},u=a.AsyncState[i];return u?Promise.resolve(u(e)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:a.State})}catch(e){return Promise.reject(e)}},j=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},A=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};t.enableES5();var R="undefined"==typeof window?n.useEffect:n.useLayoutEffect,x=function(e,t){C(e,t,void 0)},C=function(e,t,n){Array.isArray(t)?t.forEach(function(t){a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),n?a.subscriptions[e+"_"+t].push(n):a.subscriptions[e+"_"+t]=[]}):(a.subscriptions[e+"_"+t]||(a.subscriptions[e+"_"+t]=[]),n?a.subscriptions[e+"_"+t].push(n):a.subscriptions[e+"_"+t]=[])},T=function(e){return a.State[e]},M=function(e,t){void 0===t&&(t={type:"o"});var n={};return Object.keys(a.Actions[e]).forEach(function(r){return n[r]=function(n,i){try{var c=o({action:a.Actions[e][r],actionName:r,consumerActions:w,middlewareConfig:i,modelName:e,newState:null,params:n},t,{Global:a});return Promise.resolve(_(a.Middlewares[e]?a.Middlewares[e]:p,c))}catch(e){return Promise.reject(e)}}}),n},I=function(e,t){var r,o=n.useState({})[1],i=n.useRef(""),c=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(a.mutableState),s=!!c,u=s?c.selector:t,l=s?c:T(e);if(R(function(){a.uid+=1;var t=""+a.uid;return i.current=t,a.Setter.functionSetter[e]||(a.Setter.functionSetter[e]={}),a.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete a.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=M(e,{__hash:i.current,type:"f"});return[u?u(l):l,f]},G=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=a.State,t}return i(t,e),t.prototype.render=function(){var e=this.props.children;return a.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:o({},this.state)},e)},t}(n.PureComponent);e.Consumer=y,e.Model=function(e,t,n){if(void 0!==e.state){a.storeId+=1;var i="__"+a.storeId;a.State=r(a.State,function(t){t[i]=e.state}),e.middlewares&&(a.Middlewares[i]=e.middlewares),a.Actions[i]=e.actions,a.AsyncState[i]=e.asyncState,t&&(a.Context[i]=t);var c=M(i);return{__id:i,actions:c,getState:function(){return T(i)},subscribe:function(e,t){return C(i,e,t)},unsubscribe:function(e){return x(i,e)},useStore:function(e){return I(i,e)}}}t?a.gid=1:a.gid+=1;var s="";if(a.gid>1&&(s=a.gid+"_"),e.actions){console.error("invalid model(s) schema: ",e);var u=function(e){return function(){return e}};return{__ERROR__:!0,actions:u({}),getActions:u({}),getInitialState:u({}),getState:u({}),subscribe:u(),unsubscribe:u(),useStore:u([{},{}])}}t&&!t.__FROM_SERVER__&&(a.State=r(a.State,function(e){Object.assign(e,t||{})})),n&&(a.Context.__global=n);var l={};return Object.keys(e).forEach(function(n){var i=s+n,c=e[n];if(c.__ERROR__)return console.error(i+" model's schema is invalid"),a.State=r(a.State,function(e){e[i]={}}),void(a.Actions[i]={});void 0===c.useStore?(t&&t.__FROM_SERVER__?a.State=r(a.State,function(e){e[i]=o({},c.state,t[i])}):a.State[i]||(a.State=r(a.State,function(e){e[i]=c.state})),c.middlewares&&(a.Middlewares[i]=c.middlewares),a.Actions[i]=c.actions,a.AsyncState[i]=c.asyncState):(a.State[i]&&t||(a.State=r(a.State,function(e){e[i]=e[c.__id]})),t&&t.__FROM_SERVER__&&(a.State=r(a.State,function(e){e[i]=o({},e[c.__id],t[i])})),a.Actions[i]=a.Actions[c.__id],a.AsyncState[i]=a.AsyncState[c.__id],a.Middlewares[i]=a.Middlewares[c.__id],a.Context[i]=a.Context[c.__id]),l[n]=M(i)}),{actions:l,getActions:function(e){return M(s+e)},getInitialState:function(e,t){try{return Promise.resolve(O(e,o({},t,{prefix:s})))}catch(e){return Promise.reject(e)}},getState:function(e){return T(s+e)},subscribe:function(e,t,n){return C(s+e,t,n)},unsubscribe:function(e,t){return x(s+e,t)},useStore:function(e,t){return I(s+e,t)}}},e.Provider=G,e.actionMiddlewares=p,e.connect=function(e,t,r){return function(c){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=a.Actions[e];return n.createElement(c,Object.assign({},i.props,{state:o({},l,t?t(u):u),actions:o({},m,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},e.createStore=function(e,t){var n="string"==typeof e;a.storeId+=n?0:1;var r=n?e:a.storeId.toString();a.Actions[r]||(a.Actions[r]={}),a.mutableState[r]||(a.mutableState[r]={count:0});var o=function(){return a.mutableState[r].count=0,a.currentStoreId=r,a.mutableState[r].cachedResult=t?t():e(),a.mutableState[r].cachedResult};return a.mutableState[r].selector=o,{useStore:function(){return I(r,o)},getState:function(){return o()},getStore:function(){return a.mutableState[r].cachedResult},subscribe:function(e){a.subscriptions[r]||(a.subscriptions[r]=[]),a.subscriptions[r].push(e)},unsubscribe:function(e){if(a.subscriptions[r]&&e){var t=a.subscriptions[r].indexOf(e);t>=0&&a.subscriptions[r].splice(t,1)}}}},e.getInitialState=O,e.getState=T,e.middlewares=v,e.useModel=function(e){var t=a.currentStoreId,n=a.mutableState[t].count;return a.mutableState[t].count+=1,a.mutableState[t].hasOwnProperty(n)||(a.mutableState[t][n]="function"==typeof e?e():e),[a.mutableState[t][n],function(e){try{return a.mutableState[t][n]="function"==typeof e?r(a.mutableState[t][n],e):a.mutableState[t][n]&&e&&"Object"===a.mutableState[t][n].constructor.name&&"Object"===e.constructor.name?o({},a.mutableState[t][n],e):e,Promise.resolve(_(p,{Global:a,action:function(){return"function"==typeof e?a.mutableState[t][n]:e},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:t,newState:{},params:void 0,type:"u"}))}catch(e){return Promise.reject(e)}}]}}); |
{ | ||
"name": "react-model", | ||
"version": "4.3.0-alpha.1", | ||
"version": "4.3.0-rc.0", | ||
"description": "The State management library for React", | ||
@@ -5,0 +5,0 @@ "main": "./dist/react-model.js", |
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
Possible typosquat attack
Supply chain riskThere is a package with a similar name that is downloaded much more often.
Did you mean |
---|
react-modal |
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
0
114135