UNPKG

jotai

Version:

👻 Next gen state management that will spook you

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