UNPKG

mobx-react

Version:

React bindings for MobX. Create fully reactive components.

3 lines (2 loc) 9.9 kB
import{Reaction as e,_allowStateChanges as r,untracked as n,isObservableMap as t,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 n=this,t=arguments.length,o=new Array(t>2?t-2:0),i=2;i<t;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(n,o)}))}}function h(e,r){return function(){for(var n=arguments.length,t=new Array(n),o=0;o<n;o++)t[o]=arguments[o];v.call.apply(v,[this,e,r].concat(t))}}function O(e,r,n){var t=function(e,r){var n=e[m]=e[m]||{},t=n[r]=n[r]||{};return t.locks=t.locks||0,t.methods=t.methods||[],t}(e,r);t.methods.indexOf(n)<0&&t.methods.push(n);var o=Object.getOwnPropertyDescriptor(e,r);if(!o||!o[y]){var i=function e(r,n,t,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,n,t,o,i);Object.defineProperty(this,n,a)}},a.configurable=!0,a.enumerable=t,a}(e,r,o?o.enumerable:void 0,t,e[r]);Object.defineProperty(e,r,i)}}var w=Symbol("ObserverAdministration"),g=Symbol("isMobXReactObserver");function j(e){var r;return null!=(r=e[w])?r:e[w]={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(n){var t=n.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(n){var e;null==(e=r.reaction)||e.dispose(),r.reaction=null}else r.reactionInvalidatedBeforeMount=!0}))}(o),o.mounted||f.register(this,o,this));var n=void 0,i=void 0;if(o.reaction.track((function(){try{i=r(!1,t)}catch(e){n=e}})),n)throw n;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 n=Object.keys(e),t=Object.keys(r);if(n.length!==t.length)return!1;for(var o=0;o<n.length;o++)if(!Object.hasOwnProperty.call(r,n[o])||!d(e[n[o]],r[n[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[g]){var n=x(e);throw new Error("The provided component class ("+n+") has already been declared as an observer component.")}if(e[g]=!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 t=r.render;if("function"!=typeof t){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()?t:P.call(this,t)}),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 n=arguments[r];for(var t in n)({}).hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e}).apply(null,arguments)}var C=["children"],k=c.createContext({});function S(e){var r=e.children,n=function(e,r){if(null==e)return{};var n={};for(var t in e)if({}.hasOwnProperty.call(e,t)){if(r.indexOf(t)>=0)continue;n[t]=e[t]}return n}(e,C),t=c.useContext(k),o=c.useRef(U({},t,n));return c.createElement(k.Provider,{value:o.current},r)}function M(e,r,n,t){var o,i,a,s=c.forwardRef((function(n,t){var o=U({},n),i=c.useContext(k);return Object.assign(o,e(i||{},o)||{}),t&&(o.ref=t),c.createElement(r,o)}));return t&&(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 n=e.displayName||e.name||e.constructor&&e.constructor.name||"Component";return r?"inject-with-"+r+"("+n+")":"inject("+n+")"}(r,n),s}function R(e){return function(r,n){return e.forEach((function(e){if(!(e in n)){if(!(e in r))throw new Error("MobX injector: Store '"+e+"' is not available! Make sure it is provided by some Provider");n[e]=r[e]}})),n}}function I(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];if("function"==typeof arguments[0]){var t=arguments[0];return function(e){return M(t,e,t.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 n="string"==typeof r?e[r]:r;null!=n&&(Array.isArray(n)?n.map((function(e){return e()})):n())}))}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 n=Object.getPrototypeOf(e).constructor,t=Object.getPrototypeOf(e.constructor),o=Object.getPrototypeOf(Object.getPrototypeOf(e));if(n!==c.Component&&n!==c.PureComponent&&t!==c.Component&&t!==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,t,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 n((function(){return i=i||"<<anonymous>>",c=c||o,null==t[o]?r?new Error("The "+a+" `"+c+"` is marked as required in `"+i+"`, but its value is `"+(null===t[o]?"null":"undefined")+"`."):null:e.apply(void 0,[t,o,i,a,c].concat(u))}))}var t=r.bind(null,!1);return t.isRequired=r.bind(null,!0),t}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 n((function(){if(e&&_(a[c])===r.toLowerCase())return null;var n;switch(r){case"Array":n=i;break;case"Object":n=o;break;case"Map":n=t;break;default:throw new Error("Unexpected mobxType: "+r)}var u=a[c];if(!n(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(t,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 n((function(){if("function"!=typeof r)return new Error("Property `"+c+"` of component `"+i+"` has invalid PropType notation.");var n=X(e,"Array")(t,o,i,a,c);if(n instanceof Error)return n;for(var s=t[o],l=0;l<s.length;l++)if((n=r.apply(void 0,[s,l,i,a,c+"["+l+"]"].concat(u)))instanceof Error)return n;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