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