t2-mapper/docs/assets/useBaseQuery-R36yzvIy.js
2026-03-15 20:49:43 -07:00

1 line
No EOL
8.6 KiB
JavaScript

import{r as e}from"./chunk-DECur_0Z.js";import{r as t,t as n}from"./jsx-runtime-BpGWiA-R.js";import{$ as r,Q as i,X as a,Y as o,Z as s,at as c,ct as l,et as u,it as d,lt as f,nt as p,ot as m,rt as h,st as g,tt as _,ut as v}from"./index-CqP0naT6.js";var y=class extends v{constructor(e,t){super(),this.options=t,this.#e=e,this.#s=null,this.#o=i(),this.bindMethods(),this.setOptions(t)}#e;#t=void 0;#n=void 0;#r=void 0;#i;#a;#o;#s;#c;#l;#u;#d;#f;#p;#m=new Set;bindMethods(){this.refetch=this.refetch.bind(this)}onSubscribe(){this.listeners.size===1&&(this.#t.addObserver(this),x(this.#t,this.options)?this.#h():this.updateResult(),this.#y())}onUnsubscribe(){this.hasListeners()||this.destroy()}shouldFetchOnReconnect(){return S(this.#t,this.options,this.options.refetchOnReconnect)}shouldFetchOnWindowFocus(){return S(this.#t,this.options,this.options.refetchOnWindowFocus)}destroy(){this.listeners=new Set,this.#b(),this.#x(),this.#t.removeObserver(this)}setOptions(e){let t=this.options,n=this.#t;if(this.options=this.#e.defaultQueryOptions(e),this.options.enabled!==void 0&&typeof this.options.enabled!=`boolean`&&typeof this.options.enabled!=`function`&&typeof d(this.options.enabled,this.#t)!=`boolean`)throw Error(`Expected enabled to be a boolean or a callback that returns a boolean`);this.#S(),this.#t.setOptions(this.options),t._defaulted&&!m(this.options,t)&&this.#e.getQueryCache().notify({type:`observerOptionsUpdated`,query:this.#t,observer:this});let r=this.hasListeners();r&&C(this.#t,n,this.options,t)&&this.#h(),this.updateResult(),r&&(this.#t!==n||d(this.options.enabled,this.#t)!==d(t.enabled,this.#t)||c(this.options.staleTime,this.#t)!==c(t.staleTime,this.#t))&&this.#g();let i=this.#_();r&&(this.#t!==n||d(this.options.enabled,this.#t)!==d(t.enabled,this.#t)||i!==this.#p)&&this.#v(i)}getOptimisticResult(e){let t=this.#e.getQueryCache().build(this.#e,e),n=this.createResult(t,e);return T(this,n)&&(this.#r=n,this.#a=this.options,this.#i=this.#t.state),n}getCurrentResult(){return this.#r}trackResult(e,t){return new Proxy(e,{get:(e,n)=>(this.trackProp(n),t?.(n),n===`promise`&&(this.trackProp(`data`),!this.options.experimental_prefetchInRender&&this.#o.status===`pending`&&this.#o.reject(Error(`experimental_prefetchInRender feature flag is not enabled`))),Reflect.get(e,n))})}trackProp(e){this.#m.add(e)}getCurrentQuery(){return this.#t}refetch({...e}={}){return this.fetch({...e})}fetchOptimistic(e){let t=this.#e.defaultQueryOptions(e),n=this.#e.getQueryCache().build(this.#e,t);return n.fetch().then(()=>this.createResult(n,t))}fetch(e){return this.#h({...e,cancelRefetch:e.cancelRefetch??!0}).then(()=>(this.updateResult(),this.#r))}#h(e){this.#S();let t=this.#t.fetch(this.options,e);return e?.throwOnError||(t=t.catch(p)),t}#g(){this.#b();let e=c(this.options.staleTime,this.#t);if(u||this.#r.isStale||!_(e))return;let t=l(this.#r.dataUpdatedAt,e)+1;this.#d=f.setTimeout(()=>{this.#r.isStale||this.updateResult()},t)}#_(){return(typeof this.options.refetchInterval==`function`?this.options.refetchInterval(this.#t):this.options.refetchInterval)??!1}#v(e){this.#x(),this.#p=e,!(u||d(this.options.enabled,this.#t)===!1||!_(this.#p)||this.#p===0)&&(this.#f=f.setInterval(()=>{(this.options.refetchIntervalInBackground||r.isFocused())&&this.#h()},this.#p))}#y(){this.#g(),this.#v(this.#_())}#b(){this.#d&&=(f.clearTimeout(this.#d),void 0)}#x(){this.#f&&=(f.clearInterval(this.#f),void 0)}createResult(e,t){let n=this.#t,r=this.options,o=this.#r,s=this.#i,c=this.#a,l=e===n?this.#n:e.state,{state:u}=e,f={...u},p=!1,m;if(t._optimisticResults){let i=this.hasListeners(),o=!i&&x(e,t),s=i&&C(e,n,t,r);(o||s)&&(f={...f,...a(u.data,e.options)}),t._optimisticResults===`isRestoring`&&(f.fetchStatus=`idle`)}let{error:g,errorUpdatedAt:_,status:v}=f;m=f.data;let y=!1;if(t.placeholderData!==void 0&&m===void 0&&v===`pending`){let e;o?.isPlaceholderData&&t.placeholderData===c?.placeholderData?(e=o.data,y=!0):e=typeof t.placeholderData==`function`?t.placeholderData(this.#u?.state.data,this.#u):t.placeholderData,e!==void 0&&(v=`success`,m=h(o?.data,e,t),p=!0)}if(t.select&&m!==void 0&&!y)if(o&&m===s?.data&&t.select===this.#c)m=this.#l;else try{this.#c=t.select,m=t.select(m),m=h(o?.data,m,t),this.#l=m,this.#s=null}catch(e){this.#s=e}this.#s&&(g=this.#s,m=this.#l,_=Date.now(),v=`error`);let b=f.fetchStatus===`fetching`,S=v===`pending`,T=v===`error`,E=S&&b,D=m!==void 0,O={status:v,fetchStatus:f.fetchStatus,isPending:S,isSuccess:v===`success`,isError:T,isInitialLoading:E,isLoading:E,data:m,dataUpdatedAt:f.dataUpdatedAt,error:g,errorUpdatedAt:_,failureCount:f.fetchFailureCount,failureReason:f.fetchFailureReason,errorUpdateCount:f.errorUpdateCount,isFetched:f.dataUpdateCount>0||f.errorUpdateCount>0,isFetchedAfterMount:f.dataUpdateCount>l.dataUpdateCount||f.errorUpdateCount>l.errorUpdateCount,isFetching:b,isRefetching:b&&!S,isLoadingError:T&&!D,isPaused:f.fetchStatus===`paused`,isPlaceholderData:p,isRefetchError:T&&D,isStale:w(e,t),refetch:this.refetch,promise:this.#o,isEnabled:d(t.enabled,e)!==!1};if(this.options.experimental_prefetchInRender){let t=O.data!==void 0,r=O.status===`error`&&!t,a=e=>{r?e.reject(O.error):t&&e.resolve(O.data)},o=()=>{a(this.#o=O.promise=i())},s=this.#o;switch(s.status){case`pending`:e.queryHash===n.queryHash&&a(s);break;case`fulfilled`:(r||O.data!==s.value)&&o();break;case`rejected`:(!r||O.error!==s.reason)&&o();break}}return O}updateResult(){let e=this.#r,t=this.createResult(this.#t,this.options);this.#i=this.#t.state,this.#a=this.options,this.#i.data!==void 0&&(this.#u=this.#t),!m(t,e)&&(this.#r=t,this.#C({listeners:(()=>{if(!e)return!0;let{notifyOnChangeProps:t}=this.options,n=typeof t==`function`?t():t;if(n===`all`||!n&&!this.#m.size)return!0;let r=new Set(n??this.#m);return this.options.throwOnError&&r.add(`error`),Object.keys(this.#r).some(t=>{let n=t;return this.#r[n]!==e[n]&&r.has(n)})})()}))}#S(){let e=this.#e.getQueryCache().build(this.#e,this.options);if(e===this.#t)return;let t=this.#t;this.#t=e,this.#n=e.state,this.hasListeners()&&(t?.removeObserver(this),e.addObserver(this))}onQueryUpdate(){this.updateResult(),this.hasListeners()&&this.#y()}#C(e){s.batch(()=>{e.listeners&&this.listeners.forEach(e=>{e(this.#r)}),this.#e.getQueryCache().notify({query:this.#t,type:`observerResultsUpdated`})})}};function b(e,t){return d(t.enabled,e)!==!1&&e.state.data===void 0&&!(e.state.status===`error`&&t.retryOnMount===!1)}function x(e,t){return b(e,t)||e.state.data!==void 0&&S(e,t,t.refetchOnMount)}function S(e,t,n){if(d(t.enabled,e)!==!1&&c(t.staleTime,e)!==`static`){let r=typeof n==`function`?n(e):n;return r===`always`||r!==!1&&w(e,t)}return!1}function C(e,t,n,r){return(e!==t||d(r.enabled,e)===!1)&&(!n.suspense||e.state.status!==`error`)&&w(e,n)}function w(e,t){return d(t.enabled,e)!==!1&&e.isStaleByTime(c(t.staleTime,e))}function T(e,t){return!m(e.getCurrentResult(),t)}var E=e(t(),1),D=E.createContext(!1),O=()=>E.useContext(D);D.Provider,n();function k(){let e=!1;return{clearReset:()=>{e=!1},reset:()=>{e=!0},isReset:()=>e}}var A=E.createContext(k()),j=()=>E.useContext(A),M=(e,t,n)=>{let r=n?.state.error&&typeof e.throwOnError==`function`?g(e.throwOnError,[n.state.error,n]):e.throwOnError;(e.suspense||e.experimental_prefetchInRender||r)&&(t.isReset()||(e.retryOnMount=!1))},N=e=>{E.useEffect(()=>{e.clearReset()},[e])},P=({result:e,errorResetBoundary:t,throwOnError:n,query:r,suspense:i})=>e.isError&&!t.isReset()&&!e.isFetching&&r&&(i&&e.data===void 0||g(n,[e.error,r])),F=(e,t)=>t.state.data===void 0,I=e=>{if(e.suspense){let t=1e3,n=e=>e===`static`?e:Math.max(e??t,t),r=e.staleTime;e.staleTime=typeof r==`function`?(...e)=>n(r(...e)):n(r),typeof e.gcTime==`number`&&(e.gcTime=Math.max(e.gcTime,t))}},L=(e,t)=>e.isLoading&&e.isFetching&&!t,R=(e,t)=>e?.suspense&&t.isPending,z=(e,t,n)=>t.fetchOptimistic(e).catch(()=>{n.clearReset()});function B(e,t,n){let r=O(),i=j(),a=o(n),c=a.defaultQueryOptions(e);a.getDefaultOptions().queries?._experimental_beforeQuery?.(c);let l=a.getQueryCache().get(c.queryHash);c._optimisticResults=r?`isRestoring`:`optimistic`,I(c),M(c,i,l),N(i);let d=!a.getQueryCache().get(c.queryHash),[f]=E.useState(()=>new t(a,c)),m=f.getOptimisticResult(c),h=!r&&e.subscribed!==!1;if(E.useSyncExternalStore(E.useCallback(e=>{let t=h?f.subscribe(s.batchCalls(e)):p;return f.updateResult(),t},[f,h]),()=>f.getCurrentResult(),()=>f.getCurrentResult()),E.useEffect(()=>{f.setOptions(c)},[c,f]),R(c,m))throw z(c,f,i);if(P({result:m,errorResetBoundary:i,throwOnError:c.throwOnError,query:l,suspense:c.suspense}))throw m.error;return a.getDefaultOptions().queries?._experimental_afterQuery?.(c,m),c.experimental_prefetchInRender&&!u&&L(m,r)&&(d?z(c,f,i):l?.promise)?.catch(p).finally(()=>{f.updateResult()}),c.notifyOnChangeProps?m:f.trackResult(m)}export{F as n,y as r,B as t};