UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

2 lines (1 loc) • 4.93 kB
System.register([],function(M){"use strict";return{execute:function(){M("atom",R);let N=0;function R(s,u){const v=`atom${++N}`,h={toString:()=>v};return typeof s=="function"?h.read=s:(h.init=s,h.read=T,h.write=U),u&&(h.write=u),h}function T(s){return s(this)}function U(s,u,v){return u(this,typeof v=="function"?v(s(this)):v)}const b=(s,u)=>s.unstable_is?s.unstable_is(u):u===s,O=s=>"init"in s,_=s=>!!s.write,m=new WeakMap,V=(s,u)=>{m.set(s,u),s.catch(()=>{}).finally(()=>m.delete(s))},C=(s,u)=>{const v=m.get(s);v&&(m.delete(s),v(u))},q=(s,u)=>{s.status="fulfilled",s.value=u},B=(s,u)=>{s.status="rejected",s.reason=u},X=s=>typeof(s==null?void 0:s.then)=="function",E=(s,u)=>!!s&&"v"in s&&"v"in u&&Object.is(s.v,u.v),F=(s,u)=>!!s&&"e"in s&&"e"in u&&Object.is(s.e,u.e),p=s=>!!s&&"v"in s&&s.v instanceof Promise,$=(s,u)=>"v"in s&&"v"in u&&s.v.orig&&s.v.orig===u.v.orig,y=s=>{if("e"in s)throw s.e;return s.v},x=M("createStore",()=>{const s=new WeakMap,u=new WeakMap,v=[],h=new WeakMap,g=t=>s.get(t),G=(t,r)=>{r.d.forEach((a,e)=>{var o;if(!h.has(e)){const i=g(e);(o=v[v.length-1])==null||o.add(e),h.set(e,[i,new Set]),i&&G(e,i)}h.get(e)[1].add(t)})},H=(t,r)=>{var a;const e=g(t);if(s.set(t,r),h.has(t)||((a=v[v.length-1])==null||a.add(t),h.set(t,[e,new Set]),G(t,r)),p(e)){const o="v"in r?r.v instanceof Promise?r.v:Promise.resolve(r.v):Promise.reject(r.e);e.v!==o&&C(e.v,o)}},I=(t,r,a,e)=>{const o=new Map(e?r.d:null);let i=!1;a.forEach((n,c)=>{!n&&b(t,c)&&(n=r),n&&(o.set(c,n),r.d.get(c)!==n&&(i=!0))}),(i||r.d.size!==o.size)&&(r.d=o)},z=(t,r,a,e)=>{const o=g(t),i={d:(o==null?void 0:o.d)||new Map,v:r};if(a&&I(t,i,a,e),E(o,i)&&o.d===i.d)return o;if(p(o)&&p(i)&&$(o,i)){if(o.d===i.d)return o;i.v=o.v}return H(t,i),i},J=(t,r,a,e)=>{if(X(r)){let o;const i=()=>{const c=g(t);if(!p(c)||c.v!==n)return;const f=z(t,n,a);u.has(t)&&c.d!==f.d&&L(t,f,c.d)},n=new Promise((c,f)=>{let l=!1;r.then(d=>{l||(l=!0,q(n,d),c(d),i())},d=>{l||(l=!0,B(n,d),f(d),i())}),o=d=>{l||(l=!0,d.then(w=>q(n,w),w=>B(n,w)),c(d))}});return n.orig=r,n.status="pending",V(n,c=>{c&&o(c),e==null||e()}),z(t,n,a,!0)}return z(t,r,a)},Q=(t,r,a)=>{const e=g(t),o={d:(e==null?void 0:e.d)||new Map,e:r};return a&&I(t,o,a),F(e,o)&&e.d===o.d?e:(H(t,o),o)},S=(t,r)=>{const a=g(t);if(!r&&a&&(u.has(t)||Array.from(a.d).every(([l,d])=>{if(l===t)return!0;const w=S(l);return w===d||E(w,d)})))return a;const e=new Map;let o=!0;const i=l=>{if(b(t,l)){const w=g(l);if(w)return e.set(l,w),y(w);if(O(l))return e.set(l,void 0),l.init;throw new Error("no atom init")}const d=S(l);return e.set(l,d),y(d)};let n,c;const f={get signal(){return n||(n=new AbortController),n.signal},get setSelf(){return!c&&_(t)&&(c=(...l)=>{if(!o)return j(t,...l)}),c}};try{const l=t.read(i,f);return J(t,l,e,()=>n==null?void 0:n.abort())}catch(l){return Q(t,l,e)}finally{o=!1}},Y=t=>y(S(t)),Z=t=>{let r=u.get(t);return r||(r=W(t)),r},P=(t,r)=>!r.l.size&&(!r.t.size||r.t.size===1&&r.t.has(t)),tt=t=>{const r=u.get(t);r&&P(t,r)&&A(t)},nt=t=>{const r=n=>{var c,f;const l=new Set((c=u.get(n))==null?void 0:c.t);return(f=h.get(n))==null||f[1].forEach(d=>{l.add(d)}),l},a=new Array,e=new Set,o=n=>{if(!e.has(n)){e.add(n);for(const c of r(n))n!==c&&o(c);a.push(n)}};o(t);const i=new Set([t]);for(let n=a.length-1;n>=0;--n){const c=a[n],f=g(c);if(!f)continue;let l=!1;for(const d of f.d.keys())if(d!==c&&i.has(d)){l=!0;break}if(l){const d=S(c,!0);E(f,d)||i.add(c)}}},K=(t,...r)=>{let a=!0;const e=n=>y(S(n)),o=(n,...c)=>{let f;if(b(t,n)){if(!O(n))throw new Error("atom not writable");const l=g(n),d=J(n,c[0]);E(l,d)||nt(n)}else f=K(n,...c);return a||D([n]),f},i=t.write(e,o,...r);return a=!1,i},j=(t,...r)=>{v.push(new Set([t]));const a=K(t,...r);return D(v.pop()),a},W=(t,r,a)=>{var e;const o=a||[];(e=g(t))==null||e.d.forEach((n,c)=>{const f=u.get(c);f?f.t.add(t):c!==t&&W(c,t,o)}),S(t);const i={t:new Set(r&&[r]),l:new Set};if(u.set(t,i),_(t)&&t.onMount){const{onMount:n}=t;o.push(()=>{const c=n((...f)=>j(t,...f));c&&(i.u=c)})}return a||o.forEach(n=>n()),i},A=t=>{var r;const a=(r=u.get(t))==null?void 0:r.u;a&&a(),u.delete(t);const e=g(t);e&&(p(e)&&C(e.v),e.d.forEach((o,i)=>{if(i!==t){const n=u.get(i);n&&(n.t.delete(t),P(i,n)&&A(i))}}))},L=(t,r,a)=>{const e=new Set(r.d.keys()),o=new Set;a==null||a.forEach((i,n)=>{if(e.has(n)){e.delete(n);return}o.add(n);const c=u.get(n);c&&c.t.delete(t)}),e.forEach(i=>{const n=u.get(i);n?n.t.add(t):u.has(t)&&W(i,t)}),o.forEach(i=>{const n=u.get(i);n&&P(i,n)&&A(i)})},D=t=>{const r=[],a=e=>{var o;if(!h.has(e))return;const[i,n]=h.get(e);h.delete(e),r.push([e,i]),n.forEach(a),(o=g(e))==null||o.d.forEach((c,f)=>a(f))};t.forEach(a),r.forEach(([e,o])=>{const i=g(e);if(i&&i!==o){const n=u.get(e);n&&i.d!==(o==null?void 0:o.d)&&L(e,i,o==null?void 0:o.d),n&&!(!p(o)&&(E(o,i)||F(o,i)))&&n.l.forEach(c=>c())}})};return{get:Y,set:j,sub:(t,r)=>{const a=Z(t);D([t]);const e=a.l;return e.add(r),()=>{e.delete(r),tt(t)}}}});let k;const et=M("getDefaultStore",()=>(k||(k=x()),k))}}});