@zedux/react
Version:
A Molecular State Engine for React
3 lines (2 loc) • 40 kB
JavaScript
(function(u,_){typeof exports=="object"&&typeof module<"u"?_(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],_):(u=typeof globalThis<"u"?globalThis:u||self,_(u.ZeduxReact={},u.React))})(this,function(u,_){"use strict";const Le=i=>{const e=Object.getPrototypeOf(i);return e?Object.getPrototypeOf(e)?"complex object":"object":"prototype-less object"},Fe=i=>{const e=typeof i;return e!=="object"?e:i?Array.isArray(i)?"array":Le(i):"null"},C=i=>{const e=t=>{const s={type:e.type};return typeof t<"u"&&(s.payload=t),s};return e.type=i,e},ae=(i,e)=>typeof i=="string"?i:i.type,Ke=(i,e)=>i.map(t=>ae(t)),Ve=i=>{const e={},t=(s=i,n)=>{const r=e[n.type]||[];return Ye(r,s,n)};return t.reduce=(s,n)=>{const r=Array.isArray(s)?Ke(s):[ae(s)];return Ze(e,r,n),t},t},Ze=(i,e,t)=>{e.forEach(s=>{i[s]||(i[s]=[]),i[s].push(t)})},Ye=(i,e,t)=>i.reduce((s,n)=>n(s,t.payload,t),e),m=(i,e)=>{var t;return((t=i==null?void 0:i.constructor)===null||t===void 0?void 0:t.$$typeof)===e.$$typeof},H=i=>{if(typeof i!="object"||!i)return!1;const e=Object.getPrototypeOf(i);return e?Object.getPrototypeOf(e)===null:!1},I="@@zedux/",p={batch:`${I}batch`,delegate:`${I}delegate`,hydrate:`${I}hydrate`,ignore:`${I}ignore`,inherit:`${I}inherit`,merge:`${I}merge`,prime:`${I}prime`},O=1,L=2,de=3,ue=4,qe=Symbol.for("zedux.store"),Xe=(i,e,t)=>{const s={};return Object.entries(i).forEach(([n,r])=>{const o=[...t,n];s[n]=he(r,e,o)}),s},Qe=(i,e,t,s)=>i===L?{type:i}:i===de?{type:i,reducer:e}:{type:i,destroy:t(s,e),reducer:et(e),store:e},Je=i=>typeof i=="function"?de:i&&m(i,E)?ue:H(i)?O:L,he=(i,e,t=[])=>{const s=Je(i);return s!==O?Qe(s,i,e,t):{type:s,children:Xe(i,e,t)}},et=i=>(t,s)=>{(s.type===p.hydrate||s.type===p.merge)&&(s={type:p.hydrate,payload:t});const n={metaType:p.inherit,payload:s};return i.dispatch(n)},tt=(i,{create:e,get:t,isNode:s,set:n,size:r})=>(o=e(),c)=>{let a=e(),d=!1;return Object.keys(i).forEach(h=>{const{reducer:l}=i[h],f=s(o)?t(o,h):void 0,v=l(f,c);a=n(a,h,v),d||(d=v!==f)}),d||(d=!s(o)||!Object.keys(i).length&&!!r(o)),d?a:o},V=i=>{if(!i)return;const{children:e,destroy:t}=i;t&&t(),e&&Object.values(e).forEach(V)},le=(i,e,t)=>{const s=Object.assign({},i.children);return Object.keys(e.children).forEach(n=>{var r;const o=e.children[n],c=(r=i.children)===null||r===void 0?void 0:r[n],a=fe(c,o,t);if(a.type===L){delete s[n];return}s[n]=a}),{children:s,reducer:tt(s,t),type:O}},fe=(i,e,t)=>e.type!==O?(V(i),e):!i||i.type!==O?(V(i),le({type:L},e,t)):le(i,e,t),_e=(i,e,t)=>{if(!t.isNode(i)||!t.isNode(e))return[e,e!==i];let s=!1;const n=t.clone(i);return t.iterate(e,(r,o)=>{const c=t.get(n,r),[a,d]=t.isNode(o)?_e(c,o,t):[o,o!==c];d&&(s||(s=d),t.set(n,r,a))}),[s?n:i,s]},st=(i,e,t)=>!e||!t?i.payload:(e.payload=i.payload,t),pe=(i,e)=>{for(;i.metaType;){if(i.metaType===e)return i.metaData;i=i.payload}},ye=i=>{for(;i.metaType;)i=i.payload;return i},ge=(i,e)=>{let t=i,s=null,n=null;for(;t.metaType;){if(t.metaType===e)return st(t,s,n);const r=Object.assign({},t);s&&(s.payload=r),s=r,t=t.payload,n||(n=s)}return i},Z="Minified Error",nt=(i,e)=>{for(const t of e){if(i.type!==O)throw new ReferenceError(Z);if(i=i.children[t],!i)throw new ReferenceError(Z)}return i},it=(i,e)=>{const t=pe(e,p.delegate);if(!t||!i)return!1;const s=nt(i,t);if(s.type!==ue)throw new TypeError(Z);s.store.dispatch(ge(e,p.delegate))},me=(i,e,t,s)=>{if(!e.length)return t;const n=s.clone(i),r=e[0];return s.set(n,r,me(s.get(i,r),e.slice(1),t,s))},rt=Object.freeze(Object.defineProperty({__proto__:null,clone:i=>Object.assign({},i),create:()=>({}),get:(i,e)=>i[e],isNode:H,iterate:(i,e)=>{Object.entries(i).forEach(([t,s])=>e(t,s))},set:(i,e,t)=>(i[e]=t,i),size:i=>Object.keys(i).length},Symbol.toStringTag,{value:"Module"})),ot={scheduleNow:i=>i.task()},ve={type:p.prime},T=(i,e)=>new E(i,e);class E{constructor(e,t){this._isSolo=!0,this._subscribers=[],this.dispatch=s=>this._isSolo?this._dispatch(s):(this._scheduler.scheduleNow({task:()=>this._dispatch(s),type:0}),this._state),this.setState=(s,n)=>this._isSolo?this._setState(s,n):(this._scheduler.scheduleNow({task:()=>this._setState(s,n),type:0}),this._state),this._state=t,this._scheduler=E._scheduler||ot,e&&this.use(e)}actionStream(){return{[Symbol.observable](){return this},"@@observable"(){return this},subscribe:e=>this.subscribe({effects:({action:t,error:s})=>{var n,r;s&&typeof e!="function"?(n=e.error)===null||n===void 0||n.call(e,s):t&&(typeof e=="function"?e(t):(r=e.next)===null||r===void 0||r.call(e,t))}})}}getState(){return this._state}setStateDeep(e,t){return this._isSolo?this._setState(e,t,!0):(this._scheduler.scheduleNow({task:()=>this._setState(e,t,!0),type:0}),this._state)}subscribe(e){const t=typeof e=="function"?{next:e}:e;return this._subscribers.push(t),{unsubscribe:()=>{this._subscribers=this._subscribers.filter(n=>n!==t)}}}use(e){const t=he(e,(n,r)=>this._registerChildStore(n,r)),s=this._tree=fe(this._tree,t,this.constructor.hierarchyConfig);return(this._rootReducer=s.reducer)&&this._dispatchAction(ve,ve,this._state),this}_register(e){const t=this._parents||(this._parents=[]);return t.push(e),this._isSolo=!1,()=>{const s=t.indexOf(e);s>-1&&t.splice(s,1)}}[Symbol.observable](){return this}"@@observable"(){return this}_dispatch(e){return it(this._tree,e)!==!1?this._state:this._routeAction(e)}_dispatchAction(e,t,s){this._isDispatching=!0;let n,r=s;try{this._rootReducer&&(r=this._rootReducer(s,t))}catch(o){throw n=o,o}finally{this._isDispatching=!1,this._notify(r,e,n)}return r}_dispatchHydration(e,t,s){const n=t===p.hydrate?e:_e(this._state,e,this.constructor.hierarchyConfig)[0];if(n===this._state&&!s)return this._state;const r={meta:s,payload:n,type:t};return this._dispatchAction(r,r,n)}_dispatchStateSetter(e,t,s){let n;try{n=e(this._state)}catch(r){throw r}return this._dispatchHydration(n,s?p.merge:p.hydrate,t)}_doNotify(e){const{_subscribers:t}=this,s=e.newState!==e.oldState;for(const n of t)e.error&&n.error&&n.error(e.error),s&&n.next&&n.next(e.newState,e.oldState,e.action),n.effects&&n.effects(e)}_notify(e,t,s){var n;const r={action:t,error:s,newState:e,oldState:this._state,store:this};if(this._state=e,this._isSolo)return this._doNotify(r);this._scheduler.scheduleNow({task:()=>this._doNotify(r),type:1}),(n=this._parents)===null||n===void 0||n.forEach(o=>o(r))}_registerChildStore(e,t){this._isSolo=!1;const s=({action:n,error:r,newState:o,oldState:c})=>{if(this._isDispatching)return;const a=o===c?this._state:me(this._state,e,o,this.constructor.hierarchyConfig),d={metaType:p.delegate,metaData:e,payload:n};this._notify(a,d,r)};return t._register(s)}_routeAction(e){const t=ye(e);return t.type===p.hydrate||t.type===p.merge?this._dispatchHydration(t.payload,t.type,t.meta):this._dispatchAction(e,t,this._state)}_setState(e,t,s=!1){return typeof e=="function"?this._dispatchStateSetter(e,t,s):this._dispatchHydration(e,s?p.merge:p.hydrate,t)}}E.hierarchyConfig=rt,E.$$typeof=qe;const S={ecosystemWiped:C("ecosystemWiped"),edgeCreated:C("edgeCreated"),edgeRemoved:C("edgeRemoved"),evaluationFinished:C("evaluationFinished"),instanceReused:C("instanceReused"),stateChanged:C("stateChanged"),statusChanged:C("statusChanged")},Se=i=>({error:i,isError:!0,isLoading:!1,isSuccess:!1,status:"error"}),Y=i=>({data:i,isError:!1,isLoading:!0,isSuccess:!1,status:"loading"}),be=i=>({data:i,isError:!1,isLoading:!1,isSuccess:!0,status:"success"}),q=1,N=2,w=4,F=(i,e)=>!i||!e||i.length!==e.length||i.some((t,s)=>e[s]!==t),j="@@zedux";class ${constructor(e){this.promise=void 0,this.value=e,this.store=m(e,E)?e:void 0,m(e,$)&&Object.assign(this,e)}addExports(e){return this.exports?this.exports=Object.assign(Object.assign({},this.exports),e):this.exports=e,this}setExports(e){return this.exports=e,this}setPromise(e){return this.promise=e,this}setTtl(e){return this.ttl=e,this}}$.$$typeof=Symbol.for(`${j}/AtomApi`);class k{addDependent({callback:e,operation:t="addDependent"}={}){const s=this.ecosystem._idGenerator.generateNodeId();return this.ecosystem._graph.addEdge(s,this.id,t,q|N,e),()=>this.ecosystem._graph.removeEdge(s,this.id)}}k.$$typeof=Symbol.for(`${j}/AtomInstanceBase`);const Ee=1,X=2,je=i=>m(i,E)?Ee:X,ct=i=>{const e=je(i),t=e===Ee?i:T();return e===X&&t.setState(typeof i=="function"?()=>i:i),[e,t]};class Q extends k{constructor(e,t,s,n){super(),this.ecosystem=e,this.template=t,this.id=s,this.params=n,this.status="Initializing",this.nextReasons=[],this.dispatch=r=>this.store.dispatch(r),this.setState=(r,o)=>this.store.setState(r,o),this.setStateDeep=(r,o)=>this.store.setStateDeep(r,o),this._scheduleEvaluation=(r,o)=>{this.status!=="Destroyed"&&(this.nextReasons.push(r),!(this.nextReasons.length>1)&&this.ecosystem._scheduler.schedule({id:this.id,task:this.evaluationTask,type:2},o))},this.evaluationTask=()=>this._evaluationTask(),this._createdAt=e._idGenerator.now(),this.exports=this.exports,this.promise=this.promise,this.store=this.store,this._promiseStatus=this._promiseStatus}destroy(e){var t,s,n,r;if(this.status==="Destroyed"||!e&&(!((t=this.ecosystem._graph.nodes[this.id])===null||t===void 0)&&t.refCount))return;(s=this._cancelDestruction)===null||s===void 0||s.call(this),this._cancelDestruction=void 0,this._setStatus("Destroyed"),this.nextReasons.length&&this.ecosystem._scheduler.unschedule(this.evaluationTask);const o=[];(n=this._injectors)===null||n===void 0||n.forEach(c=>{var a;if(c.type!=="@@zedux/effect"){o.push(c);return}(a=c.cleanup)===null||a===void 0||a.call(c)}),o.forEach(c=>{var a;(a=c.cleanup)===null||a===void 0||a.call(c)}),this.ecosystem._graph.removeDependencies(this.id),(r=this._subscription)===null||r===void 0||r.unsubscribe(),this.ecosystem._destroyAtomInstance(this.id)}getState(){return this.store.getState()}invalidate(e="invalidate",t="External"){this._scheduleEvaluation({operation:e,sourceType:t,type:"cache invalidated"},!1),this.ecosystem._scheduler.flush()}get _infusedSetter(){if(this._set)return this._set;const e=(t,s)=>this.setState(t,s);return this._set=Object.assign(e,this.exports)}_init(){const e=this._doEvaluate();[this._stateType,this.store]=ct(e),this._subscription=this.store.subscribe((s,n,r)=>{if(this._isEvaluating){this._bufferedUpdate={newState:s,oldState:n,action:r};return}this._handleStateChange(s,n,r)}),this._setStatus("Active"),this.ecosystem._graph.flushUpdates();const t=this.ecosystem._consumeHydration(this);this.template.manualHydration||typeof t>"u"||this.store.setState(t)}_scheduleDestruction(){if(this.status!=="Active")return;this._setStatus("Stale");const e=this._getTtl();if(e==null||e===-1)return;if(e===0)return this.destroy();if(typeof e=="number"){const s=setTimeout(()=>{this._cancelDestruction=void 0,this.destroy()},e);this._cancelDestruction=()=>{this._setStatus("Active"),this._cancelDestruction=void 0,clearTimeout(s)};return}if(typeof e.then=="function"){let s=!1;Promise.allSettled([e]).then(()=>{this._cancelDestruction=void 0,s||this.destroy()}),this._cancelDestruction=()=>{this._setStatus("Active"),this._cancelDestruction=void 0,s=!0};return}const t=e.subscribe(()=>{this._cancelDestruction=void 0,this.destroy()});this._cancelDestruction=()=>{this._setStatus("Active"),this._cancelDestruction=void 0,t.unsubscribe()}}_doEvaluate(){const{_evaluationStack:e,_graph:t}=this.ecosystem;this._nextInjectors=[];let s;e.start(this),this._isEvaluating=!0,t.bufferUpdates(this.id);try{s=this._evaluate()}catch(n){throw this._nextInjectors.forEach(r=>{var o;(o=r.cleanup)===null||o===void 0||o.call(r)}),this._nextInjectors=void 0,t.destroyBuffer(),n}finally{e.finish(),this._isEvaluating=!1,this._bufferedUpdate&&(this._handleStateChange(this._bufferedUpdate.newState,this._bufferedUpdate.oldState,this._bufferedUpdate.action),this._bufferedUpdate=void 0),this.prevReasons=this.nextReasons,this.nextReasons=[]}return this._injectors=this._nextInjectors,this._nextInjectors=void 0,this.status!=="Initializing"&&t.flushUpdates(),s}_evaluate(){var e;const{_value:t}=this.template;if(typeof t!="function")return t;try{const s=t(...this.params);if(!m(s,$))return s;const n=this.api=s;return this.status==="Initializing"&&n.exports&&(this.exports=n.exports),typeof((e=n.value)===null||e===void 0?void 0:e.then)=="function"?this._setPromise(n.value,!0):(n.promise&&this._setPromise(n.promise),n.value)}catch(s){throw console.error(`Zedux: Error while evaluating atom "${this.template.key}" with params:`,this.params,s),s}}_evaluationTask(){const e=this._doEvaluate();je(e)===X&&this.store.setState(typeof e=="function"?()=>e:e)}_getTtl(){var e,t,s;if(((e=this.api)===null||e===void 0?void 0:e.ttl)==null)return(t=this.template.ttl)!==null&&t!==void 0?t:(s=this.ecosystem.atomDefaults)===null||s===void 0?void 0:s.ttl;const{ttl:n}=this.api;return typeof n=="function"?n():n}_handleStateChange(e,t,s){this.ecosystem._graph.scheduleDependents(this.id,this.nextReasons,e,t,!1),this.ecosystem._mods.stateChanged&&this.ecosystem.modBus.dispatch(S.stateChanged({action:s,instance:this,newState:e,oldState:t,reasons:this.nextReasons})),s.meta!==p.batch&&this.ecosystem._scheduler.flush()}_setStatus(e){const t=this.status;this.status=e,this.ecosystem._mods.statusChanged&&this.ecosystem.modBus.dispatch(S.statusChanged({newStatus:e,node:this,oldStatus:t}))}_setPromise(e,t){var s;const n=(s=this.store)===null||s===void 0?void 0:s.getState();if(e===this.promise)return n;this.promise=e,e.then(o=>{this.promise===e&&(this._promiseStatus="success",t&&this.store.setState(be(o)))}).catch(o=>{this.promise===e&&(this._promiseStatus="error",this._promiseError=o,t&&this.store.setState(Se(o)))});const r=Y(n==null?void 0:n.data);return this._promiseStatus=r.status,this.ecosystem._graph.scheduleDependents(this.id,this.nextReasons,void 0,void 0,!0,"promise changed","Updated",!0),r}}const Ae=(i,e,t)=>new Ce(i,e,t);let g=[];const x=()=>g[g.length-1].node,at=i=>g=i;class dt{constructor(e){this.ecosystem=e;const{_graph:t,selectors:s}=e,n=(c,a)=>{const{id:d,store:h}=e.getInstance(c,a);return g.length&&t.addEdge(g[g.length-1].node.id,d,"get",0),h.getState()},r=(c,a,d)=>{var h;const l=e.getInstance(c,a);return g.length&&t.addEdge(g[g.length-1].node.id,l.id,(d==null?void 0:d[1])||"getInstance",(h=d==null?void 0:d[0])!==null&&h!==void 0?h:w),l},o=(c,...a)=>{if(!g.length)return e.select(c,...a);const d=s.getCache(c,a);return t.addEdge(g[g.length-1].node.id,d.id,"select",0),d.result};this.atomGetters={ecosystem:e,get:n,getInstance:r,select:o}}finish(){const e=g.pop(),{_idGenerator:t,_mods:s,modBus:n}=this.ecosystem;if(g.length||(E._scheduler=void 0),!e||!s.evaluationFinished)return;const r=e.start?t.now(!0)-e.start:0,o={node:e.node,time:r};n.dispatch(S.evaluationFinished(o))}read(){return g[g.length-1]}start(e){const{_idGenerator:t,_mods:s,_scheduler:n}=this.ecosystem,r={node:e,start:s.evaluationFinished?t.now(!0):void 0};g.push(r),E._scheduler=n}}const J=()=>x().ecosystem._evaluationStack.atomGetters;class K{constructor(e,t,s){this.key=e,this._value=t,this._config=s,Object.assign(this,s)}}K.$$typeof=Symbol.for(`${j}/AtomTemplateBase`);class Ce extends K{constructor(e,t,s){super(e,(...n)=>t(J(),...n),s),this._get=t}_createInstance(e,t,s){return new Q(e,this,t,s)}getInstanceId(e,t){const s=this.key;return t!=null&&t.length?`${s}-${e._idGenerator.hashParams(t,e.complexParams)}`:s}override(e){const t=Ae(this.key,e||this._get,this._config);return t._isOverride=!0,t}}const Ie=(i,e,t)=>new we(i,e,t);class we extends K{_createInstance(e,t,s){return new Q(e,this,t,s)}getInstanceId(e,t){const s=this.key;return t!=null&&t.length?`${s}-${e._idGenerator.hashParams(t,e.complexParams)}`:s}override(e){const t=Ie(this.key,e,this._config);return t._isOverride=!0,t}}let R=T(null,{});const ee=i=>R.getState()[i],ut=()=>({stack:g,store:R}),ht=i=>{R=i.store,at(i.stack)},lt=()=>{Object.values(R.getState().ecosystems).forEach(e=>{e.destroy(!0)})},te=({ecosystem:i,nodes:e},t)=>{const s=e[t];s&&!s.refCount&&(s.isSelector?i.selectors._destroySelector(t):i._instances[t]._scheduleDestruction())},ft=({ecosystem:i,nodes:e},t)=>{var s,n;e[t].isSelector||(n=(s=i._instances[t])._cancelDestruction)===null||n===void 0||n.call(s)};class _t{constructor(e){this.ecosystem=e,this.nodes={},this.updateStack=[]}addEdge(e,t,s,n,r){const{ecosystem:o,updateStack:c}=this,a={callback:r,createdAt:o._idGenerator.now(),flags:n,operation:s};if(!c.length)return this.finishAddingEdge(e,t,a);const{dependencies:d,key:h}=c[c.length-1],l=d.get(t);return!l||l.flags>n?(d.set(t,a),a):l}addNode(e,t){this.nodes[e]||(this.nodes[e]={dependencies:new Map,dependents:new Map,isSelector:t,refCount:0,weight:1})}bufferUpdates(e){this.updateStack.push({key:e,dependencies:new Map})}destroyBuffer(){const{dependencies:e,key:t}=this.updateStack[this.updateStack.length-1],s=this.nodes[t].dependencies;for(const n of e.keys())s.get(n)||te(this,n);this.updateStack.pop()}flushUpdates(){const{nodes:e,updateStack:t}=this,{dependencies:s,key:n}=t[t.length-1],r=e[n].dependencies;for(const o of r.keys()){const c=e[o].dependents.get(n);if(c.flags&(q|N))continue;const a=s.get(o);(!a||a.flags!==c.flags)&&this.removeEdge(n,o)}for(const[o,c]of s.entries())r.get(o)||this.finishAddingEdge(n,o,c);t.pop()}removeDependencies(e){const t=this.nodes[e];if(!t)return;const s=t.dependencies;if(s)for(const n of s.keys())this.removeEdge(e,n)}removeEdge(e,t){const s=this.nodes[t],n=this.nodes[e];if(n&&n.dependencies.delete(t),!s)return;const r=s.dependents.get(e);if(!r){s.isSelector&&te(this,t);return}s.dependents.delete(e),s.refCount--,r.flags&w||this.recalculateNodeWeight(e,-s.weight);const{_instances:o,_mods:c,_scheduler:a,modBus:d,selectors:h}=this.ecosystem;r.task&&a.unschedule(r.task),c.edgeRemoved&&d.dispatch(S.edgeRemoved({dependency:o[t]||h._items[t],dependent:o[e]||h._items[e]||e,edge:r})),te(this,t)}removeNode(e){const t=this.nodes[e];if(t){this.scheduleDependents(e,[],void 0,void 0,!0,"node destroyed","Destroyed",!0);for(const[s,n]of t.dependents.entries()){n.flags&w||this.recalculateNodeWeight(s,-t.weight);const r=this.nodes[s];r&&r.dependencies.delete(e)}delete this.nodes[e]}}scheduleDependents(e,t,s,n,r,o="state changed",c="Updated",a=!1){const{_instances:d,_scheduler:h,selectors:l}=this.ecosystem,f=d[e],v=l._items[e],b=this.nodes[e];for(const[M,y]of b.dependents.entries()){if(y.task){if(c!=="Destroyed")continue;h.unschedule(y.task)}if(y.flags&w&&!a)continue;const D={newState:s,oldState:n,operation:y.operation,reasons:t,sourceId:e,sourceType:b.isSelector?"AtomSelector":"Atom",type:o};if(!(y.flags&N)){if(this.nodes[M].isSelector){l._scheduleEvaluation(M,D,r);continue}d[M]._scheduleEvaluation(D,r);continue}const W=()=>{var B;y.task=void 0,(B=y.callback)===null||B===void 0||B.call(y,c,f?f.store.getState():v.result,D)};h.schedule({flags:y.flags,task:W,type:3},r),y.task=W}}finishAddingEdge(e,t,s){const n=this.nodes[t];if(!n)return;s.flags&N||this.nodes[e].dependencies.set(t,!0),n.dependents.set(e,s),n.refCount++,ft(this,t),s.flags&w||this.recalculateNodeWeight(e,n.weight);const{_instances:r,_mods:o,modBus:c,selectors:a}=this.ecosystem;return o.edgeCreated&&c.dispatch(S.edgeCreated({dependency:r[t]||a._items[t],dependent:r[e]||a._items[e]||e,edge:s})),s}recalculateNodeWeight(e,t){const s=this.nodes[e];if(s){s.weight+=t;for(const n of s.dependents.keys())this.recalculateNodeWeight(n,t)}}}class pt{constructor(){this.idCounter=0,this.weakCache=new WeakMap,this.generateId=e=>`${e}-${++this.idCounter}${Math.random().toString(36).slice(2,8)}`}generateNodeId(){return this.generateId("no")}hashParams(e,t){return JSON.stringify(e,(s,n)=>m(n,k)?n.id:n&&(H(n)?Object.keys(n).sort().reduce((r,o)=>(r[o]=n[o],r),{}):!t||Array.isArray(n)?n:typeof n=="function"?this.cacheFn(n):typeof(n==null?void 0:n.constructor)=="function"?this.cacheClass(n):n))}now(e){return e&&typeof performance<"u"?performance.now():Date.now()}cacheClass(e){let t=this.weakCache.get(e);return t||(t=this.generateId(e.constructor.name||"UnknownClass"),this.weakCache.set(e,t),t)}cacheFn(e){let t=this.weakCache.get(e);return t||(t=this.generateId(e.name||"anonFn"),this.weakCache.set(e,t),t)}}class yt{constructor(e){this.ecosystem=e,this.jobs=[],this.nows=[]}flush(){this._isRunning||(this._handle&&(clearTimeout(this._handle),this._handle=void 0),this.runJobs())}schedule(e,t=!0){e.type===4?this.jobs.push(e):this.insertJob(e),t&&this.jobs.length===1&&this.setTimeout()}scheduleNow(e){if(this._isRunningNows&&e.type===0)return e.task();this.nows[e.type===1?"push":"unshift"](e),this.runJobs(!0)}unschedule(e){const t=this.jobs.findIndex(s=>s.task===e);t!==-1&&this.jobs.splice(t,1)}wipe(){this.jobs=this.jobs.filter(e=>e.type===3)}findIndex(e,t=Math.ceil(this.jobs.length/2)-1,s=1){const n=this.jobs[t];if(n==null)return t;const r=e(n);if(!r)return t;const o=Math.pow(2,s);if(o>this.jobs.length)return t+(r===1?1:0);const a=Math.round(this.jobs.length/o),d=Math.min(this.jobs.length-1,Math.max(0,t+Math.ceil(a/2)*r));return this.findIndex(e,d,s+1)}insertJob(e){var t;const{nodes:s}=this.ecosystem._graph,n=(t=e.flags)!==null&&t!==void 0?t:0,r=e.id?s[e.id].weight:0,o=this.findIndex(c=>{if(c.type!==e.type)return+(e.type-c.type>0)||-1;if(c.id){const a=s[c.id].weight;return r<a?-1:+(r>a)}return n<c.flags?-1:+(n>c.flags)});o===-1?this.jobs.push(e):this.jobs.splice(o,0,e)}runJobs(e){if(this._isRunningNows){this._runAfterNows=!e;return}const t=e?this.nows:this.jobs,s=e?"_isRunningNows":"_isRunning",n=this.nows;this[s]=!0;try{for(;t.length;)(n.length?n:t).shift().task()}finally{this[s]=!1}this._runAfterNows&&(this._runAfterNows=!1,this.runJobs())}setTimeout(){this._isRunning||(this._handle=setTimeout(()=>{this._handle=void 0,this.runJobs()}))}}const gt=(i,e)=>i===e;class A{constructor(e,t,s){this.id=e,this.selectorRef=t,this.args=s,this.nextReasons=[]}}A.$$typeof=Symbol.for(`${j}/SelectorCache`);class mt{constructor(e){this.ecosystem=e,this._items={},this._refBaseKeys=new WeakMap}addDependent(e,{callback:t,operation:s="addDependent"}={}){const{_graph:n,_idGenerator:r}=this.ecosystem,o=r.generateNodeId();return n.addEdge(o,e.id,s,q|N,t),()=>n.removeEdge(o,e.id)}dehydrate(e){const t=this.findAll(e);return Object.keys(t).forEach(s=>{t[s]=t[s].result}),t}destroyCache(e,t,s){const n=m(e,A)?e.id:this.getCacheId(e,t),r=m(e,A)?e:this._items[n];if(!r||r.isDestroyed)return;const o=this.ecosystem._graph.nodes[n];(s||!o.refCount)&&this._destroySelector(n)}find(e,t){if(m(e,A))return e;if(typeof e=="string")return Object.values(this.findAll(e))[0];const s=this.getCacheId(e,t,!0);return s&&this._items[s]}findAll(e=""){const t={},s=typeof e=="string"?e.toLowerCase():m(e,A)?e.id:this.getBaseKey(e,!0)||this._getIdealCacheId(e);return Object.values(this._items).sort((n,r)=>n.id.localeCompare(r.id)).forEach(n=>{(!s||n.id.toLowerCase().includes(s))&&(t[n.id]=n)}),t}getCache(e,t=[]){if(m(e,A))return e;const s=e,n=this.getCacheId(s,t);let r=this._items[n];return r||(r=new A(n,s,t),this._items[n]=r,this.ecosystem._graph.addNode(n,!0),this.runSelector(n,t,!0),r)}getCacheId(e,t,s){const{complexParams:n,_idGenerator:r}=this.ecosystem,o=t!=null&&t.length?r.hashParams(t,n):"",c=this.getBaseKey(e,s);return o?`${c}-${o}`:c}_destroySelector(e){const t=this._items[e];if(!t)return;const{_graph:s,_scheduler:n,_mods:r,modBus:o}=this.ecosystem;t.nextReasons.length&&t.task&&n.unschedule(t.task),s.removeDependencies(e),s.removeNode(e),delete this._items[e],t.isDestroyed=!0,r.statusChanged&&o.dispatch(S.statusChanged({newStatus:"Destroyed",node:t,oldStatus:"Active"}))}_getIdealCacheId(e){var t;const s=e.name||((t=e.selector)===null||t===void 0?void 0:t.name);return s!=="selector"&&s||void 0}_scheduleEvaluation(e,t,s){const n=this._items[e];if(n.nextReasons.push(t),n.nextReasons.length>1)return;const r=()=>{n.task=void 0,this.runSelector(e,n.args)};n.task=r,this.ecosystem._scheduler.schedule({id:e,task:r,type:2},s)}_swapRefs(e,t,s=[]){const n=this._refBaseKeys.get(e.selectorRef);n&&(this._refBaseKeys.set(t,n),this._refBaseKeys.delete(e.selectorRef),e.selectorRef=t,this.runSelector(e.id,s,!1,!0))}_wipe(){Object.keys(this._items).forEach(e=>{this._destroySelector(e)}),this._refBaseKeys=new WeakMap}getBaseKey(e,t){const s=this._refBaseKeys.get(e);if(s||t)return s;const n=this._getIdealCacheId(e)||"unnamed",r=this.ecosystem._idGenerator.generateId(`@ -${n}`);return this._refBaseKeys.set(e,r),r}runSelector(e,t,s,n){const{_evaluationStack:r,_graph:o,_mods:c,modBus:a}=this.ecosystem;o.bufferUpdates(e);const d=this._items[e],h=typeof d.selectorRef=="function"?d.selectorRef:d.selectorRef.selector,l=typeof d.selectorRef!="function"&&d.selectorRef.resultsComparator||gt;r.start(d);try{const f=h(r.atomGetters,...t);!s&&!l(f,d.result)?(n||o.scheduleDependents(e,d.nextReasons,f,d.result),c.stateChanged&&a.dispatch(S.stateChanged({cache:d,newState:f,oldState:d.result,reasons:d.nextReasons})),d.result=f):s&&(d.result=f,c.statusChanged&&a.dispatch(S.statusChanged({newStatus:"Active",node:d,oldStatus:"Initializing"})))}catch(f){throw o.destroyBuffer(),console.error(`Zedux encountered an error while running selector with id "${e}":`,f),f}finally{r.finish(),d.prevReasons=d.nextReasons,d.nextReasons=[]}o.flushUpdates()}}const vt=Object.keys(S).reduce((i,e)=>(i[e]=0,i),{}),se=i=>i.reduce((e,t)=>(e[t.key]=t,e),{});class ke{constructor(e){var t;this.modBus=T(),this.overrides={},this.selectors=new mt(this),this._graph=new _t(this),this._evaluationStack=new dt(this),this._idGenerator=new pt,this._instances={},this._mods=Object.assign({},vt),this._refCount=0,this._scheduler=new yt(this),this._storage={},this.isInitialized=!1,this.plugins=[],Object.assign(this,e),this.id||(this.id=this._idGenerator.generateId("es")),e.overrides&&this.setOverrides(e.overrides),this.context=this.context,this.isInitialized=!0,this.cleanup=(t=e.onReady)===null||t===void 0?void 0:t.call(e,this)}addOverrides(e){this.overrides=Object.assign(Object.assign({},this.overrides),se(e)),e.forEach(t=>{const s=this.findAll(t);Object.values(s).forEach(n=>n.destroy(!0))})}batch(e){const t=this._scheduler,s=t._isRunning;t._isRunning=!0;const n=e();return t._isRunning=s,t.flush(),n}dehydrate({exclude:e,excludeFlags:t,include:s,includeFlags:n,transform:r=!0}={}){return Object.values(this._instances).filter(({id:c,template:a})=>e&&e.some(d=>typeof d=="string"?c.toLowerCase().includes(d.toLowerCase()):a.key===d.key)||t&&t.some(d=>{var h;return(h=a.flags)===null||h===void 0?void 0:h.includes(d)})?!1:!!(!s&&!n||s&&s.some(d=>typeof d=="string"?c.toLowerCase().includes(d.toLowerCase()):a.key===d.key)||n&&n.some(d=>{var h;return(h=a.flags)===null||h===void 0?void 0:h.includes(d)}))).reduce((c,{id:a,store:d,template:h})=>{const l=d.getState();return c[a]=r&&h.dehydrate?h.dehydrate(l):l,c},{})}destroy(e){!e&&this._refCount>0||(this.wipe(),!R.getState()[this.id])||(this.plugins.forEach(({cleanup:s})=>s()),this.plugins=[],R.setState(s=>{const n=Object.assign({},s);return delete n[this.id],n}))}find(e,t){const s=typeof e=="string";if(!s){const r=e.getInstanceId(this,t),o=this._instances[r];if(o)return o}if(t)return this._instances[s?e:`${e.key}-${this._idGenerator.hashParams(t,this.complexParams)}`];const n=this.findAll(e);return s&&n[e]||Object.values(n)[0]}findAll(e){var t,s;const n=(t=e)===null||t===void 0?void 0:t.key,r=n||((s=e)===null||s===void 0?void 0:s.toLowerCase()),o={};return Object.values(this._instances).filter(c=>!r||(n?c.template.key===e.key:c.id.toLowerCase().includes(r))).sort((c,a)=>c.id.localeCompare(a.id)).forEach(c=>{o[c.id]=c}),o}get(e,t){return m(e,k)?e.store.getState():this.getInstance(e,t).store.getState()}getInstance(e,t){if(m(e,k))return e.status==="Destroyed"?this.getInstance(e.template,e.params):e;const s=e.getInstanceId(this,t),n=this._instances[s];if(n)return this._mods.instanceReused&&this.modBus.dispatch(S.instanceReused({instance:n,template:e})),n;const r=this.resolveAtom(e);this._graph.addNode(s);const o=r._createInstance(this,s,t||[]);return this._instances[s]=o,o._init(),o}hydrate(e,t){this.hydration=Object.assign(Object.assign({},this.hydration),e),(t==null?void 0:t.retroactive)!==!1&&Object.entries(e).forEach(([s,n])=>{const r=this._instances[s];r&&(r.setState(r.template.hydrate?r.template.hydrate(n):n),delete this.hydration[s])})}registerPlugin(e){if(this.plugins.some(r=>r.plugin===e))return;const t=e.modStore.subscribe((r,o)=>{this.recalculateMods(r,o)}),s=e.registerEcosystem(this),n=()=>{t.unsubscribe(),s&&s()};this.plugins.push({cleanup:n,plugin:e}),this.recalculateMods(e.modStore.getState())}removeOverrides(e){this.overrides=se(Object.values(this.overrides).filter(t=>e.every(s=>(typeof s=="string"?s:s.key)!==t.key))),e.forEach(t=>{const s=this.findAll(t);Object.values(s).forEach(n=>n.destroy(!0))})}reset(e){var t;this.wipe();const s=this.context;typeof e<"u"&&(this.context=e),this.cleanup=(t=this.onReady)===null||t===void 0?void 0:t.call(this,this,s)}select(e,...t){if(m(e,A))return e.result;const s=e,n=this.selectors.find(s,t);return n?n.result:(typeof s=="function"?s:s.selector)({ecosystem:this,get:this.get.bind(this),getInstance:this.getInstance.bind(this),select:this.select.bind(this)},...t)}setOverrides(e){const t=this.overrides;this.overrides=se(e),this.isInitialized&&(e.forEach(s=>{const n=this.findAll(s);Object.values(n).forEach(r=>{r.destroy(!0)})}),Object.values(t).forEach(s=>{const n=this.findAll(s);Object.values(n).forEach(r=>{r.destroy(!0)})}))}unregisterPlugin(e){const t=this.plugins.findIndex(s=>s.plugin===e);t!==-1&&(this.plugins[t].cleanup(),this.plugins.splice(t,1),this.recalculateMods(void 0,e.modStore.getState()))}viewGraph(e){const{nodes:t}=this._graph;if(e!=="top-down"&&e!=="bottom-up"){const r={};return Object.keys(t).forEach(o=>{const c=t[o];r[o]={dependencies:[...c.dependencies.keys()].map(a=>({key:a,operation:t[a].dependents.get(o).operation})),dependents:[...c.dependents.keys()].map(a=>({key:a,operation:c.dependents.get(a).operation})),weight:c.weight}}),r}const s={};Object.keys(t).forEach(r=>{const o=t[r];(e==="bottom-up"?[...o.dependents.values()].every(a=>a.flags&N):!o.dependencies.size)&&(s[r]={})});const n=r=>{if(!r)return;const o=e==="bottom-up"?r.dependencies:r.dependents,c={};for(const a of o.keys()){const d=n(t[a]);d&&(c[a]=d)}return c};return Object.keys(s).forEach(r=>{const o=t[r],c=n(o);c&&(s[r]=c)}),s}why(){var e;return(e=this._evaluationStack.read())===null||e===void 0?void 0:e.node.nextReasons}wipe(){var e;const{_instances:t,_mods:s,_scheduler:n,modBus:r,selectors:o}=this;(e=this.cleanup)===null||e===void 0||e.call(this),Object.values(t).forEach(c=>{c.destroy(!0)}),this.hydration=void 0,o._wipe(),n.wipe(),n.flush(),s.ecosystemWiped&&r.dispatch(S.ecosystemWiped({ecosystem:this}))}_consumeHydration(e){var t;const s=(t=this.hydration)===null||t===void 0?void 0:t[e.id];if(!(typeof s>"u"))return delete this.hydration[e.id],e.template.hydrate?e.template.hydrate(s):s}_decrementRefCount(){this._refCount--,this.destroyOnUnmount&&this.destroy()}_destroyAtomInstance(e){this._graph.removeNode(e),delete this._instances[e]}_incrementRefCount(){this._refCount++}recalculateMods(e,t){t&&t.forEach(s=>{this._mods[s]--}),e&&e.forEach(s=>{this._mods[s]++})}resolveAtom(e){var t;const{flags:s,overrides:n}=this,o=n[e.key]||e;return s&&((t=o.flags)===null||t===void 0||t.find(c=>!s.includes(c))),o}}class Re{constructor({initialMods:e=[],registerEcosystem:t}={}){this.modStore=T(null,e),this.registerEcosystem=t||(()=>{})}}Re.actions=S;const Oe=i=>new $(i),ne=(i={})=>{if(i!=null&&i.id){const t=ee(i.id);if(t)return t}const e=new ke(i);return R.setStateDeep({[e.id]:e}),e},G=(i,e,t)=>(...n)=>{const r=x(),{_injectors:o,_nextInjectors:c,status:a,template:d}=r;if(a==="Initializing"){const f=e(r,...n);return f.type,c==null||c.push(f),f.result}const h=o==null?void 0:o[c==null?void 0:c.length],l=t?t(h,r,...n):h;return c==null||c.push(l),l.result},ie="injectAtomInstance",re=G(ie,(i,e,t,s)=>({result:i.ecosystem._evaluationStack.atomGetters.getInstance(e,t,[s!=null&&s.subscribe?0:w,(s==null?void 0:s.operation)||ie]),type:`${j}/atom`}),(i,e,t,s,n)=>{const r=e.ecosystem._evaluationStack.atomGetters.getInstance(t,s,[n!=null&&n.subscribe?0:w,(n==null?void 0:n.operation)||ie]);return i.result=r,i}),St=(i,...e)=>x().ecosystem._evaluationStack.atomGetters.select(i,...e),bt=(i,e)=>{const t=re(i,e,{operation:"injectAtomState",subscribe:!0});return[t.getState(),t._infusedSetter]},Et=(i,e)=>re(i,e,{operation:"injectAtomValue",subscribe:!0}).store.getState(),oe=G("injectMemo",(i,e,t)=>({type:`${j}/memo`,deps:t,result:e()}),(i,e,t,s)=>{const r=F(i.deps,s)?t():i.result;return i.deps=s,i.result=r,i}),jt=(i,e)=>{const{ecosystem:t}=J();return oe(()=>(...s)=>t.batch(()=>i(...s)),e)},Te=(i,e)=>()=>{const s=i();e.cleanup=typeof s=="function"?s:void 0},Ne=G("injectEffect",(i,e,t,s)=>{const n={deps:t,type:`${j}/effect`};if(!i.ecosystem.ssr){const r=Te(e,n);n.cleanup=()=>{i.ecosystem._scheduler.unschedule(r),n.cleanup=void 0},s!=null&&s.synchronous?r():i.ecosystem._scheduler.schedule({task:r,type:4})}return n},(i,e,t,s,n)=>{var r;if(e.ecosystem.ssr||!F(i==null?void 0:i.deps,s))return i;(r=i.cleanup)===null||r===void 0||r.call(i);const c=Te(t,i);return i.cleanup=()=>{e.ecosystem._scheduler.unschedule(c),i.cleanup=void 0},i.deps=s,n!=null&&n.synchronous?c():e.ecosystem._scheduler.schedule({task:c,type:4}),i}),At=()=>{const i=x();return()=>i.invalidate("injectInvalidate","Injector")},$e=(i,e)=>e.subscribe((t,s,n)=>{if(i._isEvaluating||n.meta===p.ignore)return;const r=(n==null?void 0:n.meta)===p.batch;i._scheduleEvaluation({newState:t,oldState:s,operation:"injectStore",reasons:[{action:n,newState:t,oldState:s,operation:"dispatch",sourceType:"Store",type:"state changed"}],sourceType:"Injector",type:"state changed"},r),r||i.ecosystem._scheduler.flush()}),xe=G("injectStore",(i,e,t)=>{var s,n;const r=(s=t==null?void 0:t.subscribe)!==null&&s!==void 0?s:!0,c=(typeof e=="function"?e:d=>T(null,d??e))(t!=null&&t.hydrate?(n=i.ecosystem.hydration)===null||n===void 0?void 0:n[i.id]:void 0),a=r&&$e(i,c);return{cleanup:a?()=>a.unsubscribe():void 0,result:c,type:`${j}/store`}},(i,e,t,s)=>{var n;const r=(n=s==null?void 0:s.subscribe)!==null&&n!==void 0?n:!0;if(!!i.cleanup===r)return i;if(!r)return i.cleanup(),i.cleanup=void 0,i;const c=$e(e,i.result);return i.cleanup=()=>c.unsubscribe(),i}),Me=G("injectRef",(i,e)=>({result:{current:e},type:`${j}/ref`})),Pe=()=>x().nextReasons;var Ct=globalThis&&globalThis.__rest||function(i,e){var t={};for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&e.indexOf(s)<0&&(t[s]=i[s]);if(i!=null&&typeof Object.getOwnPropertySymbols=="function")for(var n=0,s=Object.getOwnPropertySymbols(i);n<s.length;n++)e.indexOf(s[n])<0&&Object.prototype.propertyIsEnumerable.call(i,s[n])&&(t[s[n]]=i[s[n]]);return t};const It=(i,e,t={})=>{var{dataOnly:s,initialState:n,runOnInvalidate:r}=t,o=Ct(t,["dataOnly","initialState","runOnInvalidate"]);const c=Me({counter:0}),a=xe(s?n:Y(n),o);return r&&Pe().some(d=>d.type==="cache invalidated")&&c.current.counter++,c.current.promise=oe(()=>{const d=c.current.controller,h=typeof AbortController<"u"?new AbortController:void 0;c.current.controller=h;const l=i(c.current.controller);return l===c.current.promise?c.current.promise:(d&&d.abort("updated"),s||a.setStateDeep(f=>Y(f.data)),l.then(f=>{h!=null&&h.signal.aborted||a.setState(s?f:be(f))}).catch(f=>{s||h!=null&&h.signal.aborted||a.setStateDeep(Se(f))}),l)},e&&[...e,c.current.counter]),Ne(()=>()=>{const d=c.current.controller;d&&d.abort("destroyed")},[]),Oe(a).setPromise(c.current.promise)},wt=x,De=_.createContext("@@global"),Be=2,kt=4,ze=(i,e)=>{var r,o;const t=(r=i._storage).react||(r.react={}),s=t.contexts||(t.contexts=new WeakMap),n=s.get(_.createContext)||s.set(_.createContext,{}).get(_.createContext);return n[o=e.key]||(n[o]=_.createContext(void 0))},U=()=>{const i=_.useContext(De);return ee(i)||ne({id:i})},Ge=({children:i,instance:e,instances:t})=>{const s=U(),[n,...r]=t||[e],o=ze(s,n.template);return _.createElement(o.Provider,{value:n},r.length?_.createElement(Ge,{instances:r},i):i)},Rt=({children:i,ecosystem:e,...t})=>{const s=_.useMemo(()=>e||ne({destroyOnUnmount:!0,...t}),[t.id,e]);return _.useEffect(()=>(s._incrementRefCount(),()=>s._decrementRefCount()),[s]),_.createElement(De.Provider,{value:s.id},i)},Ot=(i,e)=>{const t=U(),s=_.useContext(ze(t,i));return!e||m(s,k)||e===!0?s:t.getInstance(i,e)},Ue="_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE",Tt=`__CLIENT${Ue}`,Nt=`__SERVER${Ue}`,We=()=>{var t,s,n,r,o,c,a;const i=((r=(n=(s=(t=_[Tt]||_[Nt])==null?void 0:t.A)==null?void 0:s.getOwner)==null?void 0:n.call(s))==null?void 0:r.type)||((a=(c=(o=_.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED)==null?void 0:o.ReactCurrentOwner)==null?void 0:c.current)==null?void 0:a.type);return`${(i==null?void 0:i.displayName)||(i==null?void 0:i.name)||"rc"}-${_.useId()}`},He="useAtomInstance",ce=(i,e,{operation:t=He,subscribe:s,suspend:n}={operation:He})=>{const r=U(),o=We(),[,c]=_.useState();let a=r.getInstance(i,e);const d=a.getState();let h;const l=f=>{var v;(v=r._graph.nodes[a.id])!=null&&v.dependents.get(o)||(h=r._graph.addEdge(o,a.id,t,Be|(s?0:kt),()=>{c.mounted&&c({})}),h&&(h.isMaterialized=f,h.dependentKey=o,a._lastEdge&&(h.prevEdge=a._lastEdge),a._lastEdge=new WeakRef(h)))};if(l(),_.useEffect(()=>{var f,v;if(a=r.getInstance(i,e),h){let b=(f=h.prevEdge)==null?void 0:f.deref();for(h.isMaterialized=!0;b&&!b.isMaterialized;)r._graph.removeEdge(b.dependentKey,a.id),b.isMaterialized=!0,b=(v=b.prevEdge)==null?void 0:v.deref()}return l(!0),c.mounted=!0,(s&&a.getState()!==d||a.status==="Destroyed")&&c({}),()=>{r._graph.removeEdge(o,a.id)}},[a.id]),n!==!1){const f=a._promiseStatus;if(f==="loading")throw a.promise;if(f==="error")throw a._promiseError}return a},$t="useAtomSelector",xt=(i,...e)=>{var M;const t=U(),{_graph:s,selectors:n}=t,r=We(),[,o]=_.useState(),c=o.cache,a=!c||(i.argsComparator?!i.argsComparator(e,c.args||[]):F(c.args,e)),d=a?e:c.args,h=c&&c.selectorRef!==i&&!a?((M=s.nodes[c.id])==null?void 0:M.refCount)===1&&!n._refBaseKeys.has(i)&&n._getIdealCacheId(c.selectorRef)===n._getIdealCacheId(i):!1;h&&(o.mounted=!1,n._swapRefs(c,i,d),o.mounted=!0);let l=h?c:n.getCache(i,d),f;const v=y=>{var P;(P=s.nodes[l.id])!=null&&P.dependents.get(r)||(f=s.addEdge(r,l.id,$t,Be,()=>{o.mounted&&o({})}),f&&(f.isMaterialized=y,f.dependentKey=r,l._lastEdge&&(f.prevEdge=l._lastEdge),l._lastEdge=new WeakRef(f)),n._lastCache&&(l._prevCache=n._lastCache),n._lastCache=new WeakRef(l))};v();const b=l.result;return o.cache=l,_.useEffect(()=>{var P,D,W,B;if(l=h?c:n.getCache(i,d),f){let z=(P=f.prevEdge)==null?void 0:P.deref();for(f.isMaterialized=!0;z&&!z.isMaterialized;)t._graph.removeEdge(z.dependentKey,l.id),z.isMaterialized=!0,z=(D=z.prevEdge)==null?void 0:D.deref()}let y=(W=l._prevCache)==null?void 0:W.deref();for(l.isMaterialized=!0;y&&!y.isMaterialized;)n.destroyCache(y,[],!0),y.isMaterialized=!0,y=(B=y._prevCache)==null?void 0:B.deref();return v(!0),o.mounted=!0,(l.isDestroyed||l.result!==b)&&o({}),()=>{s.removeEdge(r,l.id)}},[l.id]),b},Mt=(i,e,t={operation:"useAtomState"})=>{const s=ce(i,e,{...t,subscribe:!0});return[s.getState(),s._infusedSetter]},Pt=(i,e,t={operation:"useAtomValue"})=>ce(i,e,{...t,subscribe:!0}).getState();u.AtomApi=$,u.AtomInstance=Q,u.AtomInstanceBase=k,u.AtomProvider=Ge,u.AtomTemplate=we,u.AtomTemplateBase=K,u.Ecosystem=ke,u.EcosystemProvider=Rt,u.IonTemplate=Ce,u.SelectorCache=A,u.Store=E,u.ZeduxPlugin=Re,u.actionFactory=C,u.api=Oe,u.atom=Ie,u.createEcosystem=ne,u.createReducer=Ve,u.createStore=T,u.detailedTypeof=Fe,u.getEcosystem=ee,u.getInternals=ut,u.getMetaData=pe,u.haveDepsChanged=F,u.injectAtomGetters=J,u.injectAtomInstance=re,u.injectAtomSelector=St,u.injectAtomState=bt,u.injectAtomValue=Et,u.injectCallback=jt,u.injectEffect=Ne,u.injectInvalidate=At,u.injectMemo=oe,u.injectPromise=It,u.injectRef=Me,u.injectSelf=wt,u.injectStore=xe,u.injectWhy=Pe,u.ion=Ae,u.is=m,u.isPlainObject=H,u.removeAllMeta=ye,u.removeMeta=ge,u.setInternals=ht,u.useAtomContext=Ot,u.useAtomInstance=ce,u.useAtomSelector=xt,u.useAtomState=Mt,u.useAtomValue=Pt,u.useEcosystem=U,u.wipe=lt,u.zeduxTypes=p,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=zedux-react.umd.min.js.map