mobx-react-lite
Version:
Lightweight React bindings for MobX based on function components and Hooks
3 lines (2 loc) • 5.16 kB
JavaScript
var t,e;t=this,e=function(t,e,i){function n(){return c}function r(t){t.reaction=new e.Reaction(`observer${t.name}`,()=>{t.stateVersion=Symbol(),null==t.onStoreChange||t.onStoreChange()})}function o(t,i="observed"){if(n())return t();const o=a.default.useRef(null);o.current||(o.current=function(t){const e={reaction:null,onStoreChange:null,stateVersion:Symbol(),name:t,subscribe:t=>(h.unregister(e),e.onStoreChange=t,e.reaction||(r(e),e.stateVersion=Symbol()),()=>{var t;e.onStoreChange=null,null==(t=e.reaction)||t.dispose(),e.reaction=null}),getSnapshot:()=>e.stateVersion};return e}(i));const s=o.current;let l,u;if(s.reaction||(r(s),h.register(o,s,s)),a.default.useDebugValue(s.reaction,e.getDependencyTree),a.default.useSyncExternalStore(s.subscribe,s.getSnapshot,s.getSnapshot),s.reaction.track(()=>{try{l=t()}catch(t){u=t}}),u)throw u;return l}function s({children:t,render:e}){t&&e&&console.error("MobX Observer: Do not use children and render in the same time in `Observer`");const i=t||e;return"function"!=typeof i?null:o(i)}var l,a=function(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}(i);if(!i.useState||!i.useSyncExternalStore)throw new Error("mobx-react-lite requires React 18 or later");if(!((null==e._getGlobalState||null==(l=e._getGlobalState())?void 0:l.version)>=7))throw new Error("mobx-react-lite requires mobx at least version 7 to be available");class u{constructor(t){this.finalize=void 0,this.registrations=new Map,this.sweepTimeout=void 0,this.sweep=(t=1e4)=>{clearTimeout(this.sweepTimeout),this.sweepTimeout=void 0;const e=Date.now();this.registrations.forEach((i,n)=>{e-i.registeredAt>=t&&(this.registrations.delete(n),this.finalize(i.value))}),this.registrations.size>0&&this.scheduleSweep()},this.finalizeAllImmediately=()=>{this.sweep(0)},this.finalize=t}register(t,e,i){this.registrations.set(i,{value:e,registeredAt:Date.now()}),this.scheduleSweep()}unregister(t){this.registrations.delete(t),0===this.registrations.size&&(clearTimeout(this.sweepTimeout),this.sweepTimeout=void 0)}scheduleSweep(){void 0===this.sweepTimeout&&(this.sweepTimeout=setTimeout(this.sweep,1e4))}}const h=new("undefined"!=typeof FinalizationRegistry?class{constructor(t){this.finalize=void 0,this.finalizeRegistration=t=>{var e;null==(e=this.native)||e.unregister(t.token),this.timer.unregister(t.token),this.finalize(t.value)},this.tokens=new WeakMap,this.staged=new Map,this.flushScheduled=!1,this.native="undefined"!=typeof FinalizationRegistry?new FinalizationRegistry(this.finalizeRegistration):void 0,this.timer=new u(this.finalizeRegistration),this.finalizeAllImmediately=()=>{this.flushRegistrations(),this.timer.finalizeAllImmediately()},this.flushRegistrations=()=>{this.flushScheduled=!1,this.staged.forEach(({target:t,value:e},i)=>{var n;const r={value:e,token:{}};this.tokens.set(i,r.token),null==(n=this.native)||n.register(t,r,r.token),this.timer.register(t,r,r.token)}),this.staged.clear()},this.finalize=t}register(t,e,i=t){this.unregister(i),this.staged.set(i,{target:t,value:e}),this.flushScheduled||(this.flushScheduled=!0,Promise.resolve().then(this.flushRegistrations))}unregister(t){if(this.staged.delete(t))return;const e=this.tokens.get(t);var i;e&&(this.tokens.delete(t),null==(i=this.native)||i.unregister(e),this.timer.unregister(e))}}:u)(t=>{const e=t.reaction;t.reaction=null,null==e||e.dispose()});let c=!1;var f,d;const b="function"==typeof Symbol&&Symbol.for,m=null!=(f=null==(d=Object.getOwnPropertyDescriptor(()=>{},"name"))?void 0:d.configurable)&&f,v=b?Symbol.for("react.forward_ref"):"function"==typeof i.forwardRef&&i.forwardRef(()=>null).$$typeof,p=b?Symbol.for("react.memo"):"function"==typeof i.memo&&i.memo(()=>null).$$typeof,y={$$typeof:!0,render:!0,compare:!0,type:!0,displayName:!0};s.displayName="Observer";const w=h.finalizeAllImmediately;t.Observer=s,t._observerFinalizationRegistry=h,t.clearTimers=w,t.enableStaticRendering=function(t){c=t},t.isUsingStaticRendering=n,t.observer=function(t){if(p&&t.$$typeof===p)throw new Error("[mobx-react-lite] You are trying to use `observer` on a function component wrapped in either another `observer` or `React.memo`. The observer already applies 'React.memo' for you.");if(n())return t;let e=!1,r=t;const s=t.displayName||t.name;if(v&&t.$$typeof===v&&(e=!0,r=t.render,"function"!=typeof r))throw new Error("[mobx-react-lite] `render` property of ForwardRef was not a function");let l=(t,e)=>o(()=>r(t,e),s);var a,u;return l.displayName=t.displayName,m&&Object.defineProperty(l,"name",{value:t.name,writable:!0,configurable:!0}),e&&(l=i.forwardRef(l)),l=i.memo(l),a=t,u=l,Object.keys(a).forEach(t=>{y[t]||Object.defineProperty(u,t,Object.getOwnPropertyDescriptor(a,t))}),l},t.useLocalObservable=function(t,n){return i.useState(()=>e.observable(t(),n,{autoBind:!0}))[0]},Object.defineProperty(t,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("mobx"),require("react")):"function"==typeof define&&define.amd?define(["exports","mobx","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mobxReactLite={},t.mobx,t.React);
//# sourceMappingURL=mobxreactlite.umd.production.min.js.map