react-model
Advanced tools
Comparing version 4.1.1 to 4.1.2-rc.0
@@ -1,1 +0,1 @@ | ||
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}; | ||
import t,{enableES5 as e}from"immer";import{createContext as r,useState as n,useRef as o,useLayoutEffect as a,createElement as c,PureComponent as i}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(i(e))},n=t.action,o=t.modelName,a=t.consumerActions,c=t.params,i=t.next,u=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(c,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,c=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"===c)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,c=o.Setter.functionSetter[r];return"f"===t.type&&a&&c&&c[a]&&c[a].setState&&c[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,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]}),R(t,C(o,c))])).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)})},c=l.AsyncState[o];return c?Promise.resolve(c(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):l.mutableState[r][n]&&e&&"Object"===l.mutableState[r][n].constructor.name&&"Object"===e.constructor.name?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 c=function(t){return function(){return t}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}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 i=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:i,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,c=n({})[1],i=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 i.current=e,l.Setter.functionSetter[t]||(l.Setter.functionSetter[t]={}),l.Setter.functionSetter[t][e]={setState:c,selector:f},function(){delete l.Setter.functionSetter[t][e]}},[]),u)return f(S);var m=F(t,{__hash:i.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),c(w.Provider,{value:s({},this.state)},t)},e}(i),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,i=a.state,u=void 0===i?{}:i,f=a.actions,S=void 0===f?{}:f;return c(N,null,function(a){var i=a[""+t],f=l.Actions[t];return c(n,Object.assign({},o.props,{state:s({},u,e?e(i):i),actions:s({},S,r?r(A(f,{modelName:t})):A(f,{modelName:t}))}))})},a}(i)}};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}; |
@@ -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 a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var c={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},i={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var r=e.next;return Promise.resolve(i.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,a=e.modelName,c=e.consumerActions,i=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(n(i,o({actions:c(u.Actions[a],{modelName:a}),state:u.State[a]},u.Context.__global||{},u.Context[a]||{}))).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,a=e.Global,c=e.type;return a.Setter.functionSetter[r]&&!e.disableSelectorUpdate&&Object.keys(a.Setter.functionSetter[r]).map(function(e){var t=a.Setter.functionSetter[r][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(a.State[r]))}),Promise.resolve(function(){if(n||"u"===c)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,a=e.__hash,c=o.Setter.functionSetter[r];return"f"===e.type&&a&&c&&c[a]&&c[a].setState&&c[a].setState(o.State[r]),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var r=e.next,n=e.Global.subscriptions[e.modelName+"_"+e.actionName];return n&&n.forEach(function(t){t(e)}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var r=e.Global;return!0===i.logger.enable||"function"==typeof i.logger.enable&&i.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&&i.devtools.enable&&r.devTools.send(e.modelName+"_"+e.actionName,r.State),t})}catch(e){return Promise.reject(e)}},v=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,a=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||a)t.setState(o.State[r]);else{var n=t.selector(o.State[r]);A(n,t.selectorRef)||(t.selectorRef=n,t.setState(o.State[r]))}}),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},p=[s,m,d,u,l,f,v,S],b={communicator:v,consoleDebugger:m,devToolsListener:d,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,r){try{var n=t.Global,o=t.modelName,a=t.next,c=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,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(a(r))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:S,tryCatch:s,config:i},_=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(_(p,{Global:c,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=c.State[e];n=r(n,t),c.State=r(c.State,function(t){t[e]=n})}else c.State=r(c.State,function(r){r[e]=o({},r[e],t)});return c.State},E=function(e,t){return new Promise(function(r){return setTimeout(function(){console.log(e),r(t)},e)})},j=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(c.State).map(function(a){try{var i=function(){if(!e||!e.modelName||a===e.modelName||-1!==e.modelName.indexOf(a)){var i=function(e){t&&t.isServer?n[a]=e:c.State=r(c.State,function(t){t[a]=o({},t[a],e)})},s=c.AsyncState[a];return s?Promise.resolve(s(e)).then(i):i({})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:c.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var r=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return r?JSON.parse(r):null},A=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 x=function(e,t){R(e,t,void 0)},R=function(e,t,r){Array.isArray(t)?t.forEach(function(t){c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),r?c.subscriptions[e+"_"+t].push(r):c.subscriptions[e+"_"+t]=[]}):(c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),r?c.subscriptions[e+"_"+t].push(r):c.subscriptions[e+"_"+t]=[])},C=function(e){return c.State[e]},T=function(e,t){void 0===t&&(t={type:"o"});var r={};return Object.keys(c.Actions[e]).forEach(function(n){return r[n]=function(r,a){try{var i=o({action:c.Actions[e][n],actionName:n,consumerActions:w,middlewareConfig:a,modelName:e,newState:null,params:r},t,{Global:c});return Promise.resolve(_(c.Middlewares[e]?c.Middlewares[e]:p,i))}catch(e){return Promise.reject(e)}}}),r},I=function(e,t){var r,o=n.useState({})[1],a=n.useRef(""),i=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(c.mutableState),s=!!i,u=s?i.selector:t,l=s?i:C(e);if(n.useLayoutEffect(function(){c.uid+=1;var t=""+c.uid;return a.current=t,c.Setter.functionSetter[e]||(c.Setter.functionSetter[e]={}),c.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete c.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=T(e,{__hash:a.current,type:"f"});return[u?u(l):l,f]},M=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=c.State,t}return a(t,e),t.prototype.render=function(){var e=this.props.children;return c.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){c.storeId+=1;var a="__"+c.storeId;c.State=r(c.State,function(t){t[a]=e.state}),e.middlewares&&(c.Middlewares[a]=e.middlewares),c.Actions[a]=e.actions,c.AsyncState[a]=e.asyncState,t&&(c.Context[a]=t);var i=T(a);return{__id:a,actions:i,getState:function(){return C(a)},subscribe:function(e,t){return R(a,e,t)},unsubscribe:function(e){return x(a,e)},useStore:function(e){return I(a,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var s=function(e){return function(){return e}};return{__ERROR__:!0,actions:s({}),getActions:s({}),getInitialState:s({}),getState:s({}),subscribe:s(),unsubscribe:s(),useStore:s([{},{}])}}t&&!t.__FROM_SERVER__&&(c.State=r(c.State,function(e){Object.assign(e,t||{})})),n&&(c.Context.__global=n),Object.keys(e).forEach(function(n){var a=e[n];if(a.__ERROR__)return console.error(n+" model's schema is invalid"),c.State=r(c.State,function(e){e[n]={}}),void(c.Actions[n]={});void 0===a.useStore?(t&&t.__FROM_SERVER__?c.State=r(c.State,function(e){e[n]=o({},a.state,t[n])}):c.State[n]||(c.State=r(c.State,function(e){e[n]=a.state})),a.middlewares&&(c.Middlewares[n]=a.middlewares),c.Actions[n]=a.actions,c.AsyncState[n]=a.asyncState):(c.State[n]&&t||(c.State=r(c.State,function(e){e[n]=e[a.__id]})),t&&t.__FROM_SERVER__&&(c.State=r(c.State,function(e){e[n]=o({},e[a.__id],t[n])})),c.Actions[n]=c.Actions[a.__id],c.AsyncState[n]=c.AsyncState[a.__id],c.Middlewares[n]=c.Middlewares[a.__id],c.Context[n]=c.Context[a.__id])});var u=Object.keys(e).reduce(function(e,t){var r;return o({},e,((r={})[t]=T(t),r))},{});return c.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,c.withDevTools&&b.config.devtools.enable&&(c.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,c.devTools.connect()),{actions:u,getActions:T,getInitialState:j,getState:C,subscribe:R,unsubscribe:x,useStore:I}},exports.Provider=M,exports.actionMiddlewares=p,exports.connect=function(e,t,r){return function(i){return function(s){function u(){return s.apply(this,arguments)||this}return a(u,s),u.prototype.render=function(){var a=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,S=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=c.Actions[e];return n.createElement(i,Object.assign({},a.props,{state:o({},l,t?t(u):u),actions:o({},S,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},exports.createStore=function(e,t){var r="string"==typeof e;c.storeId+=r?0:1;var n=r?e:c.storeId.toString();c.Actions[n]||(c.Actions[n]={}),c.mutableState[n]||(c.mutableState[n]={count:0});var o=function(){return c.mutableState[n].count=0,c.currentStoreId=n,t?t():e()};return c.mutableState[n].selector=o,{useStore:function(){return I(n,o)},getState:function(){return o()}}},exports.getInitialState=j,exports.getState=C,exports.middlewares=b,exports.useModel=function(e){var t=c.currentStoreId,n=c.mutableState[t].count;return c.mutableState[t].count+=1,c.mutableState[t].hasOwnProperty(n)||(c.mutableState[t][n]="function"==typeof e?e():e),[c.mutableState[t][n],function(e){try{return c.mutableState[t][n]="function"==typeof e?r(c.mutableState[t][n],e):"object"==typeof c.mutableState[t][n]&&"object"==typeof e?o({},c.mutableState[t][n],e):e,Promise.resolve(_(p,{Global:c,action:function(){return"function"==typeof e?c.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 t,e=require("immer"),r=(t=e)&&"object"==typeof t&&"default"in t?t.default:t,n=require("react");function o(){return(o=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 a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}var c={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},i={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(t,e){try{var r=t.next;return Promise.resolve(i.tryCatch.enable?r(e).catch(function(t){return console.log(t)}):r(e))}catch(t){return Promise.reject(t)}},u=function(t,e){try{var r=function(){return Promise.resolve(s(e))},n=t.action,a=t.modelName,c=t.consumerActions,i=t.params,s=t.next,u=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(i,o({actions:c(u.Actions[a],{modelName:a}),state:u.State[a]},u.Context.__global||{},u.Context[a]||{}))).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)}},l=function(t,e){try{var r=t.modelName,n=t.newState,o=t.next,a=t.Global,c=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"===c)return N(r,n||{}),Promise.resolve(o(e))}())}catch(t){return Promise.reject(t)}},f=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,a=t.__hash,c=o.Setter.functionSetter[r];return"f"===t.type&&a&&c&&c[a]&&c[a].setState&&c[a].setState(o.State[r]),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},S=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)}},m=function(t,e){try{var r=t.Global;return!0===i.logger.enable||"function"==typeof i.logger.enable&&i.logger.enable(t)?(console.group("%c "+t.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",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)}},d=function(t,e){try{var r=t.Global;return Promise.resolve(t.next(e)).then(function(e){return r.withDevTools&&i.devtools.enable&&r.devTools.send(t.modelName+"_"+t.actionName,r.State),e})}catch(t){return Promise.reject(t)}},v=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]);A(n,e.selectorRef)||(e.selectorRef=n,e.setState(o.State[r]))}}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},p=[s,m,d,u,l,f,v,S],b={communicator:v,consoleDebugger:m,devToolsListener:d,getNewState:u,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,r){try{var n=e.Global,o=e.modelName,a=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]}),E(t,j(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(a(r))})}catch(t){return Promise.reject(t)}}},setNewState:l,stateUpdater:f,subscription:S,tryCatch:s,config:i},_=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)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(t){g.push(t),console.log("%c \nBEGIN GROUP: %c",P,t),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=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,{Global:c,action:t,actionName:t.name,consumerActions:w,middlewareConfig:n,modelName:e.modelName,newState:null,params:r,type:"o"}))}catch(t){return Promise.reject(t)}}}(t[n],e)}),r},N=function(t,e){if("function"==typeof e){var n=c.State[t];n=r(n,e),c.State=r(c.State,function(e){e[t]=n})}else c.State=r(c.State,function(r){r[t]=o({},r[t],e)});return c.State},E=function(t,e){return new Promise(function(r){return setTimeout(function(){console.log(t),r(e)},t)})},O=function(t,e){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(c.State).map(function(a){try{var i=function(){if(!t||!t.modelName||a===t.modelName||-1!==t.modelName.indexOf(a)){var i=function(t){e&&e.isServer?n[a]=t:c.State=r(c.State,function(e){e[a]=o({},e[a],t)})},s=c.AsyncState[a];return s?Promise.resolve(s(t)).then(i):i({})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return e&&e.isServer?n:c.State})}catch(t){return Promise.reject(t)}},j=function(t,e){var r=localStorage.getItem("__REACT_MODELX__"+t+"_"+e);return r?JSON.parse(r):null},A=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.enableES5();var x=function(t,e){R(t,e,void 0)},R=function(t,e,r){Array.isArray(e)?e.forEach(function(e){c.subscriptions[t+"_"+e]||(c.subscriptions[t+"_"+e]=[]),r?c.subscriptions[t+"_"+e].push(r):c.subscriptions[t+"_"+e]=[]}):(c.subscriptions[t+"_"+e]||(c.subscriptions[t+"_"+e]=[]),r?c.subscriptions[t+"_"+e].push(r):c.subscriptions[t+"_"+e]=[])},C=function(t){return c.State[t]},T=function(t,e){void 0===e&&(e={type:"o"});var r={};return Object.keys(c.Actions[t]).forEach(function(n){return r[n]=function(r,a){try{var i=o({action:c.Actions[t][n],actionName:n,consumerActions:w,middlewareConfig:a,modelName:t,newState:null,params:r},e,{Global:c});return Promise.resolve(_(c.Middlewares[t]?c.Middlewares[t]:p,i))}catch(t){return Promise.reject(t)}}}),r},I=function(t,e){var r,o=n.useState({})[1],a=n.useRef(""),i=(r=[t],function(t){return r.reduce(function(t,e){return t&&t[e]?t[e]:null},t)})(c.mutableState),s=!!i,u=s?i.selector:e,l=s?i:C(t);if(n.useLayoutEffect(function(){c.uid+=1;var e=""+c.uid;return a.current=e,c.Setter.functionSetter[t]||(c.Setter.functionSetter[t]={}),c.Setter.functionSetter[t][e]={setState:o,selector:u},function(){delete c.Setter.functionSetter[t][e]}},[]),s)return u(l);var f=T(t,{__hash:a.current,type:"f"});return[u?u(l):l,f]},M=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=c.State,e}return a(e,t),e.prototype.render=function(){var t=this.props.children;return c.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:o({},this.state)},t)},e}(n.PureComponent);exports.Consumer=y,exports.Model=function(t,e,n){if(void 0!==t.state){c.storeId+=1;var a="__"+c.storeId;c.State=r(c.State,function(e){e[a]=t.state}),t.middlewares&&(c.Middlewares[a]=t.middlewares),c.Actions[a]=t.actions,c.AsyncState[a]=t.asyncState,e&&(c.Context[a]=e);var i=T(a);return{__id:a,actions:i,getState:function(){return C(a)},subscribe:function(t,e){return R(a,t,e)},unsubscribe:function(t){return x(a,t)},useStore:function(t){return I(a,t)}}}if(t.actions){console.error("invalid model(s) schema: ",t);var s=function(t){return function(){return t}};return{__ERROR__:!0,actions:s({}),getActions:s({}),getInitialState:s({}),getState:s({}),subscribe:s(),unsubscribe:s(),useStore:s([{},{}])}}e&&!e.__FROM_SERVER__&&(c.State=r(c.State,function(t){Object.assign(t,e||{})})),n&&(c.Context.__global=n),Object.keys(t).forEach(function(n){var a=t[n];if(a.__ERROR__)return console.error(n+" model's schema is invalid"),c.State=r(c.State,function(t){t[n]={}}),void(c.Actions[n]={});void 0===a.useStore?(e&&e.__FROM_SERVER__?c.State=r(c.State,function(t){t[n]=o({},a.state,e[n])}):c.State[n]||(c.State=r(c.State,function(t){t[n]=a.state})),a.middlewares&&(c.Middlewares[n]=a.middlewares),c.Actions[n]=a.actions,c.AsyncState[n]=a.asyncState):(c.State[n]&&e||(c.State=r(c.State,function(t){t[n]=t[a.__id]})),e&&e.__FROM_SERVER__&&(c.State=r(c.State,function(t){t[n]=o({},t[a.__id],e[n])})),c.Actions[n]=c.Actions[a.__id],c.AsyncState[n]=c.AsyncState[a.__id],c.Middlewares[n]=c.Middlewares[a.__id],c.Context[n]=c.Context[a.__id])});var u=Object.keys(t).reduce(function(t,e){var r;return o({},t,((r={})[e]=T(e),r))},{});return c.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,c.withDevTools&&b.config.devtools.enable&&(c.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,c.devTools.connect()),{actions:u,getActions:T,getInitialState:O,getState:C,subscribe:R,unsubscribe:x,useStore:I}},exports.Provider=M,exports.actionMiddlewares=p,exports.connect=function(t,e,r){return function(i){return function(s){function u(){return s.apply(this,arguments)||this}return a(u,s),u.prototype.render=function(){var a=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,S=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+t],f=c.Actions[t];return n.createElement(i,Object.assign({},a.props,{state:o({},l,e?e(u):u),actions:o({},S,r?r(w(f,{modelName:t})):w(f,{modelName:t}))}))})},u}(n.PureComponent)}},exports.createStore=function(t,e){var r="string"==typeof t;c.storeId+=r?0:1;var n=r?t:c.storeId.toString();c.Actions[n]||(c.Actions[n]={}),c.mutableState[n]||(c.mutableState[n]={count:0});var o=function(){return c.mutableState[n].count=0,c.currentStoreId=n,e?e():t()};return c.mutableState[n].selector=o,{useStore:function(){return I(n,o)},getState:function(){return o()}}},exports.getInitialState=O,exports.getState=C,exports.middlewares=b,exports.useModel=function(t){var e=c.currentStoreId,n=c.mutableState[e].count;return c.mutableState[e].count+=1,c.mutableState[e].hasOwnProperty(n)||(c.mutableState[e][n]="function"==typeof t?t():t),[c.mutableState[e][n],function(t){try{return c.mutableState[e][n]="function"==typeof t?r(c.mutableState[e][n],t):c.mutableState[e][n]&&t&&"Object"===c.mutableState[e][n].constructor.name&&"Object"===t.constructor.name?o({},c.mutableState[e][n],t):t,Promise.resolve(_(p,{Global:c,action:function(){return"function"==typeof t?c.mutableState[e][n]:t},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:e,newState:{},params:void 0,type:"u"}))}catch(t){return Promise.reject(t)}}]}; |
@@ -1,1 +0,1 @@ | ||
import t,{enableES5 as e}from"immer";import{createContext as o,useState as a,useRef as n,useLayoutEffect as s,PureComponent as r,createElement as c}from"react";var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1};const l={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},u=async(t,e)=>{const{next:o}=t;return l.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},S=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)},d=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)},m=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)},b=async(t,e)=>{const{modelName:o,actionName:a,next:n,Global:s}=t,r=s.subscriptions[`${o}_${a}`];return r&&r.forEach(e=>{e(t)}),await n(e)},_=async(t,e)=>{const{Global:o}=t;if(!0===l.logger.enable||"function"==typeof l.logger.enable&&l.logger.enable(t)){console.group(`%c ${t.modelName} State Change %c ${(new Date).toLocaleTimeString()}`,"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",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&&l.devtools.enable&&o.devTools.send(`${t.modelName}_${t.actionName}`,o.State),a},p=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=[u,_,f,S,d,m,p,b],y={communicator:p,consoleDebugger:_,devToolsListener:f,getNewState:S,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]}),O(t,R(s,l))])||null,await i(o)},setNewState:d,stateUpdater:m,subscription:b,tryCatch:u,config:l},g=async(t,e)=>(e.next=t=>t.length>0?t[0](e,t.slice(1)):e.newState,await t[0](e,t.slice(1))),h=o({}),N=h.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:i,action:t,actionName:t.name,consumerActions:A,middlewareConfig:a,modelName:e.modelName,newState:null,params:o,type:"o"};return await g(w,n)})(t[a],e)}),o},E=(e,o)=>{if("function"==typeof o){let a=i.State[e];a=t(a,o),i.State=t(i.State,t=>{t[e]=a})}else i.State=t(i.State,t=>{t[e]={...t[e],...o}});return i.State},O=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),v=async(e,o)=>{const a={__FROM_SERVER__:!0};return await Promise.all(Object.keys(i.State).map(async n=>{if(!e||!e.modelName||n===e.modelName||-1!==e.modelName.indexOf(n)){const s=i.AsyncState[n],r=s?await s(e):{};o&&o.isServer?a[n]=r:i.State=t(i.State,t=>{t[n]={...t[n],...r}})}})),o&&o.isServer?a:i.State},R=(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};function x(e){const o=i.currentStoreId,a=i.mutableState[o].count;return i.mutableState[o].count+=1,i.mutableState[o].hasOwnProperty(a)||(i.mutableState[o][a]="function"==typeof e?e():e),[i.mutableState[o][a],async e=>{i.mutableState[o][a]="function"==typeof e?t(i.mutableState[o][a],e):"object"==typeof i.mutableState[o][a]&&"object"==typeof e?{...i.mutableState[o][a],...e}:e;const n={Global:i,action:()=>"function"==typeof e?i.mutableState[o][a]:e,actionName:"setter",consumerActions:A,disableSelectorUpdate:!0,middlewareConfig:{},modelName:o,newState:{},params:void 0,type:"u"};return await g(w,n)}]}function C(t,e){const o="string"==typeof t;i.storeId+=o?0:1;const a=o?t:i.storeId.toString();i.Actions[a]||(i.Actions[a]={}),i.mutableState[a]||(i.mutableState[a]={count:0});const n=()=>(i.mutableState[a].count=0,i.currentStoreId=a,e?e():t());return i.mutableState[a].selector=n,{useStore:()=>M(a,n),getState:()=>n()}}function j(e,o,a){if(void 0!==e.state){i.storeId+=1;const a="__"+i.storeId;i.State=t(i.State,t=>{t[a]=e.state}),e.middlewares&&(i.Middlewares[a]=e.middlewares),i.Actions[a]=e.actions,i.AsyncState[a]=e.asyncState,o&&(i.Context[a]=o);const n=D(a);return{__id:a,actions:n,getState:()=>I(a),subscribe:(t,e)=>G(a,t,e),unsubscribe:t=>T(a,t),useStore:t=>M(a,t)}}{if(e.actions){console.error("invalid model(s) schema: ",e);const t=t=>(...e)=>t;return{__ERROR__:!0,actions:t({}),getActions:t({}),getInitialState:t({}),getState:t({}),subscribe:t(),unsubscribe:t(),useStore:t([{},{}])}}o&&!o.__FROM_SERVER__&&(i.State=t(i.State,t=>{Object.assign(t,o||{})})),a&&(i.Context.__global=a),Object.keys(e).forEach(a=>{const n=e[a];if(n.__ERROR__)return console.error(a+" model's schema is invalid"),i.State=t(i.State,t=>{t[a]={}}),void(i.Actions[a]={});(t=>void 0!==t.useStore)(n)?(i.State[a]&&o||(i.State=t(i.State,t=>{t[a]=t[n.__id]})),o&&o.__FROM_SERVER__&&(i.State=t(i.State,t=>{t[a]={...t[n.__id],...o[a]}})),i.Actions[a]=i.Actions[n.__id],i.AsyncState[a]=i.AsyncState[n.__id],i.Middlewares[a]=i.Middlewares[n.__id],i.Context[a]=i.Context[n.__id]):(o&&o.__FROM_SERVER__?i.State=t(i.State,t=>{t[a]={...n.state,...o[a]}}):i.State[a]||(i.State=t(i.State,t=>{t[a]=n.state})),n.middlewares&&(i.Middlewares[a]=n.middlewares),i.Actions[a]=n.actions,i.AsyncState[a]=n.asyncState)});const n=Object.keys(e).reduce((t,e)=>({...t,[e]:D(e)}),{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&y.config.devtools.enable&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:n,getActions:D,getInitialState:v,getState:I,subscribe:G,unsubscribe:T,useStore:M}}}e();const T=(t,e)=>{G(t,e,void 0)},G=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{i.subscriptions[`${t}_${e}`]||(i.subscriptions[`${t}_${e}`]=[]),o?i.subscriptions[`${t}_${e}`].push(o):i.subscriptions[`${t}_${e}`]=[]}):(i.subscriptions[`${t}_${e}`]||(i.subscriptions[`${t}_${e}`]=[]),o?i.subscriptions[`${t}_${e}`].push(o):i.subscriptions[`${t}_${e}`]=[])},I=t=>i.State[t],D=(t,e={type:"o"})=>{const o={};return Object.keys(i.Actions[t]).forEach(a=>o[a]=async(o,n)=>{const s={action:i.Actions[t][a],actionName:a,consumerActions:A,middlewareConfig:n,modelName:t,newState:null,params:o,...e,Global:i};return i.Middlewares[t]?await g(i.Middlewares[t],s):await g(w,s)}),o},M=(t,e)=>{const o=a({})[1],r=n(""),c=(l=[t],t=>l.reduce((t,e)=>t&&t[e]?t[e]:null,t))(i.mutableState);var l;const u=!!c,S=u?c.selector:e,d=u?c:I(t);if(s(()=>{i.uid+=1;const e=""+i.uid;return r.current=e,i.Setter.functionSetter[t]||(i.Setter.functionSetter[t]={}),i.Setter.functionSetter[t][e]={setState:o,selector:S},function(){delete i.Setter.functionSetter[t][e]}},[]),u)return S(d);{const e=D(t,{__hash:r.current,type:"f"});return[S?S(d):d,e]}};class k extends r{constructor(){super(...arguments),this.state=i.State}render(){const{children:t}=this.props;return i.Setter.classSetter=this.setState.bind(this),c(h.Provider,{value:{...this.state}},t)}}const P=(t,e,o)=>a=>class extends r{render(){const{state:n={},actions:s={}}=this.props;return c(N,null,r=>{const{[""+t]:l}=r,u=i.Actions[t];return c(a,Object.assign({},this.props,{state:{...n,...e?e(l):l},actions:{...s,...o?o(A(u,{modelName:t})):A(u,{modelName:t})}}))})}};export{N as Consumer,j as Model,k as Provider,w as actionMiddlewares,P as connect,C as createStore,v as getInitialState,I as getState,y as middlewares,x as useModel}; | ||
import t,{enableES5 as e}from"immer";import{createContext as o,useState as a,useRef as n,useLayoutEffect as s,PureComponent as r,createElement as c}from"react";var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1};const l={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},u=async(t,e)=>{const{next:o}=t;return l.tryCatch.enable?await o(e).catch(t=>console.log(t)):await o(e)},S=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)},d=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 A(o,a||{}),await n(e)},m=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)},b=async(t,e)=>{const{modelName:o,actionName:a,next:n,Global:s}=t,r=s.subscriptions[`${o}_${a}`];return r&&r.forEach(e=>{e(t)}),await n(e)},_=async(t,e)=>{const{Global:o}=t;if(!0===l.logger.enable||"function"==typeof l.logger.enable&&l.logger.enable(t)){console.group(`%c ${t.modelName} State Change %c ${(new Date).toLocaleTimeString()}`,"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",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&&l.devtools.enable&&o.devTools.send(`${t.modelName}_${t.actionName}`,o.State),a},p=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=[u,_,f,S,d,m,p,b],g={communicator:p,consoleDebugger:_,devToolsListener:f,getNewState:S,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]}),E(t,R(s,l))])||null,await i(o)},setNewState:d,stateUpdater:m,subscription:b,tryCatch:u,config:l},y=async(t,e)=>(e.next=t=>t.length>0?t[0](e,t.slice(1)):e.newState,await t[0](e,t.slice(1))),h=o({}),N=h.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 O=(t,e)=>{const o={};return Object.keys(t).forEach(a=>{o[a]=((t,e)=>async(o,a)=>{const n={Global:i,action:t,actionName:t.name,consumerActions:O,middlewareConfig:a,modelName:e.modelName,newState:null,params:o,type:"o"};return await y(w,n)})(t[a],e)}),o},A=(e,o)=>{if("function"==typeof o){let a=i.State[e];a=t(a,o),i.State=t(i.State,t=>{t[e]=a})}else i.State=t(i.State,t=>{t[e]={...t[e],...o}});return i.State},E=(t,e)=>new Promise(o=>setTimeout(()=>{console.log(t),o(e)},t)),v=async(e,o)=>{const a={__FROM_SERVER__:!0};return await Promise.all(Object.keys(i.State).map(async n=>{if(!e||!e.modelName||n===e.modelName||-1!==e.modelName.indexOf(n)){const s=i.AsyncState[n],r=s?await s(e):{};o&&o.isServer?a[n]=r:i.State=t(i.State,t=>{t[n]={...t[n],...r}})}})),o&&o.isServer?a:i.State},R=(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};function x(e){const o=i.currentStoreId,a=i.mutableState[o].count;return i.mutableState[o].count+=1,i.mutableState[o].hasOwnProperty(a)||(i.mutableState[o][a]="function"==typeof e?e():e),[i.mutableState[o][a],async e=>{i.mutableState[o][a]="function"==typeof e?t(i.mutableState[o][a],e):i.mutableState[o][a]&&e&&"Object"===i.mutableState[o][a].constructor.name&&"Object"===e.constructor.name?{...i.mutableState[o][a],...e}:e;const n={Global:i,action:()=>"function"==typeof e?i.mutableState[o][a]:e,actionName:"setter",consumerActions:O,disableSelectorUpdate:!0,middlewareConfig:{},modelName:o,newState:{},params:void 0,type:"u"};return await y(w,n)}]}function C(t,e){const o="string"==typeof t;i.storeId+=o?0:1;const a=o?t:i.storeId.toString();i.Actions[a]||(i.Actions[a]={}),i.mutableState[a]||(i.mutableState[a]={count:0});const n=()=>(i.mutableState[a].count=0,i.currentStoreId=a,e?e():t());return i.mutableState[a].selector=n,{useStore:()=>M(a,n),getState:()=>n()}}function j(e,o,a){if(void 0!==e.state){i.storeId+=1;const a="__"+i.storeId;i.State=t(i.State,t=>{t[a]=e.state}),e.middlewares&&(i.Middlewares[a]=e.middlewares),i.Actions[a]=e.actions,i.AsyncState[a]=e.asyncState,o&&(i.Context[a]=o);const n=D(a);return{__id:a,actions:n,getState:()=>I(a),subscribe:(t,e)=>G(a,t,e),unsubscribe:t=>T(a,t),useStore:t=>M(a,t)}}{if(e.actions){console.error("invalid model(s) schema: ",e);const t=t=>(...e)=>t;return{__ERROR__:!0,actions:t({}),getActions:t({}),getInitialState:t({}),getState:t({}),subscribe:t(),unsubscribe:t(),useStore:t([{},{}])}}o&&!o.__FROM_SERVER__&&(i.State=t(i.State,t=>{Object.assign(t,o||{})})),a&&(i.Context.__global=a),Object.keys(e).forEach(a=>{const n=e[a];if(n.__ERROR__)return console.error(a+" model's schema is invalid"),i.State=t(i.State,t=>{t[a]={}}),void(i.Actions[a]={});(t=>void 0!==t.useStore)(n)?(i.State[a]&&o||(i.State=t(i.State,t=>{t[a]=t[n.__id]})),o&&o.__FROM_SERVER__&&(i.State=t(i.State,t=>{t[a]={...t[n.__id],...o[a]}})),i.Actions[a]=i.Actions[n.__id],i.AsyncState[a]=i.AsyncState[n.__id],i.Middlewares[a]=i.Middlewares[n.__id],i.Context[a]=i.Context[n.__id]):(o&&o.__FROM_SERVER__?i.State=t(i.State,t=>{t[a]={...n.state,...o[a]}}):i.State[a]||(i.State=t(i.State,t=>{t[a]=n.state})),n.middlewares&&(i.Middlewares[a]=n.middlewares),i.Actions[a]=n.actions,i.AsyncState[a]=n.asyncState)});const n=Object.keys(e).reduce((t,e)=>({...t,[e]:D(e)}),{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&g.config.devtools.enable&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:n,getActions:D,getInitialState:v,getState:I,subscribe:G,unsubscribe:T,useStore:M}}}e();const T=(t,e)=>{G(t,e,void 0)},G=(t,e,o)=>{Array.isArray(e)?e.forEach(e=>{i.subscriptions[`${t}_${e}`]||(i.subscriptions[`${t}_${e}`]=[]),o?i.subscriptions[`${t}_${e}`].push(o):i.subscriptions[`${t}_${e}`]=[]}):(i.subscriptions[`${t}_${e}`]||(i.subscriptions[`${t}_${e}`]=[]),o?i.subscriptions[`${t}_${e}`].push(o):i.subscriptions[`${t}_${e}`]=[])},I=t=>i.State[t],D=(t,e={type:"o"})=>{const o={};return Object.keys(i.Actions[t]).forEach(a=>o[a]=async(o,n)=>{const s={action:i.Actions[t][a],actionName:a,consumerActions:O,middlewareConfig:n,modelName:t,newState:null,params:o,...e,Global:i};return i.Middlewares[t]?await y(i.Middlewares[t],s):await y(w,s)}),o},M=(t,e)=>{const o=a({})[1],r=n(""),c=(l=[t],t=>l.reduce((t,e)=>t&&t[e]?t[e]:null,t))(i.mutableState);var l;const u=!!c,S=u?c.selector:e,d=u?c:I(t);if(s(()=>{i.uid+=1;const e=""+i.uid;return r.current=e,i.Setter.functionSetter[t]||(i.Setter.functionSetter[t]={}),i.Setter.functionSetter[t][e]={setState:o,selector:S},function(){delete i.Setter.functionSetter[t][e]}},[]),u)return S(d);{const e=D(t,{__hash:r.current,type:"f"});return[S?S(d):d,e]}};class k extends r{constructor(){super(...arguments),this.state=i.State}render(){const{children:t}=this.props;return i.Setter.classSetter=this.setState.bind(this),c(h.Provider,{value:{...this.state}},t)}}const P=(t,e,o)=>a=>class extends r{render(){const{state:n={},actions:s={}}=this.props;return c(N,null,r=>{const{[""+t]:l}=r,u=i.Actions[t];return c(a,Object.assign({},this.props,{state:{...n,...e?e(l):l},actions:{...s,...o?o(O(u,{modelName:t})):O(u,{modelName:t})}}))})}};export{N as Consumer,j as Model,k as Provider,w as actionMiddlewares,P as connect,C as createStore,v as getInitialState,I as getState,g as middlewares,x 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,r){var n="default"in t?t.default:t;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 a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var i={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},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,a=e.modelName,i=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:i(u.Actions[a],{modelName:a}),state:u.State[a]},u.Context.__global||{},u.Context[a]||{}))).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,a=e.Global,i=e.type;return a.Setter.functionSetter[r]&&!e.disableSelectorUpdate&&Object.keys(a.Setter.functionSetter[r]).map(function(e){var t=a.Setter.functionSetter[r][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(a.State[r]))}),Promise.resolve(function(){if(n||"u"===i)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,a=e.__hash,i=o.Setter.functionSetter[r];return"f"===e.type&&a&&i&&i[a]&&i[a].setState&&i[a].setState(o.State[r]),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var r=e.next,n=e.Global.subscriptions[e.modelName+"_"+e.actionName];return n&&n.forEach(function(t){t(e)}),Promise.resolve(r(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&&c.devtools.enable&&r.devTools.send(e.modelName+"_"+e.actionName,r.State),t})}catch(e){return Promise.reject(e)}},v=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,a=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||a)t.setState(o.State[r]);else{var n=t.selector(o.State[r]);A(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,v,m],p={communicator:v,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,a=t.next,i=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,O(o,i))])).then(function(e){return t.newState=e||null,Promise.resolve(a(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=r.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:i,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 r=i.State[e];r=n(r,t),i.State=n(i.State,function(t){t[e]=r})}else i.State=n(i.State,function(r){r[e]=o({},r[e],t)});return i.State},E=function(e,t){return new Promise(function(r){return setTimeout(function(){console.log(e),r(t)},e)})},j=function(e,t){try{var r={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(i.State).map(function(a){try{var c=function(){if(!e||!e.modelName||a===e.modelName||-1!==e.modelName.indexOf(a)){var c=function(e){t&&t.isServer?r[a]=e:i.State=n(i.State,function(t){t[a]=o({},t[a],e)})},s=i.AsyncState[a];return s?Promise.resolve(s(e)).then(c):c({})}}();return Promise.resolve(c&&c.then?c.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?r:i.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var r=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return r?JSON.parse(r):null},A=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 R=function(e,t){x(e,t,void 0)},x=function(e,t,r){Array.isArray(t)?t.forEach(function(t){i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),r?i.subscriptions[e+"_"+t].push(r):i.subscriptions[e+"_"+t]=[]}):(i.subscriptions[e+"_"+t]||(i.subscriptions[e+"_"+t]=[]),r?i.subscriptions[e+"_"+t].push(r):i.subscriptions[e+"_"+t]=[])},C=function(e){return i.State[e]},M=function(e,t){void 0===t&&(t={type:"o"});var r={};return Object.keys(i.Actions[e]).forEach(function(n){return r[n]=function(r,a){try{var c=o({action:i.Actions[e][n],actionName:n,consumerActions:w,middlewareConfig:a,modelName:e,newState:null,params:r},t,{Global:i});return Promise.resolve(_(i.Middlewares[e]?i.Middlewares[e]:b,c))}catch(e){return Promise.reject(e)}}}),r},T=function(e,t){var n,o=r.useState({})[1],a=r.useRef(""),c=(n=[e],function(e){return n.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(i.mutableState),s=!!c,u=s?c.selector:t,l=s?c:C(e);if(r.useLayoutEffect(function(){i.uid+=1;var t=""+i.uid;return a.current=t,i.Setter.functionSetter[e]||(i.Setter.functionSetter[e]={}),i.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete i.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=M(e,{__hash:a.current,type:"f"});return[u?u(l):l,f]},I=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=i.State,t}return a(t,e),t.prototype.render=function(){var e=this.props.children;return i.Setter.classSetter=this.setState.bind(this),r.createElement(h.Provider,{value:o({},this.state)},e)},t}(r.PureComponent);e.Consumer=y,e.Model=function(e,t,r){if(void 0!==e.state){i.storeId+=1;var a="__"+i.storeId;i.State=n(i.State,function(t){t[a]=e.state}),e.middlewares&&(i.Middlewares[a]=e.middlewares),i.Actions[a]=e.actions,i.AsyncState[a]=e.asyncState,t&&(i.Context[a]=t);var c=M(a);return{__id:a,actions:c,getState:function(){return C(a)},subscribe:function(e,t){return x(a,e,t)},unsubscribe:function(e){return R(a,e)},useStore:function(e){return T(a,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var s=function(e){return function(){return e}};return{__ERROR__:!0,actions:s({}),getActions:s({}),getInitialState:s({}),getState:s({}),subscribe:s(),unsubscribe:s(),useStore:s([{},{}])}}t&&!t.__FROM_SERVER__&&(i.State=n(i.State,function(e){Object.assign(e,t||{})})),r&&(i.Context.__global=r),Object.keys(e).forEach(function(r){var a=e[r];if(a.__ERROR__)return console.error(r+" model's schema is invalid"),i.State=n(i.State,function(e){e[r]={}}),void(i.Actions[r]={});void 0===a.useStore?(t&&t.__FROM_SERVER__?i.State=n(i.State,function(e){e[r]=o({},a.state,t[r])}):i.State[r]||(i.State=n(i.State,function(e){e[r]=a.state})),a.middlewares&&(i.Middlewares[r]=a.middlewares),i.Actions[r]=a.actions,i.AsyncState[r]=a.asyncState):(i.State[r]&&t||(i.State=n(i.State,function(e){e[r]=e[a.__id]})),t&&t.__FROM_SERVER__&&(i.State=n(i.State,function(e){e[r]=o({},e[a.__id],t[r])})),i.Actions[r]=i.Actions[a.__id],i.AsyncState[r]=i.AsyncState[a.__id],i.Middlewares[r]=i.Middlewares[a.__id],i.Context[r]=i.Context[a.__id])});var u=Object.keys(e).reduce(function(e,t){var r;return o({},e,((r={})[t]=M(t),r))},{});return i.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,i.withDevTools&&p.config.devtools.enable&&(i.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,i.devTools.connect()),{actions:u,getActions:M,getInitialState:j,getState:C,subscribe:x,unsubscribe:R,useStore:T}},e.Provider=I,e.actionMiddlewares=b,e.connect=function(e,t,n){return function(c){return function(s){function u(){return s.apply(this,arguments)||this}return a(u,s),u.prototype.render=function(){var a=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return r.createElement(y,null,function(s){var u=s[""+e],f=i.Actions[e];return r.createElement(c,Object.assign({},a.props,{state:o({},l,t?t(u):u),actions:o({},m,n?n(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(r.PureComponent)}},e.createStore=function(e,t){var r="string"==typeof e;i.storeId+=r?0:1;var n=r?e:i.storeId.toString();i.Actions[n]||(i.Actions[n]={}),i.mutableState[n]||(i.mutableState[n]={count:0});var o=function(){return i.mutableState[n].count=0,i.currentStoreId=n,t?t():e()};return i.mutableState[n].selector=o,{useStore:function(){return T(n,o)},getState:function(){return o()}}},e.getInitialState=j,e.getState=C,e.middlewares=p,e.useModel=function(e){var t=i.currentStoreId,r=i.mutableState[t].count;return i.mutableState[t].count+=1,i.mutableState[t].hasOwnProperty(r)||(i.mutableState[t][r]="function"==typeof e?e():e),[i.mutableState[t][r],function(e){try{return i.mutableState[t][r]="function"==typeof e?n(i.mutableState[t][r],e):"object"==typeof i.mutableState[t][r]&&"object"==typeof e?o({},i.mutableState[t][r],e):e,Promise.resolve(_(b,{Global:i,action:function(){return"function"==typeof e?i.mutableState[t][r]: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,r){var n="default"in t?t.default:t;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 a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var c={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},mutableState:{},uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},i={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var r=e.next;return Promise.resolve(i.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,a=e.modelName,c=e.consumerActions,i=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(n(i,o({actions:c(u.Actions[a],{modelName:a}),state:u.State[a]},u.Context.__global||{},u.Context[a]||{}))).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,a=e.Global,c=e.type;return a.Setter.functionSetter[r]&&!e.disableSelectorUpdate&&Object.keys(a.Setter.functionSetter[r]).map(function(e){var t=a.Setter.functionSetter[r][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(a.State[r]))}),Promise.resolve(function(){if(n||"u"===c)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,a=e.__hash,c=o.Setter.functionSetter[r];return"f"===e.type&&a&&c&&c[a]&&c[a].setState&&c[a].setState(o.State[r]),Promise.resolve(n(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var r=e.next,n=e.Global.subscriptions[e.modelName+"_"+e.actionName];return n&&n.forEach(function(t){t(e)}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var r=e.Global;return!0===i.logger.enable||"function"==typeof i.logger.enable&&i.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&&i.devtools.enable&&r.devTools.send(e.modelName+"_"+e.actionName,r.State),t})}catch(e){return Promise.reject(e)}},v=function(e,t){try{var r=e.modelName,n=e.next,o=e.Global,a=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||a)t.setState(o.State[r]);else{var n=t.selector(o.State[r]);A(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,v,m],p={communicator:v,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,a=t.next,c=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,c))])).then(function(e){return t.newState=e||null,Promise.resolve(a(r))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:i},_=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=r.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:c,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 r=c.State[e];r=n(r,t),c.State=n(c.State,function(t){t[e]=r})}else c.State=n(c.State,function(r){r[e]=o({},r[e],t)});return c.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 r={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(c.State).map(function(a){try{var i=function(){if(!e||!e.modelName||a===e.modelName||-1!==e.modelName.indexOf(a)){var i=function(e){t&&t.isServer?r[a]=e:c.State=n(c.State,function(t){t[a]=o({},t[a],e)})},s=c.AsyncState[a];return s?Promise.resolve(s(e)).then(i):i({})}}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?r:c.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},A=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 R=function(e,t){x(e,t,void 0)},x=function(e,t,r){Array.isArray(t)?t.forEach(function(t){c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),r?c.subscriptions[e+"_"+t].push(r):c.subscriptions[e+"_"+t]=[]}):(c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),r?c.subscriptions[e+"_"+t].push(r):c.subscriptions[e+"_"+t]=[])},C=function(e){return c.State[e]},M=function(e,t){void 0===t&&(t={type:"o"});var r={};return Object.keys(c.Actions[e]).forEach(function(n){return r[n]=function(r,a){try{var i=o({action:c.Actions[e][n],actionName:n,consumerActions:w,middlewareConfig:a,modelName:e,newState:null,params:r},t,{Global:c});return Promise.resolve(_(c.Middlewares[e]?c.Middlewares[e]:b,i))}catch(e){return Promise.reject(e)}}}),r},T=function(e,t){var n,o=r.useState({})[1],a=r.useRef(""),i=(n=[e],function(e){return n.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(c.mutableState),s=!!i,u=s?i.selector:t,l=s?i:C(e);if(r.useLayoutEffect(function(){c.uid+=1;var t=""+c.uid;return a.current=t,c.Setter.functionSetter[e]||(c.Setter.functionSetter[e]={}),c.Setter.functionSetter[e][t]={setState:o,selector:u},function(){delete c.Setter.functionSetter[e][t]}},[]),s)return u(l);var f=M(e,{__hash:a.current,type:"f"});return[u?u(l):l,f]},I=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).state=c.State,t}return a(t,e),t.prototype.render=function(){var e=this.props.children;return c.Setter.classSetter=this.setState.bind(this),r.createElement(h.Provider,{value:o({},this.state)},e)},t}(r.PureComponent);e.Consumer=y,e.Model=function(e,t,r){if(void 0!==e.state){c.storeId+=1;var a="__"+c.storeId;c.State=n(c.State,function(t){t[a]=e.state}),e.middlewares&&(c.Middlewares[a]=e.middlewares),c.Actions[a]=e.actions,c.AsyncState[a]=e.asyncState,t&&(c.Context[a]=t);var i=M(a);return{__id:a,actions:i,getState:function(){return C(a)},subscribe:function(e,t){return x(a,e,t)},unsubscribe:function(e){return R(a,e)},useStore:function(e){return T(a,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var s=function(e){return function(){return e}};return{__ERROR__:!0,actions:s({}),getActions:s({}),getInitialState:s({}),getState:s({}),subscribe:s(),unsubscribe:s(),useStore:s([{},{}])}}t&&!t.__FROM_SERVER__&&(c.State=n(c.State,function(e){Object.assign(e,t||{})})),r&&(c.Context.__global=r),Object.keys(e).forEach(function(r){var a=e[r];if(a.__ERROR__)return console.error(r+" model's schema is invalid"),c.State=n(c.State,function(e){e[r]={}}),void(c.Actions[r]={});void 0===a.useStore?(t&&t.__FROM_SERVER__?c.State=n(c.State,function(e){e[r]=o({},a.state,t[r])}):c.State[r]||(c.State=n(c.State,function(e){e[r]=a.state})),a.middlewares&&(c.Middlewares[r]=a.middlewares),c.Actions[r]=a.actions,c.AsyncState[r]=a.asyncState):(c.State[r]&&t||(c.State=n(c.State,function(e){e[r]=e[a.__id]})),t&&t.__FROM_SERVER__&&(c.State=n(c.State,function(e){e[r]=o({},e[a.__id],t[r])})),c.Actions[r]=c.Actions[a.__id],c.AsyncState[r]=c.AsyncState[a.__id],c.Middlewares[r]=c.Middlewares[a.__id],c.Context[r]=c.Context[a.__id])});var u=Object.keys(e).reduce(function(e,t){var r;return o({},e,((r={})[t]=M(t),r))},{});return c.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,c.withDevTools&&p.config.devtools.enable&&(c.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,c.devTools.connect()),{actions:u,getActions:M,getInitialState:O,getState:C,subscribe:x,unsubscribe:R,useStore:T}},e.Provider=I,e.actionMiddlewares=b,e.connect=function(e,t,n){return function(i){return function(s){function u(){return s.apply(this,arguments)||this}return a(u,s),u.prototype.render=function(){var a=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return r.createElement(y,null,function(s){var u=s[""+e],f=c.Actions[e];return r.createElement(i,Object.assign({},a.props,{state:o({},l,t?t(u):u),actions:o({},m,n?n(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(r.PureComponent)}},e.createStore=function(e,t){var r="string"==typeof e;c.storeId+=r?0:1;var n=r?e:c.storeId.toString();c.Actions[n]||(c.Actions[n]={}),c.mutableState[n]||(c.mutableState[n]={count:0});var o=function(){return c.mutableState[n].count=0,c.currentStoreId=n,t?t():e()};return c.mutableState[n].selector=o,{useStore:function(){return T(n,o)},getState:function(){return o()}}},e.getInitialState=O,e.getState=C,e.middlewares=p,e.useModel=function(e){var t=c.currentStoreId,r=c.mutableState[t].count;return c.mutableState[t].count+=1,c.mutableState[t].hasOwnProperty(r)||(c.mutableState[t][r]="function"==typeof e?e():e),[c.mutableState[t][r],function(e){try{return c.mutableState[t][r]="function"==typeof e?n(c.mutableState[t][r],e):c.mutableState[t][r]&&e&&"Object"===c.mutableState[t][r].constructor.name&&"Object"===e.constructor.name?o({},c.mutableState[t][r],e):e,Promise.resolve(_(b,{Global:c,action:function(){return"function"==typeof e?c.mutableState[t][r]: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.1.1", | ||
"version": "4.1.2-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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108809
1362
3