UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

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