UNPKG

mobx-react

Version:

React bindings for MobX. Create fully reactive components.

3 lines (2 loc) 9.91 kB
import{Reaction as e,_allowStateChanges as r,untracked as t,isObservableMap as n,isObservableObject as o,isObservableArray as i,observable as a}from"mobx";import c,{PureComponent as s,Component as u}from"react";import{isUsingStaticRendering as l,_observerFinalizationRegistry as f,observer as p}from"mobx-react-lite";export{Observer,enableStaticRendering,isUsingStaticRendering,observerBatching,useAsObservableSource,useLocalObservable,useLocalStore,useObserver,useStaticRendering}from"mobx-react-lite";function d(e,r){return e===r?0!==e||1/e==1/r:e!=e&&r!=r}var b={$$typeof:1,render:1,compare:1,type:1,childContextTypes:1,contextType:1,contextTypes:1,defaultProps:1,getDefaultProps:1,getDerivedStateFromError:1,getDerivedStateFromProps:1,mixins:1,displayName:1,propTypes:1},m=Symbol("patchMixins"),y=Symbol("patchedDefinition");function v(e,r){for(var t=this,n=arguments.length,o=new Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];r.locks++;try{var a;return null!=e&&(a=e.apply(this,o)),a}finally{r.locks--,0===r.locks&&r.methods.forEach((function(e){e.apply(t,o)}))}}function h(e,r){return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];v.call.apply(v,[this,e,r].concat(n))}}function O(e,r,t){var n=function(e,r){var t=e[m]=e[m]||{},n=t[r]=t[r]||{};return n.locks=n.locks||0,n.methods=n.methods||[],n}(e,r);n.methods.indexOf(t)<0&&n.methods.push(t);var o=Object.getOwnPropertyDescriptor(e,r);if(!o||!o[y]){var i=function e(r,t,n,o,i){var a,c=h(i,o);return(a={})[y]=!0,a.get=function(){return c},a.set=function(i){if(this===r)c=h(i,o);else{var a=e(this,t,n,o,i);Object.defineProperty(this,t,a)}},a.configurable=!0,a.enumerable=n,a}(e,r,o?o.enumerable:void 0,n,e[r]);Object.defineProperty(e,r,i)}}var g=Symbol("ObserverAdministration"),w=Symbol("isMobXReactObserver");function j(e){var r;return null!=(r=e[g])?r:e[g]={reaction:null,mounted:!1,reactionInvalidatedBeforeMount:!1,forceUpdate:null,name:x(e.constructor),state:void 0,props:void 0,context:void 0}}function x(e){return e.displayName||e.name||"<component>"}function P(t){var n=t.bind(this),o=j(this);return function(){o.reaction||(o.reaction=function(r){return new e(r.name+".render()",(function(){if(r.mounted)try{null==r.forceUpdate||r.forceUpdate()}catch(t){var e;null==(e=r.reaction)||e.dispose(),r.reaction=null}else r.reactionInvalidatedBeforeMount=!0}))}(o),o.mounted||f.register(this,o,this));var t=void 0,i=void 0;if(o.reaction.track((function(){try{i=r(!1,n)}catch(e){t=e}})),t)throw t;return i}}function E(e,r){return l()&&console.warn("[mobx-react] It seems that a re-rendering of a React component is triggered while in static (server-side) mode. Please make sure components are rendered only once server-side."),this.state!==r||!function(e,r){if(d(e,r))return!0;if("object"!=typeof e||null===e||"object"!=typeof r||null===r)return!1;var t=Object.keys(e),n=Object.keys(r);if(t.length!==n.length)return!1;for(var o=0;o<t.length;o++)if(!Object.hasOwnProperty.call(r,t[o])||!d(e[t[o]],r[t[o]]))return!1;return!0}(this.props,e)}function A(e,r){if(r&&"class"!==r.kind)throw new Error("The @observer decorator can be used on classes only");return!0===e.isMobxInjector&&console.warn("Mobx observer: You are trying to use `observer` on a component that already has `inject`. Please apply `observer` before applying `inject`"),Object.prototype.isPrototypeOf.call(u,e)||Object.prototype.isPrototypeOf.call(s,e)?function(e){var r=e.prototype;if(e[w]){var t=x(e);throw new Error("The provided component class ("+t+") has already been declared as an observer component.")}if(e[w]=!0,r.componentWillReact)throw new Error("The componentWillReact life-cycle event is no longer supported");if(e.__proto__!==s)if(r.shouldComponentUpdate){if(r.shouldComponentUpdate!==E)throw new Error("It is not allowed to use shouldComponentUpdate in observer based components.")}else r.shouldComponentUpdate=E;var n=r.render;if("function"!=typeof n){var o=x(e);throw new Error("[mobx-react] class component ("+o+") is missing `render` method.\n`observer` requires `render` being a function defined on prototype.\n`render = () => {}` or `render = function() {}` is not supported.")}r.render=function(){return Object.defineProperty(this,"render",{configurable:!1,writable:!1,value:l()?n:P.call(this,n)}),this.render()};var i=r.componentDidMount;return r.componentDidMount=function(){var e=this,r=j(this);return r.mounted=!0,f.unregister(this),r.forceUpdate=function(){return e.forceUpdate()},r.reaction&&!r.reactionInvalidatedBeforeMount||r.forceUpdate(),null==i?void 0:i.apply(this,arguments)},O(r,"componentWillUnmount",(function(){var e;if(!l()){var r=j(this);null==(e=r.reaction)||e.dispose(),r.reaction=null,r.forceUpdate=null,r.mounted=!1,r.reactionInvalidatedBeforeMount=!1}})),e}(e):p(e)}function U(){return(U=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var C=["children"],k=c.createContext({});function S(e){var r=e.children,t=function(e,r){if(null==e)return{};var t,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)r.indexOf(t=i[n])>=0||(o[t]=e[t]);return o}(e,C),n=c.useContext(k),o=c.useRef(U({},n,t));return c.createElement(k.Provider,{value:o.current},r)}function M(e,r,t,n){var o,i,a,s=c.forwardRef((function(t,n){var o=U({},t),i=c.useContext(k);return Object.assign(o,e(i||{},o)||{}),n&&(o.ref=n),c.createElement(r,o)}));return n&&(s=A(s)),s.isMobxInjector=!0,o=r,i=s,a=Object.getOwnPropertyNames(Object.getPrototypeOf(o)),Object.getOwnPropertyNames(o).forEach((function(e){b[e]||-1!==a.indexOf(e)||Object.defineProperty(i,e,Object.getOwnPropertyDescriptor(o,e))})),s.wrappedComponent=r,s.displayName=function(e,r){var t=e.displayName||e.name||e.constructor&&e.constructor.name||"Component";return r?"inject-with-"+r+"("+t+")":"inject("+t+")"}(r,t),s}function R(e){return function(r,t){return e.forEach((function(e){if(!(e in t)){if(!(e in r))throw new Error("MobX injector: Store '"+e+"' is not available! Make sure it is provided by some Provider");t[e]=r[e]}})),t}}function I(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];if("function"==typeof arguments[0]){var n=arguments[0];return function(e){return M(n,e,n.name,!0)}}return function(e){return M(R(r),e,r.join("-"),!1)}}S.displayName="MobXProvider";var T=Number.parseInt(c.version.split(".")[0]),D=!1,N=Symbol("disposeOnUnmountProto"),q=Symbol("disposeOnUnmountInst");function B(){var e=this;[].concat(this[N]||[],this[q]||[]).forEach((function(r){var t="string"==typeof r?e[r]:r;null!=t&&(Array.isArray(t)?t.map((function(e){return e()})):t())}))}function L(e,r){if(Array.isArray(r))return r.map((function(r){return L(e,r)}));D||(T>=18?console.error("[mobx-react] disposeOnUnmount is not compatible with React 18 and higher. Don't use it."):console.warn("[mobx-react] disposeOnUnmount is deprecated. It won't work correctly with React 18 and higher."),D=!0);var t=Object.getPrototypeOf(e).constructor,n=Object.getPrototypeOf(e.constructor),o=Object.getPrototypeOf(Object.getPrototypeOf(e));if(t!==c.Component&&t!==c.PureComponent&&n!==c.Component&&n!==c.PureComponent&&o!==c.Component&&o!==c.PureComponent)throw new Error("[mobx-react] disposeOnUnmount only supports direct subclasses of React.Component or React.PureComponent.");if("string"!=typeof r&&"function"!=typeof r&&!Array.isArray(r))throw new Error("[mobx-react] disposeOnUnmount only works if the parameter is either a property key or a function.");var i=!!e[N]||!!e[q];return("string"==typeof r?e[N]||(e[N]=[]):e[q]||(e[q]=[])).push(r),i||O(e,"componentWillUnmount",B),"string"!=typeof r?r:void 0}function W(e){function r(r,n,o,i,a,c){for(var s=arguments.length,u=new Array(s>6?s-6:0),l=6;l<s;l++)u[l-6]=arguments[l];return t((function(){return i=i||"<<anonymous>>",c=c||o,null==n[o]?r?new Error("The "+a+" `"+c+"` is marked as required in `"+i+"`, but its value is `"+(null===n[o]?"null":"undefined")+"`."):null:e.apply(void 0,[n,o,i,a,c].concat(u))}))}var n=r.bind(null,!1);return n.isRequired=r.bind(null,!0),n}function _(e){var r=typeof e;return Array.isArray(e)?"array":e instanceof RegExp?"object":function(e,r){return"symbol"===e||"Symbol"===r["@@toStringTag"]||"function"==typeof Symbol&&r instanceof Symbol}(r,e)?"symbol":r}function X(e,r){return W((function(a,c,s,u,l){return t((function(){if(e&&_(a[c])===r.toLowerCase())return null;var t;switch(r){case"Array":t=i;break;case"Object":t=o;break;case"Map":t=n;break;default:throw new Error("Unexpected mobxType: "+r)}var u=a[c];if(!t(u)){var f=function(e){var r=_(e);if("object"===r){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return r}(u),p=e?" or javascript `"+r.toLowerCase()+"`":"";return new Error("Invalid prop `"+l+"` of type `"+f+"` supplied to `"+s+"`, expected `mobx.Observable"+r+"`"+p+".")}return null}))}))}function F(e,r){return W((function(n,o,i,a,c){for(var s=arguments.length,u=new Array(s>5?s-5:0),l=5;l<s;l++)u[l-5]=arguments[l];return t((function(){if("function"!=typeof r)return new Error("Property `"+c+"` of component `"+i+"` has invalid PropType notation.");var t=X(e,"Array")(n,o,i,a,c);if(t instanceof Error)return t;for(var s=n[o],l=0;l<s.length;l++)if((t=r.apply(void 0,[s,l,i,a,c+"["+l+"]"].concat(u)))instanceof Error)return t;return null}))}))}var $={observableArray:X(!1,"Array"),observableArrayOf:F.bind(null,!1),observableMap:X(!1,"Map"),observableObject:X(!1,"Object"),arrayOrObservableArray:X(!0,"Array"),arrayOrObservableArrayOf:F.bind(null,!0),objectOrObservableObject:X(!0,"Object")};if(!u)throw new Error("mobx-react requires React to be available");if(!a)throw new Error("mobx-react requires mobx to be available");export{k as MobXProviderContext,$ as PropTypes,S as Provider,L as disposeOnUnmount,I as inject,A as observer}; //# sourceMappingURL=mobxreact.esm.production.min.js.map