UNPKG

jotai

Version:

👻 Next gen state management that will spook you

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