UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

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