react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
3 lines (2 loc) • 24.6 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactQuery={},e.React)}(this,(function(e,t){"use strict";function r(){return(r=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 n;t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t,(n=e.QueryStatus||(e.QueryStatus={})).Idle="idle",n.Loading="loading",n.Error="error",n.Success="success";var i=0,u={},o="undefined"==typeof window;function c(){}var s=console||{error:c,warn:c,log:c};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 t,n,i,u;return y(e[0])?(t=e[0].queryKey,n=e[0].queryFn,i=e[0].config,u=e[1]):y(e[1])?(t=e[0],i=e[1],u=e[2]):(t=e[0],n=e[1],i=e[2],u=e[3]),i=i?r({queryKey:t},i):{queryKey:t},n&&(i=r({},i,{queryFn:n})),[t,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(t){return{status:t,isLoading:t===e.QueryStatus.Loading,isSuccess:t===e.QueryStatus.Success,isError:t===e.QueryStatus.Error,isIdle:t===e.QueryStatus.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,t,n,i){return r({},m.shared,m.queries,null==e?void 0:e.shared,null==e?void 0:e.queries,null==t?void 0:t.shared,null==t?void 0:t.queries,n,i)}var C=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 t=e.prototype;return t.subscribe=function(e){return this.started=!0,this.updateListener=e,this.currentQuery.subscribeObserver(this),this.optionalFetch(),this.updateRefetchInterval(),this.unsubscribe.bind(this)},t.unsubscribe=function(e){this.started=!1,this.updateListener=void 0,this.clearRefetchInterval(),this.currentQuery.unsubscribeObserver(this,e)},t.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()}},t.getCurrentResult=function(){return this.currentResult},t.clear=function(){return this.currentQuery.clear()},t.refetch=function(){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.refetch()}catch(e){return Promise.reject(e)}},t.fetchMore=function(e,t){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.fetchMore(e,t)}catch(e){return Promise.reject(e)}},t.fetch=function(){try{return this.currentQuery.updateConfig(this.config),this.currentQuery.fetch().catch((function(e){s.error(e)}))}catch(e){return Promise.reject(e)}},t.optionalFetch=function(){!this.config.enabled||this.config.suspense&&this.currentResult.isFetched||!this.currentResult.isStale||!this.config.refetchOnMount&&1!==this.currentQuery.observers.length||this.fetch()},t.updateRefetchInterval=function(){var e=this;o||(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)))},t.clearRefetchInterval=function(){this.refetchIntervalId&&(clearInterval(this.refetchIntervalId),this.refetchIntervalId=void 0)},t.createResult=function(){var e=this.currentQuery,t=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==t?void 0:t.isSuccess)&&(v=t.data,g=t.updatedAt,y=t.status),r({},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})},t.updateQuery=function(){var e=this.currentQuery,t=e?r({},this.config,{initialData:void 0}):this.config,n=t.queryCache.buildQuery(t.queryKey,t);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)},t.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 Q(){}function w(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}function F(e,t,r){return r?t?t(e):e:(e&&e.then||(e=Promise.resolve(e)),t?e.then(t):e)}function q(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 E(e,t,r){if(!e.s){if(r instanceof O){if(!r.s)return void(r.o=E.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(E.bind(null,e,t),E.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var R,O=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{E(n,1,u(this.v))}catch(e){E(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?E(n,1,t?t(i):i):r?E(n,1,r(i)):E(n,2,i)}catch(e){E(n,2,e)}},n},e}();function M(e){return e instanceof O&&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"}(R||(R={}));var P=function(){function t(t){var n,i,u,c,s,a;if(this.config=t.config,this.queryCache=t.queryCache,this.queryKey=t.queryKey,this.queryHash=t.queryHash,this.notifyGlobalListeners=t.notifyGlobalListeners,this.observers=[],this.state=(n=t.config,u="function"==typeof n.initialData?n.initialData():n.initialData,c=void 0!==u,s=!n.enabled||("function"==typeof n.initialStale?n.initialStale():null!=(i=n.initialStale)?i:!c),r({},b(a=c?e.QueryStatus.Success:n.enabled?e.QueryStatus.Loading:e.QueryStatus.Idle),{error:null,isFetched:!1,isFetching:a===e.QueryStatus.Loading,isFetchingMore:!1,failureCount:0,isStale:s,data:u,updatedAt:c?Date.now():0})),t.config.infinite){var l=t.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)])}!o&&this.state.data&&(this.scheduleStaleTimeout(),this.heal(),this.scheduleGarbageCollection())}var n=t.prototype;return n.updateConfig=function(e){this.config=e},n.dispatch=function(t){var n=this;this.state=function(t,n){switch(n.type){case R.Failed:return r({},t,{failureCount:t.failureCount+1});case R.MarkStale:return r({},t,{isStale:!0});case R.Fetch:var i=void 0!==t.data?e.QueryStatus.Success:e.QueryStatus.Loading;return r({},t,b(i),{isFetching:!0,failureCount:0});case R.Success:return r({},t,b(e.QueryStatus.Success),{data:n.data,error:null,isStale:n.isStale,isFetched:!0,isFetching:!1,updatedAt:Date.now(),failureCount:0});case R.Error:return r({},t,{failureCount:t.failureCount+1,isFetched:!0,isFetching:!1,isStale:!0},!n.cancelled&&r({},b(e.QueryStatus.Error),{error:n.error,throwInErrorBoundary:!0}));case R.SetState:return a(n.updater,t);default:return t}}(this.state,t),this.observers.forEach((function(e){return e.onQueryUpdate(n.state,t)})),this.notifyGlobalListeners(this)},n.scheduleStaleTimeout=function(){var e=this;o||(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:R.MarkStale})},n.scheduleGarbageCollection=function(){var t=this;o||(this.clearCacheTimeout(),this.config.cacheTime!==1/0&&(this.cacheTimeout=setTimeout((function(){t.clear()}),void 0===this.state.data&&this.state.status!==e.QueryStatus.Error?0:this.config.cacheTime)))},n.refetch=function(){try{var e=this;return function(e){if(e&&e.then)return e.then(Q)}(w((function(){return function(e,t){if(!t)return e&&e.then?e.then(Q):Promise.resolve()}(e.fetch())}),(function(e){s.error(e)})))}catch(e){return Promise.reject(e)}},n.heal=function(){this.clearCacheTimeout(),this.cancelled=null},n.cancel=function(){this.cancelled=u,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:R.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:R.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 t=new C(r({queryCache:this.queryCache,queryKey:this.queryKey},this.config));return t.subscribe(e),t},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 w((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()},F(u,(function(e){if(delete r.shouldContinueRetryOnFocus,delete r.cancelPromises,r.cancelled)throw r.cancelled;return e}))}),(function(n){var i,u,o=!1;if(delete r.cancelPromises,r.cancelled)throw r.cancelled;return i=function(e){if(o)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:R.Failed}),!d())return r.shouldContinueRetryOnFocus=!0,o=!0,new Promise(c);delete r.shouldContinueRetryOnFocus;var i=a(r.config.retryDelay,r.state.failureCount);return o=!0,F(new Promise((function(n,u){r.retryTimeout=setTimeout(q((function(){return r.cancelled?u(r.cancelled):w((function(){return F(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 t=this,n=t.config.queryFn;if(!n)return;if(t.promise)return t.promise;if(t.config.infinite){var i=t.config,u=t.state.data,o=null==e?void 0:e.fetchMore,c=n;n=q((function(){var e,r,n=!1,u=[],o=t.pageVariables?[].concat(t.pageVariables):[],s=[];return e=function(e,t){var r;do{var n=e();if(n&&n.then){if(!M(n)){r=!0;break}n=n.v}var i=t();if(M(i)&&(i=i.v),!i)return n}while(!i.then);var u=new O,o=E.bind(null,u,2);return(r?n.then(c):i.then(s)).then(void 0,o),u;function c(r){for(n=r;M(i=t())&&(i=i.v),i;){if(i.then)return void i.then(s).then(void 0,o);if((n=e())&&n.then){if(!M(n))return void n.then(c).then(void 0,o);n=n.v}}E(u,1,n)}function s(r){if(r){do{if((n=e())&&n.then){if(!M(n))return void n.then(c).then(void 0,o);n=n.v}if(M(r=t())&&(r=r.v),!r)return void E(u,1,n)}while(!r.then);r.then(s).then(void 0,o)}else E(u,1,n)}}((function(){var e=o.shift();return function(e){var t=e();if(t&&t.then)return t.then(Q)}((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 F(c.apply(void 0,r),(function(e){o.call(u,e),s.push(r)}))}var a=u.push;return F(c.apply(void 0,e),(function(t){a.call(u,t),s.push(e)}))}))}),(function(){return!n&&!!o.length})),r=function(){return t.fetchMoreVariable=i.getFetchMore(u[u.length-1],u),t.state.canFetchMore=Boolean(t.fetchMoreVariable),t.pageVariables=s,u},e&&e.then?e.then(r):r(e)})),o&&(n=q((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;t.setState((function(e){return r({},e,{isFetchingMore:s?"previous":"next"})}));var a=[].concat(n,[e]);return t.pageVariables?t.pageVariables[s?"unshift":"push"](a):t.pageVariables=[a],F(c.apply(void 0,a),(function(e){var r;return r=u?s?[e].concat(u):[].concat(u,[e]):[e],t.fetchMoreVariable=i.getFetchMore(e,r),t.state.canFetchMore=Boolean(t.fetchMoreVariable),r}))}),(function(e,n){return t.setState((function(e){return r({},e,{isFetchingMore:!1})})),I(e,n)}))})))}return t.promise=q((function(){return t.cancelled=null,w((function(){return t.state.isFetching||t.dispatch({type:R.Fetch}),F(t.tryFetchData(n,t.queryKey),(function(e){return t.setData(e),delete t.promise,e}))}),(function(e){if(t.dispatch({type:R.Error,cancelled:e===t.cancelled,error:e}),delete t.promise,e!==t.cancelled)throw e}))}))(),t.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}})},t}();function L(){}function j(e,t){if(!t)return e&&e.then?e.then(L):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 t=e.prototype;return t.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)}))},t.getDefaultConfig=function(){return this.config.defaultConfig},t.getDefaultedQueryConfig=function(e){return S(this.getDefaultConfig(),void 0,e,{queryCache:this})},t.subscribe=function(e){var t=this;return this.globalListeners.push(e),function(){t.globalListeners.splice(t.globalListeners.indexOf(e),1)}},t.clear=function(e){Object.values(this.queries).forEach((function(e){return e.clear()})),this.queries={},(null==e?void 0:e.notify)&&this.notifyGlobalListeners()},t.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)},t.getQuery=function(e){return this.getQueries(e,{exact:!0})[0]},t.getQueryData=function(e){var t;return null==(t=this.getQuery(e))?void 0:t.state.data},t.removeQueries=function(e,t){this.getQueries(e,t).forEach((function(e){return e.clear()}))},t.cancelQueries=function(e,t){this.getQueries(e,t).forEach((function(e){return e.cancel()}))},t.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)}},t.resetErrorBoundaries=function(){this.getQueries(!0).forEach((function(e){e.state.throwInErrorBoundary=!1}))},t.buildQuery=function(e,t){var r,n=this,i=this.getDefaultedQueryConfig(t),u=i.queryKeySerializerFn(e),c=u[0],s=u[1];return this.queries[c]&&(r=this.queries[c]).updateConfig(i),r||(r=new P({queryCache:this,queryKey:s,queryHash:c,config:i,notifyGlobalListeners:function(e){n.notifyGlobalListeners(e)}}),this.config.frozen||(this.queries[c]=r,o?this.notifyGlobalListeners():setTimeout((function(){n.notifyGlobalListeners()})))),r},t.prefetchQuery=function(){try{for(var e=this,t=arguments.length,n=new Array(t),i=0;i<t;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],c=u[1],a=u[2],l=r({retry:!1},c);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;s.error(e)}))}catch(e){return Promise.reject(e)}},t.setQueryData=function(e,t,r){var n=this.getQuery(e);n||(n=this.buildQuery(e,r)),n.setData(t)},e}(),V=G({frozen:o}),K=[V];function G(e){return new A(e)}var x,B=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(s.error)}))};function H(e){x&&x(),x=e(B)}H((function(e){var t;if(!o&&(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 z=t.createContext(V),W=function(){return t.useContext(z)},J=t.createContext(void 0);function N(){return t.useContext(J)}var U;function _(e){var r=t.useRef(e);return r.current=e,t.useCallback((function(){return r.current}),[])}function X(e){var r=t.useRef(!1);return t[o?"useEffect":"useLayoutEffect"]((function(){return r.current=!0,function(){r.current=!1}}),[]),t.useCallback((function(){return r.current?e.apply(void 0,arguments):void 0}),[e])}function Y(){var e=X(t.useState()[1]);return t.useCallback((function(){return e({})}),[e])}function Z(e){var t=N(),n=W(),i=(null==e?void 0:e.queryCache)||n;return function(e,t,n,i){return r({},m.shared,m.mutations,null==e?void 0:e.shared,null==e?void 0:e.mutations,null==t?void 0:t.shared,null==t?void 0:t.mutations,n,i)}(i.getDefaultConfig(),t,e,{queryCache:i})}function $(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"}(U||(U={}));var ee=function(){return r({},b(e.QueryStatus.Idle),{data:void 0,error:null})};function te(t,n){switch(n.type){case U.Reset:return ee();case U.Loading:return r({},b(e.QueryStatus.Loading),{data:void 0,error:null});case U.Resolve:return r({},b(e.QueryStatus.Success),{data:n.data,error:null});case U.Reject:return r({},b(e.QueryStatus.Error),{data:void 0,error:n.error});default:return t}}function re(e){void 0===e&&(e={}),e=function(e){var t=N(),r=W(),n=(null==e?void 0:e.queryCache)||r;return S(n.getDefaultConfig(),t,e,{queryCache:n})}(e);var r=Y(),n=t.useRef(),i=!n.current,u=n.current||new C(e);n.current=u,t.useEffect((function(){return u.subscribe((function(){Promise.resolve().then(r)}))}),[u,r]),i||u.updateConfig(e);var o=u.getCurrentResult();if(e.suspense||e.useErrorBoundary){if(o.isError&&o.query.state.throwInErrorBoundary)throw o.error;if(e.enabled&&e.suspense&&!o.isSuccess)throw u.subscribe(),u.fetch().finally((function(){u.unsubscribe(!0)}))}return o}e.ReactQueryCacheProvider=function(e){var r=e.queryCache,n=e.children,i=t.useMemo((function(){return r||G()}),[r]);return t.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]),t.createElement(z.Provider,{value:i},n)},e.ReactQueryConfigProvider=function(e){var n=e.config,i=e.children,u=N(),o=t.useMemo((function(){return u?(t=n,{shared:r({},(e=u).shared,t.shared),queries:r({},e.queries,t.queries),mutations:r({},e.mutations,t.mutations)}):n;var e,t}),[n,u]);return t.createElement(J.Provider,{value:o},i)},e.deepIncludes=f,e.makeQueryCache=G,e.queryCache=V,e.queryCaches=K,e.setConsole=function(e){s=e},e.setFocusHandler=H,e.stableStringify=h,e.useInfiniteQuery=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];var i=v(t)[1];return re(r({},i,{infinite:!0}))},e.useIsFetching=function(){var e=W(),r=Y(),n=e.isFetching,i=_(n);return t.useEffect((function(){return e.subscribe((function(e){i()!==e.isFetching&&r()}))}),[i,e,r]),n},e.useMutation=function(e,n){void 0===n&&(n={});var u,o=_(n=Z(n)),c=t.useReducer(te,null,ee),a=c[0],l=X(c[1]),h=_(e),f=t.useRef(),d=t.useCallback((u=function(e,t){void 0===t&&(t={});var r=o(),n=i++;f.current=n;var u,c=function(){return f.current===n};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:U.Loading}),$(null==r.onMutate?void 0:r.onMutate(e),(function(n){return u=n,$(h()(e),(function(n){return c()&&l({type:U.Resolve,data:n}),$(null==r.onSuccess?void 0:r.onSuccess(n,e),(function(){return $(null==t.onSuccess?void 0:t.onSuccess(n,e),(function(){return $(null==r.onSettled?void 0:r.onSettled(n,null,e),(function(){return $(null==t.onSettled?void 0:t.onSettled(n,null,e),(function(){return n}))}))}))}))}))}))}),(function(n){return s.error(n),$(null==r.onError?void 0:r.onError(n,e,u),(function(){return $(null==t.onError?void 0:t.onError(n,e,u),(function(){return $(null==r.onSettled?void 0:r.onSettled(void 0,n,e,u),(function(){return $(null==t.onSettled?void 0:t.onSettled(void 0,n,e,u),(function(){var e;if(c()&&l({type:U.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=t.useCallback((function(){l({type:U.Reset})}),[l]);return t.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,r({},a,{reset:v})]},e.usePaginatedQuery=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];var i=v(t)[1],u=re(r({},i,{keepPreviousData:!0}));return r({},u,{resolvedData:u.data,latestData:u.query.state.data===u.data?u.data:void 0})},e.useQuery=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];var n=v(t)[1];return re(n)},e.useQueryCache=W,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=react-query.production.min.js.map