@tanstack/query-core 5.102.7 → 5.103.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/build/legacy/environmentManager.cjs +15 -1
  2. package/build/legacy/environmentManager.cjs.map +1 -1
  3. package/build/legacy/environmentManager.d.cts +15 -1
  4. package/build/legacy/environmentManager.d.cts.map +1 -1
  5. package/build/legacy/environmentManager.d.ts +15 -1
  6. package/build/legacy/environmentManager.d.ts.map +1 -1
  7. package/build/legacy/environmentManager.js +15 -1
  8. package/build/legacy/environmentManager.js.map +1 -1
  9. package/build/legacy/focusManager.cjs +57 -0
  10. package/build/legacy/focusManager.cjs.map +1 -1
  11. package/build/legacy/focusManager.d.cts +57 -0
  12. package/build/legacy/focusManager.d.cts.map +1 -1
  13. package/build/legacy/focusManager.d.ts +57 -0
  14. package/build/legacy/focusManager.d.ts.map +1 -1
  15. package/build/legacy/focusManager.js +57 -0
  16. package/build/legacy/focusManager.js.map +1 -1
  17. package/build/legacy/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
  18. package/build/legacy/hydration-Cq7QYAzB.d.ts.map +1 -0
  19. package/build/legacy/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
  20. package/build/legacy/hydration-DwR10Hi-.d.cts.map +1 -0
  21. package/build/legacy/hydration.cjs +57 -4
  22. package/build/legacy/hydration.cjs.map +1 -1
  23. package/build/legacy/hydration.d.cts +1 -1
  24. package/build/legacy/hydration.d.ts +1 -1
  25. package/build/legacy/hydration.js +57 -4
  26. package/build/legacy/hydration.js.map +1 -1
  27. package/build/legacy/index.cjs.map +1 -1
  28. package/build/legacy/index.d.cts +5 -5
  29. package/build/legacy/index.d.ts +5 -5
  30. package/build/legacy/index.js.map +1 -1
  31. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  32. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  33. package/build/legacy/infiniteQueryObserver.cjs +67 -0
  34. package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
  35. package/build/legacy/infiniteQueryObserver.d.cts +68 -1
  36. package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
  37. package/build/legacy/infiniteQueryObserver.d.ts +68 -1
  38. package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
  39. package/build/legacy/infiniteQueryObserver.js +67 -0
  40. package/build/legacy/infiniteQueryObserver.js.map +1 -1
  41. package/build/legacy/mutation.cjs +74 -0
  42. package/build/legacy/mutation.cjs.map +1 -1
  43. package/build/legacy/mutation.d.cts +1 -1
  44. package/build/legacy/mutation.d.ts +1 -1
  45. package/build/legacy/mutation.js +74 -0
  46. package/build/legacy/mutation.js.map +1 -1
  47. package/build/legacy/mutationCache.cjs +74 -0
  48. package/build/legacy/mutationCache.cjs.map +1 -1
  49. package/build/legacy/mutationCache.d.cts +1 -1
  50. package/build/legacy/mutationCache.d.ts +1 -1
  51. package/build/legacy/mutationCache.js +74 -0
  52. package/build/legacy/mutationCache.js.map +1 -1
  53. package/build/legacy/mutationObserver.cjs +71 -0
  54. package/build/legacy/mutationObserver.cjs.map +1 -1
  55. package/build/legacy/mutationObserver.d.cts +1 -1
  56. package/build/legacy/mutationObserver.d.ts +1 -1
  57. package/build/legacy/mutationObserver.js +71 -0
  58. package/build/legacy/mutationObserver.js.map +1 -1
  59. package/build/legacy/notifyManager.cjs +45 -1
  60. package/build/legacy/notifyManager.cjs.map +1 -1
  61. package/build/legacy/notifyManager.d.cts +83 -2
  62. package/build/legacy/notifyManager.d.cts.map +1 -1
  63. package/build/legacy/notifyManager.d.ts +83 -2
  64. package/build/legacy/notifyManager.d.ts.map +1 -1
  65. package/build/legacy/notifyManager.js +45 -1
  66. package/build/legacy/notifyManager.js.map +1 -1
  67. package/build/legacy/onlineManager.cjs +47 -0
  68. package/build/legacy/onlineManager.cjs.map +1 -1
  69. package/build/legacy/onlineManager.d.cts +47 -0
  70. package/build/legacy/onlineManager.d.cts.map +1 -1
  71. package/build/legacy/onlineManager.d.ts +47 -0
  72. package/build/legacy/onlineManager.d.ts.map +1 -1
  73. package/build/legacy/onlineManager.js +47 -0
  74. package/build/legacy/onlineManager.js.map +1 -1
  75. package/build/legacy/queriesObserver.cjs +64 -0
  76. package/build/legacy/queriesObserver.cjs.map +1 -1
  77. package/build/legacy/queriesObserver.d.cts +73 -1
  78. package/build/legacy/queriesObserver.d.cts.map +1 -1
  79. package/build/legacy/queriesObserver.d.ts +73 -1
  80. package/build/legacy/queriesObserver.d.ts.map +1 -1
  81. package/build/legacy/queriesObserver.js +64 -0
  82. package/build/legacy/queriesObserver.js.map +1 -1
  83. package/build/legacy/query.cjs +141 -0
  84. package/build/legacy/query.cjs.map +1 -1
  85. package/build/legacy/query.d.cts +1 -1
  86. package/build/legacy/query.d.ts +1 -1
  87. package/build/legacy/query.js +141 -0
  88. package/build/legacy/query.js.map +1 -1
  89. package/build/legacy/queryCache.cjs +120 -0
  90. package/build/legacy/queryCache.cjs.map +1 -1
  91. package/build/legacy/queryCache.d.cts +1 -1
  92. package/build/legacy/queryCache.d.ts +1 -1
  93. package/build/legacy/queryCache.js +120 -0
  94. package/build/legacy/queryCache.js.map +1 -1
  95. package/build/legacy/queryClient.cjs +363 -0
  96. package/build/legacy/queryClient.cjs.map +1 -1
  97. package/build/legacy/queryClient.d.cts +1 -1
  98. package/build/legacy/queryClient.d.ts +1 -1
  99. package/build/legacy/queryClient.js +363 -0
  100. package/build/legacy/queryClient.js.map +1 -1
  101. package/build/legacy/queryObserver.cjs +116 -2
  102. package/build/legacy/queryObserver.cjs.map +1 -1
  103. package/build/legacy/queryObserver.d.cts +1 -1
  104. package/build/legacy/queryObserver.d.ts +1 -1
  105. package/build/legacy/queryObserver.js +116 -2
  106. package/build/legacy/queryObserver.js.map +1 -1
  107. package/build/legacy/retryer.cjs +18 -0
  108. package/build/legacy/retryer.cjs.map +1 -1
  109. package/build/legacy/retryer.d.cts +1 -1
  110. package/build/legacy/retryer.d.ts +1 -1
  111. package/build/legacy/retryer.js +18 -0
  112. package/build/legacy/retryer.js.map +1 -1
  113. package/build/legacy/streamedQuery.cjs +10 -1
  114. package/build/legacy/streamedQuery.cjs.map +1 -1
  115. package/build/legacy/streamedQuery.d.cts +26 -2
  116. package/build/legacy/streamedQuery.d.cts.map +1 -1
  117. package/build/legacy/streamedQuery.d.ts +26 -2
  118. package/build/legacy/streamedQuery.d.ts.map +1 -1
  119. package/build/legacy/streamedQuery.js +10 -1
  120. package/build/legacy/streamedQuery.js.map +1 -1
  121. package/build/legacy/timeoutManager.cjs +99 -0
  122. package/build/legacy/timeoutManager.cjs.map +1 -1
  123. package/build/legacy/timeoutManager.d.cts +105 -0
  124. package/build/legacy/timeoutManager.d.cts.map +1 -1
  125. package/build/legacy/timeoutManager.d.ts +105 -0
  126. package/build/legacy/timeoutManager.d.ts.map +1 -1
  127. package/build/legacy/timeoutManager.js +99 -0
  128. package/build/legacy/timeoutManager.js.map +1 -1
  129. package/build/legacy/types.cjs.map +1 -1
  130. package/build/legacy/types.d.cts +1 -1
  131. package/build/legacy/types.d.ts +1 -1
  132. package/build/legacy/types.js.map +1 -1
  133. package/build/legacy/utils.cjs +76 -0
  134. package/build/legacy/utils.cjs.map +1 -1
  135. package/build/legacy/utils.d.cts +1 -1
  136. package/build/legacy/utils.d.ts +1 -1
  137. package/build/legacy/utils.js +76 -0
  138. package/build/legacy/utils.js.map +1 -1
  139. package/build/modern/environmentManager.cjs +15 -1
  140. package/build/modern/environmentManager.cjs.map +1 -1
  141. package/build/modern/environmentManager.d.cts +15 -1
  142. package/build/modern/environmentManager.d.cts.map +1 -1
  143. package/build/modern/environmentManager.d.ts +15 -1
  144. package/build/modern/environmentManager.d.ts.map +1 -1
  145. package/build/modern/environmentManager.js +15 -1
  146. package/build/modern/environmentManager.js.map +1 -1
  147. package/build/modern/focusManager.cjs +57 -0
  148. package/build/modern/focusManager.cjs.map +1 -1
  149. package/build/modern/focusManager.d.cts +57 -0
  150. package/build/modern/focusManager.d.cts.map +1 -1
  151. package/build/modern/focusManager.d.ts +57 -0
  152. package/build/modern/focusManager.d.ts.map +1 -1
  153. package/build/modern/focusManager.js +57 -0
  154. package/build/modern/focusManager.js.map +1 -1
  155. package/build/modern/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
  156. package/build/modern/hydration-Cq7QYAzB.d.ts.map +1 -0
  157. package/build/modern/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
  158. package/build/modern/hydration-DwR10Hi-.d.cts.map +1 -0
  159. package/build/modern/hydration.cjs +57 -4
  160. package/build/modern/hydration.cjs.map +1 -1
  161. package/build/modern/hydration.d.cts +1 -1
  162. package/build/modern/hydration.d.ts +1 -1
  163. package/build/modern/hydration.js +57 -4
  164. package/build/modern/hydration.js.map +1 -1
  165. package/build/modern/index.cjs.map +1 -1
  166. package/build/modern/index.d.cts +5 -5
  167. package/build/modern/index.d.ts +5 -5
  168. package/build/modern/index.js.map +1 -1
  169. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  170. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  171. package/build/modern/infiniteQueryObserver.cjs +67 -0
  172. package/build/modern/infiniteQueryObserver.cjs.map +1 -1
  173. package/build/modern/infiniteQueryObserver.d.cts +68 -1
  174. package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
  175. package/build/modern/infiniteQueryObserver.d.ts +68 -1
  176. package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
  177. package/build/modern/infiniteQueryObserver.js +67 -0
  178. package/build/modern/infiniteQueryObserver.js.map +1 -1
  179. package/build/modern/mutation.cjs +74 -0
  180. package/build/modern/mutation.cjs.map +1 -1
  181. package/build/modern/mutation.d.cts +1 -1
  182. package/build/modern/mutation.d.ts +1 -1
  183. package/build/modern/mutation.js +74 -0
  184. package/build/modern/mutation.js.map +1 -1
  185. package/build/modern/mutationCache.cjs +74 -0
  186. package/build/modern/mutationCache.cjs.map +1 -1
  187. package/build/modern/mutationCache.d.cts +1 -1
  188. package/build/modern/mutationCache.d.ts +1 -1
  189. package/build/modern/mutationCache.js +74 -0
  190. package/build/modern/mutationCache.js.map +1 -1
  191. package/build/modern/mutationObserver.cjs +71 -0
  192. package/build/modern/mutationObserver.cjs.map +1 -1
  193. package/build/modern/mutationObserver.d.cts +1 -1
  194. package/build/modern/mutationObserver.d.ts +1 -1
  195. package/build/modern/mutationObserver.js +71 -0
  196. package/build/modern/mutationObserver.js.map +1 -1
  197. package/build/modern/notifyManager.cjs +45 -1
  198. package/build/modern/notifyManager.cjs.map +1 -1
  199. package/build/modern/notifyManager.d.cts +83 -2
  200. package/build/modern/notifyManager.d.cts.map +1 -1
  201. package/build/modern/notifyManager.d.ts +83 -2
  202. package/build/modern/notifyManager.d.ts.map +1 -1
  203. package/build/modern/notifyManager.js +45 -1
  204. package/build/modern/notifyManager.js.map +1 -1
  205. package/build/modern/onlineManager.cjs +47 -0
  206. package/build/modern/onlineManager.cjs.map +1 -1
  207. package/build/modern/onlineManager.d.cts +47 -0
  208. package/build/modern/onlineManager.d.cts.map +1 -1
  209. package/build/modern/onlineManager.d.ts +47 -0
  210. package/build/modern/onlineManager.d.ts.map +1 -1
  211. package/build/modern/onlineManager.js +47 -0
  212. package/build/modern/onlineManager.js.map +1 -1
  213. package/build/modern/queriesObserver.cjs +64 -0
  214. package/build/modern/queriesObserver.cjs.map +1 -1
  215. package/build/modern/queriesObserver.d.cts +73 -1
  216. package/build/modern/queriesObserver.d.cts.map +1 -1
  217. package/build/modern/queriesObserver.d.ts +73 -1
  218. package/build/modern/queriesObserver.d.ts.map +1 -1
  219. package/build/modern/queriesObserver.js +64 -0
  220. package/build/modern/queriesObserver.js.map +1 -1
  221. package/build/modern/query.cjs +141 -0
  222. package/build/modern/query.cjs.map +1 -1
  223. package/build/modern/query.d.cts +1 -1
  224. package/build/modern/query.d.ts +1 -1
  225. package/build/modern/query.js +141 -0
  226. package/build/modern/query.js.map +1 -1
  227. package/build/modern/queryCache.cjs +120 -0
  228. package/build/modern/queryCache.cjs.map +1 -1
  229. package/build/modern/queryCache.d.cts +1 -1
  230. package/build/modern/queryCache.d.ts +1 -1
  231. package/build/modern/queryCache.js +120 -0
  232. package/build/modern/queryCache.js.map +1 -1
  233. package/build/modern/queryClient.cjs +363 -0
  234. package/build/modern/queryClient.cjs.map +1 -1
  235. package/build/modern/queryClient.d.cts +1 -1
  236. package/build/modern/queryClient.d.ts +1 -1
  237. package/build/modern/queryClient.js +363 -0
  238. package/build/modern/queryClient.js.map +1 -1
  239. package/build/modern/queryObserver.cjs +116 -2
  240. package/build/modern/queryObserver.cjs.map +1 -1
  241. package/build/modern/queryObserver.d.cts +1 -1
  242. package/build/modern/queryObserver.d.ts +1 -1
  243. package/build/modern/queryObserver.js +116 -2
  244. package/build/modern/queryObserver.js.map +1 -1
  245. package/build/modern/retryer.cjs +18 -0
  246. package/build/modern/retryer.cjs.map +1 -1
  247. package/build/modern/retryer.d.cts +1 -1
  248. package/build/modern/retryer.d.ts +1 -1
  249. package/build/modern/retryer.js +18 -0
  250. package/build/modern/retryer.js.map +1 -1
  251. package/build/modern/streamedQuery.cjs +10 -1
  252. package/build/modern/streamedQuery.cjs.map +1 -1
  253. package/build/modern/streamedQuery.d.cts +26 -2
  254. package/build/modern/streamedQuery.d.cts.map +1 -1
  255. package/build/modern/streamedQuery.d.ts +26 -2
  256. package/build/modern/streamedQuery.d.ts.map +1 -1
  257. package/build/modern/streamedQuery.js +10 -1
  258. package/build/modern/streamedQuery.js.map +1 -1
  259. package/build/modern/timeoutManager.cjs +99 -0
  260. package/build/modern/timeoutManager.cjs.map +1 -1
  261. package/build/modern/timeoutManager.d.cts +105 -0
  262. package/build/modern/timeoutManager.d.cts.map +1 -1
  263. package/build/modern/timeoutManager.d.ts +105 -0
  264. package/build/modern/timeoutManager.d.ts.map +1 -1
  265. package/build/modern/timeoutManager.js +99 -0
  266. package/build/modern/timeoutManager.js.map +1 -1
  267. package/build/modern/types.cjs.map +1 -1
  268. package/build/modern/types.d.cts +1 -1
  269. package/build/modern/types.d.ts +1 -1
  270. package/build/modern/types.js.map +1 -1
  271. package/build/modern/utils.cjs +76 -0
  272. package/build/modern/utils.cjs.map +1 -1
  273. package/build/modern/utils.d.cts +1 -1
  274. package/build/modern/utils.d.ts +1 -1
  275. package/build/modern/utils.js +76 -0
  276. package/build/modern/utils.js.map +1 -1
  277. package/package.json +9 -9
  278. package/src/environmentManager.ts +15 -1
  279. package/src/focusManager.ts +57 -0
  280. package/src/hydration.ts +82 -16
  281. package/src/index.ts +3 -0
  282. package/src/infiniteQueryObserver.ts +67 -0
  283. package/src/mutation.ts +108 -0
  284. package/src/mutationCache.ts +91 -0
  285. package/src/mutationObserver.ts +71 -0
  286. package/src/notifyManager.ts +46 -1
  287. package/src/onlineManager.ts +47 -0
  288. package/src/queriesObserver.ts +72 -0
  289. package/src/query.ts +193 -0
  290. package/src/queryCache.ts +134 -0
  291. package/src/queryClient.ts +369 -0
  292. package/src/queryObserver.ts +117 -4
  293. package/src/retryer.ts +18 -0
  294. package/src/streamedQuery.ts +25 -1
  295. package/src/timeoutManager.ts +105 -0
  296. package/src/types.ts +147 -5
  297. package/src/utils.ts +107 -0
  298. package/build/legacy/hydration-Bjs0MSgg.d.ts.map +0 -1
  299. package/build/legacy/hydration-DWGr3cFC.d.cts.map +0 -1
  300. package/build/modern/hydration-Bjs0MSgg.d.ts.map +0 -1
  301. package/build/modern/hydration-DWGr3cFC.d.cts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"queryCache.js","names":["#queries"],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\nexport interface QueryCacheConfig {\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\nexport interface QueryStore {\n has: (queryHash: string) => boolean\n set: (queryHash: string, query: Query) => void\n get: (queryHash: string) => Query | undefined\n delete: (queryHash: string) => void\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap) {\n query.destroy()\n\n if (queryInMap === query) {\n this.#queries.delete(query.queryHash)\n }\n\n this.notify({ type: 'removed', query })\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n | Query<TQueryFnData, TError, TData, TQueryKey>\n | undefined\n }\n\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;AA2FA,IAAa,aAAb,cAAgC,aAAiC;CAC/D;CAEA,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;EAEjB,KAAKA,2BAAW,IAAI,IAAmB;CACzC;CAEA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAa,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAI,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;CAEA,IAAI,OAAwC;EAC1C,IAAI,CAAC,KAAKA,SAAS,IAAI,MAAM,SAAS,GAAG;GACvC,KAAKA,SAAS,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;CAEA,OAAO,OAAwC;EAC7C,MAAM,aAAa,KAAKA,SAAS,IAAI,MAAM,SAAS;EAEpD,IAAI,YAAY;GACd,MAAM,QAAQ;GAEd,IAAI,eAAe,OACjB,KAAKA,SAAS,OAAO,MAAM,SAAS;GAGtC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;CAEA,QAAc;EACZ,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;CAEA,IAME,WAC2D;EAC3D,OAAO,KAAKA,SAAS,IAAI,SAAS;CAGpC;CAEA,SAAuB;EACrB,OAAO,CAAC,GAAG,KAAKA,SAAS,OAAO,CAAC;CACnC;CAEA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzB,WAAW,kBAAkB,KAAK,CACpC;CACF;CAEA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAU,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;CAEA,OAAO,OAAoC;EACzC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;CAEA,UAAgB;EACd,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;CAEA,WAAiB;EACf,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"queryCache.js","names":["#queries"],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every query handled by a `QueryCache`, regardless of which\n * component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can\n * override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these\n * are fire-and-forget: their return value is not awaited before the query settles.\n */\nexport interface QueryCacheConfig {\n /** Called when any query in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n /** Called when any query in the cache is successful. */\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n /** Called when any query in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\n/**\n * The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from\n * the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or\n * one of its observers is added, removed, or has its results or options updated.\n */\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\nexport interface QueryStore {\n has: (queryHash: string) => boolean\n set: (queryHash: string, query: Query) => void\n get: (queryHash: string) => Query | undefined\n delete: (queryHash: string) => void\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\n/**\n * The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta\n * information, and state of the queries it contains.\n *\n * Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`\n * for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as queries being added, removed, or updated — updates made\n * outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do\n * not notify subscribers.\n *\n * @example\n * ```ts\n * const unsubscribe = queryCache.subscribe((event) => {\n * console.log(event.type, event.query)\n * })\n * ```\n */\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n /**\n * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or\n * builds and adds a new one to the cache if none exists yet. Used by framework adapters and\n * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing\n * the reactive `QueryObserver` machinery.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.build(queryClient, {\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n * ```\n */\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n /** @internal */\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n /**\n * Destroys the given `Query` and removes it from the cache, notifying subscribers with a\n * `'removed'` event. A no-op if the query is no longer the one currently stored under its hash\n * (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror\n * removals across `QueryCache` instances.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const query = queryCache.find({ queryKey: ['posts'] })\n *\n * if (query) {\n * queryCache.remove(query)\n * }\n * ```\n */\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap) {\n query.destroy()\n\n if (queryInMap === query) {\n this.#queries.delete(query.queryHash)\n }\n\n this.notify({ type: 'removed', query })\n }\n }\n\n /**\n * Removes all queries from the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * queryCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n /**\n * Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none\n * exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather\n * than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash\n * to look up directly.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const queryHash = hashKey(['posts'])\n *\n * const query = queryCache.get(queryHash)\n * ```\n */\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n | Query<TQueryFnData, TError, TData, TQueryKey>\n | undefined\n }\n\n /**\n * Returns all queries within the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.getAll()\n * ```\n */\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing query instance from the\n * cache. This instance not only contains all the state for the query, but all of the instances,\n * and underlying guts of the query as well. If the query does not exist, `undefined` is\n * returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to\n * decide whether a query is fresh enough to be used as an initial value).\n *\n * @see {@link QueryCache#findAll}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.find({ queryKey: ['posts'] })\n * ```\n */\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing query instances from the cache\n * that partially match a query key. If no queries match, an empty array is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about queries in rare scenarios.\n *\n * @see {@link QueryCache#find}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.findAll({ queryKey: ['posts'] })\n * ```\n */\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n /** @internal */\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /** @internal */\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n /** @internal */\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA0HA,IAAa,aAAb,cAAgC,aAAiC;CAC/D;CAEA,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;EAEjB,KAAKA,2BAAW,IAAI,IAAmB;CACzC;;;;;;;;;;;;;;;;;CAkBA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAa,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAI,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;;CAGA,IAAI,OAAwC;EAC1C,IAAI,CAAC,KAAKA,SAAS,IAAI,MAAM,SAAS,GAAG;GACvC,KAAKA,SAAS,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;;;;;;;;;;;;;;;;;CAkBA,OAAO,OAAwC;EAC7C,MAAM,aAAa,KAAKA,SAAS,IAAI,MAAM,SAAS;EAEpD,IAAI,YAAY;GACd,MAAM,QAAQ;GAEd,IAAI,eAAe,OACjB,KAAKA,SAAS,OAAO,MAAM,SAAS;GAGtC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;;;;;;;;;;;CAYA,QAAc;EACZ,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;;;;;;;;;;;;;;;CAgBA,IAME,WAC2D;EAC3D,OAAO,KAAKA,SAAS,IAAI,SAAS;CAGpC;;;;;;;;;;;CAYA,SAAuB;EACrB,OAAO,CAAC,GAAG,KAAKA,SAAS,OAAO,CAAC;CACnC;;;;;;;;;;;;;;;;;;;CAoBA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzB,WAAW,kBAAkB,KAAK,CACpC;CACF;;;;;;;;;;;;;;;;CAiBA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAU,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;;CAGA,OAAO,OAAoC;EACzC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,UAAgB;EACd,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,WAAiB;EACf,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
@@ -6,6 +6,24 @@ const require_onlineManager = require("./onlineManager.cjs");
6
6
  const require_mutationCache = require("./mutationCache.cjs");
7
7
  const require_queryCache = require("./queryCache.cjs");
8
8
  //#region src/queryClient.ts
9
+ /**
10
+ * `QueryClient` is used to interact with a cache of queries and mutations. It owns a
11
+ * `QueryCache` and a `MutationCache` (creating default ones if none are passed in) and holds
12
+ * the default options that are applied to queries and mutations created through it.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const queryClient = new QueryClient({
17
+ * defaultOptions: {
18
+ * queries: {
19
+ * staleTime: Infinity,
20
+ * },
21
+ * },
22
+ * })
23
+ *
24
+ * await queryClient.query({ queryKey: ['posts'], queryFn: fetchPosts })
25
+ * ```
26
+ */
9
27
  var QueryClient = class {
10
28
  #queryCache;
11
29
  #mutationCache;
@@ -23,6 +41,12 @@ var QueryClient = class {
23
41
  this.#mutationDefaults = /* @__PURE__ */ new Map();
24
42
  this.#mountCount = 0;
25
43
  }
44
+ /**
45
+ * Called by a framework adapter's `QueryClientProvider`-equivalent when it mounts, to start
46
+ * listening for focus/online events and resume paused mutations. Ref-counted via an internal
47
+ * mount count, so nested or multiple providers sharing the same `QueryClient` don't tear down
48
+ * the shared listeners until the last one unmounts.
49
+ */
26
50
  mount() {
27
51
  this.#mountCount++;
28
52
  if (this.#mountCount !== 1) return;
@@ -39,6 +63,11 @@ var QueryClient = class {
39
63
  }
40
64
  });
41
65
  }
66
+ /**
67
+ * The inverse of {@link QueryClient#mount} — called by a framework adapter's
68
+ * `QueryClientProvider`-equivalent when it unmounts. Only tears down the focus/online
69
+ * listeners once the mount count returns to `0`.
70
+ */
42
71
  unmount() {
43
72
  this.#mountCount--;
44
73
  if (this.#mountCount !== 0) return;
@@ -47,12 +76,35 @@ var QueryClient = class {
47
76
  this.#unsubscribeOnline?.();
48
77
  this.#unsubscribeOnline = void 0;
49
78
  }
79
+ /**
80
+ * Returns the number of queries in the cache that are currently fetching, optionally
81
+ * matching a set of filters. This includes background-fetching, loading new pages, and
82
+ * loading more infinite query results.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * if (queryClient.isFetching()) {
87
+ * console.log('At least one query is fetching!')
88
+ * }
89
+ * ```
90
+ */
50
91
  isFetching(filters) {
51
92
  return this.#queryCache.findAll({
52
93
  ...filters,
53
94
  fetchStatus: "fetching"
54
95
  }).length;
55
96
  }
97
+ /**
98
+ * Returns the number of mutations in the cache that are currently pending, optionally
99
+ * matching a set of filters.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * if (queryClient.isMutating()) {
104
+ * console.log('At least one mutation is pending!')
105
+ * }
106
+ * ```
107
+ */
56
108
  isMutating(filters) {
57
109
  return this.#mutationCache.findAll({
58
110
  ...filters,
@@ -65,6 +117,8 @@ var QueryClient = class {
65
117
  *
66
118
  * Hint: Do not use this function inside a component, because it won't receive updates.
67
119
  * Use `useQuery` to create a `QueryObserver` that subscribes to changes.
120
+ *
121
+ * @see {@link QueryClient#getQueriesData}
68
122
  */
69
123
  getQueryData(queryKey) {
70
124
  const options = this.defaultQueryOptions({ queryKey });
@@ -81,11 +135,49 @@ var QueryClient = class {
81
135
  if (options.revalidateIfStale && query.isStaleByTime(require_utils.resolveQueryValue(defaultedOptions.staleTime, query))) this.prefetchQuery(defaultedOptions);
82
136
  return Promise.resolve(cachedData);
83
137
  }
138
+ /**
139
+ * Imperative (non-reactive) way to retrieve the cached data of multiple queries at once.
140
+ * Only queries matching the given filters are returned; if none match, an empty array is
141
+ * returned.
142
+ *
143
+ * Because the matched queries can hold data of different shapes (e.g. a broad filter can match
144
+ * queries with unrelated data types), the `TQueryFnData` generic defaults to `unknown` rather
145
+ * than being inferred. Passing a more specific type is a convenience for call sites that know
146
+ * every matched query holds the same shape — it is not checked against the actual cache
147
+ * contents.
148
+ *
149
+ * @see {@link QueryClient#getQueryData}
150
+ * @example
151
+ * ```ts
152
+ * const data = queryClient.getQueriesData({ queryKey: ['posts'] })
153
+ * ```
154
+ */
84
155
  getQueriesData(filters) {
85
156
  return this.#queryCache.findAll(filters).map(({ queryKey, state }) => {
86
157
  return [queryKey, state.data];
87
158
  });
88
159
  }
160
+ /**
161
+ * Synchronous way to immediately update a query's cached data. If the updater (or the value
162
+ * passed) resolves to `undefined`, the cache is left untouched and no query is created;
163
+ * otherwise, if the query does not exist yet, it will be created. To update multiple queries
164
+ * at once by partially matching query keys, use {@link QueryClient#setQueriesData} instead.
165
+ *
166
+ * Updates must be performed immutably: do not mutate `oldData`, or data previously retrieved
167
+ * via {@link QueryClient#getQueryData}, in place.
168
+ *
169
+ * @param queryKey - The query key to set data for.
170
+ * @param updater - Either the new data, or a function that receives the current data (which
171
+ * may be `undefined`) and returns the new data.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * queryClient.setQueryData(['posts'], newPosts)
176
+ *
177
+ * // Or, using an updater function that receives the current data:
178
+ * queryClient.setQueryData(['posts'], (oldPosts) => [...oldPosts, newPost])
179
+ * ```
180
+ */
89
181
  setQueryData(queryKey, updater, options) {
90
182
  const defaultedOptions = this.defaultQueryOptions({ queryKey });
91
183
  const prevData = this.#queryCache.get(defaultedOptions.queryHash)?.state.data;
@@ -96,13 +188,47 @@ var QueryClient = class {
96
188
  manual: true
97
189
  });
98
190
  }
191
+ /**
192
+ * Synchronous way to immediately update the cached data of multiple queries at once, using
193
+ * filters or partial query key matching. Only queries that already exist and match the given
194
+ * filters are updated; no new cache entries are created. Internally this calls
195
+ * {@link QueryClient#setQueryData} for each matching query.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * queryClient.setQueriesData({ queryKey: ['posts'] }, (oldPosts) =>
200
+ * oldPosts ? oldPosts.filter((post) => post.id !== deletedId) : oldPosts,
201
+ * )
202
+ * ```
203
+ */
99
204
  setQueriesData(filters, updater, options) {
100
205
  return require_notifyManager.notifyManager.batch(() => this.#queryCache.findAll(filters).map(({ queryKey }) => [queryKey, this.setQueryData(queryKey, updater, options)]));
101
206
  }
207
+ /**
208
+ * Imperative (non-reactive) way to retrieve an existing query's state. If the query does not
209
+ * exist, `undefined` is returned.
210
+ *
211
+ * @example
212
+ * ```ts
213
+ * const state = queryClient.getQueryState(['posts'])
214
+ * console.log(state?.dataUpdatedAt)
215
+ * ```
216
+ */
102
217
  getQueryState(queryKey) {
103
218
  const options = this.defaultQueryOptions({ queryKey });
104
219
  return this.#queryCache.get(options.queryHash)?.state;
105
220
  }
221
+ /**
222
+ * Removes queries from the cache that match the given filters. Unlike
223
+ * {@link QueryClient#invalidateQueries} or {@link QueryClient#refetchQueries}, this removes
224
+ * matching queries from the cache instead of refetching them. Without filters, every query in
225
+ * the cache is removed.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * queryClient.removeQueries({ queryKey: ['posts'], exact: true })
230
+ * ```
231
+ */
106
232
  removeQueries(filters) {
107
233
  const queryCache = this.#queryCache;
108
234
  require_notifyManager.notifyManager.batch(() => {
@@ -111,6 +237,16 @@ var QueryClient = class {
111
237
  });
112
238
  });
113
239
  }
240
+ /**
241
+ * Resets queries matching the given filters back to their initial state (e.g. any
242
+ * `initialData`), notifying subscribers rather than removing them. Active queries among the
243
+ * matched set are then refetched, and the returned promise resolves once that refetch settles.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * await queryClient.resetQueries({ queryKey: ['posts'], exact: true })
248
+ * ```
249
+ */
114
250
  resetQueries(filters, options) {
115
251
  const queryCache = this.#queryCache;
116
252
  return require_notifyManager.notifyManager.batch(() => {
@@ -125,6 +261,19 @@ var QueryClient = class {
125
261
  }, options);
126
262
  });
127
263
  }
264
+ /**
265
+ * Cancels outgoing fetches for queries matching the given filters. Most useful when performing
266
+ * optimistic updates, since any outgoing refetch that resolves afterwards would otherwise
267
+ * overwrite the optimistic update. By default (`revert: true`), a cancelled query's data is
268
+ * reverted to its state before the outgoing fetch started.
269
+ *
270
+ * The returned promise never rejects, even if individual cancellations fail.
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * await queryClient.cancelQueries({ queryKey: ['posts'], exact: true })
275
+ * ```
276
+ */
128
277
  cancelQueries(filters, cancelOptions = {}) {
129
278
  const defaultedCancelOptions = {
130
279
  revert: true,
@@ -133,6 +282,19 @@ var QueryClient = class {
133
282
  const promises = require_notifyManager.notifyManager.batch(() => this.#queryCache.findAll(filters).map((query) => query.cancel(defaultedCancelOptions)));
134
283
  return Promise.all(promises).then(require_utils.noop).catch(require_utils.noop);
135
284
  }
285
+ /**
286
+ * Marks queries matching the given filters as invalidated. Unlike
287
+ * {@link QueryClient#removeQueries}, invalidated queries stay in the cache.
288
+ *
289
+ * Unless `filters.refetchType` is `'none'`, matching queries are then refetched via
290
+ * {@link QueryClient#refetchQueries}, using `filters.refetchType` if set, otherwise
291
+ * `filters.type`, otherwise `'active'`.
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * await queryClient.invalidateQueries({ queryKey: ['posts'], refetchType: 'active' })
296
+ * ```
297
+ */
136
298
  invalidateQueries(filters, options = {}) {
137
299
  return require_notifyManager.notifyManager.batch(() => {
138
300
  this.#queryCache.findAll(filters).forEach((query) => {
@@ -145,6 +307,21 @@ var QueryClient = class {
145
307
  }, options);
146
308
  });
147
309
  }
310
+ /**
311
+ * Refetches queries matching the given filters, regardless of whether they are stale. Without
312
+ * filters, every query in the cache is refetched. Queries that are disabled, or static (only
313
+ * have observers with a static `staleTime`), are never refetched.
314
+ *
315
+ * By default (`cancelRefetch: true`), a currently running fetch is cancelled before the new
316
+ * one starts. The returned promise resolves once all matching queries have settled; it does
317
+ * not reject on individual query failures unless `throwOnError` is set.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * // refetch all active queries partially matching a query key:
322
+ * await queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })
323
+ * ```
324
+ */
148
325
  refetchQueries(filters, options = {}) {
149
326
  const fetchOptions = {
150
327
  ...options,
@@ -157,6 +334,37 @@ var QueryClient = class {
157
334
  }));
158
335
  return Promise.all(promises).then(require_utils.noop);
159
336
  }
337
+ /**
338
+ * Asynchronous method to fetch and cache a query, resolving with the data or throwing with
339
+ * the error.
340
+ *
341
+ * If the query already exists in the cache and its data is not stale (per the given
342
+ * `staleTime`), the cached data is returned without fetching. Otherwise, the query is fetched
343
+ * and the promise resolves once the fetch settles. If a `select` function is provided, it is
344
+ * applied to the data in both cases (cached or freshly fetched) before it is returned.
345
+ *
346
+ * Unlike a reactive observer, retries are disabled by default here (`retry: false`) unless
347
+ * explicitly configured, since there is no component to catch a thrown error and retry through
348
+ * re-render.
349
+ *
350
+ * The accepted options are `QueryObserverOptions` minus the fields that only make sense for a
351
+ * reactive observer — `enabled`, `refetchInterval`, `refetchIntervalInBackground`,
352
+ * `refetchOnWindowFocus`, `refetchOnReconnect`, `refetchOnMount`, `retryOnMount`,
353
+ * `notifyOnChangeProps`, `throwOnError`, `suspense`, and `placeholderData` are not part of this
354
+ * method's options.
355
+ *
356
+ * This method replaces the deprecated `fetchQuery`, and — combined with
357
+ * `{ staleTime: 'static' }` — the deprecated `ensureQueryData`.
358
+ *
359
+ * @example
360
+ * ```ts
361
+ * try {
362
+ * const data = await queryClient.query({ queryKey, queryFn, staleTime: 10000 })
363
+ * } catch (error) {
364
+ * console.log(error)
365
+ * }
366
+ * ```
367
+ */
160
368
  async query(options) {
161
369
  const defaultedOptions = this.defaultQueryOptions(options);
162
370
  if (defaultedOptions.retry === void 0) defaultedOptions.retry = false;
@@ -181,6 +389,27 @@ var QueryClient = class {
181
389
  prefetchQuery(options) {
182
390
  return this.fetchQuery(options).then(require_utils.noop).catch(require_utils.noop);
183
391
  }
392
+ /**
393
+ * Asynchronous method to fetch and cache an infinite query, resolving with an
394
+ * {@link InfiniteData} object or throwing with the error.
395
+ *
396
+ * Behaves like {@link QueryClient#query}, accepting the same options (minus
397
+ * `initialPageParam`), plus the required `initialPageParam`, and an optional `pages` /
398
+ * `getNextPageParam` pair used to refetch a fixed number of pages from the start.
399
+ *
400
+ * This method replaces the deprecated `fetchInfiniteQuery`, and — combined with
401
+ * `{ staleTime: 'static' }` — the deprecated `ensureInfiniteQueryData`.
402
+ *
403
+ * @example
404
+ * ```ts
405
+ * try {
406
+ * const data = await queryClient.infiniteQuery({ queryKey, queryFn, initialPageParam: 0 })
407
+ * console.log(data.pages)
408
+ * } catch (error) {
409
+ * console.log(error)
410
+ * }
411
+ * ```
412
+ */
184
413
  infiniteQuery(options) {
185
414
  options._type = "infinite";
186
415
  return this.query(options);
@@ -205,28 +434,118 @@ var QueryClient = class {
205
434
  options._type = "infinite";
206
435
  return this.ensureQueryData(options);
207
436
  }
437
+ /**
438
+ * Resumes mutations that were paused because there was no network connection. Does nothing
439
+ * (resolving immediately) if the client is currently offline.
440
+ *
441
+ * @example
442
+ * ```ts
443
+ * import { QueryClient } from '@tanstack/query-core'
444
+ *
445
+ * const queryClient = new QueryClient()
446
+ * await queryClient.resumePausedMutations()
447
+ * ```
448
+ */
208
449
  resumePausedMutations() {
209
450
  if (require_onlineManager.onlineManager.isOnline()) return this.#mutationCache.resumePausedMutations();
210
451
  return Promise.resolve();
211
452
  }
453
+ /**
454
+ * Returns the query cache this client is connected to.
455
+ *
456
+ * @example
457
+ * ```ts
458
+ * import { QueryClient } from '@tanstack/query-core'
459
+ *
460
+ * const queryClient = new QueryClient()
461
+ * const queryCache = queryClient.getQueryCache()
462
+ * const queries = queryCache.findAll({ queryKey: ['posts'] })
463
+ * ```
464
+ */
212
465
  getQueryCache() {
213
466
  return this.#queryCache;
214
467
  }
468
+ /**
469
+ * Returns the mutation cache this client is connected to.
470
+ *
471
+ * @example
472
+ * ```ts
473
+ * import { QueryClient } from '@tanstack/query-core'
474
+ *
475
+ * const queryClient = new QueryClient()
476
+ * const mutationCache = queryClient.getMutationCache()
477
+ * const mutations = mutationCache.findAll({ status: 'pending' })
478
+ * ```
479
+ */
215
480
  getMutationCache() {
216
481
  return this.#mutationCache;
217
482
  }
483
+ /**
484
+ * Returns the default options that were set when creating the client, or via
485
+ * {@link QueryClient#setDefaultOptions}.
486
+ *
487
+ * @example
488
+ * ```ts
489
+ * import { QueryClient } from '@tanstack/query-core'
490
+ *
491
+ * const queryClient = new QueryClient()
492
+ * const defaultOptions = queryClient.getDefaultOptions()
493
+ * ```
494
+ */
218
495
  getDefaultOptions() {
219
496
  return this.#defaultOptions;
220
497
  }
498
+ /**
499
+ * Dynamically sets the default options for this client, overwriting any previously defined
500
+ * default options.
501
+ *
502
+ * @see {@link QueryClient#getDefaultOptions}
503
+ * @example
504
+ * ```ts
505
+ * import { QueryClient } from '@tanstack/query-core'
506
+ *
507
+ * const queryClient = new QueryClient()
508
+ * queryClient.setDefaultOptions({
509
+ * queries: {
510
+ * staleTime: Infinity,
511
+ * },
512
+ * })
513
+ * ```
514
+ */
221
515
  setDefaultOptions(options) {
222
516
  this.#defaultOptions = options;
223
517
  }
518
+ /**
519
+ * Sets default options for queries whose query key partially matches the given `queryKey`.
520
+ *
521
+ * If several registered query defaults match a given query key, they are merged together in
522
+ * registration order by {@link QueryClient#getQueryDefaults}, so register defaults from the
523
+ * most generic key to the least generic one — more specific defaults should be registered
524
+ * after more generic ones so they take precedence.
525
+ *
526
+ * @example
527
+ * ```ts
528
+ * queryClient.setQueryDefaults(['posts'], { queryFn: fetchPosts })
529
+ *
530
+ * await queryClient.query({ queryKey: ['posts'] })
531
+ * ```
532
+ */
224
533
  setQueryDefaults(queryKey, options) {
225
534
  this.#queryDefaults.set(require_utils.hashKey(queryKey), {
226
535
  queryKey,
227
536
  defaultOptions: options
228
537
  });
229
538
  }
539
+ /**
540
+ * Returns the default options registered for queries whose query key partially matches the
541
+ * given `queryKey`, via {@link QueryClient#setQueryDefaults}. If multiple registered defaults
542
+ * match, they are merged together in registration order.
543
+ *
544
+ * @example
545
+ * ```ts
546
+ * const defaultOptions = queryClient.getQueryDefaults(['posts'])
547
+ * ```
548
+ */
230
549
  getQueryDefaults(queryKey) {
231
550
  const defaults = [...this.#queryDefaults.values()];
232
551
  const result = {};
@@ -235,12 +554,33 @@ var QueryClient = class {
235
554
  });
236
555
  return result;
237
556
  }
557
+ /**
558
+ * Sets default options for mutations whose mutation key partially matches the given
559
+ * `mutationKey`. As with {@link QueryClient#setQueryDefaults}, the order of registration
560
+ * matters when several registered defaults match the same mutation key.
561
+ *
562
+ * @see {@link QueryClient#getMutationDefaults}
563
+ * @example
564
+ * ```ts
565
+ * queryClient.setMutationDefaults(['addPost'], { mutationFn: addPost })
566
+ * ```
567
+ */
238
568
  setMutationDefaults(mutationKey, options) {
239
569
  this.#mutationDefaults.set(require_utils.hashKey(mutationKey), {
240
570
  mutationKey,
241
571
  defaultOptions: options
242
572
  });
243
573
  }
574
+ /**
575
+ * Returns the default options registered for mutations whose mutation key partially matches
576
+ * the given `mutationKey`, via {@link QueryClient#setMutationDefaults}. If multiple registered
577
+ * defaults match, they are merged together in registration order.
578
+ *
579
+ * @example
580
+ * ```ts
581
+ * const defaultOptions = queryClient.getMutationDefaults(['addPost'])
582
+ * ```
583
+ */
244
584
  getMutationDefaults(mutationKey) {
245
585
  const defaults = [...this.#mutationDefaults.values()];
246
586
  const result = {};
@@ -249,6 +589,12 @@ var QueryClient = class {
249
589
  });
250
590
  return result;
251
591
  }
592
+ /**
593
+ * Called by framework adapters (e.g. inside `useQuery`) to resolve the options passed by the
594
+ * caller into their final, defaulted form: merging `queryClient.setQueryDefaults` for the
595
+ * given `queryKey`, then the client's own `defaultOptions.queries`, then the caller's options
596
+ * on top. A no-op if the options are already defaulted (`_defaulted: true`).
597
+ */
252
598
  defaultQueryOptions(options) {
253
599
  if (options._defaulted) return options;
254
600
  const defaultedOptions = {
@@ -264,6 +610,12 @@ var QueryClient = class {
264
610
  if (defaultedOptions.queryFn === require_utils.skipToken) defaultedOptions.enabled = false;
265
611
  return defaultedOptions;
266
612
  }
613
+ /**
614
+ * The mutation counterpart of {@link QueryClient#defaultQueryOptions}. Called by framework
615
+ * adapters (e.g. inside `useMutation`) to merge `queryClient.setMutationDefaults` for the
616
+ * given `mutationKey`, then the client's `defaultOptions.mutations`, then the caller's options
617
+ * on top. A no-op if the options are already defaulted (`_defaulted: true`).
618
+ */
267
619
  defaultMutationOptions(options) {
268
620
  if (options?._defaulted) return options;
269
621
  return {
@@ -273,6 +625,17 @@ var QueryClient = class {
273
625
  _defaulted: true
274
626
  };
275
627
  }
628
+ /**
629
+ * Clears both the query cache and the mutation cache this client is connected to.
630
+ *
631
+ * @example
632
+ * ```ts
633
+ * import { QueryClient } from '@tanstack/query-core'
634
+ *
635
+ * const queryClient = new QueryClient()
636
+ * queryClient.clear()
637
+ * ```
638
+ */
276
639
  clear() {
277
640
  this.#queryCache.clear();
278
641
  this.#mutationCache.clear();