jotai
Version:
👻 Primitive and flexible state management for React
2 lines (1 loc) • 7.4 kB
JavaScript
System.register(["jotai/vanilla"],function(P){"use strict";var m;return{setters:[function(W){m=W.atom}],execute:function(){P({atomFamily:J,atomWithDefault:Y,atomWithObservable:nt,atomWithReducer:z,atomWithRefresh:ft,atomWithReset:$,atomWithStorage:et,createJSONStorage:R,freezeAtom:q,freezeAtomCreator:B,loadable:st,selectAtom:F,splitAtom:X,unstable_withStorageValidator:Z,unwrap:lt});const W=P("RESET",Symbol(""));function $(n){const e=m(n,(o,u,a)=>{const r=typeof a=="function"?a(o(e)):a;u(e,r===W?n:r)});return e}function z(n,e){return m(n,function(o,u,a){u(this,e(o(this),a))})}function J(n,e){let o=null;const u=new Map,a=r=>{let t;if(e===void 0)t=u.get(r);else for(const[c,i]of u)if(e(c,r)){t=i;break}if(t!==void 0)if(o!=null&&o(t[1],r))a.remove(r);else return t[0];const s=n(r);return u.set(r,[s,Date.now()]),s};return a.remove=r=>{if(e===void 0)u.delete(r);else for(const[t]of u)if(e(t,r)){u.delete(t);break}},a.setShouldRemove=r=>{if(o=r,!!o)for(const[t,s]of u)o(s[1],t)&&u.delete(t)},a}const I=(n,e,o)=>(e.has(o)?e:e.set(o,n())).get(o),_=new WeakMap,C=(n,e,o,u)=>{const a=I(()=>new WeakMap,_,e),r=I(()=>new WeakMap,a,o);return I(n,r,u)};function F(n,e,o=Object.is){return C(()=>{const u=Symbol(),a=([t,s])=>{if(s===u)return e(t);const c=e(t,s);return o(s,c)?s:c},r=m(t=>{const s=t(r),c=t(n);return c instanceof Promise||s instanceof Promise?Promise.all([c,s]).then(a):a([c,s])});return r.init=u,r},n,e,o)}const L=new WeakMap,K=(n,e)=>(L.has(e)?L:L.set(e,n())).get(e),E=n=>{if(typeof n!="object"||n===null)return;Object.freeze(n);const e=Object.getOwnPropertyNames(n);for(const o of e){const u=n[o];E(u)}return n};function q(n){return K(()=>m(e=>E(e(n)),(e,o,u)=>o(n,u)),n)}function B(n){return(...e)=>{const o=n(...e),u=o.read;return o.read=function(a,r){return E(u.call(this,a,r))},o}}const V=(n,e,o)=>(e.has(o)?e:e.set(o,n())).get(o),G=new WeakMap,H=(n,e,o)=>{const u=V(()=>new WeakMap,G,e);return V(n,u,o)},Q={},j=n=>!!n.write,U=n=>typeof n=="function";function X(n,e){return H(()=>{const o=new WeakMap,u=(t,s)=>{let c=o.get(t);if(c)return c;const i=s&&o.get(s),l=[],f=[];return t.forEach((v,b)=>{const h=e?e(v):b;f[b]=h;const y=i&&i.atomList[i.keyList.indexOf(h)];if(y){l[b]=y;return}const O=g=>{const d=g(a),p=g(n),k=u(p,d==null?void 0:d.arr).keyList.indexOf(h);if(k<0||k>=p.length){const w=t[u(t).keyList.indexOf(h)];if(w)return w;throw new Error("splitAtom: index out of bounds for read")}return p[k]},M=(g,d,p)=>{const k=g(a),w=g(n),S=u(w,k==null?void 0:k.arr).keyList.indexOf(h);if(S<0||S>=w.length)throw new Error("splitAtom: index out of bounds for write");const T=U(p)?p(w[S]):p;Object.is(w[S],T)||d(n,[...w.slice(0,S),T,...w.slice(S+1)])};l[b]=j(n)?m(O,M):m(O)}),i&&i.keyList.length===f.length&&i.keyList.every((v,b)=>v===f[b])?c=i:c={arr:t,atomList:l,keyList:f},o.set(t,c),c},a=m(t=>{const s=t(a),c=t(n);return u(c,s==null?void 0:s.arr)});a.init=void 0;const r=j(n)?m(t=>t(a).atomList,(t,s,c)=>{switch(c.type){case"remove":{const i=t(r).indexOf(c.atom);if(i>=0){const l=t(n);s(n,[...l.slice(0,i),...l.slice(i+1)])}break}case"insert":{const i=c.before?t(r).indexOf(c.before):t(r).length;if(i>=0){const l=t(n);s(n,[...l.slice(0,i),c.value,...l.slice(i)])}break}case"move":{const i=t(r).indexOf(c.atom),l=c.before?t(r).indexOf(c.before):t(r).length;if(i>=0&&l>=0){const f=t(n);i<l?s(n,[...f.slice(0,i),...f.slice(i+1,l),f[i],...f.slice(l)]):s(n,[...f.slice(0,l),f[i],...f.slice(l,i),...f.slice(i+1)])}break}}}):m(t=>t(a).atomList);return r},n,e||Q)}function Y(n){const e=Symbol(),o=m(e),u=m((a,r)=>{const t=a(o);return t!==e?t:n(a,r)},(a,r,t)=>{if(t===W)r(o,e);else if(typeof t=="function"){const s=a(u);r(o,t(s))}else r(o,t)});return u}const A=n=>typeof(n==null?void 0:n.then)=="function";function Z(n){return e=>({...e,getItem:(o,u)=>{const a=t=>n(t)?t:u,r=e.getItem(o,u);return A(r)?r.then(a):a(r)}})}function R(n=()=>{try{return window.localStorage}catch(o){return}},e){let o,u;const a={getItem:(r,t)=>{var s,c;const i=f=>{if(f=f||"",o!==f){try{u=JSON.parse(f,e==null?void 0:e.reviver)}catch(v){return t}o=f}return u},l=(c=(s=n())==null?void 0:s.getItem(r))!=null?c:null;return A(l)?l.then(i):i(l)},setItem:(r,t)=>{var s;return(s=n())==null?void 0:s.setItem(r,JSON.stringify(t,e==null?void 0:e.replacer))},removeItem:r=>{var t;return(t=n())==null?void 0:t.removeItem(r)}};return typeof window!="undefined"&&typeof window.addEventListener=="function"&&window.Storage&&(a.subscribe=(r,t,s)=>{if(!(n()instanceof window.Storage))return()=>{};const c=i=>{if(i.storageArea===n()&&i.key===r){let l;try{l=JSON.parse(i.newValue||"")}catch(f){l=s}t(l)}};return window.addEventListener("storage",c),()=>{window.removeEventListener("storage",c)}}),a}const tt=R();function et(n,e,o=tt,u){const a=u==null?void 0:u.getOnInit,r=m(a?o.getItem(n,e):e);return r.onMount=t=>{a||t(o.getItem(n,e));let s;return o.subscribe&&(s=o.subscribe(n,t,e)),s},m(t=>t(r),(t,s,c)=>{const i=typeof c=="function"?c(t(r)):c;return i===W?(s(r,e),o.removeItem(n)):i instanceof Promise?i.then(l=>(s(r,l),o.setItem(n,l))):(s(r,i),o.setItem(n,i))})}function nt(n,e){const o=a=>{if("e"in a)throw a.e;return a.d},u=m(a=>{var r;let t=n(a);const s=(r=t[Symbol.observable])==null?void 0:r.call(t);s&&(t=s);let c;const i=()=>new Promise(d=>{c=d}),l=e&&"initialValue"in e?{d:typeof e.initialValue=="function"?e.initialValue():e.initialValue}:i();let f,v;const b=d=>{v=d,c==null||c(d),f==null||f(d)};let h,y;const O=()=>!f,M=()=>{h&&(clearTimeout(y),h.unsubscribe()),h=t.subscribe({next:d=>b({d}),error:d=>b({e:d}),complete:()=>{}}),O()&&e!=null&&e.unstable_timeout&&(y=setTimeout(()=>{h&&(h.unsubscribe(),h=void 0)},e.unstable_timeout))};M();const g=m(v||l);return g.onMount=d=>(f=d,v&&d(v),h?clearTimeout(y):M(),()=>{f=void 0,h&&(h.unsubscribe(),h=void 0)}),[g,t,i,M,O]});return m(a=>{const[r]=a(u),t=a(r);return t instanceof Promise?t.then(o):o(t)},(a,r,t)=>{const[s,c,i,l,f]=a(u);if("next"in c)f()&&(r(s,i()),l()),c.next(t);else throw new Error("observable is not subject")})}const x=new WeakMap,ot=(n,e)=>(x.has(e)?x:x.set(e,n())).get(e),rt=n=>n instanceof Promise,D={state:"loading"};function st(n){return ot(()=>{const e=new WeakMap,o=m(0),u=m((a,{setSelf:r})=>{a(o);let t;try{t=a(n)}catch(l){return{state:"hasError",error:l}}if(!rt(t))return{state:"hasData",data:t};const s=t,c=e.get(s);return c||(s.status==="fulfilled"?e.set(s,{state:"hasData",data:s.value}):s.status==="rejected"?e.set(s,{state:"hasError",error:s.reason}):s.then(l=>{e.set(s,{state:"hasData",data:l})},l=>{e.set(s,{state:"hasError",error:l})}).finally(r),e.get(s)||(e.set(s,D),D))},(a,r)=>{r(o,t=>t+1)});return m(a=>a(u))},n)}const N=(n,e,o)=>(e.has(o)?e:e.set(o,n())).get(o),it=new WeakMap,at=(n,e,o)=>{const u=N(()=>new WeakMap,it,e);return N(n,u,o)},ut=n=>n instanceof Promise,ct=()=>{};function lt(n,e=ct){return at(()=>{const o=new WeakMap,u=new WeakMap,a=m(0),r=m((t,{setSelf:s})=>{t(a);const c=t(r),i=t(n);if(!ut(i))return{v:i};if(i!==(c==null?void 0:c.p)&&(i.status==="fulfilled"?u.set(i,i.value):i.status==="rejected"?o.set(i,i.reason):i.then(l=>u.set(i,l),l=>o.set(i,l)).finally(s)),o.has(i))throw o.get(i);return u.has(i)?{p:i,v:u.get(i)}:c&&"v"in c?{p:i,f:e(c.v),v:c.v}:{p:i,f:e()}},(t,s)=>{s(a,c=>c+1)});return r.init=void 0,m(t=>{const s=t(r);return"f"in s?s.f:s.v},(t,s,...c)=>s(n,...c))},n,e)}function ft(n,e){const o=m(0);return m((u,a)=>(u(o),n(u,a)),(u,a,...r)=>{if(r.length===0)a(o,t=>t+1);else if(e)return e(u,a,...r)})}}}});