UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

3 lines (2 loc) 24.1 kB
import e from"react";function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var r;!function(e){e.Idle="idle",e.Loading="loading",e.Error="error",e.Success="success"}(r||(r={}));var n=0,i={},u="undefined"==typeof window;function o(){}var c=console||{error:o,warn:o,log:o};function s(e){c=e}function a(e,t){return"function"==typeof e?e(t):e}function l(e,t){if("function"==typeof t)throw new Error("Cannot stringify non JSON value");return y(t)?Object.keys(t).sort().reduce((function(e,r){return e[r]=t[r],e}),{}):t}function h(e){return JSON.stringify(e,l)}function f(e,t){return e===t||typeof e==typeof t&&("object"==typeof e&&!Object.keys(t).some((function(r){return!f(e[r],t[r])})))}function d(){return"undefined"==typeof document||[void 0,"visible","prerender"].includes(document.visibilityState)}function v(e){var r,n,i,u;return y(e[0])?(r=e[0].queryKey,n=e[0].queryFn,i=e[0].config,u=e[1]):y(e[1])?(r=e[0],i=e[1],u=e[2]):(r=e[0],n=e[1],i=e[2],u=e[3]),i=i?t({queryKey:r},i):{queryKey:r},n&&(i=t({},i,{queryFn:n})),[r,i,u]}function y(e){return e&&"object"==typeof e&&!Array.isArray(e)}function g(e){if(!p(e))return!1;var t=e.constructor;if(void 0===t)return!0;var r=t.prototype;return!!p(r)&&!!r.hasOwnProperty("isPrototypeOf")}function p(e){return"[object Object]"===Object.prototype.toString.call(e)}function b(e){return{status:e,isLoading:e===r.Loading,isSuccess:e===r.Success,isError:e===r.Error,isIdle:e===r.Idle}}var m={queries:{queryKeySerializerFn:function(e){try{var t=Array.isArray(e)?e:[e],r=h(t);return[r,t=JSON.parse(r)]}catch(e){throw new Error("A valid query key is required!")}},enabled:!0,retry:3,retryDelay:function(e){return Math.min(1e3*Math.pow(2,e),3e4)},staleTime:0,cacheTime:3e5,refetchOnWindowFocus:!0,refetchOnMount:!0}};function S(e,r,n,i){return t({},m.shared,m.queries,null==e?void 0:e.shared,null==e?void 0:e.queries,null==r?void 0:r.shared,null==r?void 0:r.queries,n,i)}var w=function(){function e(e){this.config=e,this.clear=this.clear.bind(this),this.refetch=this.refetch.bind(this),this.fetchMore=this.fetchMore.bind(this),this.updateQuery()}var r=e.prototype;return r.subscribe=function(e){return this.started=!0,this.updateListener=e,this.currentQuery.subscribeObserver(this),this.optionalFetch(),this.updateRefetchInterval(),this.unsubscribe.bind(this)},r.unsubscribe=function(e){this.started=!1,this.updateListener=void 0,this.clearRefetchInterval(),this.currentQuery.unsubscribeObserver(this,e)},r.updateConfig=function(e){var t=this.config;this.config=e;var r=this.updateQuery();if(this.started){if(r)return this.optionalFetch(),void this.updateRefetchInterval();e.enabled&&!t.enabled&&this.optionalFetch(),e.enabled===t.enabled&&e.refetchInterval===t.refetchInterval&&e.refetchIntervalInBackground===t.refetchIntervalInBackground||this.updateRefetchInterval()}},r.getCurrentResult=function(){return this.currentResult},r.clear=function(){return this.currentQuery.clear()},r.refetch=function(){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.refetch()}catch(e){return Promise.reject(e)}},r.fetchMore=function(e,t){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.fetchMore(e,t)}catch(e){return Promise.reject(e)}},r.fetch=function(){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.fetch().catch((function(e){c.error(e)}))}catch(e){return Promise.reject(e)}},r.optionalFetch=function(){!this.config.enabled||this.config.suspense&&this.currentResult.isFetched||!this.currentResult.isStale||!this.config.refetchOnMount&&1!==this.currentQuery.observers.length||this.fetch()},r.updateRefetchInterval=function(){var e=this;u||(this.clearRefetchInterval(),!this.config.enabled||!this.config.refetchInterval||this.config.refetchInterval<0||this.config.refetchInterval===1/0||(this.refetchIntervalId=setInterval((function(){(e.config.refetchIntervalInBackground||d())&&e.fetch()}),this.config.refetchInterval)))},r.clearRefetchInterval=function(){this.refetchIntervalId&&(clearInterval(this.refetchIntervalId),this.refetchIntervalId=void 0)},r.createResult=function(){var e=this.currentQuery,r=this.previousResult,n=this.config,i=e.state,u=i.canFetchMore,o=i.error,c=i.failureCount,s=i.isFetched,a=i.isFetching,l=i.isFetchingMore,h=i.isLoading,f=i.isStale,d=e.state,v=d.data,y=d.status,g=d.updatedAt;return n.keepPreviousData&&h&&(null==r?void 0:r.isSuccess)&&(v=r.data,g=r.updatedAt,y=r.status),t({},b(y),{canFetchMore:u,clear:this.clear,data:v,error:o,failureCount:c,fetchMore:this.fetchMore,isFetched:s,isFetching:a,isFetchingMore:l,isStale:f,query:e,refetch:this.refetch,updatedAt:g})},r.updateQuery=function(){var e=this.currentQuery,r=e?t({},this.config,{initialData:void 0}):this.config,n=r.queryCache.buildQuery(r.queryKey,r);return n!==e&&(this.previousResult=this.currentResult,this.currentQuery=n,this.currentResult=this.createResult(),this.started&&(null==e||e.unsubscribeObserver(this),this.currentQuery.subscribeObserver(this)),!0)},r.onQueryUpdate=function(e,t){var r;this.currentResult=this.createResult();var n,i,u,o,c=this.currentResult,s=c.data,a=c.error,l=c.isSuccess,h=c.isError;if("Success"===t.type&&l)null==(n=(i=this.config).onSuccess)||n.call(i,s),null==(u=(o=this.config).onSettled)||u.call(o,s,null),this.updateRefetchInterval();else if("Error"===t.type&&h){var f,d,v,y;null==(f=(d=this.config).onError)||f.call(d,a),null==(v=(y=this.config).onSettled)||v.call(y,void 0,a),this.updateRefetchInterval()}null==(r=this.updateListener)||r.call(this,this.currentResult)},e}();function C(){}function F(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}function q(e,t,r){return r?t?t(e):e:(e&&e.then||(e=Promise.resolve(e)),t?e.then(t):e)}function E(e){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];try{return Promise.resolve(e.apply(this,t))}catch(e){return Promise.reject(e)}}}function R(e,t,r){if(!e.s){if(r instanceof M){if(!r.s)return void(r.o=R.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(R.bind(null,e,t),R.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var O,M=function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var u=1&i?t:r;if(u){try{R(n,1,u(this.v))}catch(e){R(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?R(n,1,t?t(i):i):r?R(n,1,r(i)):R(n,2,i)}catch(e){R(n,2,e)}},n},e}();function Q(e){return e instanceof M&&1&e.s}function I(e,t){if(e)throw t;return t}function T(e,t){try{var r=e()}catch(e){return t(!0,e)}return r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}!function(e){e.Failed="Failed",e.MarkStale="MarkStale",e.Fetch="Fetch",e.Success="Success",e.Error="Error",e.SetState="SetState"}(O||(O={}));var L=function(){function e(e){var n,i,o,c,s,a;if(this.config=e.config,this.queryCache=e.queryCache,this.queryKey=e.queryKey,this.queryHash=e.queryHash,this.notifyGlobalListeners=e.notifyGlobalListeners,this.observers=[],this.state=(n=e.config,o="function"==typeof n.initialData?n.initialData():n.initialData,c=void 0!==o,s=!n.enabled||("function"==typeof n.initialStale?n.initialStale():null!=(i=n.initialStale)?i:!c),t({},b(a=c?r.Success:n.enabled?r.Loading:r.Idle),{error:null,isFetched:!1,isFetching:a===r.Loading,isFetchingMore:!1,failureCount:0,isStale:s,data:o,updatedAt:c?Date.now():0})),e.config.infinite){var l=e.config,h=this.state.data;void 0!==h&&(this.fetchMoreVariable=l.getFetchMore(h[h.length-1],h),this.state.canFetchMore=Boolean(this.fetchMoreVariable)),this.pageVariables||(this.pageVariables=[[].concat(this.queryKey)])}!u&&this.state.data&&(this.scheduleStaleTimeout(),this.heal(),this.scheduleGarbageCollection())}var n=e.prototype;return n.updateConfig=function(e){this.config=e},n.dispatch=function(e){var n=this;this.state=function(e,n){switch(n.type){case O.Failed:return t({},e,{failureCount:e.failureCount+1});case O.MarkStale:return t({},e,{isStale:!0});case O.Fetch:var i=void 0!==e.data?r.Success:r.Loading;return t({},e,b(i),{isFetching:!0,failureCount:0});case O.Success:return t({},e,b(r.Success),{data:n.data,error:null,isStale:n.isStale,isFetched:!0,isFetching:!1,updatedAt:Date.now(),failureCount:0});case O.Error:return t({},e,{failureCount:e.failureCount+1,isFetched:!0,isFetching:!1,isStale:!0},!n.cancelled&&t({},b(r.Error),{error:n.error,throwInErrorBoundary:!0}));case O.SetState:return a(n.updater,e);default:return e}}(this.state,e),this.observers.forEach((function(t){return t.onQueryUpdate(n.state,e)})),this.notifyGlobalListeners(this)},n.scheduleStaleTimeout=function(){var e=this;u||(this.clearStaleTimeout(),this.state.isStale||this.config.staleTime===1/0||(this.staleTimeout=setTimeout((function(){e.invalidate()}),this.config.staleTime)))},n.invalidate=function(){this.clearStaleTimeout(),this.state.isStale||this.dispatch({type:O.MarkStale})},n.scheduleGarbageCollection=function(){var e=this;u||(this.clearCacheTimeout(),this.config.cacheTime!==1/0&&(this.cacheTimeout=setTimeout((function(){e.clear()}),void 0===this.state.data&&this.state.status!==r.Error?0:this.config.cacheTime)))},n.refetch=function(){try{var e=this;return function(e){if(e&&e.then)return e.then(C)}(F((function(){return function(e,t){if(!t)return e&&e.then?e.then(C):Promise.resolve()}(e.fetch())}),(function(e){c.error(e)})))}catch(e){return Promise.reject(e)}},n.heal=function(){this.clearCacheTimeout(),this.cancelled=null},n.cancel=function(){this.cancelled=i,this.cancelPromises&&this.cancelPromises(),delete this.promise},n.clearTimersObservers=function(){this.observers.forEach((function(e){e.clearRefetchInterval()}))},n.clearStaleTimeout=function(){this.staleTimeout&&(clearTimeout(this.staleTimeout),this.staleTimeout=void 0)},n.clearCacheTimeout=function(){this.cacheTimeout&&(clearTimeout(this.cacheTimeout),this.cacheTimeout=void 0)},n.clearRetryTimeout=function(){this.retryTimeout&&(clearTimeout(this.retryTimeout),this.retryTimeout=void 0)},n.setState=function(e){this.dispatch({type:O.SetState,updater:e})},n.setData=function(e){var t,r,n=this.state.data,i=a(e,n);i=function e(t,r){if(t===r)return t;var n=Array.isArray(t)&&Array.isArray(r);if(n||g(t)&&g(r)){for(var i=n?t.length:Object.keys(t).length,u=n?r:Object.keys(r),o=u.length,c=n?[]:{},s=0,a=0;a<o;a++){var l=n?a:u[a];c[l]=e(t[l],r[l]),c[l]===t[l]&&s++}return i===o&&s===i?t:c}return r}(n,i),(null==(t=(r=this.config).isDataEqual)?void 0:t.call(r,n,i))&&(i=n);var u=0===this.config.staleTime;this.dispatch({type:O.Success,data:i,isStale:u}),u||this.scheduleStaleTimeout()},n.clear=function(){this.clearStaleTimeout(),this.clearCacheTimeout(),this.clearRetryTimeout(),this.clearTimersObservers(),this.cancel(),delete this.queryCache.queries[this.queryHash],this.notifyGlobalListeners(this)},n.isEnabled=function(){return this.observers.some((function(e){return e.config.enabled}))},n.shouldRefetchOnWindowFocus=function(){return this.isEnabled()&&this.state.isStale&&this.observers.some((function(e){return e.config.refetchOnWindowFocus}))},n.subscribe=function(e){var r=new w(t({queryCache:this.queryCache,queryKey:this.queryKey},this.config));return r.subscribe(e),r},n.subscribeObserver=function(e){this.observers.push(e),this.heal()},n.unsubscribeObserver=function(e,t){this.observers=this.observers.filter((function(t){return t!==e})),this.observers.length||(this.cancel(),t||this.scheduleGarbageCollection())},n.tryFetchData=function(e,t){try{var r=this;return F((function(){var n=r.config.queryFnParamsFilter,i=n?n(t):t,u=e.apply(void 0,i);return r.cancelPromises=function(){var e;return null==(e=u)||null==e.cancel?void 0:e.cancel()},q(u,(function(e){if(delete r.shouldContinueRetryOnFocus,delete r.cancelPromises,r.cancelled)throw r.cancelled;return e}))}),(function(n){var i,u,c=!1;if(delete r.cancelPromises,r.cancelled)throw r.cancelled;return i=function(e){if(c)return e;throw n},(u=function(){if(!0===r.config.retry||r.state.failureCount<r.config.retry||"function"==typeof r.config.retry&&r.config.retry(r.state.failureCount,n)){if(r.dispatch({type:O.Failed}),!d())return r.shouldContinueRetryOnFocus=!0,c=!0,new Promise(o);delete r.shouldContinueRetryOnFocus;var i=a(r.config.retryDelay,r.state.failureCount);return c=!0,q(new Promise((function(n,u){r.retryTimeout=setTimeout(E((function(){return r.cancelled?u(r.cancelled):F((function(){return q(r.tryFetchData(e,t),(function(e){if(r.cancelled)return u(r.cancelled);n(e)}))}),(function(e){if(r.cancelled)return u(r.cancelled);u(e)}))})),i)})))}}())&&u.then?u.then(i):i(u)}))}catch(e){return Promise.reject(e)}},n.fetch=function(e){try{var r=this,n=r.config.queryFn;if(!n)return;if(r.promise)return r.promise;if(r.config.infinite){var i=r.config,u=r.state.data,o=null==e?void 0:e.fetchMore,c=n;n=E((function(){var e,t,n=!1,u=[],o=r.pageVariables?[].concat(r.pageVariables):[],s=[];return e=function(e,t){var r;do{var n=e();if(n&&n.then){if(!Q(n)){r=!0;break}n=n.v}var i=t();if(Q(i)&&(i=i.v),!i)return n}while(!i.then);var u=new M,o=R.bind(null,u,2);return(r?n.then(c):i.then(s)).then(void 0,o),u;function c(r){for(n=r;Q(i=t())&&(i=i.v),i;){if(i.then)return void i.then(s).then(void 0,o);if((n=e())&&n.then){if(!Q(n))return void n.then(c).then(void 0,o);n=n.v}}R(u,1,n)}function s(r){if(r){do{if((n=e())&&n.then){if(!Q(n))return void n.then(c).then(void 0,o);n=n.v}if(Q(r=t())&&(r=r.v),!r)return void R(u,1,n)}while(!r.then);r.then(s).then(void 0,o)}else R(u,1,n)}}((function(){var e=o.shift();return function(e){var t=e();if(t&&t.then)return t.then(C)}((function(){if(u.length){var t=i.getFetchMore(u[u.length-1],u);if(!t)return void(n=!0);var r=[].concat(e.slice(0,-1),[t]),o=u.push;return q(c.apply(void 0,r),(function(e){o.call(u,e),s.push(r)}))}var a=u.push;return q(c.apply(void 0,e),(function(t){a.call(u,t),s.push(e)}))}))}),(function(){return!n&&!!o.length})),t=function(){return r.fetchMoreVariable=i.getFetchMore(u[u.length-1],u),r.state.canFetchMore=Boolean(r.fetchMoreVariable),r.pageVariables=s,u},e&&e.then?e.then(t):t(e)})),o&&(n=E((function(){for(var e=arguments.length,n=new Array(e),s=0;s<e;s++)n[s]=arguments[s];return T((function(){var e=o.fetchMoreVariable,s=o.previous;r.setState((function(e){return t({},e,{isFetchingMore:s?"previous":"next"})}));var a=[].concat(n,[e]);return r.pageVariables?r.pageVariables[s?"unshift":"push"](a):r.pageVariables=[a],q(c.apply(void 0,a),(function(e){var t;return t=u?s?[e].concat(u):[].concat(u,[e]):[e],r.fetchMoreVariable=i.getFetchMore(e,t),r.state.canFetchMore=Boolean(r.fetchMoreVariable),t}))}),(function(e,n){return r.setState((function(e){return t({},e,{isFetchingMore:!1})})),I(e,n)}))})))}return r.promise=E((function(){return r.cancelled=null,F((function(){return r.state.isFetching||r.dispatch({type:O.Fetch}),q(r.tryFetchData(n,r.queryKey),(function(e){return r.setData(e),delete r.promise,e}))}),(function(e){if(r.dispatch({type:O.Error,cancelled:e===r.cancelled,error:e}),delete r.promise,e!==r.cancelled)throw e}))}))(),r.promise}catch(e){return Promise.reject(e)}},n.fetchMore=function(e,t){return this.fetch({fetchMore:{fetchMoreVariable:null!=e?e:this.fetchMoreVariable,previous:(null==t?void 0:t.previous)||!1}})},e}();function P(){}function j(e,t){if(!t)return e&&e.then?e.then(P):Promise.resolve()}function D(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}function k(e,t){var r=e();return r&&r.then?r.then(t):t(r)}var A=function(){function e(e){this.config=e||{},this.globalListeners=[],this.queries={},this.isFetching=0}var r=e.prototype;return r.notifyGlobalListeners=function(e){var t=this;this.isFetching=Object.values(this.queries).reduce((function(e,t){return t.state.isFetching?e+1:e}),0),this.globalListeners.forEach((function(r){return r(t,e)}))},r.getDefaultConfig=function(){return this.config.defaultConfig},r.getDefaultedQueryConfig=function(e){return S(this.getDefaultConfig(),void 0,e,{queryCache:this})},r.subscribe=function(e){var t=this;return this.globalListeners.push(e),function(){t.globalListeners.splice(t.globalListeners.indexOf(e),1)}},r.clear=function(e){Object.values(this.queries).forEach((function(e){return e.clear()})),this.queries={},(null==e?void 0:e.notify)&&this.notifyGlobalListeners()},r.getQueries=function(e,t){if(!0===e)return Object.values(this.queries);var r;if("function"==typeof e)r=e;else{var n=this.getDefaultedQueryConfig().queryKeySerializerFn(e),i=n[0],u=n[1];r=function(e){return(null==t?void 0:t.exact)?e.queryHash===i:f(e.queryKey,u)}}return Object.values(this.queries).filter(r)},r.getQuery=function(e){return this.getQueries(e,{exact:!0})[0]},r.getQueryData=function(e){var t;return null==(t=this.getQuery(e))?void 0:t.state.data},r.removeQueries=function(e,t){this.getQueries(e,t).forEach((function(e){return e.clear()}))},r.cancelQueries=function(e,t){this.getQueries(e,t).forEach((function(e){return e.cancel()}))},r.invalidateQueries=function(e,t){try{var r=this,n=t||{},i=n.refetchActive,u=void 0===i||i,o=n.refetchInactive,c=void 0!==o&&o,s=n.throwOnError;return D((function(){return j(Promise.all(r.getQueries(e,t).map((function(e){if(e.observers.length){if(u&&e.isEnabled())return e.fetch()}else if(c)return e.fetch();return e.invalidate()}))))}),(function(e){if(s)throw e}))}catch(e){return Promise.reject(e)}},r.resetErrorBoundaries=function(){this.getQueries(!0).forEach((function(e){e.state.throwInErrorBoundary=!1}))},r.buildQuery=function(e,t){var r,n=this,i=this.getDefaultedQueryConfig(t),o=i.queryKeySerializerFn(e),c=o[0],s=o[1];return this.queries[c]&&(r=this.queries[c]).updateConfig(i),r||(r=new L({queryCache:this,queryKey:s,queryHash:c,config:i,notifyGlobalListeners:function(e){n.notifyGlobalListeners(e)}}),this.config.frozen||(this.queries[c]=r,u?this.notifyGlobalListeners():setTimeout((function(){n.notifyGlobalListeners()})))),r},r.prefetchQuery=function(){try{for(var e=this,r=arguments.length,n=new Array(r),i=0;i<r;i++)n[i]=arguments[i];y(n[1])&&(n[1].hasOwnProperty("throwOnError")||n[1].hasOwnProperty("force"))&&(n[3]=n[1],n[1]=void 0,n[2]=void 0);var u=v(n),o=u[0],s=u[1],a=u[2],l=t({retry:!1},s);return D((function(){var t=e.buildQuery(o,l);return k((function(){if((null==a?void 0:a.force)||t.state.isStale)return j(t.fetch())}),(function(){return t.state.data}))}),(function(e){if(null==a?void 0:a.throwOnError)throw e;c.error(e)}))}catch(e){return Promise.reject(e)}},r.setQueryData=function(e,t,r){var n=this.getQuery(e);n||(n=this.buildQuery(e,r)),n.setData(t)},e}(),V=G({frozen:u}),K=[V];function G(e){return new A(e)}var B,x=function(){d()&&(void 0===navigator.onLine||navigator.onLine)&&K.forEach((function(e){return e.invalidateQueries((function(e){return!!e.shouldRefetchOnWindowFocus()&&(e.shouldContinueRetryOnFocus&&delete e.promise,!0)})).catch(c.error)}))};function z(e){B&&B(),B=e(x)}z((function(e){var t;if(!u&&(null==(t=window)?void 0:t.addEventListener))return window.addEventListener("visibilitychange",e,!1),window.addEventListener("focus",e,!1),function(){window.removeEventListener("visibilitychange",e),window.removeEventListener("focus",e)}}));var H=e.createContext(V),W=function(){return e.useContext(H)},J=function(t){var r=t.queryCache,n=t.children,i=e.useMemo((function(){return r||G()}),[r]);return e.useEffect((function(){return K.push(i),function(){var e=K.indexOf(i);e>-1&&K.splice(e,1),null==r&&i.clear({notify:!1})}}),[i,r]),e.createElement(H.Provider,{value:i},n)},N=e.createContext(void 0);function U(){return e.useContext(N)}var X,Y=function(r){var n=r.config,i=r.children,u=U(),o=e.useMemo((function(){return u?(r=n,{shared:t({},(e=u).shared,r.shared),queries:t({},e.queries,r.queries),mutations:t({},e.mutations,r.mutations)}):n;var e,r}),[n,u]);return e.createElement(N.Provider,{value:o},i)};function Z(t){var r=e.useRef(t);return r.current=t,e.useCallback((function(){return r.current}),[])}function $(t){var r=e.useRef(!1);return e[u?"useEffect":"useLayoutEffect"]((function(){return r.current=!0,function(){r.current=!1}}),[]),e.useCallback((function(){return r.current?t.apply(void 0,arguments):void 0}),[t])}function _(){var t=$(e.useState()[1]);return e.useCallback((function(){return t({})}),[t])}function ee(){var t=W(),r=_(),n=t.isFetching,i=Z(n);return e.useEffect((function(){return t.subscribe((function(e){i()!==e.isFetching&&r()}))}),[i,t,r]),n}function te(e){var r=U(),n=W(),i=(null==e?void 0:e.queryCache)||n;return function(e,r,n,i){return t({},m.shared,m.mutations,null==e?void 0:e.shared,null==e?void 0:e.mutations,null==r?void 0:r.shared,null==r?void 0:r.mutations,n,i)}(i.getDefaultConfig(),r,e,{queryCache:i})}function re(e,t,r){return r?t?t(e):e:(e&&e.then||(e=Promise.resolve(e)),t?e.then(t):e)}!function(e){e.Reset="Reset",e.Loading="Loading",e.Resolve="Resolve",e.Reject="Reject"}(X||(X={}));var ne=function(){return t({},b(r.Idle),{data:void 0,error:null})};function ie(e,n){switch(n.type){case X.Reset:return ne();case X.Loading:return t({},b(r.Loading),{data:void 0,error:null});case X.Resolve:return t({},b(r.Success),{data:n.data,error:null});case X.Reject:return t({},b(r.Error),{data:void 0,error:n.error});default:return e}}function ue(r,i){void 0===i&&(i={});var u,o=Z(i=te(i)),s=e.useReducer(ie,null,ne),a=s[0],l=$(s[1]),h=Z(r),f=e.useRef(),d=e.useCallback((u=function(e,t){void 0===t&&(t={});var r=o(),i=n++;f.current=i;var u,s=function(){return f.current===i};return function(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}((function(){return l({type:X.Loading}),re(null==r.onMutate?void 0:r.onMutate(e),(function(n){return u=n,re(h()(e),(function(n){return s()&&l({type:X.Resolve,data:n}),re(null==r.onSuccess?void 0:r.onSuccess(n,e),(function(){return re(null==t.onSuccess?void 0:t.onSuccess(n,e),(function(){return re(null==r.onSettled?void 0:r.onSettled(n,null,e),(function(){return re(null==t.onSettled?void 0:t.onSettled(n,null,e),(function(){return n}))}))}))}))}))}))}),(function(n){return c.error(n),re(null==r.onError?void 0:r.onError(n,e,u),(function(){return re(null==t.onError?void 0:t.onError(n,e,u),(function(){return re(null==r.onSettled?void 0:r.onSettled(void 0,n,e,u),(function(){return re(null==t.onSettled?void 0:t.onSettled(void 0,n,e,u),(function(){var e;if(s()&&l({type:X.Reject,error:n}),null!=(e=t.throwOnError)?e:r.throwOnError)throw n}))}))}))}))}))},function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];try{return Promise.resolve(u.apply(this,e))}catch(e){return Promise.reject(e)}}),[l,o,h]),v=e.useCallback((function(){l({type:X.Reset})}),[l]);return e.useEffect((function(){var e=o(),t=e.suspense,r=e.useErrorBoundary;if((null!=r?r:t)&&a.error)throw a.error}),[o,a.error]),[d,t({},a,{reset:v})]}function oe(t){void 0===t&&(t={}),t=function(e){var t=U(),r=W(),n=(null==e?void 0:e.queryCache)||r;return S(n.getDefaultConfig(),t,e,{queryCache:n})}(t);var r=_(),n=e.useRef(),i=!n.current,u=n.current||new w(t);n.current=u,e.useEffect((function(){return u.subscribe((function(){Promise.resolve().then(r)}))}),[u,r]),i||u.updateConfig(t);var o=u.getCurrentResult();if(t.suspense||t.useErrorBoundary){if(o.isError&&o.query.state.throwInErrorBoundary)throw o.error;if(t.enabled&&t.suspense&&!o.isSuccess)throw u.subscribe(),u.fetch().finally((function(){u.unsubscribe(!0)}))}return o}function ce(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];var n=v(t)[1];return oe(n)}function se(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var i=v(r)[1],u=oe(t({},i,{keepPreviousData:!0}));return t({},u,{resolvedData:u.data,latestData:u.query.state.data===u.data?u.data:void 0})}function ae(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var i=v(r)[1];return oe(t({},i,{infinite:!0}))}export{r as QueryStatus,J as ReactQueryCacheProvider,Y as ReactQueryConfigProvider,f as deepIncludes,G as makeQueryCache,V as queryCache,K as queryCaches,s as setConsole,z as setFocusHandler,h as stableStringify,ae as useInfiniteQuery,ee as useIsFetching,ue as useMutation,se as usePaginatedQuery,ce as useQuery,W as useQueryCache}; //# sourceMappingURL=react-query.min.mjs.map