jotai
Version:
👻 Next gen state management that will spook you
2 lines (1 loc) • 7.27 kB
JavaScript
System.register(["react"],function(O){"use strict";var F,G,y,q,$,x,C,H,J;return{setters:[function(w){F=w.createContext,G=w.useState,y=w.useEffect,q=w.useRef,$=w.createElement,x=w.useContext,C=w.useCallback,H=w.useReducer,J=w.useDebugValue}],execute:function(){O({atom:_t,useAtom:mt,useAtomValue:et,useSetAtom:nt});const w=Symbol(),K=c=>!!c[w],lt=c=>!c[w].c,b=c=>{var i,f;(f=(i=c[w]).c)==null||f.call(i)},R=(c,i)=>{const f=c[w].o,l=i[w].o;return f===l||c===l||K(f)&&R(f,i)},Q=c=>{const i={o:c,c:null},f=new Promise(l=>{i.c=()=>{i.c=null,l()},c.then(i.c,i.c)});return f[w]=i,f};var at=Object.defineProperty,dt=Object.defineProperties,pt=Object.getOwnPropertyDescriptors,X=Object.getOwnPropertySymbols,St=Object.prototype.hasOwnProperty,At=Object.prototype.propertyIsEnumerable,Y=(c,i,f)=>i in c?at(c,i,{enumerable:!0,configurable:!0,writable:!0,value:f}):c[i]=f,ht=(c,i)=>{for(var f in i||(i={}))St.call(i,f)&&Y(c,f,i[f]);if(X)for(var f of X(i))At.call(i,f)&&Y(c,f,i[f]);return c},wt=(c,i)=>dt(c,pt(i));const I=c=>"init"in c,T="r",N="w",z="c",j="s",Et="h",Z=c=>{const i=new WeakMap,f=new WeakMap,l=new Map;if(c)for(const[t,e]of c){const o={v:e,r:0,d:new Map};i.set(t,o)}const A=new WeakMap,g=(t,e,o)=>{let r=A.get(e);r||(r=new Map,A.set(e,r)),o.then(()=>{r.get(t)===o&&(r.delete(t),r.size||A.delete(e))}),r.set(t,o)},S=t=>{const e=new Set,o=A.get(t);return o&&(A.delete(t),o.forEach((r,s)=>{b(r),e.add(s)})),e},d=new WeakMap,E=t=>{let e=d.get(t);return e||(e=new Map,d.set(t,e)),e},a=(t,e)=>{if(t){const o=E(t);let r=o.get(e);return r||(r=a(t.p,e),r&&("p"in r&&r.p.then(()=>o.delete(e)),o.set(e,r))),r}return i.get(e)},h=(t,e,o)=>{if(t)E(t).set(e,o);else{const r=i.get(e);i.set(e,o),l.has(e)||l.set(e,r)}},m=(t,e=new Map,o)=>{if(!o)return e;const r=new Map;let s=!1;return o.forEach(n=>{var u;const p=((u=a(t,n))==null?void 0:u.r)||0;r.set(n,p),e.get(n)!==p&&(s=!0)}),e.size===r.size&&!s?e:r},rt=(t,e,o,r,s)=>{const n=a(t,e);if(n){if(s&&(!("p"in n)||!R(n.p,s)))return n;"p"in n&&b(n.p)}const u={v:o,r:(n==null?void 0:n.r)||0,d:m(t,n==null?void 0:n.d,r)};return!n||!("v"in n)||!Object.is(n.v,o)?(++u.r,u.d.has(e)&&(u.d=new Map(u.d).set(e,u.r))):u.d!==n.d&&(u.d.size!==n.d.size||!Array.from(u.d.keys()).every(p=>n.d.has(p)))&&Promise.resolve().then(()=>{v(t)}),h(t,e,u),u},ot=(t,e,o,r,s)=>{const n=a(t,e);if(n){if(s&&(!("p"in n)||!R(n.p,s)))return n;"p"in n&&b(n.p)}const u={e:o,r:(n==null?void 0:n.r)||0,d:m(t,n==null?void 0:n.d,r)};return h(t,e,u),u},st=(t,e,o,r)=>{const s=a(t,e);if(s&&"p"in s){if(R(s.p,o))return s;b(s.p)}g(t,e,o);const n={p:o,r:(s==null?void 0:s.r)||0,d:m(t,s==null?void 0:s.d,r)};return h(t,e,n),n},V=(t,e,o,r)=>{if(o instanceof Promise){const s=Q(o.then(n=>{rt(t,e,n,r,s),v(t)}).catch(n=>{if(n instanceof Promise)return K(n)?n.then(()=>{M(t,e,!0)}):n;ot(t,e,n,r,s),v(t)}));return st(t,e,s,r)}return rt(t,e,o,r)},Mt=(t,e)=>{const o=a(t,e);if(o){const r=wt(ht({},o),{i:o.r});h(t,e,r)}},M=(t,e,o)=>{if(!o){const s=a(t,e);if(s&&(s.r!==s.i&&"p"in s&&!lt(s.p)||(s.d.forEach((n,u)=>{if(u!==e)if(!f.has(u))M(t,u);else{const p=a(t,u);p&&p.r===p.i&&M(t,u)}}),Array.from(s.d).every(([n,u])=>{const p=a(t,n);return p&&"v"in p&&p.r===u}))))return s}const r=new Set;try{const s=e.read(n=>{r.add(n);const u=n===e?a(t,n):M(t,n);if(u){if("e"in u)throw u.e;if("p"in u)throw u.p;return u.v}if(I(n))return n.init;throw new Error("no atom init")});return V(t,e,s,r)}catch(s){if(s instanceof Promise){const n=Q(s);return st(t,e,n,r)}return ot(t,e,s,r)}},vt=(t,e)=>M(e,t),Pt=t=>{let e=f.get(t);return e||(e=L(t)),e},U=(t,e)=>!e.l.size&&(!e.t.size||e.t.size===1&&e.t.has(t)),Ot=t=>{const e=f.get(t);e&&U(t,e)&&B(t)},D=(t,e)=>{const o=f.get(e);o==null||o.t.forEach(r=>{r!==e&&(Mt(t,r),D(t,r))})},ct=(t,e,o)=>{let r=!0;const s=(p,P)=>{const _=M(t,p);if("e"in _)throw _.e;if("p"in _){if(P!=null&&P.unstable_promise)return _.p.then(()=>s(p,P));throw _.p}if("v"in _)return _.v;throw new Error("no value found")},n=(p,P)=>{let _;if(p===e){if(!I(p))throw new Error("atom not writable");S(p).forEach(ft=>{ft!==t&&V(ft,p,P)}),V(t,p,P),D(t,p)}else _=ct(t,p,P);return r||v(t),_},u=e.write(s,n,o);return r=!1,t=void 0,u},it=(t,e,o)=>{const r=ct(o,t,e);return v(o),r},yt=t=>!!t.write,L=(t,e)=>{const o={t:new Set(e&&[e]),l:new Set};if(f.set(t,o),M(void 0,t).d.forEach((s,n)=>{const u=f.get(n);u?u.t.add(t):n!==t&&L(n,t)}),yt(t)&&t.onMount){const s=u=>it(t,u),n=t.onMount(s);n&&(o.u=n)}return o},B=t=>{var e;const o=(e=f.get(t))==null?void 0:e.u;o&&o(),f.delete(t);const r=a(void 0,t);r&&r.d.forEach((s,n)=>{if(n!==t){const u=f.get(n);u&&(u.t.delete(t),U(n,u)&&B(n))}})},ut=(t,e,o)=>{const r=new Set(e.d.keys());o==null||o.forEach((s,n)=>{if(r.has(n)){r.delete(n);return}const u=f.get(n);u&&(u.t.delete(t),U(n,u)&&B(n))}),r.forEach(s=>{const n=f.get(s);n?n.t.add(t):f.has(t)&&L(s,t)})},v=t=>{if(t){E(t).forEach((o,r)=>{if(o!==i.get(r)){const s=f.get(r);s==null||s.l.forEach(n=>n(t))}});return}for(;l.size;){const e=Array.from(l);l.clear(),e.forEach(([o,r])=>{const s=a(void 0,o);s&&s.d!==(r==null?void 0:r.d)&&ut(o,s,r==null?void 0:r.d);const n=f.get(o);n==null||n.l.forEach(u=>u())})}},Ct=t=>{E(t).forEach((o,r)=>{const s=i.get(r);(o.r>((s==null?void 0:s.r)||0)||"v"in o&&o.r===(s==null?void 0:s.r)&&o.d!==(s==null?void 0:s.d))&&(i.set(r,o),o.d!==(s==null?void 0:s.d)&&ut(r,o,s==null?void 0:s.d))})},bt=(t,e)=>{e&&Ct(e),v(void 0)},Rt=(t,e)=>{const r=Pt(t).l;return r.add(e),()=>{r.delete(e),Ot(t)}},Tt=(t,e)=>{for(const[o,r]of t)I(o)&&(V(e,o,r),D(e,o));v(e)};return{[T]:vt,[N]:it,[z]:bt,[j]:Rt,[Et]:Tt}},Vt=O("unstable_createStore",c=>{const i=Z(c),f=S=>{const d=i[T](S);if("e"in d)throw d.e;if(!("p"in d))return d.v},l=S=>new Promise((d,E)=>{const a=i[T](S);"e"in a?E(a.e):"p"in a?d(a.p.then(()=>l(S))):d(a.v)});return{get:f,asyncGet:l,set:(S,d)=>i[N](S,d),sub:(S,d)=>i[j](S,d),SECRET_INTERNAL_store:i}}),tt=(c,i)=>({s:i?i(c).SECRET_INTERNAL_store:Z(c)}),k=new Map,W=O("SECRET_INTERNAL_getScopeContext",c=>(k.has(c)||k.set(c,F(tt())),k.get(c))),xt=O("Provider",({children:c,initialValues:i,scope:f,unstable_createStore:l,unstable_enableVersionedWrite:A})=>{const[g,S]=G();y(()=>{g&&(d.current.s[z](null,g),delete g.p)},[g]);const d=q();d.current||(d.current=tt(i,l),A&&(d.current.w=a=>{S(h=>{const m=h?{p:h}:{};return a(m),m})}));const E=W(f);return $(E.Provider,{value:d.current},c)});let gt=0;function _t(c,i){const f=`atom${++gt}`,l={toString:()=>f};return typeof c=="function"?l.read=c:(l.init=c,l.read=A=>A(l),l.write=(A,g,S)=>g(l,typeof S=="function"?S(A(l)):S)),i&&(l.write=i),l}function et(c,i){const f=W(i),{s:l}=x(f),A=C(a=>{const h=l[T](c,a);if("e"in h)throw h.e;if("p"in h)throw h.p;if("v"in h)return h.v;throw new Error("no atom value")},[l,c]),[[g,S,d],E]=H(C((a,h)=>{const m=A(h);return Object.is(a[1],m)&&a[2]===c?a:[h,m,c]},[A,c]),void 0,()=>{const h=A(void 0);return[void 0,h,c]});return d!==c&&E(void 0),y(()=>{const a=l[j](c,E);return E(void 0),a},[l,c]),y(()=>{l[z](c,g)}),J(S),S}function nt(c,i){const f=W(i),{s:l,w:A}=x(f);return C(S=>{if(!("write"in c)&&typeof process=="object"&&process.env.NODE_ENV!=="production")throw new Error("not writable atom");const d=E=>l[N](c,S,E);return A?A(d):d()},[l,A,c])}function mt(c,i){return"scope"in c&&(console.warn("atom.scope is deprecated. Please do useAtom(atom, scope) instead."),i=c.scope),[et(c,i),nt(c,i)]}}}});