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