@sampettersson/constate
Version:
Yet another React state management library that lets you work with local state and scale up to global state with ease
2 lines (1 loc) • 5.87 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e(t.constate={},t.React)}(this,function(t,s){"use strict";var n=s.createContext({state:{}}),o=n.Consumer;function u(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function a(){return(a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(t[o]=n[o])}return t}).apply(this,arguments)}function p(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},o=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),o.forEach(function(t){u(e,t,n[t])})}return e}function r(t,e){t.prototype=Object.create(e.prototype),(t.prototype.constructor=t).__proto__=e}function f(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var c=function(o,r){return Object.keys(o).reduce(function(t,e){var n;return p({},t,((n={})[e]=r(o[e],e),n))},{})},l=function(t,e){return"function"==typeof t?t(e):t},d=function(i){function t(t){var c;u(f(f(c=i.call(this,t)||this)),"state",c.props.initialState),u(f(f(c)),"ignoreState",!1),u(f(f(c)),"unmount",void 0),u(f(f(c)),"getEffectProps",function(n){var t=c.props,e=t.context,o=t.state;return{state:e&&o?o:c.state,setState:function(t,e){return c.handleSetState(t,e,n)}}}),u(f(f(c)),"handleSetState",function(e,r,i){var s,t=c.props,n=t.context,o=t.setContextState,a=function(t){return l(e,s=t)},u=function(){var t=c.props,e=t.state,n=void 0===e?c.state:e,o=t.onUpdate;o&&c.ignoreState!==n&&o(p({},c.getEffectProps("onUpdate"),{prevState:s,type:i})),r&&r()};n&&o?o(n,a,u,i):c.setState(a,u)});var e=t.context,n=t.setContextState,o=t.state,r=t.initialState;return e&&n&&!o&&n(e,function(t){return Object.assign({},r,t)},void 0,"initialState"),c}r(t,i);var e=t.prototype;return e.componentDidMount=function(){var t=this,e=this.props,n=e.context,o=e.mountContainer,r=e.onMount,i=function(){return r&&r(t.getEffectProps("onMount"))};n&&o?this.unmount=o(n,i):n||i()},e.shouldComponentUpdate=function(t,e){var n=this,o=this.props,r=o.context,i=o.state,s=t.state,a=t.shouldUpdate,u=!0;return r&&i&&s&&a?(u=a({state:i,setState:function(t,e){return n.handleSetState(t,e,"shouldUpdate")},nextState:s}),this.ignoreState=!u&&s):!r&&a&&(u=a({state:this.state,setState:function(t,e){return n.handleSetState(t,e,"shouldUpdate")},nextState:e}),this.ignoreState=!u&&e),u},e.componentWillUnmount=function(){var t=this,e=this.props,n=e.context,o=e.onUnmount,r=function(){return o&&o(t.getEffectProps("onUnmount"))};this.unmount?this.unmount(r):n||r()},e.render=function(){var n,e,o,t=this.props,r=t.state,i=t.children,s=t.actions,a=t.selectors,u=t.effects;return i(Object.assign({},r||this.state,s&&(o=this.handleSetState,c(s,function(t,e){return function(){return o(t.apply(void 0,arguments),void 0,e)}})),a&&(e=r||this.state,c(a,function(t){return function(){return t.apply(void 0,arguments)(e)}})),u&&(n=this.getEffectProps,c(u,function(t,e){return function(){return t.apply(void 0,arguments)(n(e))}}))))},t}(s.Component);u(d,"defaultProps",{initialState:{}});var e=function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e.prototype.render=function(){var r=this,i=this.props.context;return void 0!==i?s.createElement(o,null,function(t){var e=t.state,n=t.setContextState,o=t.mountContainer;return s.createElement(d,a({},r.props,{state:e[i],setContextState:n,mountContainer:o}))}):s.createElement(d,this.props)},e}(s.Component),i="undefined"!=typeof window&&window.__REDUX_DEVTOOLS_EXTENSION__,h=function(o){function t(t){var a;u(f(f(a=o.call(this,t)||this)),"containers",{}),u(f(f(a)),"devtools",void 0),u(f(f(a)),"mountContainer",function(e,t){return a.containers[e]||(a.containers[e]=0,t&&a.setState(null,t)),a.containers[e]+=1,function(t){1===a.containers[e]&&t&&t(),a.containers[e]-=1}}),u(f(f(a)),"setContextState",function(n,o,r,i){var s;a.setState(function(t){var e;return s=t.state,{state:Object.assign({},t.state,(e={},e[n]=Object.assign({},t.state[n],l(o,t.state[n]||{})),e))}},function(){if(a.props.onUpdate){var t=p({},a.getProps("Provider/onUpdate"),{prevState:s,context:n,type:i});a.props.onUpdate(t)}if(r&&r(),a.devtools&&i){var e=n?n+"/"+i:i;a.devtools.send(e,a.state.state)}})}),u(f(f(a)),"getProps",function(o){var t=a.state,e=t.state,r=t.setContextState;return{state:e,setContextState:function(o){function t(t,e,n){return o.apply(this,arguments)}return t.toString=function(){return o.toString()},t}(function(t,e,n){return r(t,e,n,o)})}});var e=t.devtools,n=t.initialState;return a.state={state:n,mountContainer:a.mountContainer,setContextState:a.setContextState},e&&i&&(a.devtools=i.connect({name:"Context"}),a.devtools.init(n),a.devtools.subscribe(function(e){"DISPATCH"===e.type&&e.state&&a.setState(function(t){return{state:Object.assign({},t.state,JSON.parse(e.state))}})})),a}r(t,o);var e=t.prototype;return e.componentDidMount=function(){this.props.onMount&&this.props.onMount(this.getProps("Provider/onMount"))},e.componentWillUnmount=function(){if(this.props.onUnmount){var t=this.getProps(),e=(t.setContextState,function(t,e){if(null==t)return{};var n,o,r={},i=Object.keys(t);for(o=0;o<i.length;o++)n=i[o],0<=e.indexOf(n)||(r[n]=t[n]);return r}(t,["setContextState"]));this.props.onUnmount(e)}this.devtools&&i&&(this.devtools.unsubscribe(),i.disconnect())},e.render=function(){return s.createElement(n.Provider,{value:this.state},this.props.children)},t}(s.Component);u(h,"defaultProps",{initialState:{}}),t.Consumer=o,t.Container=e,t.Provider=h,Object.defineProperty(t,"__esModule",{value:!0})});