jotai
Version:
👻 Next gen state management that will spook you
2 lines (1 loc) • 8.84 kB
JavaScript
System.register(["jotai/vanilla/utils","jotai","react"],function(N){"use strict";var E,S,m,P,V,M,j,W,I,k,U;return{setters:[function(b){E=b.RESET,S=b.unstable_NO_STORAGE_VALUE,N({RESET:b.RESET,unstable_NO_STORAGE_VALUE:b.unstable_NO_STORAGE_VALUE})},function(b){m=b.atom,P=b.SECRET_INTERNAL_getScopeContext,V=b.useAtom,M=b.useSetAtom,j=b.SECRET_INTERNAL_registerPromiseAbort,W=b},function(b){I=b.useContext,k=b.useCallback,U=b.useMemo}],execute:function(){N({abortableAtom:yt,atomFamily:B,atomWithDefault:it,atomWithHash:dt,atomWithObservable:bt,atomWithReducer:q,atomWithReset:b,atomWithStorage:J,createJSONStorage:z,freezeAtom:nt,freezeAtomCreator:ot,loadable:pt,selectAtom:Z,splitAtom:at,useAtomCallback:tt,useHydrateAtoms:At,useReducerAtom:K,useResetAtom:$,waitForAll:lt});function b(t){const n=m(t,(r,s,o)=>{const e=typeof o=="function"?o(r(n)):o;s(n,e===E?t:e)});return n}const G="w",H="h";function $(t,n){const r=P(n),s=I(r).s;return k(()=>s[G](t,E),[s,t])}function K(t,n,r){const[s,o]=V(t,r),e=k(a=>{o(i=>n(i,a))},[o,n]);return[s,e]}function q(t,n){const r=m(t,(s,o,e)=>o(r,n(s(r),e)));return r}function B(t,n){let r=null;const s=new Map,o=e=>{let a;if(n===void 0)a=s.get(e);else for(const[c,u]of s)if(n(c,e)){a=u;break}if(a!==void 0)if(r!=null&&r(a[1],e))o.remove(e);else return a[0];const i=t(e);return s.set(e,[i,Date.now()]),i};return o.remove=e=>{if(n===void 0)s.delete(e);else for(const[a]of s)if(n(a,e)){s.delete(a);break}},o.setShouldRemove=e=>{if(r=e,!!r)for(const[a,i]of s)r(i[1],a)&&s.delete(a)},o}const Q=(t,n)=>{do{const[r,...s]=n,o=t.get(r);if(!o)return;if(!s.length)return o[1];t=o[0],n=s}while(n.length)},X=(t,n,r)=>{do{const[s,...o]=n;let e=t.get(s);if(e||(e=[new WeakMap],t.set(s,e)),!o.length){e[1]=r;return}t=e[0],n=o}while(n.length)},O=()=>{const t=new WeakMap;return(n,r)=>{const s=Q(t,r);if(s)return s;const o=n();return X(t,r,o),o}},Y=O();function Z(t,n,r=Object.is){return Y(()=>{const s=m(()=>({}));return m(o=>{const e=n(o(t)),a=o(s);return"prev"in a&&r(a.prev,e)?a.prev:(a.prev=e,e)})},[t,n,r])}function tt(t,n){const r=U(()=>m(null,(o,e,[a,i,c])=>{try{i(t(o,e,a))}catch(u){c(u)}}),[t]),s=M(r,n);return k(o=>{let e=!0,a={};const i=new Promise((c,u)=>{s([o,l=>{e?a={v:l}:c(l)},l=>{e?a={e:l}:u(l)}])});if(e=!1,"e"in a)throw a.e;return"v"in a?a.v:i},[s])}const et=O(),x=t=>{if(typeof t!="object"||t===null)return;Object.freeze(t);const n=Object.getOwnPropertyNames(t);for(const r of n){const s=t[r];x(s)}return t};function nt(t){return et(()=>m(n=>x(n(t)),(n,r,s)=>r(t,s)),[t])}function ot(t){return(...n)=>{const r=t(...n),s=r.read;return r.read=o=>x(s(o)),r}}const rt=O(),D=t=>!!t.write,st=t=>typeof t=="function";function at(t,n){return rt(()=>{const r=new WeakMap,s=(c,u)=>{let l=r.get(c);if(l)return l;const f=u&&r.get(u),d=[],h=[];return c.forEach((p,w)=>{const v=n?n(p):w;h[w]=v;const R=f&&f.atomList[f.keyList.indexOf(v)];if(R){d[w]=R;return}const A=L=>{const C=L(o),y=L(t),T=s(y,C.prev).keyList.indexOf(v);if(T<0||T>=y.length){const g=c[s(c).keyList.indexOf(v)];if(g)return g;throw new Error("splitAtom: index out of bounds for read")}return y[T]},St=(L,C,y)=>{const T=L(o),g=L(t),_=s(g,T.prev).keyList.indexOf(v);if(_<0||_>=g.length)throw new Error("splitAtom: index out of bounds for write");const Et=st(y)?y(g[_]):y;C(t,[...g.slice(0,_),Et,...g.slice(_+1)])};d[w]=D(t)?m(A,St):m(A)}),f&&f.keyList.length===h.length&&f.keyList.every((p,w)=>p===h[w])?l=f:l={atomList:d,keyList:h},r.set(c,l),l},o=m(()=>({})),e=c=>{const u=c(o),l=c(t),f=s(l,u.prev);return u.prev=l,f.atomList},a=(c,u,l)=>{switch("read"in l&&(console.warn("atomToRemove is deprecated. use action with type"),l={type:"remove",atom:l}),l.type){case"remove":{const f=c(i).indexOf(l.atom);if(f>=0){const d=c(t);u(t,[...d.slice(0,f),...d.slice(f+1)])}break}case"insert":{const f=l.before?c(i).indexOf(l.before):c(i).length;if(f>=0){const d=c(t);u(t,[...d.slice(0,f),l.value,...d.slice(f)])}break}case"move":{const f=c(i).indexOf(l.atom),d=l.before?c(i).indexOf(l.before):c(i).length;if(f>=0&&d>=0){const h=c(t);f<d?u(t,[...h.slice(0,f),...h.slice(f+1,d),h[f],...h.slice(d)]):u(t,[...h.slice(0,d),h[f],...h.slice(d,f),...h.slice(f+1)])}break}}},i=D(t)?m(e,a):m(e);return i},n?[t,n]:[t])}function it(t){const n=Symbol(),r=m(n),s=m(o=>{const e=o(r);return e!==n?e:t(o)},(o,e,a)=>a===E?e(r,n):e(r,typeof a=="function"?a(o(s)):a));return s}const ct=O(),ut=m(()=>[]);function lt(t){const n=()=>{const r=ft(t);return m(s=>{const o=[],e=r.map((a,i)=>{try{return s(a)}catch(c){if(c instanceof Promise)o[i]=c;else throw c}});if(o.length)throw Promise.all(o);return mt(t,e)})};return Array.isArray(t)?t.length?ct(n,t):ut:n()}const ft=t=>Array.isArray(t)?t:Object.getOwnPropertyNames(t).map(n=>t[n]),mt=(t,n)=>Array.isArray(t)?n:Object.getOwnPropertyNames(t).reduce((r,s,o)=>({...r,[s]:n[o]}),{});function z(t){let n,r;const s={getItem:o=>{var e,a;const i=u=>{if(u=u||"",n!==u){try{r=JSON.parse(u)}catch{return S}n=u}return r},c=(a=(e=t())==null?void 0:e.getItem(o))!=null?a:null;return c instanceof Promise?c.then(i):i(c)},setItem:(o,e)=>{var a;return(a=t())==null?void 0:a.setItem(o,JSON.stringify(e))},removeItem:o=>{var e;return(e=t())==null?void 0:e.removeItem(o)}};return typeof window<"u"&&typeof window.addEventListener=="function"&&(s.subscribe=(o,e)=>{const a=i=>{i.key===o&&i.newValue&&e(JSON.parse(i.newValue))};return window.addEventListener("storage",a),()=>{window.removeEventListener("storage",a)}}),s}const ht=z(()=>typeof window<"u"?window.localStorage:void 0);function J(t,n,r=ht){const s=()=>{const e=r.getItem(t);return e instanceof Promise?e.then(a=>a===S?n:a):e===S?n:e},o=m(r.delayInit?n:s());return o.onMount=e=>{let a;if(r.subscribe&&(a=r.subscribe(t,e),e(s())),r.delayInit){const i=s();i instanceof Promise?i.then(e):e(i)}return a},m(e=>e(o),(e,a,i)=>{const c=typeof i=="function"?i(e(o)):i;return c===E?(a(o,n),r.removeItem(t)):(a(o,c),r.setItem(t,c))})}function dt(t,n,r){const s=(r==null?void 0:r.serialize)||JSON.stringify,o=(r==null?void 0:r.deserialize)||(i=>{try{return JSON.parse(i||"")}catch{return S}}),e=(r==null?void 0:r.subscribe)||(i=>(window.addEventListener("hashchange",i),()=>{window.removeEventListener("hashchange",i)})),a={getItem:i=>{if(typeof location>"u")return S;const c=new URLSearchParams(location.hash.slice(1)).get(i);return o(c)},setItem:(i,c)=>{const u=new URLSearchParams(location.hash.slice(1));u.set(i,s(c)),r!=null&&r.replaceState?history.replaceState(null,"",location.pathname+location.search+"#"+u.toString()):location.hash=u.toString()},removeItem:i=>{const c=new URLSearchParams(location.hash.slice(1));c.delete(i),r!=null&&r.replaceState?history.replaceState(null,"",location.pathname+location.search+"#"+c.toString()):location.hash=c.toString()},...(r==null?void 0:r.delayInit)&&{delayInit:!0},subscribe:(i,c)=>e(()=>{const u=new URLSearchParams(location.hash.slice(1)).get(i);c(u!==null?o(u):n)})};return J(t,n,a)}function bt(t,n){const r=m(s=>{var o;let e=t(s);const a=(o=e[Symbol.observable])==null?void 0:o.call(e);a&&(e=a);let i;const c=()=>new Promise(A=>{i=A}),u=n&&"initialValue"in n?{d:typeof n.initialValue=="function"?n.initialValue():n.initialValue}:c();let l,f;const d=A=>{f=A,i==null||i(A),l==null||l(A)};let h,p;const w=()=>!l,v=()=>{h&&(clearTimeout(p),h.unsubscribe()),h=e.subscribe({next:A=>d({d:A}),error:A=>d({e:A}),complete:()=>{}}),w()&&n!=null&&n.unstable_timeout&&(p=setTimeout(()=>{h&&(h.unsubscribe(),h=void 0)},n.unstable_timeout))};v();const R=m(f||u);return R.onMount=A=>(l=A,f&&A(f),h?clearTimeout(p):v(),()=>{l=void 0,h&&(h.unsubscribe(),h=void 0)}),[R,e,c,v,w]});return m(s=>{const[o]=s(r),e=s(o);if("e"in e)throw e.e;return e.d},(s,o,e)=>{const[a,i,c,u,l]=s(r);if("next"in i)l()&&(o(a,c()),u()),i.next(e);else throw new Error("observable is not subject")})}const F=new WeakMap;function At(t,n){const r=P(n),s=I(r),o=s.s,e=wt(s),a=[];for(const i of t){const c=i[0];e.has(c)||(e.add(c),a.push(i))}a.length&&o[H](a)}function wt(t){let n=F.get(t);return n||(n=new WeakSet,F.set(t,n)),n}const vt=O(),gt={state:"loading"};function pt(t){return vt(()=>{const n=new WeakMap,r=m(s=>{let o;try{const i=s(t);return m({state:"hasData",data:i})}catch(i){if(i instanceof Promise)o=i;else return m({state:"hasError",error:i})}const e=n.get(o);if(e)return e;const a=m(gt,async(i,c)=>{try{const u=await i(t,{unstable_promise:!0});c(a,{state:"hasData",data:u})}catch(u){c(a,{state:"hasError",error:u})}});return a.onMount=i=>{i()},n.set(o,a),a});return m(s=>{const o=s(r);return s(o)})},[t])}function yt(t,n){return m(r=>{const s=new AbortController,o=t(r,{signal:s.signal});return o instanceof Promise&&j(o,()=>s.abort()),o},n)}N({useAtomValue:(...t)=>(console.warn("[DEPRECATED]: use `useAtomValue` from `jotai` instead."),W.useAtomValue(...t)),useUpdateAtom:(...t)=>(console.warn("[DEPRECATED]: use `useSetAtom` from `jotai` instead."),W.useSetAtom(...t))})}}});