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