react-model
Advanced tools
Comparing version 3.1.2 to 4.0.0-dev.20200615.0
@@ -26,2 +26,3 @@ /// <reference types="react" /> | ||
declare const getCache: (modelName: string, actionName: string) => any; | ||
export { Consumer, consumerActions, GlobalContext, setPartialState, timeout, getCache, getInitialState }; | ||
declare const shallowEqual: (objA: any, objB: any) => boolean; | ||
export { Consumer, consumerActions, GlobalContext, setPartialState, shallowEqual, timeout, getCache, getInitialState }; |
@@ -1,1 +0,1 @@ | ||
var e,t=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e,n=require("react"),o={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},r={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},i=function(e,t){try{var n=e.next,o=r.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},c=function(e,t){try{var n=e.modelName,o=e.next,r=e.Global;return Promise.resolve((0,e.action)(e.params,Object.assign({},{actions:(0,e.consumerActions)(r.Actions[n],{modelName:n}),state:r.State[n]},r.Context.__global||{},r.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},s=function(e,t){try{var n=e.modelName,o=e.newState,r=e.next,i=function(){if(o)return P(n,o),Promise.resolve(r(t)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,o=e.next,r=e.Global,i=e.__hash,c=r.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(r.State[n]),Promise.resolve(o(t)).then(function(){})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.next,o=e.Global.subscriptions[e.modelName+"_"+e.actionName];return o&&o.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.Global,o=!0===r.logger.enable||"function"==typeof r.logger.enable&&r.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&r.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,o=e.next,r=e.actionName,i=e.Global;return i.Setter.classSetter&&i.Setter.classSetter(i.State),i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&(t.depActions&&-1===t.depActions.indexOf(r)||t.setState(i.State[n]))}),Promise.resolve(o(t)).then(function(){})}catch(e){return Promise.reject(e)}},d=[i,l,f,c,s,a,m,u],S={communicator:m,consoleDebugger:l,devToolsListener:f,getNewState:c,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var o=t.Global,r=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(o.Actions[r],{modelName:r}),state:o.State[r]}),y(e,E(r,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:s,stateUpdater:a,subscription:u,tryCatch:i,config:r},v=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},_=n.createContext({}),h=_.Consumer;if(!console.group){var p=[],b="-".repeat(80);console.group=function(e){p.push(e),console.log("%c \nBEGIN GROUP: %c",b,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",p.pop(),b)}}}var g=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,r){try{return Promise.resolve(v(d,{Global:o,action:e,actionName:e.name,consumerActions:g,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},P=function(e,n){if("function"==typeof n){var r=o.State[e];r=t(r,n),o.State=t(o.State,function(t){t[e]=r})}else o.State=t(o.State,function(t){t[e]=Object.assign({},t[e],n)});return o.State},y=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},N=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(o.State).map(function(r){try{var i=function(){if(!e||!e.modelName||r===e.modelName||-1!==e.modelName.indexOf(r)){function i(e){t&&t.isServer?n[r]=e:o.State[r]=Object.assign({},o.State[r],e)}var c=o.AsyncState[r];return c?Promise.resolve(c(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:o.State})}catch(e){return Promise.reject(e)}},E=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},w=function(e,t){O(e,t,void 0)},O=function(e,t,n){Array.isArray(t)?t.forEach(function(t){o.subscriptions[e+"_"+t]||(o.subscriptions[e+"_"+t]=[]),n?o.subscriptions[e+"_"+t].push(n):o.subscriptions[e+"_"+t]=[]}):(o.subscriptions[e+"_"+t]||(o.subscriptions[e+"_"+t]=[]),n?o.subscriptions[e+"_"+t].push(n):o.subscriptions[e+"_"+t]=[])},j=function(e){return o.State[e]},A=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(o.Actions[e]).forEach(function(r){var i=r[0],c=r[1];return n[i]=function(n,r){try{var s=Object.assign({},{action:c,actionName:i,consumerActions:g,middlewareConfig:r,modelName:e,newState:null,params:n},t,{Global:o}),a=o.Middlewares[e]?Promise.resolve(v(o.Middlewares[e],s)).then(function(){}):Promise.resolve(v(d,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},x=function(e,t){var r=n.useState({})[1],i=n.useRef("");n.useEffect(function(){o.uid+=1;var n=""+o.uid;return i.current=n,o.Setter.functionSetter[e]||(o.Setter.functionSetter[e]={}),o.Setter.functionSetter[e][n]={setState:r,depActions:t},function(){delete o.Setter.functionSetter[e][n]}},[]);var c=A(e,{__hash:i.current,type:"function"});return[j(e),c]},R=function(e){function t(){e.apply(this,arguments),this.state=o.State}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t.prototype.render=function(){var e=this.props.children;return o.Setter.classSetter=this.setState.bind(this),n.createElement(_.Provider,{value:Object.assign({},this.state)},e)},t}(n.PureComponent);exports.actionMiddlewares=d,exports.Model=function(e,t,n){if(void 0!==e.state){o.uid+=1;var r="__"+o.uid;o.State[r]=e.state,e.middlewares&&(o.Middlewares[r]=e.middlewares),o.Actions[r]=e.actions,o.AsyncState[r]=e.asyncState,t&&(o.Context[r]=t);var i=A(r);return{__id:r,actions:i,getState:j(r),subscribe:function(e,t){return O(r,e,t)},unsubscribe:function(e){return w(r,e)},useStore:function(e){return x(r,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(e){return function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return e}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}t&&!t.__FROM_SERVER__&&(o.State=t||{}),n&&(o.Context.__global=n),Object.entries(e).forEach(function(e){var n=e[0],r=e[1];if(r.__ERROR__)return console.error(n+" model's schema is invalid"),o.State[n]={},void(o.Actions[n]={});void 0!==r.useStore?(o.State[n]&&t||(o.State[n]=o.State[r.__id]),t&&t.__FROM_SERVER__&&(o.State[n]=Object.assign({},o.State[r.__id],t[n])),o.Actions[n]=o.Actions[r.__id],o.AsyncState[n]=o.AsyncState[r.__id],o.Middlewares[n]=o.Middlewares[r.__id],o.Context[n]=o.Context[r.__id]):(t&&t.__FROM_SERVER__?o.State[n]=Object.assign({},r.state,t[n]):o.State[n]||(o.State[n]=r.state),r.middlewares&&(o.Middlewares[n]=r.middlewares),o.Actions[n]=r.actions,o.AsyncState[n]=r.asyncState)});var s=Object.keys(e).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t]=A(t),n))},{});return o.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,o.withDevTools&&(o.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,o.devTools.connect()),{actions:s,getActions:A,getInitialState:N,getState:j,subscribe:O,unsubscribe:w,useStore:x}},exports.middlewares=S,exports.Provider=R,exports.Consumer=h,exports.connect=function(e,t,r){return function(i){return function(c){function s(){c.apply(this,arguments)}return c&&(s.__proto__=c),(s.prototype=Object.create(c&&c.prototype)).constructor=s,s.prototype.render=function(){var c=this,s=this.props,a=s.state;void 0===a&&(a={});var u=s.actions;return void 0===u&&(u={}),n.createElement(h,null,function(s){var l=s[""+e],f=o.Actions[e];return n.createElement(i,Object.assign({},c.props,{state:Object.assign({},a,t?t(l):l),actions:Object.assign({},u,r?r(g(f,{modelName:e})):g(f,{modelName:e}))}))})},s}(n.PureComponent)}},exports.getState=j,exports.getInitialState=N; | ||
var e,t=(e=require("immer"))&&"object"==typeof e&&"default"in e?e.default:e,n=require("react"),r={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},o={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},i=function(e,t){try{var n=e.next,r=o.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},c=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return Promise.resolve((0,e.action)(e.params,Object.assign({},{actions:(0,e.consumerActions)(o.Actions[n],{modelName:n}),state:o.State[n]},o.Context.__global||{},o.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(r(t)).then(function(){})})}catch(e){return Promise.reject(e)}},s=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=function(){if(r)return y(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.Global,r=!0===o.logger.enable||"function"==typeof o.logger.enable&&o.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&o.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);E(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},d=[i,l,f,c,s,a,m,u],S={communicator:m,consoleDebugger:l,devToolsListener:f,getNewState:c,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]}),P(e,O(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:s,stateUpdater:a,subscription:u,tryCatch:i,config:o},v=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},h=n.createContext({}),_=h.Consumer;if(!console.group){var p=[],b="-".repeat(80);console.group=function(e){p.push(e),console.log("%c \nBEGIN GROUP: %c",b,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",p.pop(),b)}}}var g=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,o){try{return Promise.resolve(v(d,{Global:r,action:e,actionName:e.name,consumerActions:g,middlewareConfig:o,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},y=function(e,n){if("function"==typeof n){var o=r.State[e];o=t(o,n),r.State=t(r.State,function(t){t[e]=o})}else r.State=t(r.State,function(t){t[e]=Object.assign({},t[e],n)});return r.State},P=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},N=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(r.State).map(function(o){try{var i=function(){if(!e||!e.modelName||o===e.modelName||-1!==e.modelName.indexOf(o)){function i(e){t&&t.isServer?n[o]=e:r.State[o]=Object.assign({},r.State[o],e)}var c=r.AsyncState[o];return c?Promise.resolve(c(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:r.State})}catch(e){return Promise.reject(e)}},O=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},E=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},w=function(e,t){j(e,t,void 0)},j=function(e,t,n){Array.isArray(t)?t.forEach(function(t){r.subscriptions[e+"_"+t]||(r.subscriptions[e+"_"+t]=[]),n?r.subscriptions[e+"_"+t].push(n):r.subscriptions[e+"_"+t]=[]}):(r.subscriptions[e+"_"+t]||(r.subscriptions[e+"_"+t]=[]),n?r.subscriptions[e+"_"+t].push(n):r.subscriptions[e+"_"+t]=[])},x=function(e){return r.State[e]},A=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(r.Actions[e]).forEach(function(o){var i=o[0],c=o[1];return n[i]=function(n,o){try{var s=Object.assign({},{action:c,actionName:i,consumerActions:g,middlewareConfig:o,modelName:e,newState:null,params:n},t,{Global:r}),a=r.Middlewares[e]?Promise.resolve(v(r.Middlewares[e],s)).then(function(){}):Promise.resolve(v(d,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},R=function(e,t){var o=n.useState({})[1],i=n.useRef("");n.useEffect(function(){r.uid+=1;var n=""+r.uid;return i.current=n,r.Setter.functionSetter[e]||(r.Setter.functionSetter[e]={}),r.Setter.functionSetter[e][n]={setState:o,selector:t},function(){delete r.Setter.functionSetter[e][n]}},[]);var c=A(e,{__hash:i.current,type:"function"});return[t?t(x(e)):x(e),c]},C=function(e){function t(){e.apply(this,arguments),this.state=r.State}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t.prototype.render=function(){var e=this.props.children;return r.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:Object.assign({},this.state)},e)},t}(n.PureComponent);exports.actionMiddlewares=d,exports.Model=function(e,t,n){if(void 0!==e.state){r.uid+=1;var o="__"+r.uid;r.State[o]=e.state,e.middlewares&&(r.Middlewares[o]=e.middlewares),r.Actions[o]=e.actions,r.AsyncState[o]=e.asyncState,t&&(r.Context[o]=t);var i=A(o);return{__id:o,actions:i,getState:x(o),subscribe:function(e,t){return j(o,e,t)},unsubscribe:function(e){return w(o,e)},useStore:function(e){return R(o,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(e){return function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return e}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}t&&!t.__FROM_SERVER__&&(r.State=t||{}),n&&(r.Context.__global=n),Object.entries(e).forEach(function(e){var n=e[0],o=e[1];if(o.__ERROR__)return console.error(n+" model's schema is invalid"),r.State[n]={},void(r.Actions[n]={});void 0!==o.useStore?(r.State[n]&&t||(r.State[n]=r.State[o.__id]),t&&t.__FROM_SERVER__&&(r.State[n]=Object.assign({},r.State[o.__id],t[n])),r.Actions[n]=r.Actions[o.__id],r.AsyncState[n]=r.AsyncState[o.__id],r.Middlewares[n]=r.Middlewares[o.__id],r.Context[n]=r.Context[o.__id]):(t&&t.__FROM_SERVER__?r.State[n]=Object.assign({},o.state,t[n]):r.State[n]||(r.State[n]=o.state),o.middlewares&&(r.Middlewares[n]=o.middlewares),r.Actions[n]=o.actions,r.AsyncState[n]=o.asyncState)});var s=Object.keys(e).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t]=A(t),n))},{});return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),{actions:s,getActions:A,getInitialState:N,getState:x,subscribe:j,unsubscribe:w,useStore:R}},exports.middlewares=S,exports.Provider=C,exports.Consumer=_,exports.connect=function(e,t,o){return function(i){return function(c){function s(){c.apply(this,arguments)}return c&&(s.__proto__=c),(s.prototype=Object.create(c&&c.prototype)).constructor=s,s.prototype.render=function(){var c=this,s=this.props,a=s.state;void 0===a&&(a={});var u=s.actions;return void 0===u&&(u={}),n.createElement(_,null,function(s){var l=s[""+e],f=r.Actions[e];return n.createElement(i,Object.assign({},c.props,{state:Object.assign({},a,t?t(l):l),actions:Object.assign({},u,o?o(g(f,{modelName:e})):g(f,{modelName:e}))}))})},s}(n.PureComponent)}},exports.getState=x,exports.getInitialState=N; |
@@ -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.reactModel={},e.immer,e.react)}(this,function(e,t,n){t=t&&t.hasOwnProperty("default")?t.default:t;var o={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},r={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},i=function(e,t){try{var n=e.next,o=r.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},c=function(e,t){try{var n=e.modelName,o=e.next,r=e.Global;return Promise.resolve((0,e.action)(e.params,Object.assign({},{actions:(0,e.consumerActions)(r.Actions[n],{modelName:n}),state:r.State[n]},r.Context.__global||{},r.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(o(t)).then(function(){})})}catch(e){return Promise.reject(e)}},s=function(e,t){try{var n=e.modelName,o=e.newState,r=e.next,i=function(){if(o)return y(n,o),Promise.resolve(r(t)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,o=e.next,r=e.Global,i=e.__hash,c=r.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(r.State[n]),Promise.resolve(o(t)).then(function(){})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.next,o=e.Global.subscriptions[e.modelName+"_"+e.actionName];return o&&o.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.Global,o=!0===r.logger.enable||"function"==typeof r.logger.enable&&r.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&r.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,o=e.next,r=e.actionName,i=e.Global;return i.Setter.classSetter&&i.Setter.classSetter(i.State),i.Setter.functionSetter[n]&&Object.keys(i.Setter.functionSetter[n]).map(function(e){var t=i.Setter.functionSetter[n][e];t&&(t.depActions&&-1===t.depActions.indexOf(r)||t.setState(i.State[n]))}),Promise.resolve(o(t)).then(function(){})}catch(e){return Promise.reject(e)}},d=[i,l,f,c,s,a,m,u],S={communicator:m,consoleDebugger:l,devToolsListener:f,getNewState:c,getNewStateWithCache:function(e){return void 0===e&&(e=5e3),function(t,n){try{var o=t.Global,r=t.modelName,i=t.next,c=t.actionName;return Promise.resolve(Promise.race([(0,t.action)(t.params,{actions:(0,t.consumerActions)(o.Actions[r],{modelName:r}),state:o.State[r]}),P(e,E(r,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:s,stateUpdater:a,subscription:u,tryCatch:i,config:r},v=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},_=n.createContext({}),h=_.Consumer;if(!console.group){var p=[],b="-".repeat(80);console.group=function(e){p.push(e),console.log("%c \nBEGIN GROUP: %c",b,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",p.pop(),b)}}}var g=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,r){try{return Promise.resolve(v(d,{Global:o,action:e,actionName:e.name,consumerActions:g,middlewareConfig:r,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},y=function(e,n){if("function"==typeof n){var r=o.State[e];r=t(r,n),o.State=t(o.State,function(t){t[e]=r})}else o.State=t(o.State,function(t){t[e]=Object.assign({},t[e],n)});return o.State},P=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},N=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(o.State).map(function(r){try{var i=function(){if(!e||!e.modelName||r===e.modelName||-1!==e.modelName.indexOf(r)){function i(e){t&&t.isServer?n[r]=e:o.State[r]=Object.assign({},o.State[r],e)}var c=o.AsyncState[r];return c?Promise.resolve(c(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:o.State})}catch(e){return Promise.reject(e)}},E=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},w=function(e,t){O(e,t,void 0)},O=function(e,t,n){Array.isArray(t)?t.forEach(function(t){o.subscriptions[e+"_"+t]||(o.subscriptions[e+"_"+t]=[]),n?o.subscriptions[e+"_"+t].push(n):o.subscriptions[e+"_"+t]=[]}):(o.subscriptions[e+"_"+t]||(o.subscriptions[e+"_"+t]=[]),n?o.subscriptions[e+"_"+t].push(n):o.subscriptions[e+"_"+t]=[])},j=function(e){return o.State[e]},A=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(o.Actions[e]).forEach(function(r){var i=r[0],c=r[1];return n[i]=function(n,r){try{var s=Object.assign({},{action:c,actionName:i,consumerActions:g,middlewareConfig:r,modelName:e,newState:null,params:n},t,{Global:o}),a=o.Middlewares[e]?Promise.resolve(v(o.Middlewares[e],s)).then(function(){}):Promise.resolve(v(d,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},x=function(e,t){var r=n.useState({})[1],i=n.useRef("");n.useEffect(function(){o.uid+=1;var n=""+o.uid;return i.current=n,o.Setter.functionSetter[e]||(o.Setter.functionSetter[e]={}),o.Setter.functionSetter[e][n]={setState:r,depActions:t},function(){delete o.Setter.functionSetter[e][n]}},[]);var c=A(e,{__hash:i.current,type:"function"});return[j(e),c]},R=function(e){function t(){e.apply(this,arguments),this.state=o.State}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t.prototype.render=function(){var e=this.props.children;return o.Setter.classSetter=this.setState.bind(this),n.createElement(_.Provider,{value:Object.assign({},this.state)},e)},t}(n.PureComponent);e.actionMiddlewares=d,e.Model=function(e,t,n){if(void 0!==e.state){o.uid+=1;var r="__"+o.uid;o.State[r]=e.state,e.middlewares&&(o.Middlewares[r]=e.middlewares),o.Actions[r]=e.actions,o.AsyncState[r]=e.asyncState,t&&(o.Context[r]=t);var i=A(r);return{__id:r,actions:i,getState:j(r),subscribe:function(e,t){return O(r,e,t)},unsubscribe:function(e){return w(r,e)},useStore:function(e){return x(r,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(e){return function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return e}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}t&&!t.__FROM_SERVER__&&(o.State=t||{}),n&&(o.Context.__global=n),Object.entries(e).forEach(function(e){var n=e[0],r=e[1];if(r.__ERROR__)return console.error(n+" model's schema is invalid"),o.State[n]={},void(o.Actions[n]={});void 0!==r.useStore?(o.State[n]&&t||(o.State[n]=o.State[r.__id]),t&&t.__FROM_SERVER__&&(o.State[n]=Object.assign({},o.State[r.__id],t[n])),o.Actions[n]=o.Actions[r.__id],o.AsyncState[n]=o.AsyncState[r.__id],o.Middlewares[n]=o.Middlewares[r.__id],o.Context[n]=o.Context[r.__id]):(t&&t.__FROM_SERVER__?o.State[n]=Object.assign({},r.state,t[n]):o.State[n]||(o.State[n]=r.state),r.middlewares&&(o.Middlewares[n]=r.middlewares),o.Actions[n]=r.actions,o.AsyncState[n]=r.asyncState)});var s=Object.keys(e).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t]=A(t),n))},{});return o.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,o.withDevTools&&(o.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,o.devTools.connect()),{actions:s,getActions:A,getInitialState:N,getState:j,subscribe:O,unsubscribe:w,useStore:x}},e.middlewares=S,e.Provider=R,e.Consumer=h,e.connect=function(e,t,r){return function(i){return function(c){function s(){c.apply(this,arguments)}return c&&(s.__proto__=c),(s.prototype=Object.create(c&&c.prototype)).constructor=s,s.prototype.render=function(){var c=this,s=this.props,a=s.state;void 0===a&&(a={});var u=s.actions;return void 0===u&&(u={}),n.createElement(h,null,function(s){var l=s[""+e],f=o.Actions[e];return n.createElement(i,Object.assign({},c.props,{state:Object.assign({},a,t?t(l):l),actions:Object.assign({},u,r?r(g(f,{modelName:e})):g(f,{modelName:e}))}))})},s}(n.PureComponent)}},e.getState=j,e.getInitialState=N}); | ||
!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.reactModel={},e.immer,e.react)}(this,function(e,t,n){t=t&&t.hasOwnProperty("default")?t.default:t;var r={Actions:{},AsyncState:{},Context:{__global:{}},Middlewares:{},Setter:{classSetter:void 0,functionSetter:{}},State:{},devTools:void 0,subscriptions:{},uid:0,withDevTools:!1},o={logger:{enable:"production"!==process.env.NODE_ENV},devtools:{enable:"production"!==process.env.NODE_ENV},tryCatch:{enable:"production"===process.env.NODE_ENV}},i=function(e,t){try{var n=e.next,r=o.tryCatch.enable?Promise.resolve(n(t).catch(function(e){return console.log(e)})).then(function(){}):Promise.resolve(n(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},c=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return Promise.resolve((0,e.action)(e.params,Object.assign({},{actions:(0,e.consumerActions)(o.Actions[n],{modelName:n}),state:o.State[n]},o.Context.__global||{},o.Context[n]||{}))).then(function(n){return e.newState=n||null,Promise.resolve(r(t)).then(function(){})})}catch(e){return Promise.reject(e)}},s=function(e,t){try{var n=e.modelName,r=e.newState,o=e.next,i=function(){if(r)return y(n,r),Promise.resolve(o(t)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},a=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global,i=e.__hash,c=o.Setter.functionSetter[n];return"function"===e.type&&i&&c&&c[i]&&c[i].setState&&c[i].setState(o.State[n]),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},u=function(e,t){try{var n=e.next,r=e.Global.subscriptions[e.modelName+"_"+e.actionName];return r&&r.forEach(function(e){e()}),Promise.resolve(n(t)).then(function(){})}catch(e){return Promise.reject(e)}},l=function(e,t){try{var n=e.Global,r=!0===o.logger.enable||"function"==typeof o.logger.enable&&o.logger.enable(e)?(console.group("%c "+e.modelName+" State Change %c "+(new Date).toLocaleTimeString(),"color: gray; font-weight: lighter;","color: black; font-weight: bold;"),console.log("%c Previous","color: #9E9E9E; font-weight: bold",n.State[e.modelName]),console.log("%c Action","color: #03A9F4; font-weight: bold",e.actionName,"payload: "+e.params),Promise.resolve(e.next(t)).then(function(){console.log("%c Next","color: #4CAF50; font-weight: bold",n.State[e.modelName]),console.groupEnd()})):Promise.resolve(e.next(t)).then(function(){});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f=function(e,t){try{var n=e.Global;return Promise.resolve(e.next(t)).then(function(){n.withDevTools&&o.devtools.enable&&n.devTools.send(e.modelName+"_"+e.actionName,n.State)})}catch(e){return Promise.reject(e)}},m=function(e,t){try{var n=e.modelName,r=e.next,o=e.Global;return o.Setter.classSetter&&o.Setter.classSetter(o.State),o.Setter.functionSetter[n]&&Object.keys(o.Setter.functionSetter[n]).map(function(e){var t=o.Setter.functionSetter[n][e];if(t)if(t.selector){var r=t.selector(o.State[n]);w(r,t.selectorRef)||(t.selectorRef=r,t.setState(o.State[n]))}else t.setState(o.State[n])}),Promise.resolve(r(t)).then(function(){})}catch(e){return Promise.reject(e)}},d=[i,l,f,c,s,a,m,u],S={communicator:m,consoleDebugger:l,devToolsListener:f,getNewState:c,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]}),P(e,N(o,c))])).then(function(e){return t.newState=e||null,Promise.resolve(i(n)).then(function(){})})}catch(e){return Promise.reject(e)}}},setNewState:s,stateUpdater:a,subscription:u,tryCatch:i,config:o},v=function(e,t){try{t.next=function(e){return e.length>0&&e[0](t,e.slice(1))};var n=function(){if(e.length>0)return Promise.resolve(e[0](t,e.slice(1))).then(function(){})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},h=n.createContext({}),_=h.Consumer;if(!console.group){var p=[],b="-".repeat(80);console.group=function(e){p.push(e),console.log("%c \nBEGIN GROUP: %c",b,e),console.groupEnd=function(){console.log("END GROUP: %c\n%c",p.pop(),b)}}}var g=function(e,t){var n={};return Object.entries(e).forEach(function(e){n[e[0]]=function(e,t){return function(n,o){try{return Promise.resolve(v(d,{Global:r,action:e,actionName:e.name,consumerActions:g,middlewareConfig:o,modelName:t.modelName,newState:null,params:n,type:"outer"})).then(function(){})}catch(e){return Promise.reject(e)}}}(e[1],t)}),n},y=function(e,n){if("function"==typeof n){var o=r.State[e];o=t(o,n),r.State=t(r.State,function(t){t[e]=o})}else r.State=t(r.State,function(t){t[e]=Object.assign({},t[e],n)});return r.State},P=function(e,t){return new Promise(function(n){return setTimeout(function(){console.log(e),n(t)},e)})},O=function(e,t){try{var n={__FROM_SERVER__:!0};return Promise.resolve(Promise.all(Object.keys(r.State).map(function(o){try{var i=function(){if(!e||!e.modelName||o===e.modelName||-1!==e.modelName.indexOf(o)){function i(e){t&&t.isServer?n[o]=e:r.State[o]=Object.assign({},r.State[o],e)}var c=r.AsyncState[o];return c?Promise.resolve(c(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:r.State})}catch(e){return Promise.reject(e)}},N=function(e,t){var n=localStorage.getItem("__REACT_MODELX__"+e+"_"+t);return n?JSON.parse(n):null},w=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},E=function(e,t){j(e,t,void 0)},j=function(e,t,n){Array.isArray(t)?t.forEach(function(t){r.subscriptions[e+"_"+t]||(r.subscriptions[e+"_"+t]=[]),n?r.subscriptions[e+"_"+t].push(n):r.subscriptions[e+"_"+t]=[]}):(r.subscriptions[e+"_"+t]||(r.subscriptions[e+"_"+t]=[]),n?r.subscriptions[e+"_"+t].push(n):r.subscriptions[e+"_"+t]=[])},A=function(e){return r.State[e]},R=function(e,t){void 0===t&&(t={type:"outer"});var n={};return Object.entries(r.Actions[e]).forEach(function(o){var i=o[0],c=o[1];return n[i]=function(n,o){try{var s=Object.assign({},{action:c,actionName:i,consumerActions:g,middlewareConfig:o,modelName:e,newState:null,params:n},t,{Global:r}),a=r.Middlewares[e]?Promise.resolve(v(r.Middlewares[e],s)).then(function(){}):Promise.resolve(v(d,s)).then(function(){});return Promise.resolve(a&&a.then?a.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}}),n},x=function(e,t){var o=n.useState({})[1],i=n.useRef("");n.useEffect(function(){r.uid+=1;var n=""+r.uid;return i.current=n,r.Setter.functionSetter[e]||(r.Setter.functionSetter[e]={}),r.Setter.functionSetter[e][n]={setState:o,selector:t},function(){delete r.Setter.functionSetter[e][n]}},[]);var c=R(e,{__hash:i.current,type:"function"});return[t?t(A(e)):A(e),c]},C=function(e){function t(){e.apply(this,arguments),this.state=r.State}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t.prototype.render=function(){var e=this.props.children;return r.Setter.classSetter=this.setState.bind(this),n.createElement(h.Provider,{value:Object.assign({},this.state)},e)},t}(n.PureComponent);e.actionMiddlewares=d,e.Model=function(e,t,n){if(void 0!==e.state){r.uid+=1;var o="__"+r.uid;r.State[o]=e.state,e.middlewares&&(r.Middlewares[o]=e.middlewares),r.Actions[o]=e.actions,r.AsyncState[o]=e.asyncState,t&&(r.Context[o]=t);var i=R(o);return{__id:o,actions:i,getState:A(o),subscribe:function(e,t){return j(o,e,t)},unsubscribe:function(e){return E(o,e)},useStore:function(e){return x(o,e)}}}if(e.actions){console.error("invalid model(s) schema: ",e);var c=function(e){return function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return e}};return{__ERROR__:!0,actions:c({}),getActions:c({}),getInitialState:c({}),getState:c({}),subscribe:c(),unsubscribe:c(),useStore:c([{},{}])}}t&&!t.__FROM_SERVER__&&(r.State=t||{}),n&&(r.Context.__global=n),Object.entries(e).forEach(function(e){var n=e[0],o=e[1];if(o.__ERROR__)return console.error(n+" model's schema is invalid"),r.State[n]={},void(r.Actions[n]={});void 0!==o.useStore?(r.State[n]&&t||(r.State[n]=r.State[o.__id]),t&&t.__FROM_SERVER__&&(r.State[n]=Object.assign({},r.State[o.__id],t[n])),r.Actions[n]=r.Actions[o.__id],r.AsyncState[n]=r.AsyncState[o.__id],r.Middlewares[n]=r.Middlewares[o.__id],r.Context[n]=r.Context[o.__id]):(t&&t.__FROM_SERVER__?r.State[n]=Object.assign({},o.state,t[n]):r.State[n]||(r.State[n]=o.state),o.middlewares&&(r.Middlewares[n]=o.middlewares),r.Actions[n]=o.actions,r.AsyncState[n]=o.asyncState)});var s=Object.keys(e).reduce(function(e,t){var n;return Object.assign({},e,((n={})[t]=R(t),n))},{});return r.withDevTools="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,r.withDevTools&&(r.devTools=window.__REDUX_DEVTOOLS_EXTENSION__,r.devTools.connect()),{actions:s,getActions:R,getInitialState:O,getState:A,subscribe:j,unsubscribe:E,useStore:x}},e.middlewares=S,e.Provider=C,e.Consumer=_,e.connect=function(e,t,o){return function(i){return function(c){function s(){c.apply(this,arguments)}return c&&(s.__proto__=c),(s.prototype=Object.create(c&&c.prototype)).constructor=s,s.prototype.render=function(){var c=this,s=this.props,a=s.state;void 0===a&&(a={});var u=s.actions;return void 0===u&&(u={}),n.createElement(_,null,function(s){var l=s[""+e],f=r.Actions[e];return n.createElement(i,Object.assign({},c.props,{state:Object.assign({},a,t?t(l):l),actions:Object.assign({},u,o?o(g(f,{modelName:e})):g(f,{modelName:e}))}))})},s}(n.PureComponent)}},e.getState=A,e.getInitialState=O}); |
{ | ||
"name": "react-model", | ||
"version": "3.1.2", | ||
"version": "4.0.0-dev.20200615.0", | ||
"description": "The State management library for React", | ||
"main": "./dist/react-model.js", | ||
"module": "./dist/react-model.mjs", | ||
"umd:main": "./dist/react-model.umd.js", | ||
@@ -41,3 +42,3 @@ "types": "./src/index", | ||
"jest": "^24.1.0", | ||
"microbundle": "^0.11.0", | ||
"microbundle": "^0.12.0", | ||
"prettier": "^2.0.0", | ||
@@ -50,3 +51,3 @@ "react": "^16.8.4", | ||
"remark-preset-lint-recommended": "^4.0.0", | ||
"ts-jest": "^25.1.0", | ||
"ts-jest": "^26.0.0", | ||
"tslint": "^5.14.0", | ||
@@ -53,0 +54,0 @@ "typescript": "^3.4.5" |
@@ -9,7 +9,7 @@ # react-model · ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![npm version](https://img.shields.io/npm/v/react-model.svg?style=flat)](https://www.npmjs.com/package/react-model) [![minified size](https://badgen.net/bundlephobia/min/react)](https://bundlephobia.com/result?p=react-model) [![Build Status](https://travis-ci.org/byte-fe/react-model.svg?branch=master)](https://travis-ci.org/byte-fe/react-model) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/react-model/dist/react-model.js) [![downloads](https://img.shields.io/npm/dt/react-model.svg)](https://www.npmjs.com/package/react-model) [![Coverage Status](https://codecov.io/gh/byte-fe/react-model/branch/master/graph/badge.svg)](https://codecov.io/gh/byte-fe/react-model) [![Greenkeeper badge](https://badges.greenkeeper.io/byte-fe/react-model.svg)](https://greenkeeper.io/) ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg) | ||
⚔ Fully TypeScript Support | ||
⚔ Full TypeScript Support | ||
📦 Built with microbundle | ||
⚙️ Middlewares Pipline ( redux-devtools support ... ) | ||
⚙️ Middleware Pipline ( redux-devtools support ... ) | ||
@@ -60,4 +60,4 @@ ☂️ 100% test coverage, safe on production | ||
* [SSR Support: only return asyncState from server side](#ssr-with-nextjs) | ||
* [Expand Context](#expand-context) | ||
* [feat(middleware): support enable/disable sepecific middleware](#how-can-i-disable-the-console-debugger) | ||
* fix(stateupdater): fix the issue that setState on unmounted component | ||
@@ -107,3 +107,3 @@ ## Quick Start | ||
Every model have their own state and actions. | ||
Every model has its own state and actions. | ||
@@ -117,3 +117,3 @@ ```typescript | ||
interface StateType = { | ||
interface StateType { | ||
counter: number | ||
@@ -127,3 +127,3 @@ light: boolean | ||
interface ActionsParamType = { | ||
interface ActionsParamType { | ||
increment: number | ||
@@ -138,3 +138,3 @@ openLight: undefined | ||
return { | ||
counter: state.counter + (params || 1) | ||
counter: state.counter + (payload || 1) | ||
} | ||
@@ -181,3 +181,3 @@ }, | ||
react-model keep the state and actions in the separate private store. So you need to register them if you want to use them as the public models. | ||
react-model keeps the application state and actions in separate private stores. So you need to register them if you want to use them as the public models. | ||
@@ -201,3 +201,3 @@ `model/index.ts` | ||
The functional component in React ^16.8.0 can use Hooks to connect the global store. | ||
The actions return from useStore can invoke the dom changes. | ||
The actions returned from useStore can invoke dom changes. | ||
@@ -235,5 +235,5 @@ The execution of actions returned by useStore will invoke the rerender of current component first. | ||
1. use useStore on the subComponents which need it. | ||
2. [use useStore with depActions and React.memo to prevent child components rerender frequently.](https://github.com/ArrayZoneYour/react-model-todomvc/blob/master/src/components/TodoItem.tsx) | ||
2. use useStore selector. (version >= v4.0.0-rc.0) | ||
[Demo Repo](https://github.com/ArrayZoneYour/react-model-todomvc) | ||
[advance example with 1000 todo items](https://codesandbox.io/s/react-model-v4-todomvc-oxyij) | ||
@@ -339,5 +339,4 @@ [⇧ back to top](#table-of-contents) | ||
increment: async (params, { state: s }) => { | ||
// issue: https://github.com/Microsoft/TypeScript/issues/29196 | ||
// async function return produce need define type manually. | ||
return (state: typeof s) => { | ||
// return (state: typeof s) => { // TypeScript < 3.9 | ||
return state => { | ||
state.counter += params || 1 | ||
@@ -556,3 +555,3 @@ } | ||
const { useStore } = Model(ExtCounter, { name: 'test' }) | ||
// state.name = ' | ||
// state.name = '' | ||
const [state, actions] = useStore() | ||
@@ -676,3 +675,2 @@ // ... | ||
Just remove consoleDebugger middleware. | ||
@@ -679,0 +677,0 @@ ```typescript |
@@ -119,2 +119,29 @@ import produce from 'immer' | ||
const shallowEqual = (objA: any, objB: any) => { | ||
if (objA === objB) return true | ||
if ( | ||
typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null | ||
) { | ||
return false | ||
} | ||
const keysA = Object.keys(objA) | ||
const keysB = Object.keys(objB) | ||
if (keysA.length !== keysB.length) return false | ||
for (let i = 0; i < keysA.length; i++) { | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || | ||
objA[keysA[i]] !== objB[keysA[i]] | ||
) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
export { | ||
@@ -125,2 +152,3 @@ Consumer, | ||
setPartialState, | ||
shallowEqual, | ||
timeout, | ||
@@ -127,0 +155,0 @@ getCache, |
@@ -10,3 +10,4 @@ type Setter = { | ||
setState: React.Dispatch<any> | ||
depActions?: string[] | ||
selector?: Function | ||
selectorRef?: unknown | ||
} | ||
@@ -16,2 +17,8 @@ } | ||
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y | ||
? 1 | ||
: 2 | ||
? true | ||
: false | ||
interface Global { | ||
@@ -113,8 +120,22 @@ Actions: { | ||
type Selector<S, R> = (state: S) => R | ||
interface API<MT extends ModelType = ModelType<any, any, {}>> { | ||
__id: string | ||
__ERROR__?: boolean | ||
useStore: ( | ||
depActions?: Array<keyof MT['actions']> | ||
) => [Get<MT, 'state'>, getConsumerActionsType<Get<MT, 'actions'>>] | ||
useStore: < | ||
F extends Selector<Get<MT, 'state'>, any> = Selector< | ||
Get<MT, 'state'>, | ||
unknown | ||
> | ||
>( | ||
selector?: F | ||
) => [ | ||
F extends Selector<Get<MT, 'state'>, any> | ||
? Equals<F, Selector<Get<MT, 'state'>, unknown>> extends true | ||
? Get<MT, 'state'> | ||
: ReturnType<F> | ||
: Get<MT, 'state'>, | ||
getConsumerActionsType<Get<MT, 'actions'>> | ||
] | ||
getState: () => Readonly<Get<MT, 'state'>> | ||
@@ -132,9 +153,25 @@ subscribe: ( | ||
interface APIs<M extends Models> { | ||
useStore: <K extends keyof M>( | ||
useStore: < | ||
K extends keyof M, | ||
S extends M[K] extends API | ||
? ArgumentTypes<Get<M[K], 'useStore'>>[1] | ||
: M[K] extends ModelType | ||
? Selector<Get<M[K], 'state'>, unknown> | ||
: any | ||
>( | ||
name: K, | ||
depActions?: Array<keyof Get<M[K], 'actions'>> | ||
selector?: S | ||
) => M[K] extends API | ||
? ReturnType<Get<M[K], 'useStore'>> | ||
? S extends (...args: any) => void | ||
? ReturnType<S> | ||
: ReturnType<Get<M[K], 'useStore'>> | ||
: M[K] extends ModelType | ||
? [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>] | ||
? S extends (...args: any) => void | ||
? [ | ||
Equals<ReturnType<S>, unknown> extends true | ||
? Get<M[K], 'state'> | ||
: ReturnType<S>, | ||
getConsumerActionsType<Get<M[K], 'actions'>> | ||
] | ||
: [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>] | ||
: any | ||
@@ -141,0 +178,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { getCache, setPartialState, timeout } from './helper' | ||
import { getCache, setPartialState, timeout, shallowEqual } from './helper' | ||
// -- Middlewares -- | ||
@@ -145,3 +145,3 @@ | ||
const communicator: Middleware = async (context, restMiddlewares) => { | ||
const { modelName, next, actionName, Global } = context | ||
const { modelName, next, Global } = context | ||
if (Global.Setter.classSetter) { | ||
@@ -154,7 +154,10 @@ Global.Setter.classSetter(Global.State) | ||
if (setter) { | ||
if ( | ||
!setter.depActions || | ||
setter.depActions.indexOf(actionName) !== -1 | ||
) { | ||
if (!setter.selector) { | ||
setter.setState(Global.State[modelName]) | ||
} else { | ||
const newSelectorRef = setter.selector(Global.State[modelName]) | ||
if (!shallowEqual(newSelectorRef, setter.selectorRef)) { | ||
setter.selectorRef = newSelectorRef | ||
setter.setState(Global.State[modelName]) | ||
} | ||
} | ||
@@ -161,0 +164,0 @@ } |
Sorry, the diff of this file is not supported yet
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
85748
1141
3
868