1 line
10 KiB
JavaScript
1 line
10 KiB
JavaScript
import{O as e,Q as t,bt as n,dt as r,ft as i,g as a,gt as o,ht as s,k as c,pt as l}from"./vue.runtime.esm-bundler-DQjudA8G.js";import{A as u,C as d,D as f,E as p,F as m,I as h,M as g,N as _,O as v,P as y,S as b,T as x,_ as S,b as C,j as w,k as T,v as E,w as D,x as O,y as k}from"./index-CcML0o3b.js";var A=class extends h{constructor(e,t){super(),this.options=t,this.#e=e,this.#s=null,this.#o=d(),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 u(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&&!g(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||u(this.options.enabled,this.#t)!==u(t.enabled,this.#t)||w(this.options.staleTime,this.#t)!==w(t.staleTime,this.#t))&&this.#g();let i=this.#_();r&&(this.#t!==n||u(this.options.enabled,this.#t)!==u(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=w(this.options.staleTime,this.#t);if(p||this.#r.isStale||!f(e))return;let t=y(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,!(p||u(this.options.enabled,this.#t)===!1||!f(this.#p)||this.#p===0)&&(this.#f=m.setInterval(()=>{(this.options.refetchIntervalInBackground||D.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},f=!1,p;if(t._optimisticResults){let i=this.hasListeners(),a=!i&&M(e,t),o=i&&P(e,n,t,r);(a||o)&&(l={...l,...O(c.data,e.options)}),t._optimisticResults===`isRestoring`&&(l.fetchStatus=`idle`)}let{error:m,errorUpdatedAt:h,status:g}=l;p=l.data;let _=!1;if(t.placeholderData!==void 0&&p===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`,p=T(i?.data,e,t),f=!0)}if(t.select&&p!==void 0&&!_)if(i&&p===a?.data&&t.select===this.#c)p=this.#l;else try{this.#c=t.select,p=t.select(p),p=T(i?.data,p,t),this.#l=p,this.#s=null}catch(e){this.#s=e}this.#s&&(m=this.#s,p=this.#l,h=Date.now(),g=`error`);let v=l.fetchStatus===`fetching`,y=g===`pending`,b=g===`error`,x=y&&v,S=p!==void 0,C={status:g,fetchStatus:l.fetchStatus,isPending:y,isSuccess:g===`success`,isError:b,isInitialLoading:x,isLoading:x,data:p,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:b&&!S,isPaused:l.fetchStatus===`paused`,isPlaceholderData:f,isRefetchError:b&&S,isStale:F(e,t),refetch:this.refetch,promise:this.#o,isEnabled:u(t.enabled,e)!==!1};if(this.options.experimental_prefetchInRender){let t=C.data!==void 0,r=C.status===`error`&&!t,i=e=>{r?e.reject(C.error):t&&e.resolve(C.data)},a=()=>{i(this.#o=C.promise=d())},o=this.#o;switch(o.status){case`pending`:e.queryHash===n.queryHash&&i(o);break;case`fulfilled`:(r||C.data!==o.value)&&a();break;case`rejected`:(!r||C.error!==o.reason)&&a();break}}return C}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),!g(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){b.batch(()=>{e.listeners&&this.listeners.forEach(e=>{e(this.#r)}),this.#e.getQueryCache().notify({query:this.#t,type:`observerResultsUpdated`})})}};function j(e,t){return u(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(u(t.enabled,e)!==!1&&w(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||u(r.enabled,e)===!1)&&(!n.suspense||e.state.status!==`error`)&&F(e,n)}function F(e,t){return u(t.enabled,e)!==!1&&e.isStaleByTime(w(t.staleTime,e))}function I(e,t){return!g(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),g(this.options,t)||this.#e.getMutationCache().notify({type:`observerOptionsUpdated`,mutation:this.#n,observer:this}),t?.mutationKey&&this.options.mutationKey&&x(t.mutationKey)!==x(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??C();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){b.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(!e())throw Error(`vue-query hooks can only be used inside setup() function or functions that support injection context.`);let n=c(E(t));if(!n)throw Error(`No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.`);return n}function z(e,c,u){let d=u||R(),f=a(()=>{let e=c;typeof e==`function`&&(e=e());let t=S(e);typeof t.enabled==`function`&&(t.enabled=t.enabled());let n=d.defaultQueryOptions(t);return n._optimisticResults=d.isRestoring?.value?`isRestoring`:`optimistic`,n}),p=new e(d,f.value),m=f.value.shallow?s(p.getCurrentResult()):i(p.getCurrentResult()),h=()=>{};d.isRestoring&&t(d.isRestoring,e=>{e||(h(),h=p.subscribe(e=>{k(m,e)}))},{immediate:!0});let g=()=>{p.setOptions(f.value),k(m,p.getCurrentResult())};t(f,g),r(()=>{h()});let v=(...e)=>(g(),m.refetch(...e)),y=()=>new Promise((e,n)=>{let r=()=>{},i=()=>{if(f.value.enabled!==!1){p.setOptions(f.value);let t=p.getOptimisticResult(f.value);t.isStale?(r(),p.fetchOptimistic(f.value).then(e,t=>{_(f.value.throwOnError,[t,p.getCurrentQuery()])?n(t):e(p.getCurrentResult())})):(r(),e(t))}};i(),r=t(f,i)});t(()=>m.error,e=>{if(m.isError&&!m.isFetching&&_(f.value.throwOnError,[e,p.getCurrentQuery()]))throw e});let b=n(f.value.shallow?o(m):l(m));for(let e in m)typeof m[e]==`function`&&(b[e]=m[e]);return b.suspense=y,b.refetch=v,b}function B(e,t){return z(A,e,t)}function V(e,c){let u=c||R(),d=a(()=>{let t=typeof e==`function`?e():e;return u.defaultMutationOptions(S(t))}),f=new L(u,d.value),p=d.value.shallow?s(f.getCurrentResult()):i(f.getCurrentResult()),m=f.subscribe(e=>{k(p,e)}),h=(e,t)=>{f.mutate(e,t).catch(()=>{})};t(d,()=>{f.setOptions(d.value)}),r(()=>{m()});let g=n(d.value.shallow?o(p):l(p));return t(()=>p.error,e=>{if(e&&_(d.value.throwOnError,[e]))throw e}),{...g,mutate:h,mutateAsync:p.mutate,reset:p.reset}}export{B as n,R as r,V as t}; |