UNPKG

mobx-react-lite

Version:

Lightweight React bindings for MobX based on function components and Hooks

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