react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
3 lines (2 loc) • 37 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).ReactQueryCore={})}(this,(function(t){"use strict";function e(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function n(){return(n=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t}).apply(this,arguments)}var i="undefined"==typeof window;function r(){}function s(t,e){return"function"==typeof t?t(e):t}function o(t){return"number"==typeof t&&t>=0&&t!==1/0}function u(t){return Array.isArray(t)?t:[t]}function a(t,e){return t.filter((function(t){return-1===e.indexOf(t)}))}function c(t,e){return Math.max(t+(e||0)-Date.now(),0)}function h(t,e,i){return g(t)?"function"==typeof e?n({},i,{queryKey:t,queryFn:e}):n({},e,{queryKey:t}):t}function l(t,e,i){return g(t)?[n({},e,{queryKey:t}),i]:[t||{},e]}function f(t,e){var n,i=t.active,r=t.exact,s=t.fetching,o=t.inactive,u=t.predicate,a=t.queryKey,c=t.stale;if(g(a))if(r){var h=d(e.options);if(e.queryHash!==h(a))return!1}else if(!p(e.queryKey,a))return!1;return!1===o||i&&!o?n=!0:(!1===i||o&&!i)&&(n=!1),("boolean"!=typeof n||e.isActive()===n)&&(("boolean"!=typeof c||e.isStale()===c)&&(("boolean"!=typeof s||e.isFetching()===s)&&!(u&&!u(e))))}function d(t){return(null==t?void 0:t.queryKeyHashFn)||v}function v(t){return e=t,JSON.stringify(e,(function(t,e){return m(e)?Object.keys(e).sort().reduce((function(t,n){return t[n]=e[n],t}),{}):e}));var e}function p(t,e){return function t(e,n){if(e===n)return!0;if(typeof e!=typeof n)return!1;if(e&&n&&"object"==typeof e&&"object"==typeof n)return!Object.keys(n).some((function(i){return!t(e[i],n[i])}));return!1}(u(t),u(e))}function y(t,e){if(t===e)return t;var n=Array.isArray(t)&&Array.isArray(e);if(n||m(t)&&m(e)){for(var i=n?t.length:Object.keys(t).length,r=n?e:Object.keys(e),s=r.length,o=n?[]:{},u=0,a=0;a<s;a++){var c=n?a:r[a];o[c]=y(t[c],e[c]),o[c]===t[c]&&u++}return i===s&&u===i?t:o}return e}function m(t){if(!b(t))return!1;var e=t.constructor;if(void 0===e)return!0;var n=e.prototype;return!!b(n)&&!!n.hasOwnProperty("isPrototypeOf")}function b(t){return"[object Object]"===Object.prototype.toString.call(t)}function g(t){return"string"==typeof t||Array.isArray(t)}function O(t){return{status:t,isLoading:"loading"===t,isSuccess:"success"===t,isError:"error"===t,isIdle:"idle"===t}}function P(t){Promise.resolve().then(t).catch((function(t){return setTimeout((function(){throw t}))}))}var C=new(function(){function t(){this.queue=[],this.transactions=0,this.notifyFn=function(t){t()},this.batchNotifyFn=function(t){t()}}var e=t.prototype;return e.batch=function(t){this.transactions++;var e=t();return this.transactions--,this.transactions||this.flush(),e},e.schedule=function(t){var e=this;this.transactions?this.queue.push(t):P((function(){e.notifyFn(t)}))},e.batchCalls=function(t){var e=this;return function(){for(var n=arguments.length,i=new Array(n),r=0;r<n;r++)i[r]=arguments[r];e.schedule((function(){t.apply(void 0,i)}))}},e.flush=function(){var t=this,e=this.queue;this.queue=[],e.length&&P((function(){t.batchNotifyFn((function(){e.forEach((function(e){t.notifyFn(e)}))}))}))},e.setNotifyFunction=function(t){this.notifyFn=t},e.setBatchNotifyFunction=function(t){this.batchNotifyFn=t},t}()),F=console||{error:r,warn:r,log:r};function q(){return F}var R=function(){function t(){this.listeners=[]}var e=t.prototype;return e.subscribe=function(t){var e=this,n=t||function(){};return this.listeners.push(n),this.onSubscribe(),function(){e.listeners=e.listeners.filter((function(t){return t!==n})),e.onUnsubscribe()}},e.hasListeners=function(){return this.listeners.length>0},e.onSubscribe=function(){},e.onUnsubscribe=function(){},t}(),w=new(function(t){function n(){return t.apply(this,arguments)||this}e(n,t);var r=n.prototype;return r.onSubscribe=function(){this.removeEventListener||this.setDefaultEventListener()},r.setEventListener=function(t){var e=this;this.removeEventListener&&this.removeEventListener(),this.removeEventListener=t((function(t){"boolean"==typeof t?e.setFocused(t):e.onFocus()}))},r.setFocused=function(t){this.focused=t,t&&this.onFocus()},r.onFocus=function(){this.listeners.forEach((function(t){t()}))},r.isFocused=function(){return"boolean"==typeof this.focused?this.focused:"undefined"==typeof document||[void 0,"visible","prerender"].includes(document.visibilityState)},r.setDefaultEventListener=function(){var t;!i&&(null==(t=window)?void 0:t.addEventListener)&&this.setEventListener((function(t){return window.addEventListener("visibilitychange",t,!1),window.addEventListener("focus",t,!1),function(){window.removeEventListener("visibilitychange",t),window.removeEventListener("focus",t)}}))},n}(R)),E=new(function(t){function n(){return t.apply(this,arguments)||this}e(n,t);var r=n.prototype;return r.onSubscribe=function(){this.removeEventListener||this.setDefaultEventListener()},r.setEventListener=function(t){var e=this;this.removeEventListener&&this.removeEventListener(),this.removeEventListener=t((function(t){"boolean"==typeof t?e.setOnline(t):e.onOnline()}))},r.setOnline=function(t){this.online=t,t&&this.onOnline()},r.onOnline=function(){this.listeners.forEach((function(t){t()}))},r.isOnline=function(){return"boolean"==typeof this.online?this.online:void 0===navigator.onLine||navigator.onLine},r.setDefaultEventListener=function(){var t;!i&&(null==(t=window)?void 0:t.addEventListener)&&this.setEventListener((function(t){return window.addEventListener("online",t,!1),function(){window.removeEventListener("online",t)}}))},n}(R));function M(t){return Math.min(1e3*Math.pow(2,t),3e4)}function Q(t){return"function"==typeof(null==t?void 0:t.cancel)}var S=function(t){this.revert=null==t?void 0:t.revert,this.silent=null==t?void 0:t.silent};function D(t){return t instanceof S}var x=function(t){var e,n,i,r,o=this,u=!1;this.cancel=function(t){return null==e?void 0:e(t)},this.cancelRetry=function(){u=!0},this.continue=function(){return null==n?void 0:n()},this.failureCount=0,this.isPaused=!1,this.isResolved=!1,this.isTransportCancelable=!1,this.promise=new Promise((function(t,e){i=t,r=e}));var a=function(t){o.isResolved=!0,null==n||n(),i(t)},c=function(t){o.isResolved=!0,null==n||n(),r(t)};!function i(){if(!o.isResolved){var r;try{r=t.fn()}catch(t){r=Promise.reject(t)}e=function(t){if(c(new S(t)),Q(r))try{r.cancel()}catch(t){}},o.isTransportCancelable=Q(r),Promise.resolve(r).then(a).catch((function(e){var r,a;if(!o.isResolved){var h,l=null!=(r=t.retry)?r:3,f=s(null!=(a=t.retryDelay)?a:M,o.failureCount)||0,d=!0===l||"number"==typeof l&&o.failureCount<l||"function"==typeof l&&l(o.failureCount,e);if(!u&&d)o.failureCount++,null==t.onFail||t.onFail(o.failureCount,e),(h=f,new Promise((function(t){setTimeout(t,h)}))).then((function(){if(!w.isFocused()||!E.isOnline())return new Promise((function(e){n=e,o.isPaused=!0,null==t.onPause||t.onPause()})).then((function(){n=void 0,o.isPaused=!1,null==t.onContinue||t.onContinue()}))})).then((function(){u?c(e):i()}));else c(e)}}))}}()},U=function(){function t(t){this.defaultOptions=t.defaultOptions,this.setOptions(t.options),this.observers=[],this.cache=t.cache,this.queryKey=t.queryKey,this.queryHash=t.queryHash,this.initialState=t.state||this.getDefaultState(this.options),this.state=this.initialState,this.scheduleGc()}var e=t.prototype;return e.setOptions=function(t){var e;this.options=n({},this.defaultOptions,t),this.cacheTime=Math.max(this.cacheTime||0,null!=(e=this.options.cacheTime)?e:3e5)},e.setDefaultOptions=function(t){this.defaultOptions=t},e.scheduleGc=function(){var t=this;this.clearGcTimeout(),o(this.cacheTime)&&(this.gcTimeout=setTimeout((function(){t.optionalRemove()}),this.cacheTime))},e.clearGcTimeout=function(){clearTimeout(this.gcTimeout),this.gcTimeout=void 0},e.optionalRemove=function(){this.observers.length||this.state.isFetching||this.cache.remove(this)},e.setData=function(t,e){var n,i,r=this.state.data,o=s(t,r);return(null==(n=(i=this.options).isDataEqual)?void 0:n.call(i,r,o))?o=r:!1!==this.options.structuralSharing&&(o=y(r,o)),this.dispatch({data:o,type:"success",dataUpdatedAt:null==e?void 0:e.updatedAt}),o},e.setState=function(t){this.dispatch({type:"setState",state:t})},e.cancel=function(t){var e,n=this.promise;return null==(e=this.retryer)||e.cancel(t),n?n.then(r).catch(r):Promise.resolve()},e.destroy=function(){this.clearGcTimeout(),this.cancel()},e.reset=function(){this.destroy(),this.setState(this.initialState)},e.isActive=function(){return this.observers.some((function(t){return!1!==t.options.enabled}))},e.isFetching=function(){return this.state.isFetching},e.isStale=function(){return this.state.isInvalidated||!this.state.dataUpdatedAt||this.observers.some((function(t){return t.getCurrentResult().isStale}))},e.isStaleByTime=function(t){return void 0===t&&(t=0),this.state.isInvalidated||!this.state.dataUpdatedAt||!c(this.state.dataUpdatedAt,t)},e.onFocus=function(){var t,e=this.observers.find((function(t){return t.willFetchOnWindowFocus()}));e&&e.refetch(),null==(t=this.retryer)||t.continue()},e.onOnline=function(){var t,e=this.observers.find((function(t){return t.willFetchOnReconnect()}));e&&e.refetch(),null==(t=this.retryer)||t.continue()},e.addObserver=function(t){-1===this.observers.indexOf(t)&&(this.observers.push(t),this.clearGcTimeout(),this.cache.notify(this))},e.removeObserver=function(t){-1!==this.observers.indexOf(t)&&(this.observers=this.observers.filter((function(e){return e!==t})),this.observers.length||(this.retryer&&(this.retryer.isTransportCancelable?this.retryer.cancel():this.retryer.cancelRetry()),this.cacheTime?this.scheduleGc():this.cache.remove(this)),this.cache.notify(this))},e.invalidate=function(){this.state.isInvalidated||this.dispatch({type:"invalidate"})},e.fetch=function(t,e){var n,i,r=this;if(this.state.isFetching)if(this.state.dataUpdatedAt&&(null==e?void 0:e.cancelRefetch))this.cancel({silent:!0});else if(this.promise)return this.promise;if(t&&this.setOptions(t),!this.options.queryFn){var s=this.observers.find((function(t){return t.options.queryFn}));s&&this.setOptions(s.options)}var o,a,c=u(this.queryKey),h={queryKey:c,pageParam:void 0},l={fetchOptions:e,options:this.options,queryKey:c,state:this.state,fetchFn:function(){return r.options.queryFn?r.options.queryFn(h):Promise.reject("Missing queryFn")}};(null==(n=this.options.behavior)?void 0:n.onFetch)&&(null==(o=this.options.behavior)||o.onFetch(l));this.state.isFetching&&this.state.fetchMeta===(null==(i=l.fetchOptions)?void 0:i.meta)||this.dispatch({type:"fetch",meta:null==(a=l.fetchOptions)?void 0:a.meta});return this.retryer=new x({fn:l.fetchFn,onFail:function(){r.dispatch({type:"failed"})},onPause:function(){r.dispatch({type:"pause"})},onContinue:function(){r.dispatch({type:"continue"})},retry:l.options.retry,retryDelay:l.options.retryDelay}),this.promise=this.retryer.promise.then((function(t){return r.setData(t)})).catch((function(t){throw D(t)&&t.silent||r.dispatch({type:"error",error:t}),D(t)||q().error(t),t})).finally((function(){0===r.cacheTime&&r.optionalRemove()})),this.promise},e.dispatch=function(t){var e=this;this.state=this.reducer(this.state,t),C.batch((function(){e.observers.forEach((function(e){e.onQueryUpdate(t)})),e.cache.notify(e)}))},e.getDefaultState=function(t){var e="function"==typeof t.initialData?t.initialData():t.initialData,n=void 0!==e;return{data:e,dataUpdateCount:0,dataUpdatedAt:n?Date.now():0,error:null,errorUpdateCount:0,errorUpdatedAt:0,fetchFailureCount:0,fetchMeta:null,isFetching:!1,isInvalidated:!1,isPaused:!1,status:n?"success":"idle"}},e.reducer=function(t,e){var i,r;switch(e.type){case"failed":return n({},t,{fetchFailureCount:t.fetchFailureCount+1});case"pause":return n({},t,{isPaused:!0});case"continue":return n({},t,{isPaused:!1});case"fetch":return n({},t,{fetchFailureCount:0,fetchMeta:null!=(i=e.meta)?i:null,isFetching:!0,isPaused:!1,status:"idle"===t.status?"loading":t.status});case"success":return n({},t,{data:e.data,dataUpdateCount:t.dataUpdateCount+1,dataUpdatedAt:null!=(r=e.dataUpdatedAt)?r:Date.now(),error:null,fetchFailureCount:0,isFetching:!1,isInvalidated:!1,isPaused:!1,status:"success"});case"error":var s=e.error;return D(s)&&s.revert?n({},t,{fetchFailureCount:0,isFetching:!1,isPaused:!1,status:"loading"===t.status?"idle":t.status}):n({},t,{error:s,errorUpdateCount:t.errorUpdateCount+1,errorUpdatedAt:Date.now(),fetchFailureCount:t.fetchFailureCount+1,isFetching:!1,isPaused:!1,status:"error"});case"invalidate":return n({},t,{isInvalidated:!0});case"setState":return n({},t,e.state);default:return t}},t}(),A=function(t){function n(){var e;return(e=t.call(this)||this).queries=[],e.queriesMap={},e}e(n,t);var i=n.prototype;return i.build=function(t,e,n){var i,r=d(e),s=e.queryKey,o=null!=(i=e.queryHash)?i:r(s),u=this.get(o);return u||(u=new U({cache:this,queryKey:s,queryHash:o,options:t.defaultQueryOptions(e),state:n,defaultOptions:t.getQueryDefaults(s)}),this.add(u)),u},i.add=function(t){this.queriesMap[t.queryHash]||(this.queriesMap[t.queryHash]=t,this.queries.push(t),this.notify(t))},i.remove=function(t){var e=this.queriesMap[t.queryHash];e&&(t.destroy(),this.queries=this.queries.filter((function(e){return e!==t})),e===t&&delete this.queriesMap[t.queryHash],this.notify(t))},i.clear=function(){var t=this;C.batch((function(){t.queries.forEach((function(e){t.remove(e)}))}))},i.get=function(t){return this.queriesMap[t]},i.getAll=function(){return this.queries},i.find=function(t,e){var n=l(t,e)[0];return this.queries.find((function(t){return f(n,t)}))},i.findAll=function(t,e){var n=l(t,e)[0];return n?this.queries.filter((function(t){return f(n,t)})):this.queries},i.notify=function(t){var e=this;C.batch((function(){e.listeners.forEach((function(e){e(t)}))}))},i.onFocus=function(){var t=this;C.batch((function(){t.queries.forEach((function(t){t.onFocus()}))}))},i.onOnline=function(){var t=this;C.batch((function(){t.queries.forEach((function(t){t.onOnline()}))}))},n}(R),T=function(){function t(t){this.options=n({},t.defaultOptions,t.options),this.mutationId=t.mutationId,this.mutationCache=t.mutationCache,this.observers=[],this.state=t.state||I()}var e=t.prototype;return e.setState=function(t){this.dispatch({type:"setState",state:t})},e.addObserver=function(t){-1===this.observers.indexOf(t)&&this.observers.push(t)},e.removeObserver=function(t){this.observers=this.observers.filter((function(e){return e!==t}))},e.cancel=function(){return this.retryer?(this.retryer.cancel(),this.retryer.promise.then(r).catch(r)):Promise.resolve()},e.continue=function(){return this.retryer?(this.retryer.continue(),this.retryer.promise):this.execute()},e.execute=function(){var t,e=this,n="loading"===this.state.status,i=Promise.resolve();return n||(this.dispatch({type:"loading",variables:this.options.variables}),i=i.then((function(){return null==e.options.onMutate?void 0:e.options.onMutate(e.state.variables)})).then((function(t){t!==e.state.context&&e.dispatch({type:"loading",context:t,variables:e.state.variables})}))),i.then((function(){return e.executeMutation()})).then((function(e){t=e})).then((function(){return null==e.options.onSuccess?void 0:e.options.onSuccess(t,e.state.variables,e.state.context)})).then((function(){return null==e.options.onSettled?void 0:e.options.onSettled(t,null,e.state.variables,e.state.context)})).then((function(){return e.dispatch({type:"success",data:t}),t})).catch((function(t){return q().error(t),Promise.resolve().then((function(){return null==e.options.onError?void 0:e.options.onError(t,e.state.variables,e.state.context)})).then((function(){return null==e.options.onSettled?void 0:e.options.onSettled(void 0,t,e.state.variables,e.state.context)})).then((function(){throw e.dispatch({type:"error",error:t}),t}))}))},e.executeMutation=function(){var t,e=this;return this.retryer=new x({fn:function(){return e.options.mutationFn?e.options.mutationFn(e.state.variables):Promise.reject("No mutationFn found")},onFail:function(){e.dispatch({type:"failed"})},onPause:function(){e.dispatch({type:"pause"})},onContinue:function(){e.dispatch({type:"continue"})},retry:null!=(t=this.options.retry)?t:0,retryDelay:this.options.retryDelay}),this.retryer.promise},e.dispatch=function(t){var e=this;this.state=function(t,e){switch(e.type){case"failed":return n({},t,{failureCount:t.failureCount+1});case"pause":return n({},t,{isPaused:!0});case"continue":return n({},t,{isPaused:!1});case"loading":return n({},t,{context:e.context,data:void 0,error:null,isPaused:!1,status:"loading",variables:e.variables});case"success":return n({},t,{data:e.data,error:null,status:"success",isPaused:!1});case"error":return n({},t,{data:void 0,error:e.error,failureCount:t.failureCount+1,isPaused:!1,status:"error"});case"setState":return n({},t,e.state);default:return t}}(this.state,t),C.batch((function(){e.observers.forEach((function(e){e.onMutationUpdate(t)})),e.mutationCache.notify(e)}))},t}();function I(){return{context:void 0,data:void 0,error:null,failureCount:0,isPaused:!1,status:"idle",variables:void 0}}var L=function(t){function n(){var e;return(e=t.call(this)||this).mutations=[],e.mutationId=0,e}e(n,t);var i=n.prototype;return i.build=function(t,e,n){var i=new T({mutationCache:this,mutationId:++this.mutationId,options:t.defaultMutationOptions(e),state:n,defaultOptions:e.mutationKey?t.getMutationDefaults(e.mutationKey):void 0});return this.add(i),i},i.add=function(t){this.mutations.push(t),this.notify(t)},i.remove=function(t){this.mutations=this.mutations.filter((function(e){return e!==t})),t.cancel(),this.notify(t)},i.clear=function(){var t=this;C.batch((function(){t.mutations.forEach((function(e){t.remove(e)}))}))},i.getAll=function(){return this.mutations},i.notify=function(t){var e=this;C.batch((function(){e.listeners.forEach((function(e){e(t)}))}))},i.onFocus=function(){this.resumePausedMutations()},i.onOnline=function(){this.resumePausedMutations()},i.resumePausedMutations=function(){var t=this.mutations.filter((function(t){return t.state.isPaused}));return C.batch((function(){return t.reduce((function(t,e){return t.then((function(){return e.continue().catch(r)}))}),Promise.resolve())}))},n}(R);function K(){return{onFetch:function(t){t.fetchFn=function(){var e,n,i,r,s,o=null==(e=t.fetchOptions)||null==(n=e.meta)?void 0:n.fetchMore,u=null==o?void 0:o.pageParam,a="forward"===(null==o?void 0:o.direction),c="backward"===(null==o?void 0:o.direction),h=(null==(i=t.state.data)?void 0:i.pages)||[],l=(null==(r=t.state.data)?void 0:r.pageParams)||[],f=l,d=t.options.queryFn||function(){return Promise.reject("Missing queryFn")},v=function(e,n,i,r){if(void 0===i&&!n&&e.length)return Promise.resolve(e);var s,o={queryKey:t.queryKey,pageParam:i},u=d(o);u.cancel&&(s=u.cancel);var a=Promise.resolve(u).then((function(t){return f=r?[i].concat(f):[].concat(f,[i]),r?[t].concat(e):[].concat(e,[t])}));return s&&(a.cancel=s),a};if(h.length)if(a){var p=void 0!==u,y=p?u:N(t.options,h);s=v(h,p,y)}else if(c){var m=void 0!==u,b=m?u:j(t.options,h);s=v(h,m,b,!0)}else!function(){f=[];var e=void 0===t.options.getNextPageParam;s=v([],e,l[0]);for(var n=function(n){s=s.then((function(i){var r=e?l[n]:N(t.options,i);return v(i,e,r)}))},i=1;i<h.length;i++)n(i)}();else s=v([]);var g=s.then((function(t){return{pages:t,pageParams:f}}));return s.cancel&&(g.cancel=s.cancel),g}}}}function N(t,e){return null==t.getNextPageParam?void 0:t.getNextPageParam(e[e.length-1],e)}function j(t,e){return null==t.getPreviousPageParam?void 0:t.getPreviousPageParam(e[0],e)}function H(t,e){if(t.getNextPageParam&&Array.isArray(e)){var n=N(t,e);return null!=n&&!1!==n}}function k(t,e){if(t.getPreviousPageParam&&Array.isArray(e)){var n=j(t,e);return null!=n&&!1!==n}}var _=function(){function t(t){void 0===t&&(t={}),this.queryCache=t.queryCache||new A,this.mutationCache=t.mutationCache||new L,this.defaultOptions=t.defaultOptions||{},this.queryDefaults=[],this.mutationDefaults=[]}var e=t.prototype;return e.mount=function(){var t=this;this.unsubscribeFocus=w.subscribe((function(){w.isFocused()&&E.isOnline()&&(t.mutationCache.onFocus(),t.queryCache.onFocus())})),this.unsubscribeOnline=E.subscribe((function(){w.isFocused()&&E.isOnline()&&(t.mutationCache.onOnline(),t.queryCache.onOnline())}))},e.unmount=function(){var t,e;null==(t=this.unsubscribeFocus)||t.call(this),null==(e=this.unsubscribeOnline)||e.call(this)},e.isFetching=function(t,e){var n=l(t,e)[0];return n.fetching=!0,this.queryCache.findAll(n).length},e.getQueryData=function(t,e){var n;return null==(n=this.queryCache.find(t,e))?void 0:n.state.data},e.setQueryData=function(t,e,n){var i=h(t),r=this.defaultQueryOptions(i);return this.queryCache.build(this,r).setData(e,n)},e.getQueryState=function(t,e){var n;return null==(n=this.queryCache.find(t,e))?void 0:n.state},e.removeQueries=function(t,e){var n=l(t,e)[0],i=this.queryCache;C.batch((function(){i.findAll(n).forEach((function(t){i.remove(t)}))}))},e.resetQueries=function(t,e,i){var r=this,s=l(t,e,i),o=s[0],u=s[1],a=this.queryCache,c=n({},o,{active:!0});return C.batch((function(){return a.findAll(o).forEach((function(t){t.reset()})),r.refetchQueries(c,u)}))},e.cancelQueries=function(t,e,n){var i=this,s=l(t,e,n),o=s[0],u=s[1],a=void 0===u?{}:u;void 0===a.revert&&(a.revert=!0);var c=C.batch((function(){return i.queryCache.findAll(o).map((function(t){return t.cancel(a)}))}));return Promise.all(c).then(r).catch(r)},e.invalidateQueries=function(t,e,i){var r,s,o=this,u=l(t,e,i),a=u[0],c=u[1],h=n({},a,{active:null==(r=a.refetchActive)||r,inactive:null!=(s=a.refetchInactive)&&s});return C.batch((function(){return o.queryCache.findAll(a).forEach((function(t){t.invalidate()})),o.refetchQueries(h,c)}))},e.refetchQueries=function(t,e,n){var i=this,s=l(t,e,n),o=s[0],u=s[1],a=C.batch((function(){return i.queryCache.findAll(o).map((function(t){return t.fetch()}))})),c=Promise.all(a).then(r);return(null==u?void 0:u.throwOnError)||(c=c.catch(r)),c},e.fetchQuery=function(t,e,n){var i=h(t,e,n),r=this.defaultQueryOptions(i);void 0===r.retry&&(r.retry=!1);var s=this.queryCache.build(this,r);return s.isStaleByTime(r.staleTime)?s.fetch(r):Promise.resolve(s.state.data)},e.prefetchQuery=function(t,e,n){return this.fetchQuery(t,e,n).then(r).catch(r)},e.fetchInfiniteQuery=function(t,e,n){var i=h(t,e,n);return i.behavior=K(),this.fetchQuery(i)},e.prefetchInfiniteQuery=function(t,e,n){return this.fetchInfiniteQuery(t,e,n).then(r).catch(r)},e.cancelMutations=function(){var t=this,e=C.batch((function(){return t.mutationCache.getAll().map((function(t){return t.cancel()}))}));return Promise.all(e).then(r).catch(r)},e.resumePausedMutations=function(){return this.getMutationCache().resumePausedMutations()},e.executeMutation=function(t){return this.mutationCache.build(this,t).execute()},e.getQueryCache=function(){return this.queryCache},e.getMutationCache=function(){return this.mutationCache},e.getDefaultOptions=function(){return this.defaultOptions},e.setDefaultOptions=function(t){this.defaultOptions=t},e.setQueryDefaults=function(t,e){var n=this.queryDefaults.find((function(e){return v(t)===v(e.queryKey)}));n?n.defaultOptions=e:this.queryDefaults.push({queryKey:t,defaultOptions:e})},e.getQueryDefaults=function(t){var e;return t?null==(e=this.queryDefaults.find((function(e){return p(t,e.queryKey)})))?void 0:e.defaultOptions:void 0},e.setMutationDefaults=function(t,e){var n=this.mutationDefaults.find((function(e){return v(t)===v(e.mutationKey)}));n?n.defaultOptions=e:this.mutationDefaults.push({mutationKey:t,defaultOptions:e})},e.getMutationDefaults=function(t){var e;return t?null==(e=this.mutationDefaults.find((function(e){return p(t,e.mutationKey)})))?void 0:e.defaultOptions:void 0},e.defaultQueryOptions=function(t){return(null==t?void 0:t._defaulted)?t:n({},this.defaultOptions.queries,this.getQueryDefaults(null==t?void 0:t.queryKey),t,{_defaulted:!0})},e.defaultQueryObserverOptions=function(t){return this.defaultQueryOptions(t)},e.defaultMutationOptions=function(t){return(null==t?void 0:t._defaulted)?t:n({},this.defaultOptions.mutations,this.getMutationDefaults(null==t?void 0:t.mutationKey),t,{_defaulted:!0})},e.clear=function(){this.queryCache.clear(),this.mutationCache.clear()},t}(),G=function(t){function s(e,n){var i;return(i=t.call(this)||this).client=e,i.options=n,i.initialDataUpdateCount=0,i.initialErrorUpdateCount=0,i.bindMethods(),i.setOptions(n),i}e(s,t);var u=s.prototype;return u.bindMethods=function(){this.remove=this.remove.bind(this),this.refetch=this.refetch.bind(this)},u.onSubscribe=function(){1===this.listeners.length&&(this.updateQuery(),this.currentQuery.addObserver(this),this.willFetchOnMount()&&this.executeFetch(),this.updateTimers())},u.onUnsubscribe=function(){this.listeners.length||this.destroy()},u.willLoadOnMount=function(){return!1!==this.options.enabled&&!this.currentQuery.state.dataUpdatedAt},u.willRefetchOnMount=function(){return!1!==this.options.enabled&&this.currentQuery.state.dataUpdatedAt>0&&("always"===this.options.refetchOnMount||!1!==this.options.refetchOnMount&&this.isStale())},u.willFetchOnMount=function(){return this.willLoadOnMount()||this.willRefetchOnMount()},u.willFetchOnReconnect=function(){return!1!==this.options.enabled&&("always"===this.options.refetchOnReconnect||!1!==this.options.refetchOnReconnect&&this.isStale())},u.willFetchOnWindowFocus=function(){return!1!==this.options.enabled&&("always"===this.options.refetchOnWindowFocus||!1!==this.options.refetchOnWindowFocus&&this.isStale())},u.willFetchOptionally=function(){return!1!==this.options.enabled&&this.isStale()},u.isStale=function(){return this.currentQuery.isStaleByTime(this.options.staleTime)},u.destroy=function(){this.listeners=[],this.clearTimers(),this.currentQuery.removeObserver(this)},u.setOptions=function(t){var e=this.options,n=this.currentQuery;if(this.options=this.client.defaultQueryObserverOptions(t),void 0!==this.options.enabled&&"boolean"!=typeof this.options.enabled)throw new Error("Expected enabled to be a boolean");if(this.options.queryKey||(this.options.queryKey=e.queryKey),this.updateQuery(),this.listeners.length){if(this.currentQuery!==n)return this.optionalFetch(),void this.updateTimers();!1!==this.options.enabled&&!1===e.enabled&&this.optionalFetch(),this.options.enabled===e.enabled&&this.options.staleTime===e.staleTime||this.updateStaleTimeout(),this.options.enabled===e.enabled&&this.options.refetchInterval===e.refetchInterval||this.updateRefetchInterval()}},u.getCurrentResult=function(){return this.currentResult},u.getNextResult=function(t){var e=this;return new Promise((function(n,i){var r=e.subscribe((function(e){e.isFetching||(r(),e.isError&&(null==t?void 0:t.throwOnError)?i(e.error):n(e))}))}))},u.getCurrentQuery=function(){return this.currentQuery},u.remove=function(){this.client.getQueryCache().remove(this.currentQuery)},u.refetch=function(t){return this.fetch(t)},u.fetch=function(t){var e=this;return this.executeFetch(t).then((function(){return e.updateResult(),e.currentResult}))},u.optionalFetch=function(){this.willFetchOptionally()&&this.executeFetch()},u.executeFetch=function(t){this.updateQuery();var e=this.currentQuery.fetch(this.options,t);return(null==t?void 0:t.throwOnError)||(e=e.catch(r)),e},u.updateStaleTimeout=function(){var t=this;if(this.clearStaleTimeout(),!i&&!this.currentResult.isStale&&o(this.options.staleTime)){var e=c(this.currentResult.dataUpdatedAt,this.options.staleTime)+1;this.staleTimeoutId=setTimeout((function(){if(!t.currentResult.isStale){var e=t.currentResult;t.updateResult(),t.notify({listeners:t.shouldNotifyListeners(e,t.currentResult),cache:!0})}}),e)}},u.updateRefetchInterval=function(){var t=this;this.clearRefetchInterval(),!i&&!1!==this.options.enabled&&o(this.options.refetchInterval)&&(this.refetchIntervalId=setInterval((function(){(t.options.refetchIntervalInBackground||w.isFocused())&&t.executeFetch()}),this.options.refetchInterval))},u.updateTimers=function(){this.updateStaleTimeout(),this.updateRefetchInterval()},u.clearTimers=function(){this.clearStaleTimeout(),this.clearRefetchInterval()},u.clearStaleTimeout=function(){clearTimeout(this.staleTimeoutId),this.staleTimeoutId=void 0},u.clearRefetchInterval=function(){clearInterval(this.refetchIntervalId),this.refetchIntervalId=void 0},u.getNewResult=function(t){var e,i,r=this.currentQuery.state,s=r.isFetching,o=r.status,u=!1,a=!1,c=r.dataUpdatedAt;if(t&&(s=!0,"idle"===o&&(o="loading")),this.options.keepPreviousData&&!r.dataUpdateCount&&(null==(e=this.previousQueryResult)?void 0:e.isSuccess))i=this.previousQueryResult.data,c=this.previousQueryResult.dataUpdatedAt,o=this.previousQueryResult.status,u=!0;else if(this.options.select&&void 0!==r.data){var h,l;if(this.currentResult&&r.data===(null==(h=this.currentResultState)?void 0:h.data))i=this.currentResult.data;else if(i=this.options.select(r.data),!1!==this.options.structuralSharing)i=y(null==(l=this.currentResult)?void 0:l.data,i)}else i=r.data;if(void 0!==this.options.placeholderData&&void 0===i&&"loading"===o){var f="function"==typeof this.options.placeholderData?this.options.placeholderData():this.options.placeholderData;void 0!==f&&(o="success",i=f,a=!0)}return n({},O(o),{data:i,dataUpdatedAt:c,error:r.error,errorUpdatedAt:r.errorUpdateCount,failureCount:r.fetchFailureCount,isFetched:r.dataUpdateCount>0||r.errorUpdateCount>0,isFetchedAfterMount:r.dataUpdateCount>this.initialDataUpdateCount||r.errorUpdateCount>this.initialErrorUpdateCount,isFetching:s,isLoadingError:"error"===o&&0===r.dataUpdatedAt,isPlaceholderData:a,isPreviousData:u,isRefetchError:"error"===o&&0!==r.dataUpdatedAt,isStale:this.isStale(),refetch:this.refetch,remove:this.remove})},u.shouldNotifyListeners=function(t,e){var n=this.options,i=n.notifyOnChangeProps,r=n.notifyOnChangePropsExclusions;if(t===e)return!1;if(!i&&!r)return!0;for(var s=Object.keys(e),o=function(n){var o=s[n],u=t[o]!==e[o],a=null==i?void 0:i.some((function(t){return t===o})),c=null==r?void 0:r.some((function(t){return t===o}));if(u){if(r&&c)return"continue";if(!i||a)return{v:!0}}},u=0;u<s.length;u++){var a=o(u);if("continue"!==a&&"object"==typeof a)return a.v}return!1},u.updateResult=function(t){var e=this.getNewResult(t);this.currentResultState=this.currentQuery.state,function(t,e){if(t&&!e||e&&!t)return!1;for(var n in t)if(t[n]!==e[n])return!1;return!0}(e,this.currentResult)||(this.currentResult=e)},u.updateQuery=function(){var t=this.currentQuery,e=this.client.getQueryCache().build(this.client,this.options);if(e!==t){this.previousQueryResult=this.currentResult,this.currentQuery=e,this.initialDataUpdateCount=e.state.dataUpdateCount,this.initialErrorUpdateCount=e.state.errorUpdateCount;var n=t?this.willFetchOptionally():this.willFetchOnMount();this.updateResult(n),this.hasListeners()&&(null==t||t.removeObserver(this),this.currentQuery.addObserver(this),this.shouldNotifyListeners(this.previousQueryResult,this.currentResult)&&this.notify({listeners:!0}))}},u.onQueryUpdate=function(t){var e=this.currentResult;this.updateResult();var n=this.currentResult;if(this.updateTimers(),e!==n){var i={};"success"===t.type?i.onSuccess=!0:"error"===t.type&&(i.onError=!0),this.shouldNotifyListeners(e,n)&&(i.listeners=!0),this.notify(i)}},u.notify=function(t){var e=this;C.batch((function(){t.onSuccess?(null==e.options.onSuccess||e.options.onSuccess(e.currentResult.data),null==e.options.onSettled||e.options.onSettled(e.currentResult.data,null)):t.onError&&(null==e.options.onError||e.options.onError(e.currentResult.error),null==e.options.onSettled||e.options.onSettled(void 0,e.currentResult.error)),t.listeners&&e.listeners.forEach((function(t){t(e.currentResult)})),t.cache&&e.client.getQueryCache().notify(e.currentQuery)}))},s}(R),B=function(t){function n(e,n){var i;return(i=t.call(this)||this).client=e,i.queries=n||[],i.result=[],i.observers=[],i.updateObservers(),i}e(n,t);var i=n.prototype;return i.onSubscribe=function(){var t=this;1===this.listeners.length&&this.observers.forEach((function(e){e.subscribe((function(n){t.onUpdate(e,n)}))}))},i.onUnsubscribe=function(){this.listeners.length||this.destroy()},i.destroy=function(){this.listeners=[],this.observers.forEach((function(t){t.destroy()}))},i.setQueries=function(t){this.queries=t,this.updateObservers()},i.getCurrentResult=function(){return this.result},i.updateObservers=function(){var t=this,e=!1,n=this.observers,i=this.queries.map((function(i,r){var s=n[r],o=t.client.defaultQueryObserverOptions(i),u=d(o);return o.queryHash=u(o.queryKey),s&&s.getCurrentQuery().queryHash===o.queryHash||(e=!0,s=n.find((function(t){return t.getCurrentQuery().queryHash===o.queryHash}))),s?(s.setOptions(o),s):new G(t.client,o)}));(n.length!==i.length||e)&&(this.observers=i,this.result=i.map((function(t){return t.getCurrentResult()})),this.listeners.length&&(a(n,i).forEach((function(t){t.destroy()})),a(i,n).forEach((function(e){e.subscribe((function(n){t.onUpdate(e,n)}))})),this.notify()))},i.onUpdate=function(t,e){var n=this.observers.indexOf(t);-1!==n&&(this.result=function(t,e,n){var i=t.slice(0);return i[e]=n,i}(this.result,n,e),this.notify())},i.notify=function(){var t=this;C.batch((function(){t.listeners.forEach((function(e){e(t.result)}))}))},n}(R),W=function(t){function i(e,n){return t.call(this,e,n)||this}e(i,t);var r=i.prototype;return r.bindMethods=function(){t.prototype.bindMethods.call(this),this.fetchNextPage=this.fetchNextPage.bind(this),this.fetchPreviousPage=this.fetchPreviousPage.bind(this)},r.setOptions=function(e){t.prototype.setOptions.call(this,n({},e,{behavior:K()}))},r.fetchNextPage=function(t){return this.fetch({cancelRefetch:!0,throwOnError:null==t?void 0:t.throwOnError,meta:{fetchMore:{direction:"forward",pageParam:null==t?void 0:t.pageParam}}})},r.fetchPreviousPage=function(t){return this.fetch({cancelRefetch:!0,throwOnError:null==t?void 0:t.throwOnError,meta:{fetchMore:{direction:"backward",pageParam:null==t?void 0:t.pageParam}}})},r.getNewResult=function(e){var i,r,s,o,u,a,c=this.getCurrentQuery().state,h=t.prototype.getNewResult.call(this,e);return n({},h,{fetchNextPage:this.fetchNextPage,fetchPreviousPage:this.fetchPreviousPage,hasNextPage:H(this.options,null==(i=h.data)?void 0:i.pages),hasPreviousPage:k(this.options,null==(r=h.data)?void 0:r.pages),isFetchingNextPage:c.isFetching&&"forward"===(null==(s=c.fetchMeta)||null==(o=s.fetchMore)?void 0:o.direction),isFetchingPreviousPage:c.isFetching&&"backward"===(null==(u=c.fetchMeta)||null==(a=u.fetchMore)?void 0:a.direction)})},i}(G),J=function(t){function i(e,n){var i;return(i=t.call(this)||this).client=e,i.setOptions(n),i.bindMethods(),i.updateResult(),i}e(i,t);var r=i.prototype;return r.bindMethods=function(){this.mutate=this.mutate.bind(this),this.reset=this.reset.bind(this)},r.setOptions=function(t){this.options=this.client.defaultMutationOptions(t)},r.onUnsubscribe=function(){var t;this.listeners.length||(null==(t=this.currentMutation)||t.removeObserver(this))},r.onMutationUpdate=function(t){this.updateResult();var e={listeners:!0};"success"===t.type?e.onSuccess=!0:"error"===t.type&&(e.onError=!0),this.notify(e)},r.getCurrentResult=function(){return this.currentResult},r.reset=function(){this.currentMutation=void 0,this.updateResult(),this.notify({listeners:!0})},r.mutate=function(t,e){return this.mutateOptions=e,this.currentMutation&&this.currentMutation.removeObserver(this),this.currentMutation=this.client.getMutationCache().build(this.client,n({},this.options,{variables:null!=t?t:this.options.variables})),this.currentMutation.addObserver(this),this.currentMutation.execute()},r.updateResult=function(){var t=this.currentMutation?this.currentMutation.state:{context:void 0,data:void 0,error:null,failureCount:0,isPaused:!1,status:"idle",variables:void 0};this.currentResult=n({},t,O(t.status),{mutate:this.mutate,reset:this.reset})},r.notify=function(t){var e=this;C.batch((function(){e.mutateOptions&&(t.onSuccess?(null==e.mutateOptions.onSuccess||e.mutateOptions.onSuccess(e.currentResult.data,e.currentResult.variables,e.currentResult.context),null==e.mutateOptions.onSettled||e.mutateOptions.onSettled(e.currentResult.data,null,e.currentResult.variables,e.currentResult.context)):t.onError&&(null==e.mutateOptions.onError||e.mutateOptions.onError(e.currentResult.error,e.currentResult.variables,e.currentResult.context),null==e.mutateOptions.onSettled||e.mutateOptions.onSettled(void 0,e.currentResult.error,e.currentResult.variables,e.currentResult.context))),t.listeners&&e.listeners.forEach((function(t){t(e.currentResult)}))}))},i}(R);t.InfiniteQueryObserver=W,t.MutationCache=L,t.MutationObserver=J,t.QueriesObserver=B,t.QueryCache=A,t.QueryClient=_,t.QueryObserver=G,t.focusManager=w,t.hashQueryKey=v,t.isCancelledError=D,t.isError=function(t){return t instanceof Error},t.notifyManager=C,t.onlineManager=E,t.setLogger=function(t){F=t},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=react-query-core.production.min.js.map