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