1 line
10 KiB
JavaScript
1 line
10 KiB
JavaScript
import{M as e,St as t,_t as n,b as r,et as i,ht as a,j as o,mt as s,pt as c,vt as l}from"./iconify-DYZHDLa8.js";import{A as u,C as d,D as f,E as p,M as m,N as h,O as g,S as _,T as v,_ as y,b,g as x,h as S,j as C,k as w,m as T,v as E,w as D,x as O,y as k}from"./index-Uji7rFqr.js";var A=class extends h{constructor(e,t){super(),this.options=t,this.#e=e,this.#s=null,this.#o=b(),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),M(this.#t,this.options)?this.#h():this.updateResult(),this.#y())}onUnsubscribe(){this.hasListeners()||this.destroy()}shouldFetchOnReconnect(){return N(this.#t,this.options,this.options.refetchOnReconnect)}shouldFetchOnWindowFocus(){return N(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 f(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&&!w(this.options,t)&&this.#e.getQueryCache().notify({type:`observerOptionsUpdated`,query:this.#t,observer:this});let r=this.hasListeners();r&&P(this.#t,n,this.options,t)&&this.#h(),this.updateResult(),r&&(this.#t!==n||f(this.options.enabled,this.#t)!==f(t.enabled,this.#t)||g(this.options.staleTime,this.#t)!==g(t.staleTime,this.#t))&&this.#g();let i=this.#_();r&&(this.#t!==n||f(this.options.enabled,this.#t)!==f(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 I(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(v)),t}#g(){this.#b();let e=g(this.options.staleTime,this.#t);if(d||this.#r.isStale||!D(e))return;let t=C(this.#r.dataUpdatedAt,e)+1;this.#d=m.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,!(d||f(this.options.enabled,this.#t)===!1||!D(this.#p)||this.#p===0)&&(this.#f=m.setInterval(()=>{(this.options.refetchIntervalInBackground||O.isFocused())&&this.#h()},this.#p))}#y(){this.#g(),this.#v(this.#_())}#b(){this.#d&&=(m.clearTimeout(this.#d),void 0)}#x(){this.#f&&=(m.clearInterval(this.#f),void 0)}createResult(e,t){let n=this.#t,r=this.options,i=this.#r,a=this.#i,o=this.#a,s=e===n?this.#n:e.state,{state:c}=e,l={...c},u=!1,d;if(t._optimisticResults){let i=this.hasListeners(),a=!i&&M(e,t),o=i&&P(e,n,t,r);(a||o)&&(l={...l,...E(c.data,e.options)}),t._optimisticResults===`isRestoring`&&(l.fetchStatus=`idle`)}let{error:m,errorUpdatedAt:h,status:g}=l;d=l.data;let _=!1;if(t.placeholderData!==void 0&&d===void 0&&g===`pending`){let e;i?.isPlaceholderData&&t.placeholderData===o?.placeholderData?(e=i.data,_=!0):e=typeof t.placeholderData==`function`?t.placeholderData(this.#u?.state.data,this.#u):t.placeholderData,e!==void 0&&(g=`success`,d=p(i?.data,e,t),u=!0)}if(t.select&&d!==void 0&&!_)if(i&&d===a?.data&&t.select===this.#c)d=this.#l;else try{this.#c=t.select,d=t.select(d),d=p(i?.data,d,t),this.#l=d,this.#s=null}catch(e){this.#s=e}this.#s&&(m=this.#s,d=this.#l,h=Date.now(),g=`error`);let v=l.fetchStatus===`fetching`,y=g===`pending`,x=g===`error`,S=y&&v,C=d!==void 0,w={status:g,fetchStatus:l.fetchStatus,isPending:y,isSuccess:g===`success`,isError:x,isInitialLoading:S,isLoading:S,data:d,dataUpdatedAt:l.dataUpdatedAt,error:m,errorUpdatedAt:h,failureCount:l.fetchFailureCount,failureReason:l.fetchFailureReason,errorUpdateCount:l.errorUpdateCount,isFetched:l.dataUpdateCount>0||l.errorUpdateCount>0,isFetchedAfterMount:l.dataUpdateCount>s.dataUpdateCount||l.errorUpdateCount>s.errorUpdateCount,isFetching:v,isRefetching:v&&!y,isLoadingError:x&&!C,isPaused:l.fetchStatus===`paused`,isPlaceholderData:u,isRefetchError:x&&C,isStale:F(e,t),refetch:this.refetch,promise:this.#o,isEnabled:f(t.enabled,e)!==!1};if(this.options.experimental_prefetchInRender){let t=w.data!==void 0,r=w.status===`error`&&!t,i=e=>{r?e.reject(w.error):t&&e.resolve(w.data)},a=()=>{i(this.#o=w.promise=b())},o=this.#o;switch(o.status){case`pending`:e.queryHash===n.queryHash&&i(o);break;case`fulfilled`:(r||w.data!==o.value)&&a();break;case`rejected`:(!r||w.error!==o.reason)&&a();break}}return w}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),!w(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){k.batch(()=>{e.listeners&&this.listeners.forEach(e=>{e(this.#r)}),this.#e.getQueryCache().notify({query:this.#t,type:`observerResultsUpdated`})})}};function j(e,t){return f(t.enabled,e)!==!1&&e.state.data===void 0&&!(e.state.status===`error`&&t.retryOnMount===!1)}function M(e,t){return j(e,t)||e.state.data!==void 0&&N(e,t,t.refetchOnMount)}function N(e,t,n){if(f(t.enabled,e)!==!1&&g(t.staleTime,e)!==`static`){let r=typeof n==`function`?n(e):n;return r===`always`||r!==!1&&F(e,t)}return!1}function P(e,t,n,r){return(e!==t||f(r.enabled,e)===!1)&&(!n.suspense||e.state.status!==`error`)&&F(e,n)}function F(e,t){return f(t.enabled,e)!==!1&&e.isStaleByTime(g(t.staleTime,e))}function I(e,t){return!w(e.getCurrentResult(),t)}var L=class extends h{#e;#t=void 0;#n;#r;constructor(e,t){super(),this.#e=e,this.setOptions(t),this.bindMethods(),this.#i()}bindMethods(){this.mutate=this.mutate.bind(this),this.reset=this.reset.bind(this)}setOptions(e){let t=this.options;this.options=this.#e.defaultMutationOptions(e),w(this.options,t)||this.#e.getMutationCache().notify({type:`observerOptionsUpdated`,mutation:this.#n,observer:this}),t?.mutationKey&&this.options.mutationKey&&_(t.mutationKey)!==_(this.options.mutationKey)?this.reset():this.#n?.state.status===`pending`&&this.#n.setOptions(this.options)}onUnsubscribe(){this.hasListeners()||this.#n?.removeObserver(this)}onMutationUpdate(e){this.#i(),this.#a(e)}getCurrentResult(){return this.#t}reset(){this.#n?.removeObserver(this),this.#n=void 0,this.#i(),this.#a()}mutate(e,t){return this.#r=t,this.#n?.removeObserver(this),this.#n=this.#e.getMutationCache().build(this.#e,this.options),this.#n.addObserver(this),this.#n.execute(e)}#i(){let e=this.#n?.state??y();this.#t={...e,isPending:e.status===`pending`,isSuccess:e.status===`success`,isError:e.status===`error`,isIdle:e.status===`idle`,mutate:this.mutate,reset:this.reset}}#a(e){k.batch(()=>{if(this.#r&&this.hasListeners()){let t=this.#t.variables,n=this.#t.context,r={client:this.#e,meta:this.options.meta,mutationKey:this.options.mutationKey};if(e?.type===`success`){try{this.#r.onSuccess?.(e.data,t,n,r)}catch(e){Promise.reject(e)}try{this.#r.onSettled?.(e.data,null,t,n,r)}catch(e){Promise.reject(e)}}else if(e?.type===`error`){try{this.#r.onError?.(e.error,t,n,r)}catch(e){Promise.reject(e)}try{this.#r.onSettled?.(void 0,e.error,t,n,r)}catch(e){Promise.reject(e)}}}this.listeners.forEach(e=>{e(this.#t)})})}};function R(t=``){if(!o())throw Error(`vue-query hooks can only be used inside setup() function or functions that support injection context.`);let n=e(S(t));if(!n)throw Error(`No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.`);return n}function z(e,o,d){let f=d||R(),p=r(()=>{let e=o;typeof e==`function`&&(e=e());let t=T(e);typeof t.enabled==`function`&&(t.enabled=t.enabled());let n=f.defaultQueryOptions(t);return n._optimisticResults=f.isRestoring?.value?`isRestoring`:`optimistic`,n}),m=new e(f,p.value),h=p.value.shallow?n(m.getCurrentResult()):s(m.getCurrentResult()),g=()=>{};f.isRestoring&&i(f.isRestoring,e=>{e||(g(),g=m.subscribe(e=>{x(h,e)}))},{immediate:!0});let _=()=>{m.setOptions(p.value),x(h,m.getCurrentResult())};i(p,_),c(()=>{g()});let v=(...e)=>(_(),h.refetch(...e)),y=()=>new Promise((e,t)=>{let n=()=>{},r=()=>{if(p.value.enabled!==!1){m.setOptions(p.value);let r=m.getOptimisticResult(p.value);r.isStale?(n(),m.fetchOptimistic(p.value).then(e,n=>{u(p.value.throwOnError,[n,m.getCurrentQuery()])?t(n):e(m.getCurrentResult())})):(n(),e(r))}};r(),n=i(p,r)});i(()=>h.error,e=>{if(h.isError&&!h.isFetching&&u(p.value.throwOnError,[e,m.getCurrentQuery()]))throw e});let b=t(p.value.shallow?l(h):a(h));for(let e in h)typeof h[e]==`function`&&(b[e]=h[e]);return b.suspense=y,b.refetch=v,b}function B(e,t){return z(A,e,t)}function V(e,o){let d=o||R(),f=r(()=>{let t=typeof e==`function`?e():e;return d.defaultMutationOptions(T(t))}),p=new L(d,f.value),m=f.value.shallow?n(p.getCurrentResult()):s(p.getCurrentResult()),h=p.subscribe(e=>{x(m,e)}),g=(e,t)=>{p.mutate(e,t).catch(()=>{})};i(f,()=>{p.setOptions(f.value)}),c(()=>{h()});let _=t(f.value.shallow?l(m):a(m));return i(()=>m.error,e=>{if(e&&u(f.value.throwOnError,[e]))throw e}),{..._,mutate:g,mutateAsync:m.mutate,reset:m.reset}}export{B as n,R as r,V as t}; |