react-model
Version:
The State management library for React
2 lines (1 loc) • 11 kB
JavaScript
import t,{enableES5 as e}from"immer";import{createContext as r,useState as n,useRef as o,useLayoutEffect as a,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 r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function u(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var l={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},f={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},S=function(t,e){try{var r=t.next;return Promise.resolve(f.tryCatch.enable?r(e).catch(function(t){return console.log(t)}):r(e))}catch(t){return Promise.reject(t)}},m=function(t,e){try{var r=function(){return Promise.resolve(c(e))},n=t.action,o=t.modelName,a=t.consumerActions,i=t.params,c=t.next,u=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(i,s({actions:a(u.Actions[o],{modelName:o}),state:u.State[o]},u.Context.__global||{},u.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)}},d=function(t,e){try{var r=t.modelName,n=t.newState,o=t.next,a=t.Global,i=t.type;return a.Setter.functionSetter[r]&&!t.disableSelectorUpdate&&Object.keys(a.Setter.functionSetter[r]).map(function(t){var e=a.Setter.functionSetter[r][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(a.State[r]))}),Promise.resolve(function(){if(n||"u"===i)return E(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,a=t.__hash,i=o.Setter.functionSetter[r];return"f"===t.type&&a&&i&&i[a]&&i[a].setState&&i[a].setState(o.State[r]),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},b=function(t,e){try{var r=t.next,n=t.Global.subscriptions[t.modelName+"_"+t.actionName];return n&&n.forEach(function(e){e(t)}),Promise.resolve(r(e))}catch(t){return Promise.reject(t)}},p=function(t,e){try{var r=t.Global;return!0===f.logger.enable||"function"==typeof f.logger.enable&&f.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)}},_=function(t,e){try{var r=t.Global;return Promise.resolve(t.next(e)).then(function(e){return r.withDevTools&&f.devtools.enable&&r.devTools.send(t.modelName+"_"+t.actionName,r.State),e})}catch(t){return Promise.reject(t)}},h=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,a=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||a)e.setState(o.State[r]);else{var n=e.selector(o.State[r]);T(n,e.selectorRef)||(e.selectorRef=n,e.setState(o.State[r]))}}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},y=[S,p,_,m,d,v,h,b],g={communicator:h,consoleDebugger:p,devToolsListener:_,getNewState:m,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,r){try{var n=e.Global,o=e.modelName,a=e.next,i=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]}),R(t,C(o,i))])).then(function(t){return e.newState=t||null,Promise.resolve(a(r))})}catch(t){return Promise.reject(t)}}},setNewState:d,stateUpdater:v,subscription:b,tryCatch:S,config:f},P=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)}},w=r({}),N=w.Consumer;if(!console.group){var O=[],j="-".repeat(80);console.group=function(t){O.push(t),console.log("%c \nBEGIN GROUP: %c",j,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",O.pop(),j)}}}var A=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(P(y,{Global:l,action:t,actionName:t.name,consumerActions:A,middlewareConfig:n,modelName:e.modelName,newState:null,params:r,type:"o"}))}catch(t){return Promise.reject(t)}}}(t[n],e)}),r},E=function(e,r){if("function"==typeof r){var n=l.State[e];n=t(n,r),l.State=t(l.State,function(t){t[e]=n})}else l.State=t(l.State,function(t){t[e]=s({},t[e],r)});return l.State},R=function(t,e){return new Promise(function(r){return setTimeout(function(){console.log(t),r(e)},t)})},x=function(e,r){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(l.State).map(function(o){try{var a=function(){if(!e||!e.modelName||o===e.modelName||-1!==e.modelName.indexOf(o)){var a=function(e){r&&r.isServer?n[o]=e:l.State=t(l.State,function(t){t[o]=s({},t[o],e)})},i=l.AsyncState[o];return i?Promise.resolve(i(e)).then(a):a({})}}();return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return r&&r.isServer?n:l.State})}catch(t){return Promise.reject(t)}},C=function(t,e){var r=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return r?JSON.parse(r):null},T=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};function G(e){var r=l.currentStoreId,n=l.mutableState[r].count;return l.mutableState[r].count+=1,l.mutableState[r].hasOwnProperty(n)||(l.mutableState[r][n]="function"==typeof e?e():e),[l.mutableState[r][n],function(e){try{return l.mutableState[r][n]="function"==typeof e?t(l.mutableState[r][n],e):"object"==typeof l.mutableState[r][n]&&"object"==typeof e?s({},l.mutableState[r][n],e):e,Promise.resolve(P(y,{Global:l,action:function(){return"function"==typeof e?l.mutableState[r][n]:e},actionName:"setter",consumerActions:A,disableSelectorUpdate:!0,middlewareConfig:{},modelName:r,newState:{},params:void 0,type:"u"}))}catch(t){return Promise.reject(t)}}]}function I(t,e){var r="string"==typeof t;l.storeId+=r?0:1;var n=r?t:l.storeId.toString();l.Actions[n]||(l.Actions[n]={}),l.mutableState[n]||(l.mutableState[n]={count:0});var o=function(){return l.mutableState[n].count=0,l.currentStoreId=n,e?e():t()};return l.mutableState[n].selector=o,{useStore:function(){return V(n,o)},getState:function(){return o()}}}function D(e,r,n){if(void 0!==e.state){l.storeId+=1;var o="__"+l.storeId;l.State=t(l.State,function(t){t[o]=e.state}),e.middlewares&&(l.Middlewares[o]=e.middlewares),l.Actions[o]=e.actions,l.AsyncState[o]=e.asyncState,r&&(l.Context[o]=r);var a=F(o);return{__id:o,actions:a,getState:function(){return U(o)},subscribe:function(t,e){return k(o,t,e)},unsubscribe:function(t){return M(o,t)},useStore:function(t){return V(o,t)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var i=function(t){return function(){return t}};return{__ERROR__:!0,actions:i({}),getActions:i({}),getInitialState:i({}),getState:i({}),subscribe:i(),unsubscribe:i(),useStore:i([{},{}])}}r&&!r.__FROM_SERVER__&&(l.State=t(l.State,function(t){Object.assign(t,r||{})})),n&&(l.Context.__global=n),Object.keys(e).forEach(function(n){var o=e[n];if(o.__ERROR__)return console.error(n+" model's schema is invalid"),l.State=t(l.State,function(t){t[n]={}}),void(l.Actions[n]={});!function(t){return void 0!==t.useStore}(o)?(r&&r.__FROM_SERVER__?l.State=t(l.State,function(t){t[n]=s({},o.state,r[n])}):l.State[n]||(l.State=t(l.State,function(t){t[n]=o.state})),o.middlewares&&(l.Middlewares[n]=o.middlewares),l.Actions[n]=o.actions,l.AsyncState[n]=o.asyncState):(l.State[n]&&r||(l.State=t(l.State,function(t){t[n]=t[o.__id]})),r&&r.__FROM_SERVER__&&(l.State=t(l.State,function(t){t[n]=s({},t[o.__id],r[n])})),l.Actions[n]=l.Actions[o.__id],l.AsyncState[n]=l.AsyncState[o.__id],l.Middlewares[n]=l.Middlewares[o.__id],l.Context[n]=l.Context[o.__id])});var c=Object.keys(e).reduce(function(t,e){var r;return s({},t,((r={})[e]=F(e),r))},{});return l.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,l.withDevTools&&g.config.devtools.enable&&(l.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,l.devTools.connect()),{actions:c,getActions:F,getInitialState:x,getState:U,subscribe:k,unsubscribe:M,useStore:V}}e();var M=function(t,e){k(t,e,void 0)},k=function(t,e,r){Array.isArray(e)?e.forEach(function(e){l.subscriptions[t+"_"+e]||(l.subscriptions[t+"_"+e]=[]),r?l.subscriptions[t+"_"+e].push(r):l.subscriptions[t+"_"+e]=[]}):(l.subscriptions[t+"_"+e]||(l.subscriptions[t+"_"+e]=[]),r?l.subscriptions[t+"_"+e].push(r):l.subscriptions[t+"_"+e]=[])},U=function(t){return l.State[t]},F=function(t,e){void 0===e&&(e={type:"o"});var r={};return Object.keys(l.Actions[t]).forEach(function(n){return r[n]=function(r,o){try{var a=s({action:l.Actions[t][n],actionName:n,consumerActions:A,middlewareConfig:o,modelName:t,newState:null,params:r},e,{Global:l});return Promise.resolve(P(l.Middlewares[t]?l.Middlewares[t]:y,a))}catch(t){return Promise.reject(t)}}}),r},V=function(t,e){var r,i=n({})[1],c=o(""),s=(r=[t],function(t){return r.reduce(function(t,e){return t&&t[e]?t[e]:null},t)})(l.mutableState),u=!!s,f=u?s.selector:e,S=u?s:U(t);if(a(function(){l.uid+=1;var e=""+l.uid;return c.current=e,l.Setter.functionSetter[t]||(l.Setter.functionSetter[t]={}),l.Setter.functionSetter[t][e]={setState:i,selector:f},function(){delete l.Setter.functionSetter[t][e]}},[]),u)return f(S);var m=F(t,{__hash:c.current,type:"f"});return[f?f(S):S,m]},L=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=l.State,e}return u(e,t),e.prototype.render=function(){var t=this.props.children;return l.Setter.classSetter=this.setState.bind(this),i(w.Provider,{value:s({},this.state)},t)},e}(c),X=function(t,e,r){return function(n){return function(o){function a(){return o.apply(this,arguments)||this}return u(a,o),a.prototype.render=function(){var o=this,a=this.props,c=a.state,u=void 0===c?{}:c,f=a.actions,S=void 0===f?{}:f;return i(N,null,function(a){var c=a[""+t],f=l.Actions[t];return i(n,Object.assign({},o.props,{state:s({},u,e?e(c):c),actions:s({},S,r?r(A(f,{modelName:t})):A(f,{modelName:t}))}))})},a}(c)}};export{N as Consumer,D as Model,L as Provider,y as actionMiddlewares,X as connect,I as createStore,x as getInitialState,U as getState,g as middlewares,G as useModel};