UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

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