Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-model

Package Overview
Dependencies
Maintainers
2
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-model - npm Package Compare versions

Comparing version 4.1.4 to 4.2.0-rc.0

3

dist/helper.d.ts

@@ -20,5 +20,6 @@ /// <reference types="react" />

declare const getInitialState: <T extends {
modelName: string;
modelName: string | string[];
}>(context?: T | undefined, config?: {
isServer?: boolean | undefined;
prefix?: string | undefined;
} | undefined) => Promise<{

@@ -25,0 +26,0 @@ [modelName: string]: any;

@@ -1,1 +0,1 @@

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

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("immer"),require("react")):"function"==typeof define&&define.amd?define(["exports","immer","react"],t):t((e=e||self).reactModel={},e.immer,e.react)}(this,function(e,t,n){var r="default"in t?t.default:t;function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function i(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var 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},a={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(e,t){try{var n=e.next;return Promise.resolve(a.tryCatch.enable?n(t).catch(function(e){return console.log(e)}):n(t))}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=function(){return Promise.resolve(s(t))},r=e.action,i=e.modelName,c=e.consumerActions,a=e.params,s=e.next,u=e.Global,l=e.type,f=function(){if("u"!==l)return Promise.resolve(r(a,o({actions:c(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).then(function(t){e.newState=t||null});e.newState=r()}();return Promise.resolve(f&&f.then?f.then(n):n())}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=e.Global,c=e.type;return i.Setter.functionSetter[n]&&!e.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&t.selector&&!t.selectorRef&&(t.selectorRef=t.selector(i.State[n]))}),Promise.resolve(function(){if(r||"u"===c)return N(n,r||{}),Promise.resolve(o(t))}())}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"f"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i="u"===e.type?o.subscriptions[n]:o.subscriptions[n+"_"+e.actionName];return i&&i.forEach(function(t){t(e)}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},S=function(e,t){try{var n=e.Global;return!0===a.logger.enable||"function"==typeof a.logger.enable&&a.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(t){return console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd(),t})):Promise.resolve(e.next(t))}catch(e){return Promise.reject(e)}},d=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(t){return n.withDevTools&&a.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State),t})}catch(e){return Promise.reject(e)}},b=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(!t.selector||i)t.setState(o.State[n]);else{var r=t.selector(o.State[n]);A(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}}),Promise.resolve(r(t))}catch(e){return Promise.reject(e)}},p=[s,S,d,u,l,f,b,m],v={communicator:b,consoleDebugger:S,devToolsListener:d,getNewState:u,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var r=t.Global,o=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(r.Actions[o],{modelName:o}),state:r.State[o]}),O(e,j(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n))})}catch(e){return Promise.reject(e)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:a},_=function(e,t){try{return t.next=function(e){return e.length>0?e[0](t,e.slice(1)):t.newState},Promise.resolve(e[0](t,e.slice(1)))}catch(e){return Promise.reject(e)}},h=n.createContext({}),y=h.Consumer;if(!console.group){var g=[],P="-".repeat(80);console.group=function(e){g.push(e),console.log("%c \nBEGIN GROUP: %c",P,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",g.pop(),P)}}}var w=function(e,t){var n={};return Object.keys(e).forEach(function(r){n[r]=function(e,t){return function(n,r){try{return Promise.resolve(_(p,{Global:c,action:e,actionName:e.name,consumerActions:w,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"o"}))}catch(e){return Promise.reject(e)}}}(e[r],t)}),n},N=function(e,t){if("function"==typeof t){var n=c.State[e];n=r(n,t),c.State=r(c.State,function(t){t[e]=n})}else c.State=r(c.State,function(n){n[e]=o({},n[e],t)});return c.State},O=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},E=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(c.State).map(function(i){try{var a=function(){if(!e||!e.modelName||i===e.modelName||-1!==e.modelName.indexOf(i)){var a=function(e){t&&t.isServer?n[i]=e:c.State=r(c.State,function(t){t[i]=o({},t[i],e)})},s=c.AsyncState[i];return s?Promise.resolve(s(e)).then(a):a({})}}();return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return t&&t.isServer?n:c.State})}catch(e){return Promise.reject(e)}},j=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},A=function(e,t){if(e===t)return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!Object.prototype.hasOwnProperty.call(t,n[o])||e[n[o]]!==t[n[o]])return!1;return!0};t.enableES5();var R=function(e,t){x(e,t,void 0)},x=function(e,t,n){Array.isArray(t)?t.forEach(function(t){c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),n?c.subscriptions[e+"_"+t].push(n):c.subscriptions[e+"_"+t]=[]}):(c.subscriptions[e+"_"+t]||(c.subscriptions[e+"_"+t]=[]),n?c.subscriptions[e+"_"+t].push(n):c.subscriptions[e+"_"+t]=[])},C=function(e){return c.State[e]},M=function(e,t){void 0===t&&(t={type:"o"});var n={};return Object.keys(c.Actions[e]).forEach(function(r){return n[r]=function(n,i){try{var a=o({action:c.Actions[e][r],actionName:r,consumerActions:w,middlewareConfig:i,modelName:e,newState:null,params:n},t,{Global:c});return Promise.resolve(_(c.Middlewares[e]?c.Middlewares[e]:p,a))}catch(e){return Promise.reject(e)}}}),n},T=function(e,t){var r,o=n.useState({})[1],i=n.useRef(""),a=(r=[e],function(e){return r.reduce(function(e,t){return e&&e[t]?e[t]:null},e)})(c.mutableState),s=!!a,u=s?a.selector:t,l=s?a:C(e);if(n.useLayoutEffect(function(){c.uid+=1;var t=""+c.uid;return i.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:i.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 i(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);e.Consumer=y,e.Model=function(e,t,n){if(void 0!==e.state){c.storeId+=1;var i="__"+c.storeId;c.State=r(c.State,function(t){t[i]=e.state}),e.middlewares&&(c.Middlewares[i]=e.middlewares),c.Actions[i]=e.actions,c.AsyncState[i]=e.asyncState,t&&(c.Context[i]=t);var a=M(i);return{__id:i,actions:a,getState:function(){return C(i)},subscribe:function(e,t){return x(i,e,t)},unsubscribe:function(e){return R(i,e)},useStore:function(e){return T(i,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 i=e[n];if(i.__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===i.useStore?(t&&t.__FROM_SERVER__?c.State=r(c.State,function(e){e[n]=o({},i.state,t[n])}):c.State[n]||(c.State=r(c.State,function(e){e[n]=i.state})),i.middlewares&&(c.Middlewares[n]=i.middlewares),c.Actions[n]=i.actions,c.AsyncState[n]=i.asyncState):(c.State[n]&&t||(c.State=r(c.State,function(e){e[n]=e[i.__id]})),t&&t.__FROM_SERVER__&&(c.State=r(c.State,function(e){e[n]=o({},e[i.__id],t[n])})),c.Actions[n]=c.Actions[i.__id],c.AsyncState[n]=c.AsyncState[i.__id],c.Middlewares[n]=c.Middlewares[i.__id],c.Context[n]=c.Context[i.__id])});var u=Object.keys(e).reduce(function(e,t){var n;return o({},e,((n={})[t]=M(t),n))},{});return c.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,c.withDevTools&&v.config.devtools.enable&&(c.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,c.devTools.connect()),{actions:u,getActions:M,getInitialState:E,getState:C,subscribe:x,unsubscribe:R,useStore:T}},e.Provider=I,e.actionMiddlewares=p,e.connect=function(e,t,r){return function(a){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return n.createElement(y,null,function(s){var u=s[""+e],f=c.Actions[e];return n.createElement(a,Object.assign({},i.props,{state:o({},l,t?t(u):u),actions:o({},m,r?r(w(f,{modelName:e})):w(f,{modelName:e}))}))})},u}(n.PureComponent)}},e.createStore=function(e,t){var n="string"==typeof e;c.storeId+=n?0:1;var r=n?e:c.storeId.toString();c.Actions[r]||(c.Actions[r]={}),c.mutableState[r]||(c.mutableState[r]={count:0});var o=function(){return c.mutableState[r].count=0,c.currentStoreId=r,t?t():e()};return c.mutableState[r].selector=o,{useStore:function(){return T(r,o)},getState:function(){return o()},subscribe:function(e){c.subscriptions[r]||(c.subscriptions[r]=[]),c.subscriptions[r].push(e)},unsubscribe:function(e){if(c.subscriptions[r]&&e){var t=c.subscriptions[r].indexOf(e);t>=0&&c.subscriptions[r].splice(t,1)}}}},e.getInitialState=E,e.getState=C,e.middlewares=v,e.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):c.mutableState[t][n]&&e&&"Object"===c.mutableState[t][n].constructor.name&&"Object"===e.constructor.name?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)}}]}});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("immer"),require("react")):"function"==typeof define&&define.amd?define(["exports","immer","react"],e):e((t=t||self).reactModel={},t.immer,t.react)}(this,function(t,e,r){var n="default"in e?e.default:e;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 i(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:{},gid:0,uid:0,storeId:0,currentStoreId:"0",withDevTools:!1},a={logger:{enable:!1},devtools:{enable:!1},tryCatch:{enable:!0}},s=function(t,e){try{var r=t.next;return Promise.resolve(a.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,i=t.modelName,c=t.consumerActions,a=t.params,s=t.next,u=t.Global,l=t.type,f=function(){if("u"!==l)return Promise.resolve(n(a,o({actions:c(u.Actions[i],{modelName:i}),state:u.State[i]},u.Context.__global||{},u.Context[i]||{}))).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,i=t.Global,c=t.type;return i.Setter.functionSetter[r]&&!t.disableSelectorUpdate&&Object.keys(i.Setter.functionSetter[r]).map(function(t){var e=i.Setter.functionSetter[r][t];e&&e.selector&&!e.selectorRef&&(e.selectorRef=e.selector(i.State[r]))}),Promise.resolve(function(){if(n||"u"===c)return 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,i=t.__hash,c=o.Setter.functionSetter[r];return"f"===t.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[r]),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},m=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i="u"===t.type?o.subscriptions[r]:o.subscriptions[r+"_"+t.actionName];return i&&i.forEach(function(e){e(t)}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},S=function(t,e){try{var r=t.Global;return!0===a.logger.enable||"function"==typeof a.logger.enable&&a.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&&a.devtools.enable&&r.devTools.send(t.modelName+"_"+t.actionName,r.State),e})}catch(t){return Promise.reject(t)}},b=function(t,e){try{var r=t.modelName,n=t.next,o=t.Global,i=t.disableSelectorUpdate;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[r]&&Object.keys(o.Setter.functionSetter[r]).map(function(t){var e=o.Setter.functionSetter[r][t];if(e)if(!e.selector||i)e.setState(o.State[r]);else{var n=e.selector(o.State[r]);A(n,e.selectorRef)||(e.selectorRef=n,e.setState(o.State[r]))}}),Promise.resolve(n(e))}catch(t){return Promise.reject(t)}},p=[s,S,d,u,l,f,b,m],v={communicator:b,consoleDebugger:S,devToolsListener:d,getNewState:u,getNewStateWithCache:function(t){return void 0===t&&(t=5e3),function(e,r){try{var n=e.Global,o=e.modelName,i=e.next,c=e.actionName;return Promise.resolve(Promise.race([(0,e.action)(e.params,{actions:(0,e.consumerActions)(n.Actions[o],{modelName:o}),state:n.State[o]}),E(t,j(o,c))])).then(function(t){return e.newState=t||null,Promise.resolve(i(r))})}catch(t){return Promise.reject(t)}}},setNewState:l,stateUpdater:f,subscription:m,tryCatch:s,config:a},_=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=r.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 r=c.State[t];r=n(r,e),c.State=n(c.State,function(e){e[t]=r})}else c.State=n(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 r={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(c.State).map(function(i){try{var a=e&&e.prefix||"",s=function(){if(!t||!t.modelName||i===a+t.modelName||-1!==t.modelName.indexOf(a+i)){var s=function(t){e&&e.isServer?r[i]=t:c.State=n(c.State,function(e){e[i]=o({},e[i],t)})},u=c.AsyncState[i];return u?Promise.resolve(u(t)).then(s):s({})}}();return Promise.resolve(s&&s.then?s.then(function(){}):void 0)}catch(t){return Promise.reject(t)}}))).then(function(){return e&&e.isServer?r: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 R=function(t,e){x(t,e,void 0)},x=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]},M=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,i){try{var a=o({action:c.Actions[t][n],actionName:n,consumerActions:w,middlewareConfig:i,modelName:t,newState:null,params:r},e,{Global:c});return Promise.resolve(_(c.Middlewares[t]?c.Middlewares[t]:p,a))}catch(t){return Promise.reject(t)}}}),r},T=function(t,e){var n,o=r.useState({})[1],i=r.useRef(""),a=(n=[t],function(t){return n.reduce(function(t,e){return t&&t[e]?t[e]:null},t)})(c.mutableState),s=!!a,u=s?a.selector:e,l=s?a:C(t);if(r.useLayoutEffect(function(){c.uid+=1;var e=""+c.uid;return i.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=M(t,{__hash:i.current,type:"f"});return[u?u(l):l,f]},I=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).state=c.State,e}return i(e,t),e.prototype.render=function(){var t=this.props.children;return c.Setter.classSetter=this.setState.bind(this),r.createElement(h.Provider,{value:o({},this.state)},t)},e}(r.PureComponent);t.Consumer=y,t.Model=function(t,e,r){if(void 0!==t.state){c.storeId+=1;var i="__"+c.storeId;c.State=n(c.State,function(e){e[i]=t.state}),t.middlewares&&(c.Middlewares[i]=t.middlewares),c.Actions[i]=t.actions,c.AsyncState[i]=t.asyncState,e&&(c.Context[i]=e);var a=M(i);return{__id:i,actions:a,getState:function(){return C(i)},subscribe:function(t,e){return x(i,t,e)},unsubscribe:function(t){return R(i,t)},useStore:function(t){return T(i,t)}}}e?c.gid=1:c.gid+=1;var s="";if(c.gid>1&&(s=c.gid+"_"),t.actions){console.error("invalid model(s) schema: ",t);var u=function(t){return function(){return t}};return{__ERROR__:!0,actions:u({}),getActions:u({}),getInitialState:u({}),getState:u({}),subscribe:u(),unsubscribe:u(),useStore:u([{},{}])}}e&&!e.__FROM_SERVER__&&(c.State=n(c.State,function(t){Object.assign(t,e||{})})),r&&(c.Context.__global=r);var l={};return Object.keys(t).forEach(function(r){var i=s+r,a=t[r];if(a.__ERROR__)return console.error(i+" model's schema is invalid"),c.State=n(c.State,function(t){t[i]={}}),void(c.Actions[i]={});void 0===a.useStore?(e&&e.__FROM_SERVER__?c.State=n(c.State,function(t){t[i]=o({},a.state,e[i])}):c.State[i]||(c.State=n(c.State,function(t){t[i]=a.state})),a.middlewares&&(c.Middlewares[i]=a.middlewares),c.Actions[i]=a.actions,c.AsyncState[i]=a.asyncState):(c.State[i]&&e||(c.State=n(c.State,function(t){t[i]=t[a.__id]})),e&&e.__FROM_SERVER__&&(c.State=n(c.State,function(t){t[i]=o({},t[a.__id],e[i])})),c.Actions[i]=c.Actions[a.__id],c.AsyncState[i]=c.AsyncState[a.__id],c.Middlewares[i]=c.Middlewares[a.__id],c.Context[i]=c.Context[a.__id]),l[r]=M(i)}),c.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,c.withDevTools&&v.config.devtools.enable&&(c.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,c.devTools.connect()),{actions:l,getActions:function(t){return M(s+t)},getInitialState:function(t,e){try{return Promise.resolve(O(t,o({},e,{prefix:s})))}catch(t){return Promise.reject(t)}},getState:function(t){return C(s+t)},subscribe:function(t,e,r){return x(s+t,e,r)},unsubscribe:function(t,e){return R(s+t,e)},useStore:function(t,e){return T(s+t,e)}}},t.Provider=I,t.actionMiddlewares=p,t.connect=function(t,e,n){return function(a){return function(s){function u(){return s.apply(this,arguments)||this}return i(u,s),u.prototype.render=function(){var i=this,s=this.props,u=s.state,l=void 0===u?{}:u,f=s.actions,m=void 0===f?{}:f;return r.createElement(y,null,function(s){var u=s[""+t],f=c.Actions[t];return r.createElement(a,Object.assign({},i.props,{state:o({},l,e?e(u):u),actions:o({},m,n?n(w(f,{modelName:t})):w(f,{modelName:t}))}))})},u}(r.PureComponent)}},t.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 T(n,o)},getState:function(){return o()},subscribe:function(t){c.subscriptions[n]||(c.subscriptions[n]=[]),c.subscriptions[n].push(t)},unsubscribe:function(t){if(c.subscriptions[n]&&t){var e=c.subscriptions[n].indexOf(t);e>=0&&c.subscriptions[n].splice(e,1)}}}},t.getInitialState=O,t.getState=C,t.middlewares=v,t.useModel=function(t){var e=c.currentStoreId,r=c.mutableState[e].count;return c.mutableState[e].count+=1,c.mutableState[e].hasOwnProperty(r)||(c.mutableState[e][r]="function"==typeof t?t():t),[c.mutableState[e][r],function(t){try{return c.mutableState[e][r]="function"==typeof t?n(c.mutableState[e][r],t):c.mutableState[e][r]&&t&&"Object"===c.mutableState[e][r].constructor.name&&"Object"===t.constructor.name?o({},c.mutableState[e][r],t):t,Promise.resolve(_(p,{Global:c,action:function(){return"function"==typeof t?c.mutableState[e][r]:t},actionName:"setter",consumerActions:w,disableSelectorUpdate:!0,middlewareConfig:{},modelName:e,newState:{},params:void 0,type:"u"}))}catch(t){return Promise.reject(t)}}]}});
{
"name": "react-model",
"version": "4.1.4",
"version": "4.2.0-rc.0",
"description": "The State management library for React",

@@ -8,3 +8,2 @@ "main": "./dist/react-model.js",

"umd:main": "./dist/react-model.umd.js",
"types": "./src/index",
"scripts": {

@@ -11,0 +10,0 @@ "build:prod": "microbundle --define process.env.NODE_ENV=production --sourcemap false --jsx React.createElement --output dist --tsconfig ./tsconfig.json",

@@ -28,2 +28,3 @@ const State = {}

let currentStoreId = '0' // Used for useModel
let gid = 0 // The unique id of models' group

@@ -40,2 +41,3 @@ export default {

mutableState,
gid,
uid,

@@ -42,0 +44,0 @@ storeId,

@@ -87,5 +87,5 @@ import produce from 'immer'

const getInitialState = async <T extends { modelName: string }>(
const getInitialState = async <T extends { modelName: string | string[] }>(
context?: T,
config?: { isServer?: boolean }
config?: { isServer?: boolean; prefix?: string }
) => {

@@ -95,7 +95,8 @@ const ServerState: { [name: string]: any } = { __FROM_SERVER__: true }

Object.keys(Global.State).map(async (modelName) => {
let prefix = (config && config.prefix) || ''
if (
!context ||
!context.modelName ||
modelName === context.modelName ||
context.modelName.indexOf(modelName) !== -1
modelName === prefix + context.modelName ||
context.modelName.indexOf(prefix + modelName) !== -1
) {

@@ -102,0 +103,0 @@ const asyncGetter = Global.AsyncState[modelName]

@@ -45,2 +45,3 @@ type Setter = {

withDevTools: boolean
gid: number
uid: number

@@ -47,0 +48,0 @@ storeId: number

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc