UNPKG

mobx-react-lite

Version:

Lightweight React bindings for MobX based on function components and Hooks

3 lines (2 loc) • 4.76 kB
function e(e){w=e}function t(){return w}function i(e){e.reaction=new h(`observer${e.name}`,()=>{e.stateVersion=Symbol(),null==e.onStoreChange||e.onStoreChange()})}function r(e,r="observed"){if(t())return e();const n=c.useRef(null);n.current||(n.current=function(e){const t={reaction:null,onStoreChange:null,stateVersion:Symbol(),name:e,subscribe:e=>(p.unregister(t),t.onStoreChange=e,t.reaction||(i(t),t.stateVersion=Symbol()),()=>{var e;t.onStoreChange=null,null==(e=t.reaction)||e.dispose(),t.reaction=null}),getSnapshot:()=>t.stateVersion};return t}(r));const s=n.current;let o,a;if(s.reaction||(i(s),p.register(n,s,s)),c.useDebugValue(s.reaction,l),c.useSyncExternalStore(s.subscribe,s.getSnapshot,s.getSnapshot),s.reaction.track(()=>{try{o=e()}catch(e){a=e}}),a)throw a;return o}function n(e){if(T&&e.$$typeof===T)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(t())return e;let i=!1,n=e;const s=e.displayName||e.name;if(x&&e.$$typeof===x&&(i=!0,n=e.render,"function"!=typeof n))throw new Error("[mobx-react-lite] `render` property of ForwardRef was not a function");let o=(e,t)=>r(()=>n(e,t),s);var a,l;return o.displayName=e.displayName,O&&Object.defineProperty(o,"name",{value:e.name,writable:!0,configurable:!0}),i&&(o=m(o)),o=v(o),a=e,l=o,Object.keys(a).forEach(e=>{j[e]||Object.defineProperty(l,e,Object.getOwnPropertyDescriptor(a,e))}),o}function s({children:e,render:t}){e&&t&&console.error("MobX Observer: Do not use children and render in the same time in `Observer`");const i=e||t;return"function"!=typeof i?null:r(i)}function o(e,t){return f(()=>u(e(),t,{autoBind:!0}))[0]}import{_getGlobalState as a,getDependencyTree as l,Reaction as h,observable as u}from"mobx";import c,{useState as f,useSyncExternalStore as b,forwardRef as m,memo as v}from"react";var d;if(!f||!b)throw new Error("mobx-react-lite requires React 18 or later");if(!((null==a||null==(d=a())?void 0:d.version)>=7))throw new Error("mobx-react-lite requires mobx at least version 7 to be available");class y{constructor(e){this.finalize=void 0,this.registrations=new Map,this.sweepTimeout=void 0,this.sweep=(e=1e4)=>{clearTimeout(this.sweepTimeout),this.sweepTimeout=void 0;const t=Date.now();this.registrations.forEach((i,r)=>{t-i.registeredAt>=e&&(this.registrations.delete(r),this.finalize(i.value))}),this.registrations.size>0&&this.scheduleSweep()},this.finalizeAllImmediately=()=>{this.sweep(0)},this.finalize=e}register(e,t,i){this.registrations.set(i,{value:t,registeredAt:Date.now()}),this.scheduleSweep()}unregister(e){this.registrations.delete(e),0===this.registrations.size&&(clearTimeout(this.sweepTimeout),this.sweepTimeout=void 0)}scheduleSweep(){void 0===this.sweepTimeout&&(this.sweepTimeout=setTimeout(this.sweep,1e4))}}const p=new("undefined"!=typeof FinalizationRegistry?class{constructor(e){this.finalize=void 0,this.finalizeRegistration=e=>{var t;null==(t=this.native)||t.unregister(e.token),this.timer.unregister(e.token),this.finalize(e.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 y(this.finalizeRegistration),this.finalizeAllImmediately=()=>{this.flushRegistrations(),this.timer.finalizeAllImmediately()},this.flushRegistrations=()=>{this.flushScheduled=!1,this.staged.forEach(({target:e,value:t},i)=>{var r;const n={value:t,token:{}};this.tokens.set(i,n.token),null==(r=this.native)||r.register(e,n,n.token),this.timer.register(e,n,n.token)}),this.staged.clear()},this.finalize=e}register(e,t,i=e){this.unregister(i),this.staged.set(i,{target:e,value:t}),this.flushScheduled||(this.flushScheduled=!0,Promise.resolve().then(this.flushRegistrations))}unregister(e){if(this.staged.delete(e))return;const t=this.tokens.get(e);var i;t&&(this.tokens.delete(e),null==(i=this.native)||i.unregister(t),this.timer.unregister(t))}}:y)(e=>{const t=e.reaction;e.reaction=null,null==t||t.dispose()});let w=!1;var g,S;const R="function"==typeof Symbol&&Symbol.for,O=null!=(g=null==(S=Object.getOwnPropertyDescriptor(()=>{},"name"))?void 0:S.configurable)&&g,x=R?Symbol.for("react.forward_ref"):"function"==typeof m&&m(()=>null).$$typeof,T=R?Symbol.for("react.memo"):"function"==typeof v&&v(()=>null).$$typeof,j={$$typeof:!0,render:!0,compare:!0,type:!0,displayName:!0};s.displayName="Observer";const E=p.finalizeAllImmediately;export{s as Observer,p as _observerFinalizationRegistry,E as clearTimers,e as enableStaticRendering,t as isUsingStaticRendering,n as observer,o as useLocalObservable}; //# sourceMappingURL=mobxreactlite.esm.production.min.js.map