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