jotai
Version:
👻 Primitive and flexible state management for React
3 lines (2 loc) • 1.88 kB
JavaScript
'use client';
System.register(["react","jotai/vanilla","jotai/vanilla/internals"],(function(V){"use strict";var p,C,E,v,d,R,j,m,x,P,w,A;return{setters:[function(r){p=r.createContext,C=r.useContext,E=r.useRef,v=r.createElement,d=r.default,R=r.useReducer,j=r.useEffect,m=r.useDebugValue,x=r.useCallback},function(r){P=r.getDefaultStore,w=r.createStore},function(r){A=r.INTERNAL_registerAbortHandler}],execute:(function(){V({Provider:k,useAtom:H,useAtomValue:N,useSetAtom:T,useStore:b});const r=p(void 0);function b(t){const n=C(r);return(t==null?void 0:t.store)||n||P()}function k({children:t,store:n}){const u=E(null);return n?v(r.Provider,{value:n},t):(u.current===null&&(u.current=w()),v(r.Provider,{value:u.current},t))}const S=t=>typeof(t==null?void 0:t.then)=="function",h=t=>{t.status||(t.status="pending",t.then(n=>{t.status="fulfilled",t.value=n},n=>{t.status="rejected",t.reason=n}))},_=d.use||(t=>{if(t.status==="pending")throw t;if(t.status==="fulfilled")return t.value;throw t.status==="rejected"?t.reason:(h(t),t)}),y=new WeakMap,D=(t,n)=>{let u=y.get(t);return u||(u=new Promise((c,s)=>{let l=t;const f=e=>o=>{l===e&&c(o)},g=e=>o=>{l===e&&s(o)},a=()=>{try{const e=n();S(e)?(y.set(e,u),l=e,e.then(f(e),g(e)),A(e,a)):c(e)}catch(e){s(e)}};t.then(f(t),g(t)),A(t,a)}),y.set(t,u)),u};function N(t,n){const{delay:u,unstable_promiseStatus:c=!d.use}=n||{},s=b(n),[[l,f,g],a]=R(o=>{const i=s.get(t);return Object.is(o[0],i)&&o[1]===s&&o[2]===t?o:[i,s,t]},void 0,()=>[s.get(t),s,t]);let e=l;if((f!==s||g!==t)&&(a(),e=s.get(t)),j(()=>{const o=s.sub(t,()=>{if(c)try{const i=s.get(t);S(i)&&h(D(i,()=>s.get(t)))}catch(i){}if(typeof u=="number"){setTimeout(a,u);return}a()});return a(),o},[s,t,u,c]),m(e),S(e)){const o=D(e,()=>s.get(t));return c&&h(o),_(o)}return e}function T(t,n){const u=b(n);return x((...c)=>u.set(t,...c),[u,t])}function H(t,n){return[N(t,n),T(t,n)]}})}}));