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