index.development.js 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.QueryCore = {}));
  5. })(this, (function (exports) { 'use strict';
  6. class Subscribable {
  7. constructor() {
  8. this.listeners = new Set();
  9. this.subscribe = this.subscribe.bind(this);
  10. }
  11. subscribe(listener) {
  12. const identity = {
  13. listener
  14. };
  15. this.listeners.add(identity);
  16. this.onSubscribe();
  17. return () => {
  18. this.listeners.delete(identity);
  19. this.onUnsubscribe();
  20. };
  21. }
  22. hasListeners() {
  23. return this.listeners.size > 0;
  24. }
  25. onSubscribe() {// Do nothing
  26. }
  27. onUnsubscribe() {// Do nothing
  28. }
  29. }
  30. // TYPES
  31. // UTILS
  32. const isServer = typeof window === 'undefined' || 'Deno' in window;
  33. function noop() {
  34. return undefined;
  35. }
  36. function functionalUpdate(updater, input) {
  37. return typeof updater === 'function' ? updater(input) : updater;
  38. }
  39. function isValidTimeout(value) {
  40. return typeof value === 'number' && value >= 0 && value !== Infinity;
  41. }
  42. function difference(array1, array2) {
  43. return array1.filter(x => !array2.includes(x));
  44. }
  45. function replaceAt(array, index, value) {
  46. const copy = array.slice(0);
  47. copy[index] = value;
  48. return copy;
  49. }
  50. function timeUntilStale(updatedAt, staleTime) {
  51. return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
  52. }
  53. function parseQueryArgs(arg1, arg2, arg3) {
  54. if (!isQueryKey(arg1)) {
  55. return arg1;
  56. }
  57. if (typeof arg2 === 'function') {
  58. return { ...arg3,
  59. queryKey: arg1,
  60. queryFn: arg2
  61. };
  62. }
  63. return { ...arg2,
  64. queryKey: arg1
  65. };
  66. }
  67. function parseMutationArgs(arg1, arg2, arg3) {
  68. if (isQueryKey(arg1)) {
  69. if (typeof arg2 === 'function') {
  70. return { ...arg3,
  71. mutationKey: arg1,
  72. mutationFn: arg2
  73. };
  74. }
  75. return { ...arg2,
  76. mutationKey: arg1
  77. };
  78. }
  79. if (typeof arg1 === 'function') {
  80. return { ...arg2,
  81. mutationFn: arg1
  82. };
  83. }
  84. return { ...arg1
  85. };
  86. }
  87. function parseFilterArgs(arg1, arg2, arg3) {
  88. return isQueryKey(arg1) ? [{ ...arg2,
  89. queryKey: arg1
  90. }, arg3] : [arg1 || {}, arg2];
  91. }
  92. function parseMutationFilterArgs(arg1, arg2, arg3) {
  93. return isQueryKey(arg1) ? [{ ...arg2,
  94. mutationKey: arg1
  95. }, arg3] : [arg1 || {}, arg2];
  96. }
  97. function matchQuery(filters, query) {
  98. const {
  99. type = 'all',
  100. exact,
  101. fetchStatus,
  102. predicate,
  103. queryKey,
  104. stale
  105. } = filters;
  106. if (isQueryKey(queryKey)) {
  107. if (exact) {
  108. if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
  109. return false;
  110. }
  111. } else if (!partialMatchKey(query.queryKey, queryKey)) {
  112. return false;
  113. }
  114. }
  115. if (type !== 'all') {
  116. const isActive = query.isActive();
  117. if (type === 'active' && !isActive) {
  118. return false;
  119. }
  120. if (type === 'inactive' && isActive) {
  121. return false;
  122. }
  123. }
  124. if (typeof stale === 'boolean' && query.isStale() !== stale) {
  125. return false;
  126. }
  127. if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
  128. return false;
  129. }
  130. if (predicate && !predicate(query)) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. function matchMutation(filters, mutation) {
  136. const {
  137. exact,
  138. fetching,
  139. predicate,
  140. mutationKey
  141. } = filters;
  142. if (isQueryKey(mutationKey)) {
  143. if (!mutation.options.mutationKey) {
  144. return false;
  145. }
  146. if (exact) {
  147. if (hashQueryKey(mutation.options.mutationKey) !== hashQueryKey(mutationKey)) {
  148. return false;
  149. }
  150. } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
  151. return false;
  152. }
  153. }
  154. if (typeof fetching === 'boolean' && mutation.state.status === 'loading' !== fetching) {
  155. return false;
  156. }
  157. if (predicate && !predicate(mutation)) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. function hashQueryKeyByOptions(queryKey, options) {
  163. const hashFn = (options == null ? void 0 : options.queryKeyHashFn) || hashQueryKey;
  164. return hashFn(queryKey);
  165. }
  166. /**
  167. * Default query keys hash function.
  168. * Hashes the value into a stable hash.
  169. */
  170. function hashQueryKey(queryKey) {
  171. return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
  172. result[key] = val[key];
  173. return result;
  174. }, {}) : val);
  175. }
  176. /**
  177. * Checks if key `b` partially matches with key `a`.
  178. */
  179. function partialMatchKey(a, b) {
  180. return partialDeepEqual(a, b);
  181. }
  182. /**
  183. * Checks if `b` partially matches with `a`.
  184. */
  185. function partialDeepEqual(a, b) {
  186. if (a === b) {
  187. return true;
  188. }
  189. if (typeof a !== typeof b) {
  190. return false;
  191. }
  192. if (a && b && typeof a === 'object' && typeof b === 'object') {
  193. return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key]));
  194. }
  195. return false;
  196. }
  197. /**
  198. * This function returns `a` if `b` is deeply equal.
  199. * If not, it will replace any deeply equal children of `b` with those of `a`.
  200. * This can be used for structural sharing between JSON values for example.
  201. */
  202. function replaceEqualDeep(a, b) {
  203. if (a === b) {
  204. return a;
  205. }
  206. const array = isPlainArray(a) && isPlainArray(b);
  207. if (array || isPlainObject(a) && isPlainObject(b)) {
  208. const aSize = array ? a.length : Object.keys(a).length;
  209. const bItems = array ? b : Object.keys(b);
  210. const bSize = bItems.length;
  211. const copy = array ? [] : {};
  212. let equalItems = 0;
  213. for (let i = 0; i < bSize; i++) {
  214. const key = array ? i : bItems[i];
  215. copy[key] = replaceEqualDeep(a[key], b[key]);
  216. if (copy[key] === a[key]) {
  217. equalItems++;
  218. }
  219. }
  220. return aSize === bSize && equalItems === aSize ? a : copy;
  221. }
  222. return b;
  223. }
  224. /**
  225. * Shallow compare objects. Only works with objects that always have the same properties.
  226. */
  227. function shallowEqualObjects(a, b) {
  228. if (a && !b || b && !a) {
  229. return false;
  230. }
  231. for (const key in a) {
  232. if (a[key] !== b[key]) {
  233. return false;
  234. }
  235. }
  236. return true;
  237. }
  238. function isPlainArray(value) {
  239. return Array.isArray(value) && value.length === Object.keys(value).length;
  240. } // Copied from: https://github.com/jonschlinkert/is-plain-object
  241. function isPlainObject(o) {
  242. if (!hasObjectPrototype(o)) {
  243. return false;
  244. } // If has modified constructor
  245. const ctor = o.constructor;
  246. if (typeof ctor === 'undefined') {
  247. return true;
  248. } // If has modified prototype
  249. const prot = ctor.prototype;
  250. if (!hasObjectPrototype(prot)) {
  251. return false;
  252. } // If constructor does not have an Object-specific method
  253. if (!prot.hasOwnProperty('isPrototypeOf')) {
  254. return false;
  255. } // Most likely a plain Object
  256. return true;
  257. }
  258. function hasObjectPrototype(o) {
  259. return Object.prototype.toString.call(o) === '[object Object]';
  260. }
  261. function isQueryKey(value) {
  262. return Array.isArray(value);
  263. }
  264. function isError(value) {
  265. return value instanceof Error;
  266. }
  267. function sleep(timeout) {
  268. return new Promise(resolve => {
  269. setTimeout(resolve, timeout);
  270. });
  271. }
  272. /**
  273. * Schedules a microtask.
  274. * This can be useful to schedule state updates after rendering.
  275. */
  276. function scheduleMicrotask(callback) {
  277. sleep(0).then(callback);
  278. }
  279. function getAbortController() {
  280. if (typeof AbortController === 'function') {
  281. return new AbortController();
  282. }
  283. return;
  284. }
  285. function replaceData(prevData, data, options) {
  286. // Use prev data if an isDataEqual function is defined and returns `true`
  287. if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
  288. return prevData;
  289. } else if (typeof options.structuralSharing === 'function') {
  290. return options.structuralSharing(prevData, data);
  291. } else if (options.structuralSharing !== false) {
  292. // Structurally share data between prev and new data if needed
  293. return replaceEqualDeep(prevData, data);
  294. }
  295. return data;
  296. }
  297. class FocusManager extends Subscribable {
  298. constructor() {
  299. super();
  300. this.setup = onFocus => {
  301. // addEventListener does not exist in React Native, but window does
  302. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  303. if (!isServer && window.addEventListener) {
  304. const listener = () => onFocus(); // Listen to visibillitychange and focus
  305. window.addEventListener('visibilitychange', listener, false);
  306. window.addEventListener('focus', listener, false);
  307. return () => {
  308. // Be sure to unsubscribe if a new handler is set
  309. window.removeEventListener('visibilitychange', listener);
  310. window.removeEventListener('focus', listener);
  311. };
  312. }
  313. return;
  314. };
  315. }
  316. onSubscribe() {
  317. if (!this.cleanup) {
  318. this.setEventListener(this.setup);
  319. }
  320. }
  321. onUnsubscribe() {
  322. if (!this.hasListeners()) {
  323. var _this$cleanup;
  324. (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
  325. this.cleanup = undefined;
  326. }
  327. }
  328. setEventListener(setup) {
  329. var _this$cleanup2;
  330. this.setup = setup;
  331. (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
  332. this.cleanup = setup(focused => {
  333. if (typeof focused === 'boolean') {
  334. this.setFocused(focused);
  335. } else {
  336. this.onFocus();
  337. }
  338. });
  339. }
  340. setFocused(focused) {
  341. const changed = this.focused !== focused;
  342. if (changed) {
  343. this.focused = focused;
  344. this.onFocus();
  345. }
  346. }
  347. onFocus() {
  348. this.listeners.forEach(({
  349. listener
  350. }) => {
  351. listener();
  352. });
  353. }
  354. isFocused() {
  355. if (typeof this.focused === 'boolean') {
  356. return this.focused;
  357. } // document global can be unavailable in react native
  358. if (typeof document === 'undefined') {
  359. return true;
  360. }
  361. return [undefined, 'visible', 'prerender'].includes(document.visibilityState);
  362. }
  363. }
  364. const focusManager = new FocusManager();
  365. const onlineEvents = ['online', 'offline'];
  366. class OnlineManager extends Subscribable {
  367. constructor() {
  368. super();
  369. this.setup = onOnline => {
  370. // addEventListener does not exist in React Native, but window does
  371. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  372. if (!isServer && window.addEventListener) {
  373. const listener = () => onOnline(); // Listen to online
  374. onlineEvents.forEach(event => {
  375. window.addEventListener(event, listener, false);
  376. });
  377. return () => {
  378. // Be sure to unsubscribe if a new handler is set
  379. onlineEvents.forEach(event => {
  380. window.removeEventListener(event, listener);
  381. });
  382. };
  383. }
  384. return;
  385. };
  386. }
  387. onSubscribe() {
  388. if (!this.cleanup) {
  389. this.setEventListener(this.setup);
  390. }
  391. }
  392. onUnsubscribe() {
  393. if (!this.hasListeners()) {
  394. var _this$cleanup;
  395. (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
  396. this.cleanup = undefined;
  397. }
  398. }
  399. setEventListener(setup) {
  400. var _this$cleanup2;
  401. this.setup = setup;
  402. (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
  403. this.cleanup = setup(online => {
  404. if (typeof online === 'boolean') {
  405. this.setOnline(online);
  406. } else {
  407. this.onOnline();
  408. }
  409. });
  410. }
  411. setOnline(online) {
  412. const changed = this.online !== online;
  413. if (changed) {
  414. this.online = online;
  415. this.onOnline();
  416. }
  417. }
  418. onOnline() {
  419. this.listeners.forEach(({
  420. listener
  421. }) => {
  422. listener();
  423. });
  424. }
  425. isOnline() {
  426. if (typeof this.online === 'boolean') {
  427. return this.online;
  428. }
  429. if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
  430. return true;
  431. }
  432. return navigator.onLine;
  433. }
  434. }
  435. const onlineManager = new OnlineManager();
  436. function defaultRetryDelay(failureCount) {
  437. return Math.min(1000 * 2 ** failureCount, 30000);
  438. }
  439. function canFetch(networkMode) {
  440. return (networkMode != null ? networkMode : 'online') === 'online' ? onlineManager.isOnline() : true;
  441. }
  442. class CancelledError {
  443. constructor(options) {
  444. this.revert = options == null ? void 0 : options.revert;
  445. this.silent = options == null ? void 0 : options.silent;
  446. }
  447. }
  448. function isCancelledError(value) {
  449. return value instanceof CancelledError;
  450. }
  451. function createRetryer(config) {
  452. let isRetryCancelled = false;
  453. let failureCount = 0;
  454. let isResolved = false;
  455. let continueFn;
  456. let promiseResolve;
  457. let promiseReject;
  458. const promise = new Promise((outerResolve, outerReject) => {
  459. promiseResolve = outerResolve;
  460. promiseReject = outerReject;
  461. });
  462. const cancel = cancelOptions => {
  463. if (!isResolved) {
  464. reject(new CancelledError(cancelOptions));
  465. config.abort == null ? void 0 : config.abort();
  466. }
  467. };
  468. const cancelRetry = () => {
  469. isRetryCancelled = true;
  470. };
  471. const continueRetry = () => {
  472. isRetryCancelled = false;
  473. };
  474. const shouldPause = () => !focusManager.isFocused() || config.networkMode !== 'always' && !onlineManager.isOnline();
  475. const resolve = value => {
  476. if (!isResolved) {
  477. isResolved = true;
  478. config.onSuccess == null ? void 0 : config.onSuccess(value);
  479. continueFn == null ? void 0 : continueFn();
  480. promiseResolve(value);
  481. }
  482. };
  483. const reject = value => {
  484. if (!isResolved) {
  485. isResolved = true;
  486. config.onError == null ? void 0 : config.onError(value);
  487. continueFn == null ? void 0 : continueFn();
  488. promiseReject(value);
  489. }
  490. };
  491. const pause = () => {
  492. return new Promise(continueResolve => {
  493. continueFn = value => {
  494. const canContinue = isResolved || !shouldPause();
  495. if (canContinue) {
  496. continueResolve(value);
  497. }
  498. return canContinue;
  499. };
  500. config.onPause == null ? void 0 : config.onPause();
  501. }).then(() => {
  502. continueFn = undefined;
  503. if (!isResolved) {
  504. config.onContinue == null ? void 0 : config.onContinue();
  505. }
  506. });
  507. }; // Create loop function
  508. const run = () => {
  509. // Do nothing if already resolved
  510. if (isResolved) {
  511. return;
  512. }
  513. let promiseOrValue; // Execute query
  514. try {
  515. promiseOrValue = config.fn();
  516. } catch (error) {
  517. promiseOrValue = Promise.reject(error);
  518. }
  519. Promise.resolve(promiseOrValue).then(resolve).catch(error => {
  520. var _config$retry, _config$retryDelay;
  521. // Stop if the fetch is already resolved
  522. if (isResolved) {
  523. return;
  524. } // Do we need to retry the request?
  525. const retry = (_config$retry = config.retry) != null ? _config$retry : 3;
  526. const retryDelay = (_config$retryDelay = config.retryDelay) != null ? _config$retryDelay : defaultRetryDelay;
  527. const delay = typeof retryDelay === 'function' ? retryDelay(failureCount, error) : retryDelay;
  528. const shouldRetry = retry === true || typeof retry === 'number' && failureCount < retry || typeof retry === 'function' && retry(failureCount, error);
  529. if (isRetryCancelled || !shouldRetry) {
  530. // We are done if the query does not need to be retried
  531. reject(error);
  532. return;
  533. }
  534. failureCount++; // Notify on fail
  535. config.onFail == null ? void 0 : config.onFail(failureCount, error); // Delay
  536. sleep(delay) // Pause if the document is not visible or when the device is offline
  537. .then(() => {
  538. if (shouldPause()) {
  539. return pause();
  540. }
  541. return;
  542. }).then(() => {
  543. if (isRetryCancelled) {
  544. reject(error);
  545. } else {
  546. run();
  547. }
  548. });
  549. });
  550. }; // Start loop
  551. if (canFetch(config.networkMode)) {
  552. run();
  553. } else {
  554. pause().then(run);
  555. }
  556. return {
  557. promise,
  558. cancel,
  559. continue: () => {
  560. const didContinue = continueFn == null ? void 0 : continueFn();
  561. return didContinue ? promise : Promise.resolve();
  562. },
  563. cancelRetry,
  564. continueRetry
  565. };
  566. }
  567. const defaultLogger = console;
  568. function createNotifyManager() {
  569. let queue = [];
  570. let transactions = 0;
  571. let notifyFn = callback => {
  572. callback();
  573. };
  574. let batchNotifyFn = callback => {
  575. callback();
  576. };
  577. const batch = callback => {
  578. let result;
  579. transactions++;
  580. try {
  581. result = callback();
  582. } finally {
  583. transactions--;
  584. if (!transactions) {
  585. flush();
  586. }
  587. }
  588. return result;
  589. };
  590. const schedule = callback => {
  591. if (transactions) {
  592. queue.push(callback);
  593. } else {
  594. scheduleMicrotask(() => {
  595. notifyFn(callback);
  596. });
  597. }
  598. };
  599. /**
  600. * All calls to the wrapped function will be batched.
  601. */
  602. const batchCalls = callback => {
  603. return (...args) => {
  604. schedule(() => {
  605. callback(...args);
  606. });
  607. };
  608. };
  609. const flush = () => {
  610. const originalQueue = queue;
  611. queue = [];
  612. if (originalQueue.length) {
  613. scheduleMicrotask(() => {
  614. batchNotifyFn(() => {
  615. originalQueue.forEach(callback => {
  616. notifyFn(callback);
  617. });
  618. });
  619. });
  620. }
  621. };
  622. /**
  623. * Use this method to set a custom notify function.
  624. * This can be used to for example wrap notifications with `React.act` while running tests.
  625. */
  626. const setNotifyFunction = fn => {
  627. notifyFn = fn;
  628. };
  629. /**
  630. * Use this method to set a custom function to batch notifications together into a single tick.
  631. * By default React Query will use the batch function provided by ReactDOM or React Native.
  632. */
  633. const setBatchNotifyFunction = fn => {
  634. batchNotifyFn = fn;
  635. };
  636. return {
  637. batch,
  638. batchCalls,
  639. schedule,
  640. setNotifyFunction,
  641. setBatchNotifyFunction
  642. };
  643. } // SINGLETON
  644. const notifyManager = createNotifyManager();
  645. class Removable {
  646. destroy() {
  647. this.clearGcTimeout();
  648. }
  649. scheduleGc() {
  650. this.clearGcTimeout();
  651. if (isValidTimeout(this.cacheTime)) {
  652. this.gcTimeout = setTimeout(() => {
  653. this.optionalRemove();
  654. }, this.cacheTime);
  655. }
  656. }
  657. updateCacheTime(newCacheTime) {
  658. // Default to 5 minutes (Infinity for server-side) if no cache time is set
  659. this.cacheTime = Math.max(this.cacheTime || 0, newCacheTime != null ? newCacheTime : isServer ? Infinity : 5 * 60 * 1000);
  660. }
  661. clearGcTimeout() {
  662. if (this.gcTimeout) {
  663. clearTimeout(this.gcTimeout);
  664. this.gcTimeout = undefined;
  665. }
  666. }
  667. }
  668. // CLASS
  669. class Query extends Removable {
  670. constructor(config) {
  671. super();
  672. this.abortSignalConsumed = false;
  673. this.defaultOptions = config.defaultOptions;
  674. this.setOptions(config.options);
  675. this.observers = [];
  676. this.cache = config.cache;
  677. this.logger = config.logger || defaultLogger;
  678. this.queryKey = config.queryKey;
  679. this.queryHash = config.queryHash;
  680. this.initialState = config.state || getDefaultState$1(this.options);
  681. this.state = this.initialState;
  682. this.scheduleGc();
  683. }
  684. get meta() {
  685. return this.options.meta;
  686. }
  687. setOptions(options) {
  688. this.options = { ...this.defaultOptions,
  689. ...options
  690. };
  691. this.updateCacheTime(this.options.cacheTime);
  692. }
  693. optionalRemove() {
  694. if (!this.observers.length && this.state.fetchStatus === 'idle') {
  695. this.cache.remove(this);
  696. }
  697. }
  698. setData(newData, options) {
  699. const data = replaceData(this.state.data, newData, this.options); // Set data and mark it as cached
  700. this.dispatch({
  701. data,
  702. type: 'success',
  703. dataUpdatedAt: options == null ? void 0 : options.updatedAt,
  704. manual: options == null ? void 0 : options.manual
  705. });
  706. return data;
  707. }
  708. setState(state, setStateOptions) {
  709. this.dispatch({
  710. type: 'setState',
  711. state,
  712. setStateOptions
  713. });
  714. }
  715. cancel(options) {
  716. var _this$retryer;
  717. const promise = this.promise;
  718. (_this$retryer = this.retryer) == null ? void 0 : _this$retryer.cancel(options);
  719. return promise ? promise.then(noop).catch(noop) : Promise.resolve();
  720. }
  721. destroy() {
  722. super.destroy();
  723. this.cancel({
  724. silent: true
  725. });
  726. }
  727. reset() {
  728. this.destroy();
  729. this.setState(this.initialState);
  730. }
  731. isActive() {
  732. return this.observers.some(observer => observer.options.enabled !== false);
  733. }
  734. isDisabled() {
  735. return this.getObserversCount() > 0 && !this.isActive();
  736. }
  737. isStale() {
  738. return this.state.isInvalidated || !this.state.dataUpdatedAt || this.observers.some(observer => observer.getCurrentResult().isStale);
  739. }
  740. isStaleByTime(staleTime = 0) {
  741. return this.state.isInvalidated || !this.state.dataUpdatedAt || !timeUntilStale(this.state.dataUpdatedAt, staleTime);
  742. }
  743. onFocus() {
  744. var _this$retryer2;
  745. const observer = this.observers.find(x => x.shouldFetchOnWindowFocus());
  746. if (observer) {
  747. observer.refetch({
  748. cancelRefetch: false
  749. });
  750. } // Continue fetch if currently paused
  751. (_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
  752. }
  753. onOnline() {
  754. var _this$retryer3;
  755. const observer = this.observers.find(x => x.shouldFetchOnReconnect());
  756. if (observer) {
  757. observer.refetch({
  758. cancelRefetch: false
  759. });
  760. } // Continue fetch if currently paused
  761. (_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
  762. }
  763. addObserver(observer) {
  764. if (!this.observers.includes(observer)) {
  765. this.observers.push(observer); // Stop the query from being garbage collected
  766. this.clearGcTimeout();
  767. this.cache.notify({
  768. type: 'observerAdded',
  769. query: this,
  770. observer
  771. });
  772. }
  773. }
  774. removeObserver(observer) {
  775. if (this.observers.includes(observer)) {
  776. this.observers = this.observers.filter(x => x !== observer);
  777. if (!this.observers.length) {
  778. // If the transport layer does not support cancellation
  779. // we'll let the query continue so the result can be cached
  780. if (this.retryer) {
  781. if (this.abortSignalConsumed) {
  782. this.retryer.cancel({
  783. revert: true
  784. });
  785. } else {
  786. this.retryer.cancelRetry();
  787. }
  788. }
  789. this.scheduleGc();
  790. }
  791. this.cache.notify({
  792. type: 'observerRemoved',
  793. query: this,
  794. observer
  795. });
  796. }
  797. }
  798. getObserversCount() {
  799. return this.observers.length;
  800. }
  801. invalidate() {
  802. if (!this.state.isInvalidated) {
  803. this.dispatch({
  804. type: 'invalidate'
  805. });
  806. }
  807. }
  808. fetch(options, fetchOptions) {
  809. var _this$options$behavio, _context$fetchOptions;
  810. if (this.state.fetchStatus !== 'idle') {
  811. if (this.state.dataUpdatedAt && fetchOptions != null && fetchOptions.cancelRefetch) {
  812. // Silently cancel current fetch if the user wants to cancel refetches
  813. this.cancel({
  814. silent: true
  815. });
  816. } else if (this.promise) {
  817. var _this$retryer4;
  818. // make sure that retries that were potentially cancelled due to unmounts can continue
  819. (_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry(); // Return current promise if we are already fetching
  820. return this.promise;
  821. }
  822. } // Update config if passed, otherwise the config from the last execution is used
  823. if (options) {
  824. this.setOptions(options);
  825. } // Use the options from the first observer with a query function if no function is found.
  826. // This can happen when the query is hydrated or created with setQueryData.
  827. if (!this.options.queryFn) {
  828. const observer = this.observers.find(x => x.options.queryFn);
  829. if (observer) {
  830. this.setOptions(observer.options);
  831. }
  832. }
  833. {
  834. if (!Array.isArray(this.options.queryKey)) {
  835. this.logger.error("As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']");
  836. }
  837. }
  838. const abortController = getAbortController(); // Create query function context
  839. const queryFnContext = {
  840. queryKey: this.queryKey,
  841. pageParam: undefined,
  842. meta: this.meta
  843. }; // Adds an enumerable signal property to the object that
  844. // which sets abortSignalConsumed to true when the signal
  845. // is read.
  846. const addSignalProperty = object => {
  847. Object.defineProperty(object, 'signal', {
  848. enumerable: true,
  849. get: () => {
  850. if (abortController) {
  851. this.abortSignalConsumed = true;
  852. return abortController.signal;
  853. }
  854. return undefined;
  855. }
  856. });
  857. };
  858. addSignalProperty(queryFnContext); // Create fetch function
  859. const fetchFn = () => {
  860. if (!this.options.queryFn) {
  861. return Promise.reject("Missing queryFn for queryKey '" + this.options.queryHash + "'");
  862. }
  863. this.abortSignalConsumed = false;
  864. return this.options.queryFn(queryFnContext);
  865. }; // Trigger behavior hook
  866. const context = {
  867. fetchOptions,
  868. options: this.options,
  869. queryKey: this.queryKey,
  870. state: this.state,
  871. fetchFn
  872. };
  873. addSignalProperty(context);
  874. (_this$options$behavio = this.options.behavior) == null ? void 0 : _this$options$behavio.onFetch(context); // Store state in case the current fetch needs to be reverted
  875. this.revertState = this.state; // Set to fetching state if not already in it
  876. if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
  877. var _context$fetchOptions2;
  878. this.dispatch({
  879. type: 'fetch',
  880. meta: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.meta
  881. });
  882. }
  883. const onError = error => {
  884. // Optimistically update state if needed
  885. if (!(isCancelledError(error) && error.silent)) {
  886. this.dispatch({
  887. type: 'error',
  888. error: error
  889. });
  890. }
  891. if (!isCancelledError(error)) {
  892. var _this$cache$config$on, _this$cache$config, _this$cache$config$on2, _this$cache$config2;
  893. // Notify cache callback
  894. (_this$cache$config$on = (_this$cache$config = this.cache.config).onError) == null ? void 0 : _this$cache$config$on.call(_this$cache$config, error, this);
  895. (_this$cache$config$on2 = (_this$cache$config2 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on2.call(_this$cache$config2, this.state.data, error, this);
  896. {
  897. this.logger.error(error);
  898. }
  899. }
  900. if (!this.isFetchingOptimistic) {
  901. // Schedule query gc after fetching
  902. this.scheduleGc();
  903. }
  904. this.isFetchingOptimistic = false;
  905. }; // Try to fetch the data
  906. this.retryer = createRetryer({
  907. fn: context.fetchFn,
  908. abort: abortController == null ? void 0 : abortController.abort.bind(abortController),
  909. onSuccess: data => {
  910. var _this$cache$config$on3, _this$cache$config3, _this$cache$config$on4, _this$cache$config4;
  911. if (typeof data === 'undefined') {
  912. {
  913. this.logger.error("Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: " + this.queryHash);
  914. }
  915. onError(new Error(this.queryHash + " data is undefined"));
  916. return;
  917. }
  918. this.setData(data); // Notify cache callback
  919. (_this$cache$config$on3 = (_this$cache$config3 = this.cache.config).onSuccess) == null ? void 0 : _this$cache$config$on3.call(_this$cache$config3, data, this);
  920. (_this$cache$config$on4 = (_this$cache$config4 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on4.call(_this$cache$config4, data, this.state.error, this);
  921. if (!this.isFetchingOptimistic) {
  922. // Schedule query gc after fetching
  923. this.scheduleGc();
  924. }
  925. this.isFetchingOptimistic = false;
  926. },
  927. onError,
  928. onFail: (failureCount, error) => {
  929. this.dispatch({
  930. type: 'failed',
  931. failureCount,
  932. error
  933. });
  934. },
  935. onPause: () => {
  936. this.dispatch({
  937. type: 'pause'
  938. });
  939. },
  940. onContinue: () => {
  941. this.dispatch({
  942. type: 'continue'
  943. });
  944. },
  945. retry: context.options.retry,
  946. retryDelay: context.options.retryDelay,
  947. networkMode: context.options.networkMode
  948. });
  949. this.promise = this.retryer.promise;
  950. return this.promise;
  951. }
  952. dispatch(action) {
  953. const reducer = state => {
  954. var _action$meta, _action$dataUpdatedAt;
  955. switch (action.type) {
  956. case 'failed':
  957. return { ...state,
  958. fetchFailureCount: action.failureCount,
  959. fetchFailureReason: action.error
  960. };
  961. case 'pause':
  962. return { ...state,
  963. fetchStatus: 'paused'
  964. };
  965. case 'continue':
  966. return { ...state,
  967. fetchStatus: 'fetching'
  968. };
  969. case 'fetch':
  970. return { ...state,
  971. fetchFailureCount: 0,
  972. fetchFailureReason: null,
  973. fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
  974. fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused',
  975. ...(!state.dataUpdatedAt && {
  976. error: null,
  977. status: 'loading'
  978. })
  979. };
  980. case 'success':
  981. return { ...state,
  982. data: action.data,
  983. dataUpdateCount: state.dataUpdateCount + 1,
  984. dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
  985. error: null,
  986. isInvalidated: false,
  987. status: 'success',
  988. ...(!action.manual && {
  989. fetchStatus: 'idle',
  990. fetchFailureCount: 0,
  991. fetchFailureReason: null
  992. })
  993. };
  994. case 'error':
  995. const error = action.error;
  996. if (isCancelledError(error) && error.revert && this.revertState) {
  997. return { ...this.revertState,
  998. fetchStatus: 'idle'
  999. };
  1000. }
  1001. return { ...state,
  1002. error: error,
  1003. errorUpdateCount: state.errorUpdateCount + 1,
  1004. errorUpdatedAt: Date.now(),
  1005. fetchFailureCount: state.fetchFailureCount + 1,
  1006. fetchFailureReason: error,
  1007. fetchStatus: 'idle',
  1008. status: 'error'
  1009. };
  1010. case 'invalidate':
  1011. return { ...state,
  1012. isInvalidated: true
  1013. };
  1014. case 'setState':
  1015. return { ...state,
  1016. ...action.state
  1017. };
  1018. }
  1019. };
  1020. this.state = reducer(this.state);
  1021. notifyManager.batch(() => {
  1022. this.observers.forEach(observer => {
  1023. observer.onQueryUpdate(action);
  1024. });
  1025. this.cache.notify({
  1026. query: this,
  1027. type: 'updated',
  1028. action
  1029. });
  1030. });
  1031. }
  1032. }
  1033. function getDefaultState$1(options) {
  1034. const data = typeof options.initialData === 'function' ? options.initialData() : options.initialData;
  1035. const hasData = typeof data !== 'undefined';
  1036. const initialDataUpdatedAt = hasData ? typeof options.initialDataUpdatedAt === 'function' ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
  1037. return {
  1038. data,
  1039. dataUpdateCount: 0,
  1040. dataUpdatedAt: hasData ? initialDataUpdatedAt != null ? initialDataUpdatedAt : Date.now() : 0,
  1041. error: null,
  1042. errorUpdateCount: 0,
  1043. errorUpdatedAt: 0,
  1044. fetchFailureCount: 0,
  1045. fetchFailureReason: null,
  1046. fetchMeta: null,
  1047. isInvalidated: false,
  1048. status: hasData ? 'success' : 'loading',
  1049. fetchStatus: 'idle'
  1050. };
  1051. }
  1052. // CLASS
  1053. class QueryCache extends Subscribable {
  1054. constructor(config) {
  1055. super();
  1056. this.config = config || {};
  1057. this.queries = [];
  1058. this.queriesMap = {};
  1059. }
  1060. build(client, options, state) {
  1061. var _options$queryHash;
  1062. const queryKey = options.queryKey;
  1063. const queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : hashQueryKeyByOptions(queryKey, options);
  1064. let query = this.get(queryHash);
  1065. if (!query) {
  1066. query = new Query({
  1067. cache: this,
  1068. logger: client.getLogger(),
  1069. queryKey,
  1070. queryHash,
  1071. options: client.defaultQueryOptions(options),
  1072. state,
  1073. defaultOptions: client.getQueryDefaults(queryKey)
  1074. });
  1075. this.add(query);
  1076. }
  1077. return query;
  1078. }
  1079. add(query) {
  1080. if (!this.queriesMap[query.queryHash]) {
  1081. this.queriesMap[query.queryHash] = query;
  1082. this.queries.push(query);
  1083. this.notify({
  1084. type: 'added',
  1085. query
  1086. });
  1087. }
  1088. }
  1089. remove(query) {
  1090. const queryInMap = this.queriesMap[query.queryHash];
  1091. if (queryInMap) {
  1092. query.destroy();
  1093. this.queries = this.queries.filter(x => x !== query);
  1094. if (queryInMap === query) {
  1095. delete this.queriesMap[query.queryHash];
  1096. }
  1097. this.notify({
  1098. type: 'removed',
  1099. query
  1100. });
  1101. }
  1102. }
  1103. clear() {
  1104. notifyManager.batch(() => {
  1105. this.queries.forEach(query => {
  1106. this.remove(query);
  1107. });
  1108. });
  1109. }
  1110. get(queryHash) {
  1111. return this.queriesMap[queryHash];
  1112. }
  1113. getAll() {
  1114. return this.queries;
  1115. }
  1116. find(arg1, arg2) {
  1117. const [filters] = parseFilterArgs(arg1, arg2);
  1118. if (typeof filters.exact === 'undefined') {
  1119. filters.exact = true;
  1120. }
  1121. return this.queries.find(query => matchQuery(filters, query));
  1122. }
  1123. findAll(arg1, arg2) {
  1124. const [filters] = parseFilterArgs(arg1, arg2);
  1125. return Object.keys(filters).length > 0 ? this.queries.filter(query => matchQuery(filters, query)) : this.queries;
  1126. }
  1127. notify(event) {
  1128. notifyManager.batch(() => {
  1129. this.listeners.forEach(({
  1130. listener
  1131. }) => {
  1132. listener(event);
  1133. });
  1134. });
  1135. }
  1136. onFocus() {
  1137. notifyManager.batch(() => {
  1138. this.queries.forEach(query => {
  1139. query.onFocus();
  1140. });
  1141. });
  1142. }
  1143. onOnline() {
  1144. notifyManager.batch(() => {
  1145. this.queries.forEach(query => {
  1146. query.onOnline();
  1147. });
  1148. });
  1149. }
  1150. }
  1151. // CLASS
  1152. class Mutation extends Removable {
  1153. constructor(config) {
  1154. super();
  1155. this.defaultOptions = config.defaultOptions;
  1156. this.mutationId = config.mutationId;
  1157. this.mutationCache = config.mutationCache;
  1158. this.logger = config.logger || defaultLogger;
  1159. this.observers = [];
  1160. this.state = config.state || getDefaultState();
  1161. this.setOptions(config.options);
  1162. this.scheduleGc();
  1163. }
  1164. setOptions(options) {
  1165. this.options = { ...this.defaultOptions,
  1166. ...options
  1167. };
  1168. this.updateCacheTime(this.options.cacheTime);
  1169. }
  1170. get meta() {
  1171. return this.options.meta;
  1172. }
  1173. setState(state) {
  1174. this.dispatch({
  1175. type: 'setState',
  1176. state
  1177. });
  1178. }
  1179. addObserver(observer) {
  1180. if (!this.observers.includes(observer)) {
  1181. this.observers.push(observer); // Stop the mutation from being garbage collected
  1182. this.clearGcTimeout();
  1183. this.mutationCache.notify({
  1184. type: 'observerAdded',
  1185. mutation: this,
  1186. observer
  1187. });
  1188. }
  1189. }
  1190. removeObserver(observer) {
  1191. this.observers = this.observers.filter(x => x !== observer);
  1192. this.scheduleGc();
  1193. this.mutationCache.notify({
  1194. type: 'observerRemoved',
  1195. mutation: this,
  1196. observer
  1197. });
  1198. }
  1199. optionalRemove() {
  1200. if (!this.observers.length) {
  1201. if (this.state.status === 'loading') {
  1202. this.scheduleGc();
  1203. } else {
  1204. this.mutationCache.remove(this);
  1205. }
  1206. }
  1207. }
  1208. continue() {
  1209. var _this$retryer$continu, _this$retryer;
  1210. return (_this$retryer$continu = (_this$retryer = this.retryer) == null ? void 0 : _this$retryer.continue()) != null ? _this$retryer$continu : this.execute();
  1211. }
  1212. async execute() {
  1213. const executeMutation = () => {
  1214. var _this$options$retry;
  1215. this.retryer = createRetryer({
  1216. fn: () => {
  1217. if (!this.options.mutationFn) {
  1218. return Promise.reject('No mutationFn found');
  1219. }
  1220. return this.options.mutationFn(this.state.variables);
  1221. },
  1222. onFail: (failureCount, error) => {
  1223. this.dispatch({
  1224. type: 'failed',
  1225. failureCount,
  1226. error
  1227. });
  1228. },
  1229. onPause: () => {
  1230. this.dispatch({
  1231. type: 'pause'
  1232. });
  1233. },
  1234. onContinue: () => {
  1235. this.dispatch({
  1236. type: 'continue'
  1237. });
  1238. },
  1239. retry: (_this$options$retry = this.options.retry) != null ? _this$options$retry : 0,
  1240. retryDelay: this.options.retryDelay,
  1241. networkMode: this.options.networkMode
  1242. });
  1243. return this.retryer.promise;
  1244. };
  1245. const restored = this.state.status === 'loading';
  1246. try {
  1247. var _this$mutationCache$c3, _this$mutationCache$c4, _this$options$onSucce, _this$options2, _this$mutationCache$c5, _this$mutationCache$c6, _this$options$onSettl, _this$options3;
  1248. if (!restored) {
  1249. var _this$mutationCache$c, _this$mutationCache$c2, _this$options$onMutat, _this$options;
  1250. this.dispatch({
  1251. type: 'loading',
  1252. variables: this.options.variables
  1253. }); // Notify cache callback
  1254. await ((_this$mutationCache$c = (_this$mutationCache$c2 = this.mutationCache.config).onMutate) == null ? void 0 : _this$mutationCache$c.call(_this$mutationCache$c2, this.state.variables, this));
  1255. const context = await ((_this$options$onMutat = (_this$options = this.options).onMutate) == null ? void 0 : _this$options$onMutat.call(_this$options, this.state.variables));
  1256. if (context !== this.state.context) {
  1257. this.dispatch({
  1258. type: 'loading',
  1259. context,
  1260. variables: this.state.variables
  1261. });
  1262. }
  1263. }
  1264. const data = await executeMutation(); // Notify cache callback
  1265. await ((_this$mutationCache$c3 = (_this$mutationCache$c4 = this.mutationCache.config).onSuccess) == null ? void 0 : _this$mutationCache$c3.call(_this$mutationCache$c4, data, this.state.variables, this.state.context, this));
  1266. await ((_this$options$onSucce = (_this$options2 = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options2, data, this.state.variables, this.state.context)); // Notify cache callback
  1267. await ((_this$mutationCache$c5 = (_this$mutationCache$c6 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c5.call(_this$mutationCache$c6, data, null, this.state.variables, this.state.context, this));
  1268. await ((_this$options$onSettl = (_this$options3 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options3, data, null, this.state.variables, this.state.context));
  1269. this.dispatch({
  1270. type: 'success',
  1271. data
  1272. });
  1273. return data;
  1274. } catch (error) {
  1275. try {
  1276. var _this$mutationCache$c7, _this$mutationCache$c8, _this$options$onError, _this$options4, _this$mutationCache$c9, _this$mutationCache$c10, _this$options$onSettl2, _this$options5;
  1277. // Notify cache callback
  1278. await ((_this$mutationCache$c7 = (_this$mutationCache$c8 = this.mutationCache.config).onError) == null ? void 0 : _this$mutationCache$c7.call(_this$mutationCache$c8, error, this.state.variables, this.state.context, this));
  1279. if ("development" !== 'production') {
  1280. this.logger.error(error);
  1281. }
  1282. await ((_this$options$onError = (_this$options4 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options4, error, this.state.variables, this.state.context)); // Notify cache callback
  1283. await ((_this$mutationCache$c9 = (_this$mutationCache$c10 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c9.call(_this$mutationCache$c10, undefined, error, this.state.variables, this.state.context, this));
  1284. await ((_this$options$onSettl2 = (_this$options5 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options5, undefined, error, this.state.variables, this.state.context));
  1285. throw error;
  1286. } finally {
  1287. this.dispatch({
  1288. type: 'error',
  1289. error: error
  1290. });
  1291. }
  1292. }
  1293. }
  1294. dispatch(action) {
  1295. const reducer = state => {
  1296. switch (action.type) {
  1297. case 'failed':
  1298. return { ...state,
  1299. failureCount: action.failureCount,
  1300. failureReason: action.error
  1301. };
  1302. case 'pause':
  1303. return { ...state,
  1304. isPaused: true
  1305. };
  1306. case 'continue':
  1307. return { ...state,
  1308. isPaused: false
  1309. };
  1310. case 'loading':
  1311. return { ...state,
  1312. context: action.context,
  1313. data: undefined,
  1314. failureCount: 0,
  1315. failureReason: null,
  1316. error: null,
  1317. isPaused: !canFetch(this.options.networkMode),
  1318. status: 'loading',
  1319. variables: action.variables
  1320. };
  1321. case 'success':
  1322. return { ...state,
  1323. data: action.data,
  1324. failureCount: 0,
  1325. failureReason: null,
  1326. error: null,
  1327. status: 'success',
  1328. isPaused: false
  1329. };
  1330. case 'error':
  1331. return { ...state,
  1332. data: undefined,
  1333. error: action.error,
  1334. failureCount: state.failureCount + 1,
  1335. failureReason: action.error,
  1336. isPaused: false,
  1337. status: 'error'
  1338. };
  1339. case 'setState':
  1340. return { ...state,
  1341. ...action.state
  1342. };
  1343. }
  1344. };
  1345. this.state = reducer(this.state);
  1346. notifyManager.batch(() => {
  1347. this.observers.forEach(observer => {
  1348. observer.onMutationUpdate(action);
  1349. });
  1350. this.mutationCache.notify({
  1351. mutation: this,
  1352. type: 'updated',
  1353. action
  1354. });
  1355. });
  1356. }
  1357. }
  1358. function getDefaultState() {
  1359. return {
  1360. context: undefined,
  1361. data: undefined,
  1362. error: null,
  1363. failureCount: 0,
  1364. failureReason: null,
  1365. isPaused: false,
  1366. status: 'idle',
  1367. variables: undefined
  1368. };
  1369. }
  1370. // CLASS
  1371. class MutationCache extends Subscribable {
  1372. constructor(config) {
  1373. super();
  1374. this.config = config || {};
  1375. this.mutations = [];
  1376. this.mutationId = 0;
  1377. }
  1378. build(client, options, state) {
  1379. const mutation = new Mutation({
  1380. mutationCache: this,
  1381. logger: client.getLogger(),
  1382. mutationId: ++this.mutationId,
  1383. options: client.defaultMutationOptions(options),
  1384. state,
  1385. defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined
  1386. });
  1387. this.add(mutation);
  1388. return mutation;
  1389. }
  1390. add(mutation) {
  1391. this.mutations.push(mutation);
  1392. this.notify({
  1393. type: 'added',
  1394. mutation
  1395. });
  1396. }
  1397. remove(mutation) {
  1398. this.mutations = this.mutations.filter(x => x !== mutation);
  1399. this.notify({
  1400. type: 'removed',
  1401. mutation
  1402. });
  1403. }
  1404. clear() {
  1405. notifyManager.batch(() => {
  1406. this.mutations.forEach(mutation => {
  1407. this.remove(mutation);
  1408. });
  1409. });
  1410. }
  1411. getAll() {
  1412. return this.mutations;
  1413. }
  1414. find(filters) {
  1415. if (typeof filters.exact === 'undefined') {
  1416. filters.exact = true;
  1417. }
  1418. return this.mutations.find(mutation => matchMutation(filters, mutation));
  1419. }
  1420. findAll(filters) {
  1421. return this.mutations.filter(mutation => matchMutation(filters, mutation));
  1422. }
  1423. notify(event) {
  1424. notifyManager.batch(() => {
  1425. this.listeners.forEach(({
  1426. listener
  1427. }) => {
  1428. listener(event);
  1429. });
  1430. });
  1431. }
  1432. resumePausedMutations() {
  1433. var _this$resuming;
  1434. this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
  1435. const pausedMutations = this.mutations.filter(x => x.state.isPaused);
  1436. return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
  1437. }).then(() => {
  1438. this.resuming = undefined;
  1439. });
  1440. return this.resuming;
  1441. }
  1442. }
  1443. function infiniteQueryBehavior() {
  1444. return {
  1445. onFetch: context => {
  1446. context.fetchFn = () => {
  1447. var _context$fetchOptions, _context$fetchOptions2, _context$fetchOptions3, _context$fetchOptions4, _context$state$data, _context$state$data2;
  1448. const refetchPage = (_context$fetchOptions = context.fetchOptions) == null ? void 0 : (_context$fetchOptions2 = _context$fetchOptions.meta) == null ? void 0 : _context$fetchOptions2.refetchPage;
  1449. const fetchMore = (_context$fetchOptions3 = context.fetchOptions) == null ? void 0 : (_context$fetchOptions4 = _context$fetchOptions3.meta) == null ? void 0 : _context$fetchOptions4.fetchMore;
  1450. const pageParam = fetchMore == null ? void 0 : fetchMore.pageParam;
  1451. const isFetchingNextPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'forward';
  1452. const isFetchingPreviousPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'backward';
  1453. const oldPages = ((_context$state$data = context.state.data) == null ? void 0 : _context$state$data.pages) || [];
  1454. const oldPageParams = ((_context$state$data2 = context.state.data) == null ? void 0 : _context$state$data2.pageParams) || [];
  1455. let newPageParams = oldPageParams;
  1456. let cancelled = false;
  1457. const addSignalProperty = object => {
  1458. Object.defineProperty(object, 'signal', {
  1459. enumerable: true,
  1460. get: () => {
  1461. var _context$signal;
  1462. if ((_context$signal = context.signal) != null && _context$signal.aborted) {
  1463. cancelled = true;
  1464. } else {
  1465. var _context$signal2;
  1466. (_context$signal2 = context.signal) == null ? void 0 : _context$signal2.addEventListener('abort', () => {
  1467. cancelled = true;
  1468. });
  1469. }
  1470. return context.signal;
  1471. }
  1472. });
  1473. }; // Get query function
  1474. const queryFn = context.options.queryFn || (() => Promise.reject("Missing queryFn for queryKey '" + context.options.queryHash + "'"));
  1475. const buildNewPages = (pages, param, page, previous) => {
  1476. newPageParams = previous ? [param, ...newPageParams] : [...newPageParams, param];
  1477. return previous ? [page, ...pages] : [...pages, page];
  1478. }; // Create function to fetch a page
  1479. const fetchPage = (pages, manual, param, previous) => {
  1480. if (cancelled) {
  1481. return Promise.reject('Cancelled');
  1482. }
  1483. if (typeof param === 'undefined' && !manual && pages.length) {
  1484. return Promise.resolve(pages);
  1485. }
  1486. const queryFnContext = {
  1487. queryKey: context.queryKey,
  1488. pageParam: param,
  1489. meta: context.options.meta
  1490. };
  1491. addSignalProperty(queryFnContext);
  1492. const queryFnResult = queryFn(queryFnContext);
  1493. const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
  1494. return promise;
  1495. };
  1496. let promise; // Fetch first page?
  1497. if (!oldPages.length) {
  1498. promise = fetchPage([]);
  1499. } // Fetch next page?
  1500. else if (isFetchingNextPage) {
  1501. const manual = typeof pageParam !== 'undefined';
  1502. const param = manual ? pageParam : getNextPageParam(context.options, oldPages);
  1503. promise = fetchPage(oldPages, manual, param);
  1504. } // Fetch previous page?
  1505. else if (isFetchingPreviousPage) {
  1506. const manual = typeof pageParam !== 'undefined';
  1507. const param = manual ? pageParam : getPreviousPageParam(context.options, oldPages);
  1508. promise = fetchPage(oldPages, manual, param, true);
  1509. } // Refetch pages
  1510. else {
  1511. newPageParams = [];
  1512. const manual = typeof context.options.getNextPageParam === 'undefined';
  1513. const shouldFetchFirstPage = refetchPage && oldPages[0] ? refetchPage(oldPages[0], 0, oldPages) : true; // Fetch first page
  1514. promise = shouldFetchFirstPage ? fetchPage([], manual, oldPageParams[0]) : Promise.resolve(buildNewPages([], oldPageParams[0], oldPages[0])); // Fetch remaining pages
  1515. for (let i = 1; i < oldPages.length; i++) {
  1516. promise = promise.then(pages => {
  1517. const shouldFetchNextPage = refetchPage && oldPages[i] ? refetchPage(oldPages[i], i, oldPages) : true;
  1518. if (shouldFetchNextPage) {
  1519. const param = manual ? oldPageParams[i] : getNextPageParam(context.options, pages);
  1520. return fetchPage(pages, manual, param);
  1521. }
  1522. return Promise.resolve(buildNewPages(pages, oldPageParams[i], oldPages[i]));
  1523. });
  1524. }
  1525. }
  1526. const finalPromise = promise.then(pages => ({
  1527. pages,
  1528. pageParams: newPageParams
  1529. }));
  1530. return finalPromise;
  1531. };
  1532. }
  1533. };
  1534. }
  1535. function getNextPageParam(options, pages) {
  1536. return options.getNextPageParam == null ? void 0 : options.getNextPageParam(pages[pages.length - 1], pages);
  1537. }
  1538. function getPreviousPageParam(options, pages) {
  1539. return options.getPreviousPageParam == null ? void 0 : options.getPreviousPageParam(pages[0], pages);
  1540. }
  1541. /**
  1542. * Checks if there is a next page.
  1543. * Returns `undefined` if it cannot be determined.
  1544. */
  1545. function hasNextPage(options, pages) {
  1546. if (options.getNextPageParam && Array.isArray(pages)) {
  1547. const nextPageParam = getNextPageParam(options, pages);
  1548. return typeof nextPageParam !== 'undefined' && nextPageParam !== null && nextPageParam !== false;
  1549. }
  1550. return;
  1551. }
  1552. /**
  1553. * Checks if there is a previous page.
  1554. * Returns `undefined` if it cannot be determined.
  1555. */
  1556. function hasPreviousPage(options, pages) {
  1557. if (options.getPreviousPageParam && Array.isArray(pages)) {
  1558. const previousPageParam = getPreviousPageParam(options, pages);
  1559. return typeof previousPageParam !== 'undefined' && previousPageParam !== null && previousPageParam !== false;
  1560. }
  1561. return;
  1562. }
  1563. // CLASS
  1564. class QueryClient {
  1565. constructor(config = {}) {
  1566. this.queryCache = config.queryCache || new QueryCache();
  1567. this.mutationCache = config.mutationCache || new MutationCache();
  1568. this.logger = config.logger || defaultLogger;
  1569. this.defaultOptions = config.defaultOptions || {};
  1570. this.queryDefaults = [];
  1571. this.mutationDefaults = [];
  1572. this.mountCount = 0;
  1573. if (config.logger) {
  1574. this.logger.error("Passing a custom logger has been deprecated and will be removed in the next major version.");
  1575. }
  1576. }
  1577. mount() {
  1578. this.mountCount++;
  1579. if (this.mountCount !== 1) return;
  1580. this.unsubscribeFocus = focusManager.subscribe(() => {
  1581. if (focusManager.isFocused()) {
  1582. this.resumePausedMutations();
  1583. this.queryCache.onFocus();
  1584. }
  1585. });
  1586. this.unsubscribeOnline = onlineManager.subscribe(() => {
  1587. if (onlineManager.isOnline()) {
  1588. this.resumePausedMutations();
  1589. this.queryCache.onOnline();
  1590. }
  1591. });
  1592. }
  1593. unmount() {
  1594. var _this$unsubscribeFocu, _this$unsubscribeOnli;
  1595. this.mountCount--;
  1596. if (this.mountCount !== 0) return;
  1597. (_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this);
  1598. this.unsubscribeFocus = undefined;
  1599. (_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this);
  1600. this.unsubscribeOnline = undefined;
  1601. }
  1602. isFetching(arg1, arg2) {
  1603. const [filters] = parseFilterArgs(arg1, arg2);
  1604. filters.fetchStatus = 'fetching';
  1605. return this.queryCache.findAll(filters).length;
  1606. }
  1607. isMutating(filters) {
  1608. return this.mutationCache.findAll({ ...filters,
  1609. fetching: true
  1610. }).length;
  1611. }
  1612. getQueryData(queryKey, filters) {
  1613. var _this$queryCache$find;
  1614. return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data;
  1615. }
  1616. ensureQueryData(arg1, arg2, arg3) {
  1617. const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
  1618. const cachedData = this.getQueryData(parsedOptions.queryKey);
  1619. return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(parsedOptions);
  1620. }
  1621. getQueriesData(queryKeyOrFilters) {
  1622. return this.getQueryCache().findAll(queryKeyOrFilters).map(({
  1623. queryKey,
  1624. state
  1625. }) => {
  1626. const data = state.data;
  1627. return [queryKey, data];
  1628. });
  1629. }
  1630. setQueryData(queryKey, updater, options) {
  1631. const query = this.queryCache.find(queryKey);
  1632. const prevData = query == null ? void 0 : query.state.data;
  1633. const data = functionalUpdate(updater, prevData);
  1634. if (typeof data === 'undefined') {
  1635. return undefined;
  1636. }
  1637. const parsedOptions = parseQueryArgs(queryKey);
  1638. const defaultedOptions = this.defaultQueryOptions(parsedOptions);
  1639. return this.queryCache.build(this, defaultedOptions).setData(data, { ...options,
  1640. manual: true
  1641. });
  1642. }
  1643. setQueriesData(queryKeyOrFilters, updater, options) {
  1644. return notifyManager.batch(() => this.getQueryCache().findAll(queryKeyOrFilters).map(({
  1645. queryKey
  1646. }) => [queryKey, this.setQueryData(queryKey, updater, options)]));
  1647. }
  1648. getQueryState(queryKey, filters) {
  1649. var _this$queryCache$find2;
  1650. return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state;
  1651. }
  1652. removeQueries(arg1, arg2) {
  1653. const [filters] = parseFilterArgs(arg1, arg2);
  1654. const queryCache = this.queryCache;
  1655. notifyManager.batch(() => {
  1656. queryCache.findAll(filters).forEach(query => {
  1657. queryCache.remove(query);
  1658. });
  1659. });
  1660. }
  1661. resetQueries(arg1, arg2, arg3) {
  1662. const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
  1663. const queryCache = this.queryCache;
  1664. const refetchFilters = {
  1665. type: 'active',
  1666. ...filters
  1667. };
  1668. return notifyManager.batch(() => {
  1669. queryCache.findAll(filters).forEach(query => {
  1670. query.reset();
  1671. });
  1672. return this.refetchQueries(refetchFilters, options);
  1673. });
  1674. }
  1675. cancelQueries(arg1, arg2, arg3) {
  1676. const [filters, cancelOptions = {}] = parseFilterArgs(arg1, arg2, arg3);
  1677. if (typeof cancelOptions.revert === 'undefined') {
  1678. cancelOptions.revert = true;
  1679. }
  1680. const promises = notifyManager.batch(() => this.queryCache.findAll(filters).map(query => query.cancel(cancelOptions)));
  1681. return Promise.all(promises).then(noop).catch(noop);
  1682. }
  1683. invalidateQueries(arg1, arg2, arg3) {
  1684. const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
  1685. return notifyManager.batch(() => {
  1686. var _ref, _filters$refetchType;
  1687. this.queryCache.findAll(filters).forEach(query => {
  1688. query.invalidate();
  1689. });
  1690. if (filters.refetchType === 'none') {
  1691. return Promise.resolve();
  1692. }
  1693. const refetchFilters = { ...filters,
  1694. type: (_ref = (_filters$refetchType = filters.refetchType) != null ? _filters$refetchType : filters.type) != null ? _ref : 'active'
  1695. };
  1696. return this.refetchQueries(refetchFilters, options);
  1697. });
  1698. }
  1699. refetchQueries(arg1, arg2, arg3) {
  1700. const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
  1701. const promises = notifyManager.batch(() => this.queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
  1702. var _options$cancelRefetc;
  1703. return query.fetch(undefined, { ...options,
  1704. cancelRefetch: (_options$cancelRefetc = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc : true,
  1705. meta: {
  1706. refetchPage: filters.refetchPage
  1707. }
  1708. });
  1709. }));
  1710. let promise = Promise.all(promises).then(noop);
  1711. if (!(options != null && options.throwOnError)) {
  1712. promise = promise.catch(noop);
  1713. }
  1714. return promise;
  1715. }
  1716. fetchQuery(arg1, arg2, arg3) {
  1717. const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
  1718. const defaultedOptions = this.defaultQueryOptions(parsedOptions); // https://github.com/tannerlinsley/react-query/issues/652
  1719. if (typeof defaultedOptions.retry === 'undefined') {
  1720. defaultedOptions.retry = false;
  1721. }
  1722. const query = this.queryCache.build(this, defaultedOptions);
  1723. return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
  1724. }
  1725. prefetchQuery(arg1, arg2, arg3) {
  1726. return this.fetchQuery(arg1, arg2, arg3).then(noop).catch(noop);
  1727. }
  1728. fetchInfiniteQuery(arg1, arg2, arg3) {
  1729. const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
  1730. parsedOptions.behavior = infiniteQueryBehavior();
  1731. return this.fetchQuery(parsedOptions);
  1732. }
  1733. prefetchInfiniteQuery(arg1, arg2, arg3) {
  1734. return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop).catch(noop);
  1735. }
  1736. resumePausedMutations() {
  1737. return this.mutationCache.resumePausedMutations();
  1738. }
  1739. getQueryCache() {
  1740. return this.queryCache;
  1741. }
  1742. getMutationCache() {
  1743. return this.mutationCache;
  1744. }
  1745. getLogger() {
  1746. return this.logger;
  1747. }
  1748. getDefaultOptions() {
  1749. return this.defaultOptions;
  1750. }
  1751. setDefaultOptions(options) {
  1752. this.defaultOptions = options;
  1753. }
  1754. setQueryDefaults(queryKey, options) {
  1755. const result = this.queryDefaults.find(x => hashQueryKey(queryKey) === hashQueryKey(x.queryKey));
  1756. if (result) {
  1757. result.defaultOptions = options;
  1758. } else {
  1759. this.queryDefaults.push({
  1760. queryKey,
  1761. defaultOptions: options
  1762. });
  1763. }
  1764. }
  1765. getQueryDefaults(queryKey) {
  1766. if (!queryKey) {
  1767. return undefined;
  1768. } // Get the first matching defaults
  1769. const firstMatchingDefaults = this.queryDefaults.find(x => partialMatchKey(queryKey, x.queryKey)); // Additional checks and error in dev mode
  1770. {
  1771. // Retrieve all matching defaults for the given key
  1772. const matchingDefaults = this.queryDefaults.filter(x => partialMatchKey(queryKey, x.queryKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
  1773. if (matchingDefaults.length > 1) {
  1774. this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
  1775. }
  1776. }
  1777. return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
  1778. }
  1779. setMutationDefaults(mutationKey, options) {
  1780. const result = this.mutationDefaults.find(x => hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey));
  1781. if (result) {
  1782. result.defaultOptions = options;
  1783. } else {
  1784. this.mutationDefaults.push({
  1785. mutationKey,
  1786. defaultOptions: options
  1787. });
  1788. }
  1789. }
  1790. getMutationDefaults(mutationKey) {
  1791. if (!mutationKey) {
  1792. return undefined;
  1793. } // Get the first matching defaults
  1794. const firstMatchingDefaults = this.mutationDefaults.find(x => partialMatchKey(mutationKey, x.mutationKey)); // Additional checks and error in dev mode
  1795. {
  1796. // Retrieve all matching defaults for the given key
  1797. const matchingDefaults = this.mutationDefaults.filter(x => partialMatchKey(mutationKey, x.mutationKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
  1798. if (matchingDefaults.length > 1) {
  1799. this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
  1800. }
  1801. }
  1802. return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
  1803. }
  1804. defaultQueryOptions(options) {
  1805. if (options != null && options._defaulted) {
  1806. return options;
  1807. }
  1808. const defaultedOptions = { ...this.defaultOptions.queries,
  1809. ...this.getQueryDefaults(options == null ? void 0 : options.queryKey),
  1810. ...options,
  1811. _defaulted: true
  1812. };
  1813. if (!defaultedOptions.queryHash && defaultedOptions.queryKey) {
  1814. defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions);
  1815. } // dependent default values
  1816. if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
  1817. defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
  1818. }
  1819. if (typeof defaultedOptions.useErrorBoundary === 'undefined') {
  1820. defaultedOptions.useErrorBoundary = !!defaultedOptions.suspense;
  1821. }
  1822. return defaultedOptions;
  1823. }
  1824. defaultMutationOptions(options) {
  1825. if (options != null && options._defaulted) {
  1826. return options;
  1827. }
  1828. return { ...this.defaultOptions.mutations,
  1829. ...this.getMutationDefaults(options == null ? void 0 : options.mutationKey),
  1830. ...options,
  1831. _defaulted: true
  1832. };
  1833. }
  1834. clear() {
  1835. this.queryCache.clear();
  1836. this.mutationCache.clear();
  1837. }
  1838. }
  1839. class QueryObserver extends Subscribable {
  1840. constructor(client, options) {
  1841. super();
  1842. this.client = client;
  1843. this.options = options;
  1844. this.trackedProps = new Set();
  1845. this.selectError = null;
  1846. this.bindMethods();
  1847. this.setOptions(options);
  1848. }
  1849. bindMethods() {
  1850. this.remove = this.remove.bind(this);
  1851. this.refetch = this.refetch.bind(this);
  1852. }
  1853. onSubscribe() {
  1854. if (this.listeners.size === 1) {
  1855. this.currentQuery.addObserver(this);
  1856. if (shouldFetchOnMount(this.currentQuery, this.options)) {
  1857. this.executeFetch();
  1858. }
  1859. this.updateTimers();
  1860. }
  1861. }
  1862. onUnsubscribe() {
  1863. if (!this.hasListeners()) {
  1864. this.destroy();
  1865. }
  1866. }
  1867. shouldFetchOnReconnect() {
  1868. return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
  1869. }
  1870. shouldFetchOnWindowFocus() {
  1871. return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
  1872. }
  1873. destroy() {
  1874. this.listeners = new Set();
  1875. this.clearStaleTimeout();
  1876. this.clearRefetchInterval();
  1877. this.currentQuery.removeObserver(this);
  1878. }
  1879. setOptions(options, notifyOptions) {
  1880. const prevOptions = this.options;
  1881. const prevQuery = this.currentQuery;
  1882. this.options = this.client.defaultQueryOptions(options);
  1883. if (typeof (options == null ? void 0 : options.isDataEqual) !== 'undefined') {
  1884. this.client.getLogger().error("The isDataEqual option has been deprecated and will be removed in the next major version. You can achieve the same functionality by passing a function as the structuralSharing option");
  1885. }
  1886. if (!shallowEqualObjects(prevOptions, this.options)) {
  1887. this.client.getQueryCache().notify({
  1888. type: 'observerOptionsUpdated',
  1889. query: this.currentQuery,
  1890. observer: this
  1891. });
  1892. }
  1893. if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
  1894. throw new Error('Expected enabled to be a boolean');
  1895. } // Keep previous query key if the user does not supply one
  1896. if (!this.options.queryKey) {
  1897. this.options.queryKey = prevOptions.queryKey;
  1898. }
  1899. this.updateQuery();
  1900. const mounted = this.hasListeners(); // Fetch if there are subscribers
  1901. if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
  1902. this.executeFetch();
  1903. } // Update result
  1904. this.updateResult(notifyOptions); // Update stale interval if needed
  1905. if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
  1906. this.updateStaleTimeout();
  1907. }
  1908. const nextRefetchInterval = this.computeRefetchInterval(); // Update refetch interval if needed
  1909. if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
  1910. this.updateRefetchInterval(nextRefetchInterval);
  1911. }
  1912. }
  1913. getOptimisticResult(options) {
  1914. const query = this.client.getQueryCache().build(this.client, options);
  1915. const result = this.createResult(query, options);
  1916. if (shouldAssignObserverCurrentProperties(this, result, options)) {
  1917. // this assigns the optimistic result to the current Observer
  1918. // because if the query function changes, useQuery will be performing
  1919. // an effect where it would fetch again.
  1920. // When the fetch finishes, we perform a deep data cloning in order
  1921. // to reuse objects references. This deep data clone is performed against
  1922. // the `observer.currentResult.data` property
  1923. // When QueryKey changes, we refresh the query and get new `optimistic`
  1924. // result, while we leave the `observer.currentResult`, so when new data
  1925. // arrives, it finds the old `observer.currentResult` which is related
  1926. // to the old QueryKey. Which means that currentResult and selectData are
  1927. // out of sync already.
  1928. // To solve this, we move the cursor of the currentResult everytime
  1929. // an observer reads an optimistic value.
  1930. // When keeping the previous data, the result doesn't change until new
  1931. // data arrives.
  1932. this.currentResult = result;
  1933. this.currentResultOptions = this.options;
  1934. this.currentResultState = this.currentQuery.state;
  1935. }
  1936. return result;
  1937. }
  1938. getCurrentResult() {
  1939. return this.currentResult;
  1940. }
  1941. trackResult(result) {
  1942. const trackedResult = {};
  1943. Object.keys(result).forEach(key => {
  1944. Object.defineProperty(trackedResult, key, {
  1945. configurable: false,
  1946. enumerable: true,
  1947. get: () => {
  1948. this.trackedProps.add(key);
  1949. return result[key];
  1950. }
  1951. });
  1952. });
  1953. return trackedResult;
  1954. }
  1955. getCurrentQuery() {
  1956. return this.currentQuery;
  1957. }
  1958. remove() {
  1959. this.client.getQueryCache().remove(this.currentQuery);
  1960. }
  1961. refetch({
  1962. refetchPage,
  1963. ...options
  1964. } = {}) {
  1965. return this.fetch({ ...options,
  1966. meta: {
  1967. refetchPage
  1968. }
  1969. });
  1970. }
  1971. fetchOptimistic(options) {
  1972. const defaultedOptions = this.client.defaultQueryOptions(options);
  1973. const query = this.client.getQueryCache().build(this.client, defaultedOptions);
  1974. query.isFetchingOptimistic = true;
  1975. return query.fetch().then(() => this.createResult(query, defaultedOptions));
  1976. }
  1977. fetch(fetchOptions) {
  1978. var _fetchOptions$cancelR;
  1979. return this.executeFetch({ ...fetchOptions,
  1980. cancelRefetch: (_fetchOptions$cancelR = fetchOptions.cancelRefetch) != null ? _fetchOptions$cancelR : true
  1981. }).then(() => {
  1982. this.updateResult();
  1983. return this.currentResult;
  1984. });
  1985. }
  1986. executeFetch(fetchOptions) {
  1987. // Make sure we reference the latest query as the current one might have been removed
  1988. this.updateQuery(); // Fetch
  1989. let promise = this.currentQuery.fetch(this.options, fetchOptions);
  1990. if (!(fetchOptions != null && fetchOptions.throwOnError)) {
  1991. promise = promise.catch(noop);
  1992. }
  1993. return promise;
  1994. }
  1995. updateStaleTimeout() {
  1996. this.clearStaleTimeout();
  1997. if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
  1998. return;
  1999. }
  2000. const time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime); // The timeout is sometimes triggered 1 ms before the stale time expiration.
  2001. // To mitigate this issue we always add 1 ms to the timeout.
  2002. const timeout = time + 1;
  2003. this.staleTimeoutId = setTimeout(() => {
  2004. if (!this.currentResult.isStale) {
  2005. this.updateResult();
  2006. }
  2007. }, timeout);
  2008. }
  2009. computeRefetchInterval() {
  2010. var _this$options$refetch;
  2011. return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.currentResult.data, this.currentQuery) : (_this$options$refetch = this.options.refetchInterval) != null ? _this$options$refetch : false;
  2012. }
  2013. updateRefetchInterval(nextInterval) {
  2014. this.clearRefetchInterval();
  2015. this.currentRefetchInterval = nextInterval;
  2016. if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
  2017. return;
  2018. }
  2019. this.refetchIntervalId = setInterval(() => {
  2020. if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
  2021. this.executeFetch();
  2022. }
  2023. }, this.currentRefetchInterval);
  2024. }
  2025. updateTimers() {
  2026. this.updateStaleTimeout();
  2027. this.updateRefetchInterval(this.computeRefetchInterval());
  2028. }
  2029. clearStaleTimeout() {
  2030. if (this.staleTimeoutId) {
  2031. clearTimeout(this.staleTimeoutId);
  2032. this.staleTimeoutId = undefined;
  2033. }
  2034. }
  2035. clearRefetchInterval() {
  2036. if (this.refetchIntervalId) {
  2037. clearInterval(this.refetchIntervalId);
  2038. this.refetchIntervalId = undefined;
  2039. }
  2040. }
  2041. createResult(query, options) {
  2042. const prevQuery = this.currentQuery;
  2043. const prevOptions = this.options;
  2044. const prevResult = this.currentResult;
  2045. const prevResultState = this.currentResultState;
  2046. const prevResultOptions = this.currentResultOptions;
  2047. const queryChange = query !== prevQuery;
  2048. const queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
  2049. const prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
  2050. const {
  2051. state
  2052. } = query;
  2053. let {
  2054. dataUpdatedAt,
  2055. error,
  2056. errorUpdatedAt,
  2057. fetchStatus,
  2058. status
  2059. } = state;
  2060. let isPreviousData = false;
  2061. let isPlaceholderData = false;
  2062. let data; // Optimistically set result in fetching state if needed
  2063. if (options._optimisticResults) {
  2064. const mounted = this.hasListeners();
  2065. const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
  2066. const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
  2067. if (fetchOnMount || fetchOptionally) {
  2068. fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
  2069. if (!dataUpdatedAt) {
  2070. status = 'loading';
  2071. }
  2072. }
  2073. if (options._optimisticResults === 'isRestoring') {
  2074. fetchStatus = 'idle';
  2075. }
  2076. } // Keep previous data if needed
  2077. if (options.keepPreviousData && !state.dataUpdatedAt && prevQueryResult != null && prevQueryResult.isSuccess && status !== 'error') {
  2078. data = prevQueryResult.data;
  2079. dataUpdatedAt = prevQueryResult.dataUpdatedAt;
  2080. status = prevQueryResult.status;
  2081. isPreviousData = true;
  2082. } // Select data if needed
  2083. else if (options.select && typeof state.data !== 'undefined') {
  2084. // Memoize select result
  2085. if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
  2086. data = this.selectResult;
  2087. } else {
  2088. try {
  2089. this.selectFn = options.select;
  2090. data = options.select(state.data);
  2091. data = replaceData(prevResult == null ? void 0 : prevResult.data, data, options);
  2092. this.selectResult = data;
  2093. this.selectError = null;
  2094. } catch (selectError) {
  2095. {
  2096. this.client.getLogger().error(selectError);
  2097. }
  2098. this.selectError = selectError;
  2099. }
  2100. }
  2101. } // Use query data
  2102. else {
  2103. data = state.data;
  2104. } // Show placeholder data if needed
  2105. if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'loading') {
  2106. let placeholderData; // Memoize placeholder data
  2107. if (prevResult != null && prevResult.isPlaceholderData && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
  2108. placeholderData = prevResult.data;
  2109. } else {
  2110. placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData() : options.placeholderData;
  2111. if (options.select && typeof placeholderData !== 'undefined') {
  2112. try {
  2113. placeholderData = options.select(placeholderData);
  2114. this.selectError = null;
  2115. } catch (selectError) {
  2116. {
  2117. this.client.getLogger().error(selectError);
  2118. }
  2119. this.selectError = selectError;
  2120. }
  2121. }
  2122. }
  2123. if (typeof placeholderData !== 'undefined') {
  2124. status = 'success';
  2125. data = replaceData(prevResult == null ? void 0 : prevResult.data, placeholderData, options);
  2126. isPlaceholderData = true;
  2127. }
  2128. }
  2129. if (this.selectError) {
  2130. error = this.selectError;
  2131. data = this.selectResult;
  2132. errorUpdatedAt = Date.now();
  2133. status = 'error';
  2134. }
  2135. const isFetching = fetchStatus === 'fetching';
  2136. const isLoading = status === 'loading';
  2137. const isError = status === 'error';
  2138. const result = {
  2139. status,
  2140. fetchStatus,
  2141. isLoading,
  2142. isSuccess: status === 'success',
  2143. isError,
  2144. isInitialLoading: isLoading && isFetching,
  2145. data,
  2146. dataUpdatedAt,
  2147. error,
  2148. errorUpdatedAt,
  2149. failureCount: state.fetchFailureCount,
  2150. failureReason: state.fetchFailureReason,
  2151. errorUpdateCount: state.errorUpdateCount,
  2152. isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
  2153. isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
  2154. isFetching,
  2155. isRefetching: isFetching && !isLoading,
  2156. isLoadingError: isError && state.dataUpdatedAt === 0,
  2157. isPaused: fetchStatus === 'paused',
  2158. isPlaceholderData,
  2159. isPreviousData,
  2160. isRefetchError: isError && state.dataUpdatedAt !== 0,
  2161. isStale: isStale(query, options),
  2162. refetch: this.refetch,
  2163. remove: this.remove
  2164. };
  2165. return result;
  2166. }
  2167. updateResult(notifyOptions) {
  2168. const prevResult = this.currentResult;
  2169. const nextResult = this.createResult(this.currentQuery, this.options);
  2170. this.currentResultState = this.currentQuery.state;
  2171. this.currentResultOptions = this.options; // Only notify and update result if something has changed
  2172. if (shallowEqualObjects(nextResult, prevResult)) {
  2173. return;
  2174. }
  2175. this.currentResult = nextResult; // Determine which callbacks to trigger
  2176. const defaultNotifyOptions = {
  2177. cache: true
  2178. };
  2179. const shouldNotifyListeners = () => {
  2180. if (!prevResult) {
  2181. return true;
  2182. }
  2183. const {
  2184. notifyOnChangeProps
  2185. } = this.options;
  2186. const notifyOnChangePropsValue = typeof notifyOnChangeProps === 'function' ? notifyOnChangeProps() : notifyOnChangeProps;
  2187. if (notifyOnChangePropsValue === 'all' || !notifyOnChangePropsValue && !this.trackedProps.size) {
  2188. return true;
  2189. }
  2190. const includedProps = new Set(notifyOnChangePropsValue != null ? notifyOnChangePropsValue : this.trackedProps);
  2191. if (this.options.useErrorBoundary) {
  2192. includedProps.add('error');
  2193. }
  2194. return Object.keys(this.currentResult).some(key => {
  2195. const typedKey = key;
  2196. const changed = this.currentResult[typedKey] !== prevResult[typedKey];
  2197. return changed && includedProps.has(typedKey);
  2198. });
  2199. };
  2200. if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
  2201. defaultNotifyOptions.listeners = true;
  2202. }
  2203. this.notify({ ...defaultNotifyOptions,
  2204. ...notifyOptions
  2205. });
  2206. }
  2207. updateQuery() {
  2208. const query = this.client.getQueryCache().build(this.client, this.options);
  2209. if (query === this.currentQuery) {
  2210. return;
  2211. }
  2212. const prevQuery = this.currentQuery;
  2213. this.currentQuery = query;
  2214. this.currentQueryInitialState = query.state;
  2215. this.previousQueryResult = this.currentResult;
  2216. if (this.hasListeners()) {
  2217. prevQuery == null ? void 0 : prevQuery.removeObserver(this);
  2218. query.addObserver(this);
  2219. }
  2220. }
  2221. onQueryUpdate(action) {
  2222. const notifyOptions = {};
  2223. if (action.type === 'success') {
  2224. notifyOptions.onSuccess = !action.manual;
  2225. } else if (action.type === 'error' && !isCancelledError(action.error)) {
  2226. notifyOptions.onError = true;
  2227. }
  2228. this.updateResult(notifyOptions);
  2229. if (this.hasListeners()) {
  2230. this.updateTimers();
  2231. }
  2232. }
  2233. notify(notifyOptions) {
  2234. notifyManager.batch(() => {
  2235. // First trigger the configuration callbacks
  2236. if (notifyOptions.onSuccess) {
  2237. var _this$options$onSucce, _this$options, _this$options$onSettl, _this$options2;
  2238. (_this$options$onSucce = (_this$options = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options, this.currentResult.data);
  2239. (_this$options$onSettl = (_this$options2 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options2, this.currentResult.data, null);
  2240. } else if (notifyOptions.onError) {
  2241. var _this$options$onError, _this$options3, _this$options$onSettl2, _this$options4;
  2242. (_this$options$onError = (_this$options3 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options3, this.currentResult.error);
  2243. (_this$options$onSettl2 = (_this$options4 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options4, undefined, this.currentResult.error);
  2244. } // Then trigger the listeners
  2245. if (notifyOptions.listeners) {
  2246. this.listeners.forEach(({
  2247. listener
  2248. }) => {
  2249. listener(this.currentResult);
  2250. });
  2251. } // Then the cache listeners
  2252. if (notifyOptions.cache) {
  2253. this.client.getQueryCache().notify({
  2254. query: this.currentQuery,
  2255. type: 'observerResultsUpdated'
  2256. });
  2257. }
  2258. });
  2259. }
  2260. }
  2261. function shouldLoadOnMount(query, options) {
  2262. return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
  2263. }
  2264. function shouldFetchOnMount(query, options) {
  2265. return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
  2266. }
  2267. function shouldFetchOn(query, options, field) {
  2268. if (options.enabled !== false) {
  2269. const value = typeof field === 'function' ? field(query) : field;
  2270. return value === 'always' || value !== false && isStale(query, options);
  2271. }
  2272. return false;
  2273. }
  2274. function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
  2275. return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
  2276. }
  2277. function isStale(query, options) {
  2278. return query.isStaleByTime(options.staleTime);
  2279. } // this function would decide if we will update the observer's 'current'
  2280. // properties after an optimistic reading via getOptimisticResult
  2281. function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
  2282. // it is important to keep this condition like this for three reasons:
  2283. // 1. It will get removed in the v5
  2284. // 2. it reads: don't update the properties if we want to keep the previous
  2285. // data.
  2286. // 3. The opposite condition (!options.keepPreviousData) would fallthrough
  2287. // and will result in a bad decision
  2288. if (options.keepPreviousData) {
  2289. return false;
  2290. } // this means we want to put some placeholder data when pending and queryKey
  2291. // changed.
  2292. if (options.placeholderData !== undefined) {
  2293. // re-assign properties only if current data is placeholder data
  2294. // which means that data did not arrive yet, so, if there is some cached data
  2295. // we need to "prepare" to receive it
  2296. return optimisticResult.isPlaceholderData;
  2297. } // if the newly created result isn't what the observer is holding as current,
  2298. // then we'll need to update the properties as well
  2299. if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
  2300. return true;
  2301. } // basically, just keep previous properties if nothing changed
  2302. return false;
  2303. }
  2304. class QueriesObserver extends Subscribable {
  2305. constructor(client, queries) {
  2306. super();
  2307. this.client = client;
  2308. this.queries = [];
  2309. this.result = [];
  2310. this.observers = [];
  2311. this.observersMap = {};
  2312. if (queries) {
  2313. this.setQueries(queries);
  2314. }
  2315. }
  2316. onSubscribe() {
  2317. if (this.listeners.size === 1) {
  2318. this.observers.forEach(observer => {
  2319. observer.subscribe(result => {
  2320. this.onUpdate(observer, result);
  2321. });
  2322. });
  2323. }
  2324. }
  2325. onUnsubscribe() {
  2326. if (!this.listeners.size) {
  2327. this.destroy();
  2328. }
  2329. }
  2330. destroy() {
  2331. this.listeners = new Set();
  2332. this.observers.forEach(observer => {
  2333. observer.destroy();
  2334. });
  2335. }
  2336. setQueries(queries, notifyOptions) {
  2337. this.queries = queries;
  2338. notifyManager.batch(() => {
  2339. const prevObservers = this.observers;
  2340. const newObserverMatches = this.findMatchingObservers(this.queries); // set options for the new observers to notify of changes
  2341. newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
  2342. const newObservers = newObserverMatches.map(match => match.observer);
  2343. const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
  2344. const newResult = newObservers.map(observer => observer.getCurrentResult());
  2345. const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
  2346. if (prevObservers.length === newObservers.length && !hasIndexChange) {
  2347. return;
  2348. }
  2349. this.observers = newObservers;
  2350. this.observersMap = newObserversMap;
  2351. this.result = newResult;
  2352. if (!this.hasListeners()) {
  2353. return;
  2354. }
  2355. difference(prevObservers, newObservers).forEach(observer => {
  2356. observer.destroy();
  2357. });
  2358. difference(newObservers, prevObservers).forEach(observer => {
  2359. observer.subscribe(result => {
  2360. this.onUpdate(observer, result);
  2361. });
  2362. });
  2363. this.notify();
  2364. });
  2365. }
  2366. getCurrentResult() {
  2367. return this.result;
  2368. }
  2369. getQueries() {
  2370. return this.observers.map(observer => observer.getCurrentQuery());
  2371. }
  2372. getObservers() {
  2373. return this.observers;
  2374. }
  2375. getOptimisticResult(queries) {
  2376. return this.findMatchingObservers(queries).map(match => match.observer.getOptimisticResult(match.defaultedQueryOptions));
  2377. }
  2378. findMatchingObservers(queries) {
  2379. const prevObservers = this.observers;
  2380. const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
  2381. const defaultedQueryOptions = queries.map(options => this.client.defaultQueryOptions(options));
  2382. const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
  2383. const match = prevObserversMap.get(defaultedOptions.queryHash);
  2384. if (match != null) {
  2385. return [{
  2386. defaultedQueryOptions: defaultedOptions,
  2387. observer: match
  2388. }];
  2389. }
  2390. return [];
  2391. });
  2392. const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
  2393. const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
  2394. const matchingObserversSet = new Set(matchingObservers.map(match => match.observer));
  2395. const unmatchedObservers = prevObservers.filter(prevObserver => !matchingObserversSet.has(prevObserver));
  2396. const getObserver = options => {
  2397. const defaultedOptions = this.client.defaultQueryOptions(options);
  2398. const currentObserver = this.observersMap[defaultedOptions.queryHash];
  2399. return currentObserver != null ? currentObserver : new QueryObserver(this.client, defaultedOptions);
  2400. };
  2401. const newOrReusedObservers = unmatchedQueries.map((options, index) => {
  2402. if (options.keepPreviousData) {
  2403. // return previous data from one of the observers that no longer match
  2404. const previouslyUsedObserver = unmatchedObservers[index];
  2405. if (previouslyUsedObserver !== undefined) {
  2406. return {
  2407. defaultedQueryOptions: options,
  2408. observer: previouslyUsedObserver
  2409. };
  2410. }
  2411. }
  2412. return {
  2413. defaultedQueryOptions: options,
  2414. observer: getObserver(options)
  2415. };
  2416. });
  2417. const sortMatchesByOrderOfQueries = (a, b) => defaultedQueryOptions.indexOf(a.defaultedQueryOptions) - defaultedQueryOptions.indexOf(b.defaultedQueryOptions);
  2418. return matchingObservers.concat(newOrReusedObservers).sort(sortMatchesByOrderOfQueries);
  2419. }
  2420. onUpdate(observer, result) {
  2421. const index = this.observers.indexOf(observer);
  2422. if (index !== -1) {
  2423. this.result = replaceAt(this.result, index, result);
  2424. this.notify();
  2425. }
  2426. }
  2427. notify() {
  2428. notifyManager.batch(() => {
  2429. this.listeners.forEach(({
  2430. listener
  2431. }) => {
  2432. listener(this.result);
  2433. });
  2434. });
  2435. }
  2436. }
  2437. class InfiniteQueryObserver extends QueryObserver {
  2438. // Type override
  2439. // Type override
  2440. // Type override
  2441. // eslint-disable-next-line @typescript-eslint/no-useless-constructor
  2442. constructor(client, options) {
  2443. super(client, options);
  2444. }
  2445. bindMethods() {
  2446. super.bindMethods();
  2447. this.fetchNextPage = this.fetchNextPage.bind(this);
  2448. this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
  2449. }
  2450. setOptions(options, notifyOptions) {
  2451. super.setOptions({ ...options,
  2452. behavior: infiniteQueryBehavior()
  2453. }, notifyOptions);
  2454. }
  2455. getOptimisticResult(options) {
  2456. options.behavior = infiniteQueryBehavior();
  2457. return super.getOptimisticResult(options);
  2458. }
  2459. fetchNextPage({
  2460. pageParam,
  2461. ...options
  2462. } = {}) {
  2463. return this.fetch({ ...options,
  2464. meta: {
  2465. fetchMore: {
  2466. direction: 'forward',
  2467. pageParam
  2468. }
  2469. }
  2470. });
  2471. }
  2472. fetchPreviousPage({
  2473. pageParam,
  2474. ...options
  2475. } = {}) {
  2476. return this.fetch({ ...options,
  2477. meta: {
  2478. fetchMore: {
  2479. direction: 'backward',
  2480. pageParam
  2481. }
  2482. }
  2483. });
  2484. }
  2485. createResult(query, options) {
  2486. var _state$fetchMeta, _state$fetchMeta$fetc, _state$fetchMeta2, _state$fetchMeta2$fet, _state$data, _state$data2;
  2487. const {
  2488. state
  2489. } = query;
  2490. const result = super.createResult(query, options);
  2491. const {
  2492. isFetching,
  2493. isRefetching
  2494. } = result;
  2495. const isFetchingNextPage = isFetching && ((_state$fetchMeta = state.fetchMeta) == null ? void 0 : (_state$fetchMeta$fetc = _state$fetchMeta.fetchMore) == null ? void 0 : _state$fetchMeta$fetc.direction) === 'forward';
  2496. const isFetchingPreviousPage = isFetching && ((_state$fetchMeta2 = state.fetchMeta) == null ? void 0 : (_state$fetchMeta2$fet = _state$fetchMeta2.fetchMore) == null ? void 0 : _state$fetchMeta2$fet.direction) === 'backward';
  2497. return { ...result,
  2498. fetchNextPage: this.fetchNextPage,
  2499. fetchPreviousPage: this.fetchPreviousPage,
  2500. hasNextPage: hasNextPage(options, (_state$data = state.data) == null ? void 0 : _state$data.pages),
  2501. hasPreviousPage: hasPreviousPage(options, (_state$data2 = state.data) == null ? void 0 : _state$data2.pages),
  2502. isFetchingNextPage,
  2503. isFetchingPreviousPage,
  2504. isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
  2505. };
  2506. }
  2507. }
  2508. // CLASS
  2509. class MutationObserver extends Subscribable {
  2510. constructor(client, options) {
  2511. super();
  2512. this.client = client;
  2513. this.setOptions(options);
  2514. this.bindMethods();
  2515. this.updateResult();
  2516. }
  2517. bindMethods() {
  2518. this.mutate = this.mutate.bind(this);
  2519. this.reset = this.reset.bind(this);
  2520. }
  2521. setOptions(options) {
  2522. var _this$currentMutation;
  2523. const prevOptions = this.options;
  2524. this.options = this.client.defaultMutationOptions(options);
  2525. if (!shallowEqualObjects(prevOptions, this.options)) {
  2526. this.client.getMutationCache().notify({
  2527. type: 'observerOptionsUpdated',
  2528. mutation: this.currentMutation,
  2529. observer: this
  2530. });
  2531. }
  2532. (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
  2533. }
  2534. onUnsubscribe() {
  2535. if (!this.hasListeners()) {
  2536. var _this$currentMutation2;
  2537. (_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
  2538. }
  2539. }
  2540. onMutationUpdate(action) {
  2541. this.updateResult(); // Determine which callbacks to trigger
  2542. const notifyOptions = {
  2543. listeners: true
  2544. };
  2545. if (action.type === 'success') {
  2546. notifyOptions.onSuccess = true;
  2547. } else if (action.type === 'error') {
  2548. notifyOptions.onError = true;
  2549. }
  2550. this.notify(notifyOptions);
  2551. }
  2552. getCurrentResult() {
  2553. return this.currentResult;
  2554. }
  2555. reset() {
  2556. this.currentMutation = undefined;
  2557. this.updateResult();
  2558. this.notify({
  2559. listeners: true
  2560. });
  2561. }
  2562. mutate(variables, options) {
  2563. this.mutateOptions = options;
  2564. if (this.currentMutation) {
  2565. this.currentMutation.removeObserver(this);
  2566. }
  2567. this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
  2568. variables: typeof variables !== 'undefined' ? variables : this.options.variables
  2569. });
  2570. this.currentMutation.addObserver(this);
  2571. return this.currentMutation.execute();
  2572. }
  2573. updateResult() {
  2574. const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
  2575. const result = { ...state,
  2576. isLoading: state.status === 'loading',
  2577. isSuccess: state.status === 'success',
  2578. isError: state.status === 'error',
  2579. isIdle: state.status === 'idle',
  2580. mutate: this.mutate,
  2581. reset: this.reset
  2582. };
  2583. this.currentResult = result;
  2584. }
  2585. notify(options) {
  2586. notifyManager.batch(() => {
  2587. // First trigger the mutate callbacks
  2588. if (this.mutateOptions && this.hasListeners()) {
  2589. if (options.onSuccess) {
  2590. var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
  2591. (_this$mutateOptions$o = (_this$mutateOptions = this.mutateOptions).onSuccess) == null ? void 0 : _this$mutateOptions$o.call(_this$mutateOptions, this.currentResult.data, this.currentResult.variables, this.currentResult.context);
  2592. (_this$mutateOptions$o2 = (_this$mutateOptions2 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o2.call(_this$mutateOptions2, this.currentResult.data, null, this.currentResult.variables, this.currentResult.context);
  2593. } else if (options.onError) {
  2594. var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
  2595. (_this$mutateOptions$o3 = (_this$mutateOptions3 = this.mutateOptions).onError) == null ? void 0 : _this$mutateOptions$o3.call(_this$mutateOptions3, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
  2596. (_this$mutateOptions$o4 = (_this$mutateOptions4 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o4.call(_this$mutateOptions4, undefined, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
  2597. }
  2598. } // Then trigger the listeners
  2599. if (options.listeners) {
  2600. this.listeners.forEach(({
  2601. listener
  2602. }) => {
  2603. listener(this.currentResult);
  2604. });
  2605. }
  2606. });
  2607. }
  2608. }
  2609. // TYPES
  2610. // FUNCTIONS
  2611. function dehydrateMutation(mutation) {
  2612. return {
  2613. mutationKey: mutation.options.mutationKey,
  2614. state: mutation.state
  2615. };
  2616. } // Most config is not dehydrated but instead meant to configure again when
  2617. // consuming the de/rehydrated data, typically with useQuery on the client.
  2618. // Sometimes it might make sense to prefetch data on the server and include
  2619. // in the html-payload, but not consume it on the initial render.
  2620. function dehydrateQuery(query) {
  2621. return {
  2622. state: query.state,
  2623. queryKey: query.queryKey,
  2624. queryHash: query.queryHash
  2625. };
  2626. }
  2627. function defaultShouldDehydrateMutation(mutation) {
  2628. return mutation.state.isPaused;
  2629. }
  2630. function defaultShouldDehydrateQuery(query) {
  2631. return query.state.status === 'success';
  2632. }
  2633. function dehydrate(client, options = {}) {
  2634. const mutations = [];
  2635. const queries = [];
  2636. if (options.dehydrateMutations !== false) {
  2637. const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
  2638. client.getMutationCache().getAll().forEach(mutation => {
  2639. if (shouldDehydrateMutation(mutation)) {
  2640. mutations.push(dehydrateMutation(mutation));
  2641. }
  2642. });
  2643. }
  2644. if (options.dehydrateQueries !== false) {
  2645. const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
  2646. client.getQueryCache().getAll().forEach(query => {
  2647. if (shouldDehydrateQuery(query)) {
  2648. queries.push(dehydrateQuery(query));
  2649. }
  2650. });
  2651. }
  2652. return {
  2653. mutations,
  2654. queries
  2655. };
  2656. }
  2657. function hydrate(client, dehydratedState, options) {
  2658. if (typeof dehydratedState !== 'object' || dehydratedState === null) {
  2659. return;
  2660. }
  2661. const mutationCache = client.getMutationCache();
  2662. const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  2663. const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  2664. const queries = dehydratedState.queries || [];
  2665. mutations.forEach(dehydratedMutation => {
  2666. var _options$defaultOptio;
  2667. mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
  2668. mutationKey: dehydratedMutation.mutationKey
  2669. }, dehydratedMutation.state);
  2670. });
  2671. queries.forEach(({
  2672. queryKey,
  2673. state,
  2674. queryHash
  2675. }) => {
  2676. var _options$defaultOptio2;
  2677. const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
  2678. if (query) {
  2679. if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
  2680. // omit fetchStatus from dehydrated state
  2681. // so that query stays in its current fetchStatus
  2682. const {
  2683. fetchStatus: _ignored,
  2684. ...dehydratedQueryState
  2685. } = state;
  2686. query.setState(dehydratedQueryState);
  2687. }
  2688. return;
  2689. } // Restore query
  2690. queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
  2691. queryKey,
  2692. queryHash
  2693. }, // Reset fetch status to idle to avoid
  2694. // query being stuck in fetching state upon hydration
  2695. { ...state,
  2696. fetchStatus: 'idle'
  2697. });
  2698. });
  2699. }
  2700. exports.CancelledError = CancelledError;
  2701. exports.InfiniteQueryObserver = InfiniteQueryObserver;
  2702. exports.MutationCache = MutationCache;
  2703. exports.MutationObserver = MutationObserver;
  2704. exports.QueriesObserver = QueriesObserver;
  2705. exports.Query = Query;
  2706. exports.QueryCache = QueryCache;
  2707. exports.QueryClient = QueryClient;
  2708. exports.QueryObserver = QueryObserver;
  2709. exports.defaultShouldDehydrateMutation = defaultShouldDehydrateMutation;
  2710. exports.defaultShouldDehydrateQuery = defaultShouldDehydrateQuery;
  2711. exports.dehydrate = dehydrate;
  2712. exports.focusManager = focusManager;
  2713. exports.hashQueryKey = hashQueryKey;
  2714. exports.hydrate = hydrate;
  2715. exports.isCancelledError = isCancelledError;
  2716. exports.isError = isError;
  2717. exports.isServer = isServer;
  2718. exports.matchQuery = matchQuery;
  2719. exports.notifyManager = notifyManager;
  2720. exports.onlineManager = onlineManager;
  2721. exports.parseFilterArgs = parseFilterArgs;
  2722. exports.parseMutationArgs = parseMutationArgs;
  2723. exports.parseMutationFilterArgs = parseMutationFilterArgs;
  2724. exports.parseQueryArgs = parseQueryArgs;
  2725. exports.replaceEqualDeep = replaceEqualDeep;
  2726. Object.defineProperty(exports, '__esModule', { value: true });
  2727. }));
  2728. //# sourceMappingURL=index.development.js.map