index.js 12 KB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("mobx"),require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","mobx","react","react-dom"],t):t(e.mobxReact=e.mobxReact||{},e.mobx,e.React,e.ReactDOM)}(this,function(e,t,n,r){"use strict";function o(e){function n(n,r,o,i,a,s){for(var c=arguments.length,u=Array(c>6?c-6:0),p=6;p<c;p++)u[p-6]=arguments[p];return t.untracked(function(){if(i=i||"<<anonymous>>",s=s||o,null==r[o]){if(n){var t=null===r[o]?"null":"undefined";return new Error("The "+a+" `"+s+"` is marked as required in `"+i+"`, but its value is `"+t+"`.")}return null}return e.apply(void 0,[r,o,i,a,s].concat(u))})}var r=n.bind(null,!1);return r.isRequired=n.bind(null,!0),r}function i(e,t){return"symbol"===e||("Symbol"===t["@@toStringTag"]||"function"==typeof Symbol&&t instanceof Symbol)}function a(e){var t=void 0===e?"undefined":j(e);return Array.isArray(e)?"array":e instanceof RegExp?"object":i(t,e)?"symbol":t}function s(e){var t=a(e);if("object"===t){if(e instanceof Date)return"date";if(e instanceof RegExp)return"regexp"}return t}function c(e,n){return o(function(r,o,i,c,u){return t.untracked(function(){if(e&&a(r[o])===n.toLowerCase())return null;var c=void 0;switch(n){case"Array":c=t.isObservableArray;break;case"Object":c=t.isObservableObject;break;case"Map":c=t.isObservableMap;break;default:throw new Error("Unexpected mobxType: "+n)}var p=r[o];if(!c(p)){var l=s(p),f=e?" or javascript `"+n.toLowerCase()+"`":"";return new Error("Invalid prop `"+u+"` of type `"+l+"` supplied to `"+i+"`, expected `mobx.Observable"+n+"`"+f+".")}return null})})}function u(e,n){return o(function(r,o,i,a,s){for(var u=arguments.length,p=Array(u>5?u-5:0),l=5;l<u;l++)p[l-5]=arguments[l];return t.untracked(function(){if("function"!=typeof n)return new Error("Property `"+s+"` of component `"+i+"` has invalid PropType notation.");var t=c(e,"Array")(r,o,i);if(t instanceof Error)return t;for(var u=r[o],l=0;l<u.length;l++)if((t=n.apply(void 0,[u,l,i,a,s+"["+l+"]"].concat(p)))instanceof Error)return t;return null})})}function p(e){return!e.prototype.render}function l(e,t,r){var o,i,a="inject-"+(t.displayName||t.name||t.constructor&&t.constructor.name||"Unknown");r&&(a+="-with-"+r);var s=(i=o=function(n){function r(){var e,t,n,o;C(this,r);for(var i=arguments.length,a=Array(i),s=0;s<i;s++)a[s]=arguments[s];return t=n=S(this,(e=r.__proto__||Object.getPrototypeOf(r)).call.apply(e,[this].concat(a))),n.storeRef=function(e){n.wrappedInstance=e},o=t,S(n,o)}return P(r,n),M(r,[{key:"render",value:function(){var n={};for(var r in this.props)this.props.hasOwnProperty(r)&&(n[r]=this.props[r]);var o=e(this.context.mobxStores||{},n,this.context)||{};for(var i in o)n[i]=o[i];return p(t)||(n.ref=this.storeRef),x.createElement(t,n)}}]),r}(n.Component),o.displayName=a,i);return q(s,t),s.wrappedComponent=t,t.wrappedComponent?s.NativeCom=t.wrappedComponent:s.NativeCom=t,t.localCom&&(s.NativeCom.localCom=t.localCom),Object.defineProperties(s,B),s}function f(e){return function(t,n){return e.forEach(function(e){if(!(e in n)){if(!(e in t))throw new Error("MobX injector: Store '"+e+"' is not available! Make sure it is provided by some Provider");n[e]=t[e]}}),n}}function d(){var e=void 0;if("function"==typeof arguments[0])return e=arguments[0],function(t){var n=l(e,t);return n.isMobxInjector=!1,n=w(n),n.isMobxInjector=!0,n};for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return e=f(t),function(n){return l(e,n,t.join("-"))}}function b(e){if(_)try{return _.findDOMNode(e)}catch(e){return null}return null}function y(e){var t=b(e);t&&V&&V.set(t,e),z.emit({event:"render",renderTime:e.__$mobRenderEnd-e.__$mobRenderStart,totalTime:Date.now()-e.__$mobRenderStart,component:e,node:t})}function h(){if("undefined"==typeof WeakMap)throw new Error("[mobx-react] tracking components is not supported in this browser.");H||(H=!0)}function m(e){G=e}function v(e,t){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=e[t],o=F[t],i=r?!0===n?function(){o.apply(this,arguments),r.apply(this,arguments)}:function(){r.apply(this,arguments),o.apply(this,arguments)}:o;e[t]=i}function O(e,t){if(null==e||null==t||"object"!==(void 0===e?"undefined":j(e))||"object"!==(void 0===t?"undefined":j(t)))return e!==t;var n=Object.keys(e);if(n.length!==Object.keys(t).length)return!0;for(var r=void 0,o=n.length-1;r=n[o];o--)if(t[r]!==e[r])return!0;return!1}function w(e,t){if("string"==typeof e)throw new Error("Store names should be provided as array");if(window.rewriteMobx&&"function"==typeof window.rewriteMobx&&(e=window.rewriteMobx(e,t)),Array.isArray(e))return K||(K=!0,console.warn('Mobx observer: Using observer to inject stores is deprecated since 4.0. Use `@inject("store1", "store2") @observer ComponentClass` or `inject("store1", "store2")(observer(componentClass))` instead of `@observer(["store1", "store2"]) ComponentClass`')),t?d.apply(null,e)(w(t)):function(t){return w(e,t)};var r=e;if(!0===r.isMobxInjector&&console.warn("Mobx observer: You are trying to use 'observer' on a component that already has 'inject'. Please apply 'observer' before applying 'inject'"),!("function"!=typeof r||r.prototype&&r.prototype.render||r.isReactClass||x.Component.isPrototypeOf(r))){var o,i;return w((i=o=function(e){function t(){return C(this,t),S(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return P(t,e),M(t,[{key:"render",value:function(){return r.call(this,this.props,this.context)}}]),t}(n.Component),o.displayName=r.displayName||r.name,o.contextTypes=r.contextTypes,o.propTypes=r.propTypes,o.defaultProps=r.defaultProps,i))}if(!r)throw new Error("Please pass a valid component to 'observer'");return g(r.prototype||r),r.isMobXReactObserver=!0,r}function g(e){v(e,"componentWillMount",!0),["componentDidMount","componentWillUnmount","componentDidUpdate"].forEach(function(t){v(e,t)}),e.shouldComponentUpdate||(e.shouldComponentUpdate=F.shouldComponentUpdate)}var x="default"in n?n.default:n,_="default"in r?r.default:r,j="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},C=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},M=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),P=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},S=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},k=function(){function e(){C(this,e),this.listeners=[]}return M(e,[{key:"on",value:function(e){var t=this;return this.listeners.push(e),function(){var n=t.listeners.indexOf(e);-1!==n&&t.listeners.splice(n,1)}}},{key:"emit",value:function(e){this.listeners.forEach(function(t){return t(e)})}}]),e}(),R=c(!1,"Array"),E=u.bind(null,!1),T=c(!1,"Map"),A=c(!1,"Object"),I=c(!0,"Array"),U=u.bind(null,!0),D=c(!0,"Object"),N=Object.freeze({observableArray:R,observableArrayOf:E,observableMap:T,observableObject:A,arrayOrObservableArray:I,arrayOrObservableArrayOf:U,objectOrObservableObject:D}),W={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},L={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},$="function"==typeof Object.getOwnPropertySymbols,q=function(e,t,n){if("string"!=typeof t){var r=Object.getOwnPropertyNames(t);$&&(r=r.concat(Object.getOwnPropertySymbols(t)));for(var o=0;o<r.length;++o)if(!(W[r[o]]||L[r[o]]||n&&n[r[o]]))try{e[r[o]]=t[r[o]]}catch(e){}}return e},X={mobxStores:D};Object.seal(X);var B={contextTypes:{get:function(){return X},set:function(e){console.warn("Mobx Injector: you are trying to attach `contextTypes` on an component decorated with `inject` (or `observer`) HOC. Please specify the contextTypes on the wrapped component instead. It is accessible through the `wrappedComponent`")},configurable:!0,enumerable:!1},isMobxInjector:{value:!0,writable:!0,configurable:!0,enumerable:!0}},H=!1,G=!1,K=!1,V="undefined"!=typeof WeakMap?new WeakMap:void 0,z=new k,Y=new k,F={componentWillMount:function(){function e(e){var n=this[e],r=new t.Atom("reactive "+e);Object.defineProperty(this,e,{configurable:!0,enumerable:!0,get:function(){return r.reportObserved(),n},set:function(e){!a&&O(n,e)?(n=e,i=!0,r.reportChanged(),i=!1):n=e}})}var n=this;if(!0!==G){var r=this.displayName||this.name||this.constructor&&(this.constructor.displayName||this.constructor.name)||"<component>",o=this._reactInternalInstance&&this._reactInternalInstance._rootNodeID,i=!1,a=!1;e.call(this,"props"),e.call(this,"state");var s=this.render.bind(this),c=null,u=!1,p=function(){return c=new t.Reaction(r+"#"+o+".render()",function(){if(!u&&(u=!0,"function"==typeof n.componentWillReact&&n.componentWillReact(),!0!==n.__$mobxIsUnmounted)){var e=!0;try{a=!0,i||x.Component.prototype.forceUpdate.call(n),e=!1}finally{a=!1,e&&c.dispose()}}}),l.$mobx=c,n.render=l,l()},l=function(){u=!1;var e=void 0,r=void 0;if(c.track(function(){H&&(n.__$mobRenderStart=Date.now());try{r=t.extras.allowStateChanges(!1,s)}catch(t){e=t}H&&(n.__$mobRenderEnd=Date.now())}),e)throw Y.emit(e),e;return r};this.render=p}},componentWillUnmount:function(){if(!0!==G&&(this.render.$mobx&&this.render.$mobx.dispose(),this.__$mobxIsUnmounted=!0,H)){var e=b(this);e&&V&&V.delete(e),z.emit({event:"destroy",component:this,node:e})}},componentDidMount:function(){H&&y(this)},componentDidUpdate:function(){H&&y(this)},shouldComponentUpdate:function(e,t){return G&&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!==t||O(this.props,e)}},J=w(function(e){return(0,e.children)()});J.propTypes={children:function(e,t,n,r,o){if("function"!=typeof e[t])return new Error("Invalid prop `"+o+"` of type `"+j(e[t])+"` supplied to `"+n+"`, expected `function`.")}};var Q,Z,ee={children:!0,key:!0,ref:!0},te=(Z=Q=function(e){function t(){return C(this,t),S(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return P(t,e),M(t,[{key:"render",value:function(){return x.Children.only(this.props.children)}},{key:"getChildContext",value:function(){var e={},t=this.context.mobxStores;if(t)for(var n in t)e[n]=t[n];for(var r in this.props)ee[r]||"suppressChangedStoreWarning"===r||(e[r]=this.props[r]);return{mobxStores:e}}},{key:"componentWillReceiveProps",value:function(e){if(Object.keys(e).length!==Object.keys(this.props).length&&console.warn("MobX Provider: The set of provided stores has changed. Please avoid changing stores as the change might not propagate to all children"),!e.suppressChangedStoreWarning)for(var t in e)ee[t]||this.props[t]===e[t]||console.warn("MobX Provider: Provided store '"+t+"' has changed. Please avoid replacing stores as the change might not propagate to all children")}}]),t}(n.Component),Q.contextTypes={mobxStores:D},Q.childContextTypes={mobxStores:D.isRequired},Z),ne=void 0;if(ne="mobx-react",!t)throw new Error(ne+" requires the MobX package");if(!x)throw new Error(ne+" requires React to be available");"function"==typeof r.unstable_batchedUpdates&&t.extras.setReactionScheduler(r.unstable_batchedUpdates);var re=function(e){return Y.on(e)};"object"===("undefined"==typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__?"undefined":j(__MOBX_DEVTOOLS_GLOBAL_HOOK__))&&__MOBX_DEVTOOLS_GLOBAL_HOOK__.injectMobxReact(e,t),e.propTypes=N,e.PropTypes=N,e.onError=re,e.default=e,e.observer=w,e.Observer=J,e.renderReporter=z,e.componentByNodeRegistery=V,e.trackComponents=h,e.useStaticRendering=m,e.Provider=te,e.inject=d,Object.defineProperty(e,"__esModule",{value:!0})});