@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
@@ -34,6 +34,26 @@ interface ObserverFetchOptions extends FetchOptions {
34
34
  throwOnError?: boolean
35
35
  }
36
36
 
37
+ /**
38
+ * A `QueryObserver` watches a single query in the `QueryCache` and computes a
39
+ * `QueryObserverResult` from its state, recomputing and notifying subscribers
40
+ * whenever the underlying query (or the observer's options) changes. It is
41
+ * the primitive that framework adapters (e.g. `useQuery`) build their hooks
42
+ * on top of, but it can also be used directly to observe and switch between
43
+ * queries outside of any framework.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const observer = new QueryObserver(queryClient, {
48
+ * queryKey: ['posts'],
49
+ * queryFn: fetchPosts,
50
+ * })
51
+ *
52
+ * const unsubscribe = observer.subscribe((result) => {
53
+ * console.log(result.data)
54
+ * })
55
+ * ```
56
+ */
37
57
  export class QueryObserver<
38
58
  TQueryFnData = unknown,
39
59
  TError = DefaultError,
@@ -107,6 +127,11 @@ export class QueryObserver<
107
127
  }
108
128
  }
109
129
 
130
+ /**
131
+ * Returns whether the observed query is currently stale and configured
132
+ * (via the `refetchOnReconnect` option) to refetch when the network
133
+ * reconnects.
134
+ */
110
135
  shouldFetchOnReconnect(): boolean {
111
136
  return shouldFetchOn(
112
137
  this.#currentQuery,
@@ -115,6 +140,11 @@ export class QueryObserver<
115
140
  )
116
141
  }
117
142
 
143
+ /**
144
+ * Returns whether the observed query is currently stale and configured
145
+ * (via the `refetchOnWindowFocus` option) to refetch when the window
146
+ * regains focus.
147
+ */
118
148
  shouldFetchOnWindowFocus(): boolean {
119
149
  return shouldFetchOn(
120
150
  this.#currentQuery,
@@ -123,6 +153,11 @@ export class QueryObserver<
123
153
  )
124
154
  }
125
155
 
156
+ /**
157
+ * Stops observing the current query: clears all listeners, cancels the
158
+ * stale and refetch-interval timers, and removes this observer from the
159
+ * query it was observing.
160
+ */
126
161
  destroy(): void {
127
162
  this.listeners = new Set()
128
163
  this.#clearStaleTimeout()
@@ -130,6 +165,20 @@ export class QueryObserver<
130
165
  this.#currentQuery.removeObserver(this)
131
166
  }
132
167
 
168
+ /**
169
+ * Updates the observer's options. This will re-resolve the query being
170
+ * observed (switching to a different query if the `queryKey` changed),
171
+ * trigger a fetch if the new options require one and the observer has
172
+ * subscribers, recompute the current result, and reschedule the stale and
173
+ * refetch-interval timers as needed.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * observer.setOptions({ queryKey: ['posts', 1], queryFn: () => fetchPost(1) })
178
+ * // later: switch to a different query, reusing the same observer
179
+ * observer.setOptions({ queryKey: ['posts', 2], queryFn: () => fetchPost(2) })
180
+ * ```
181
+ */
133
182
  setOptions(
134
183
  options: QueryObserverOptions<
135
184
  TQueryFnData,
@@ -214,6 +263,12 @@ export class QueryObserver<
214
263
  }
215
264
  }
216
265
 
266
+ /**
267
+ * Computes the result the observer would produce for the given (already-defaulted) options
268
+ * right now, building the underlying `Query` if it doesn't exist yet, without waiting for a
269
+ * subscription callback. Called by framework adapters on every render (e.g. `useQuery`) so the
270
+ * returned value is available synchronously, ahead of `setOptions` triggering an actual fetch.
271
+ */
217
272
  getOptimisticResult(
218
273
  options: DefaultedQueryObserverOptions<
219
274
  TQueryFnData,
@@ -251,10 +306,28 @@ export class QueryObserver<
251
306
  return result
252
307
  }
253
308
 
309
+ /**
310
+ * Returns the most recently computed `QueryObserverResult` for the
311
+ * observed query. This is a point-in-time read; to be notified of updates
312
+ * as they happen, subscribe to the observer instead (its inherited
313
+ * `subscribe` method).
314
+ *
315
+ * @example
316
+ * ```ts
317
+ * const result = observer.getCurrentResult()
318
+ * console.log(result.status, result.data)
319
+ * ```
320
+ */
254
321
  getCurrentResult(): QueryObserverResult<TData, TError> {
255
322
  return this.#currentResult
256
323
  }
257
324
 
325
+ /**
326
+ * Wraps a `QueryObserverResult` in a `Proxy` that records which properties are read, via
327
+ * {@link QueryObserver#trackProp} (and an optional `onPropTracked` callback). Used by framework
328
+ * adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
329
+ * properties you actually read" behavior.
330
+ */
258
331
  trackResult(
259
332
  result: QueryObserverResult<TData, TError>,
260
333
  onPropTracked?: (key: keyof QueryObserverResult) => void,
@@ -268,14 +341,33 @@ export class QueryObserver<
268
341
  })
269
342
  }
270
343
 
344
+ /**
345
+ * Records that the given `QueryObserverResult` property was read, so a subsequent update only
346
+ * notifies this observer if a tracked property actually changed. Normally called indirectly via
347
+ * {@link QueryObserver#trackResult}'s proxy; exposed directly for adapters that track property
348
+ * access themselves (e.g. through their own reactivity system) instead of via the proxy.
349
+ */
271
350
  trackProp(key: keyof QueryObserverResult) {
272
351
  this.#trackedProps.add(key)
273
352
  }
274
353
 
354
+ /**
355
+ * Returns the `Query` instance this observer is currently observing.
356
+ */
275
357
  getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
276
358
  return this.#currentQuery
277
359
  }
278
360
 
361
+ /**
362
+ * Refetches the observed query and returns a promise that resolves with
363
+ * the resulting `QueryObserverResult`.
364
+ *
365
+ * @example
366
+ * ```ts
367
+ * const result = await observer.refetch({ cancelRefetch: false })
368
+ * console.log(result.data)
369
+ * ```
370
+ */
279
371
  refetch({ ...options }: RefetchOptions = {}): Promise<
280
372
  QueryObserverResult<TData, TError>
281
373
  > {
@@ -284,6 +376,22 @@ export class QueryObserver<
284
376
  })
285
377
  }
286
378
 
379
+ /**
380
+ * Fetches a query defined by the given options without affecting this
381
+ * observer's own tracked query or result, and returns a promise that
382
+ * resolves with the `QueryObserverResult` for that fetch. This is useful
383
+ * for prefetching data that another observer (e.g. a query about to be
384
+ * navigated to) will need, ahead of time.
385
+ *
386
+ * @example
387
+ * ```ts
388
+ * const result = await observer.fetchOptimistic({
389
+ * queryKey: ['posts', 2],
390
+ * queryFn: () => fetchPost(2),
391
+ * })
392
+ * console.log(result.data)
393
+ * ```
394
+ */
287
395
  fetchOptimistic(
288
396
  options: QueryObserverOptions<
289
397
  TQueryFnData,
@@ -402,9 +510,8 @@ export class QueryObserver<
402
510
 
403
511
  #computeRefetchInterval() {
404
512
  return (
405
- (typeof this.options.refetchInterval === 'function'
406
- ? this.options.refetchInterval(this.#currentQuery)
407
- : this.options.refetchInterval) ?? false
513
+ resolveQueryValue(this.options.refetchInterval, this.#currentQuery) ??
514
+ false
408
515
  )
409
516
  }
410
517
 
@@ -620,6 +727,11 @@ export class QueryObserver<
620
727
  return nextResult
621
728
  }
622
729
 
730
+ /**
731
+ * Recomputes and stores the current result from the current query/options, notifying listeners
732
+ * if it changed. Framework adapters call this right after subscribing to make sure no query
733
+ * update was missed in the gap between creating the observer and subscribing to it.
734
+ */
623
735
  updateResult(): void {
624
736
  const prevResult = this.#currentResult as
625
737
  | QueryObserverResult<TData, TError>
@@ -712,6 +824,7 @@ export class QueryObserver<
712
824
  }
713
825
  }
714
826
 
827
+ /** @internal */
715
828
  onQueryUpdate(): void {
716
829
  this.updateResult()
717
830
 
@@ -757,7 +870,7 @@ function shouldFetchOn(
757
870
  resolveQueryValue(options.enabled, query) !== false &&
758
871
  resolveQueryValue(options.staleTime, query) !== 'static'
759
872
  ) {
760
- const value = typeof field === 'function' ? field(query) : field
873
+ const value = resolveQueryValue(field, query)
761
874
 
762
875
  return value === 'always' || (value !== false && isStale(query, options))
763
876
  }
package/src/retryer.ts CHANGED
@@ -56,6 +56,24 @@ export function canFetch(networkMode: NetworkMode | undefined): boolean {
56
56
  : true
57
57
  }
58
58
 
59
+ /**
60
+ * The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via
61
+ * `query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch
62
+ * started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead
63
+ * resolve with the promise of the fetch that triggered the cancellation.
64
+ * @example
65
+ * ```ts
66
+ * query.cancel()
67
+ *
68
+ * try {
69
+ * await query.promise
70
+ * } catch (error) {
71
+ * if (error instanceof CancelledError) {
72
+ * // the fetch was cancelled, e.g. via `query.cancel()`
73
+ * }
74
+ * }
75
+ * ```
76
+ */
59
77
  export class CancelledError extends Error {
60
78
  revert?: boolean
61
79
  silent?: boolean
@@ -7,9 +7,16 @@ import type {
7
7
  } from './types'
8
8
 
9
9
  type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
10
+ /** The function that returns an `AsyncIterable` to stream data from. */
10
11
  streamFn: (
11
12
  context: QueryFunctionContext<TQueryKey>,
12
13
  ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>
14
+ /**
15
+ * Defines how refetches are handled.
16
+ * - `'reset'` (default): erases all data and puts the query back into `pending` state.
17
+ * - `'append'`: appends new data to the existing data.
18
+ * - `'replace'`: writes all data to the cache once the stream ends.
19
+ */
13
20
  refetchMode?: 'append' | 'reset' | 'replace'
14
21
  }
15
22
 
@@ -26,7 +33,15 @@ type ReducibleStreamedQueryParams<
26
33
  TData,
27
34
  TQueryKey extends QueryKey,
28
35
  > = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {
36
+ /**
37
+ * Reduces streamed chunks into the final data shape. Required whenever `TData` is not an
38
+ * array, since there is no default way to accumulate non-array chunks.
39
+ */
29
40
  reducer: (acc: TData, chunk: TQueryFnData) => TData
41
+ /**
42
+ * The value used while the first chunk is being fetched, and returned if the stream yields no
43
+ * values. Required together with a custom `reducer`.
44
+ */
30
45
  initialValue: TData
31
46
  }
32
47
 
@@ -39,7 +54,7 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =
39
54
  * Data will be an Array of all the chunks received.
40
55
  * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
41
56
  * The query will stay in fetchStatus 'fetching' until the stream ends.
42
- * @param queryFn - The function that returns an AsyncIterable to stream data from.
57
+ * @param streamFn - The function that returns an AsyncIterable to stream data from.
43
58
  * @param refetchMode - Defines how re-fetches are handled.
44
59
  * Defaults to `'reset'`, erases all data and puts the query back into `pending` state.
45
60
  * Set to `'append'` to append new data to the existing data.
@@ -47,6 +62,15 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =
47
62
  * @param reducer - A function to reduce the streamed chunks into the final data.
48
63
  * Defaults to a function that appends chunks to the end of the array.
49
64
  * @param initialValue - Initial value to be used while the first chunk is being fetched, and returned if the stream yields no values.
65
+ * @example
66
+ * ```ts
67
+ * await queryClient.query({
68
+ * queryKey: ['data'],
69
+ * queryFn: streamedQuery({
70
+ * streamFn: fetchDataInChunks,
71
+ * }),
72
+ * })
73
+ * ```
50
74
  */
51
75
  export function streamedQuery<
52
76
  TQueryFnData = unknown,
@@ -18,6 +18,12 @@ export type ManagedTimerId = number | { [Symbol.toPrimitive]: () => number }
18
18
 
19
19
  /**
20
20
  * Backend for timer functions.
21
+ *
22
+ * Timers are performance-sensitive: short-lived timers (delays under a few seconds) tend to be
23
+ * latency-sensitive, while long-lived ones may benefit more from coalescing — batching timers
24
+ * with similar deadlines together — which the default provider (backed by the platform's global
25
+ * `setTimeout`/`setInterval`) does not do. A custom provider can implement coalescing, and can
26
+ * also support delays longer than the ~24-day maximum of the global `setTimeout`.
21
27
  */
22
28
  export type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> =
23
29
  {
@@ -72,6 +78,31 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
72
78
  #provider: TimeoutProvider<any> = defaultTimeoutProvider
73
79
  #providerCalled = false
74
80
 
81
+ /**
82
+ * `setTimeoutProvider` can be used to set a custom implementation of the
83
+ * `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` functions,
84
+ * called a `TimeoutProvider`.
85
+ *
86
+ * This may be useful if you notice event loop performance issues with
87
+ * thousands of queries. A custom TimeoutProvider could also support timer
88
+ * delays longer than the global `setTimeout` maximum delay value of about
89
+ * 24 days.
90
+ *
91
+ * It is important to call `setTimeoutProvider` before creating a
92
+ * QueryClient or queries, so that the same provider is used consistently
93
+ * for all timers in the application, since different TimeoutProviders
94
+ * cannot cancel each others' timers.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { timeoutManager, QueryClient } from '@tanstack/query-core'
99
+ * import { CustomTimeoutProvider } from './CustomTimeoutProvider'
100
+ *
101
+ * timeoutManager.setTimeoutProvider(new CustomTimeoutProvider())
102
+ *
103
+ * export const queryClient = new QueryClient()
104
+ * ```
105
+ */
75
106
  setTimeoutProvider<TTimerId extends ManagedTimerId>(
76
107
  provider: TimeoutProvider<TTimerId>,
77
108
  ): void {
@@ -101,6 +132,26 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
101
132
  }
102
133
  }
103
134
 
135
+ /**
136
+ * `setTimeout` schedules a callback to run after approximately `delay`
137
+ * milliseconds, like the global `setTimeout` function. The callback can be
138
+ * canceled with `clearTimeout`.
139
+ *
140
+ * It returns a timer ID, which may be a number or an object that can be
141
+ * coerced to a number via `Symbol.toPrimitive`.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import { timeoutManager } from '@tanstack/query-core'
146
+ *
147
+ * const timeoutId = timeoutManager.setTimeout(
148
+ * () => console.log('ran at:', new Date()),
149
+ * 1000,
150
+ * )
151
+ *
152
+ * const timeoutIdNumber: number = Number(timeoutId)
153
+ * ```
154
+ */
104
155
  setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId {
105
156
  if (process.env.NODE_ENV !== 'production') {
106
157
  this.#providerCalled = true
@@ -108,10 +159,44 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
108
159
  return this.#provider.setTimeout(callback, delay)
109
160
  }
110
161
 
162
+ /**
163
+ * `clearTimeout` cancels a timeout callback scheduled with `setTimeout`,
164
+ * like the global `clearTimeout` function. It should be called with a
165
+ * timer ID returned by `setTimeout`.
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * import { timeoutManager } from '@tanstack/query-core'
170
+ *
171
+ * const timeoutId = timeoutManager.setTimeout(
172
+ * () => console.log('ran at:', new Date()),
173
+ * 1000,
174
+ * )
175
+ *
176
+ * timeoutManager.clearTimeout(timeoutId)
177
+ * ```
178
+ */
111
179
  clearTimeout(timeoutId: ManagedTimerId | undefined): void {
112
180
  this.#provider.clearTimeout(timeoutId)
113
181
  }
114
182
 
183
+ /**
184
+ * `setInterval` schedules a callback to be called approximately every
185
+ * `delay` milliseconds, like the global `setInterval` function.
186
+ *
187
+ * Like `setTimeout`, it returns a timer ID, which may be a number or an
188
+ * object that can be coerced to a number via `Symbol.toPrimitive`.
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * import { timeoutManager } from '@tanstack/query-core'
193
+ *
194
+ * const intervalId = timeoutManager.setInterval(
195
+ * () => console.log('ran at:', new Date()),
196
+ * 1000,
197
+ * )
198
+ * ```
199
+ */
115
200
  setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId {
116
201
  if (process.env.NODE_ENV !== 'production') {
117
202
  this.#providerCalled = true
@@ -119,11 +204,31 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
119
204
  return this.#provider.setInterval(callback, delay)
120
205
  }
121
206
 
207
+ /**
208
+ * `clearInterval` can be used to cancel an interval, like the global
209
+ * `clearInterval` function. It should be called with an interval ID
210
+ * returned by `setInterval`.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * import { timeoutManager } from '@tanstack/query-core'
215
+ *
216
+ * const intervalId = timeoutManager.setInterval(
217
+ * () => console.log('ran at:', new Date()),
218
+ * 1000,
219
+ * )
220
+ *
221
+ * timeoutManager.clearInterval(intervalId)
222
+ * ```
223
+ */
122
224
  clearInterval(intervalId: ManagedTimerId | undefined): void {
123
225
  this.#provider.clearInterval(intervalId)
124
226
  }
125
227
  }
126
228
 
229
+ /**
230
+ * Singleton instance of {@link TimeoutManager}, used throughout TanStack Query to schedule and cancel timers.
231
+ */
127
232
  export const timeoutManager = new TimeoutManager()
128
233
 
129
234
  /**