@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
package/src/mutation.ts CHANGED
@@ -23,20 +23,54 @@ interface MutationConfig<TData, TError, TVariables, TOnMutateResult> {
23
23
  state?: MutationState<TData, TError, TVariables, TOnMutateResult>
24
24
  }
25
25
 
26
+ /**
27
+ * The raw state stored on a `Mutation` instance. This is the underlying state
28
+ * that observer results (e.g. `MutationObserverResult`) are derived from.
29
+ */
26
30
  export interface MutationState<
27
31
  TData = unknown,
28
32
  TError = DefaultError,
29
33
  TVariables = unknown,
30
34
  TOnMutateResult = unknown,
31
35
  > {
36
+ /**
37
+ * The value returned by `onMutate`, if defined. Passed to `onSuccess`,
38
+ * `onError` and `onSettled` as the mutation's context.
39
+ */
32
40
  context: TOnMutateResult | undefined
41
+ /**
42
+ * The last successfully resolved data for the mutation.
43
+ */
33
44
  data: TData | undefined
45
+ /**
46
+ * The error object for the mutation, if the last attempt resulted in an error.
47
+ * - Defaults to `null`.
48
+ */
34
49
  error: TError | null
50
+ /**
51
+ * The number of times the mutation function has failed for the current attempt.
52
+ */
35
53
  failureCount: number
54
+ /**
55
+ * The reason the current attempt failed, as reported by the retryer.
56
+ */
36
57
  failureReason: TError | null
58
+ /**
59
+ * Whether the mutation is currently paused (see network mode), or is
60
+ * waiting for another mutation with the same `scope` to finish.
61
+ */
37
62
  isPaused: boolean
63
+ /**
64
+ * The status of the mutation.
65
+ */
38
66
  status: MutationStatus
67
+ /**
68
+ * The variables the mutation was last called with.
69
+ */
39
70
  variables: TVariables | undefined
71
+ /**
72
+ * The timestamp for when the mutation was submitted.
73
+ */
40
74
  submittedAt: number
41
75
  }
42
76
 
@@ -81,6 +115,23 @@ export type Action<TData, TError, TVariables, TOnMutateResult> =
81
115
 
82
116
  // CLASS
83
117
 
118
+ /**
119
+ * Represents a single mutation attempt. A `Mutation` holds the mutation's
120
+ * options, state (data/error/status), and the `MutationObserver`s currently
121
+ * subscribed to it.
122
+ *
123
+ * Instances are created and managed internally by `MutationCache`; application
124
+ * code typically interacts with mutations indirectly through `QueryClient` or
125
+ * a framework hook like `useMutation`. Direct access to a `Mutation` instance
126
+ * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * const mutationCache = queryClient.getMutationCache()
131
+ *
132
+ * const mutation = mutationCache.find({ mutationKey: ['addPost'] })
133
+ * ```
134
+ */
84
135
  export class Mutation<
85
136
  TData = unknown,
86
137
  TError = DefaultError,
@@ -113,6 +164,7 @@ export class Mutation<
113
164
  this.scheduleGc()
114
165
  }
115
166
 
167
+ /** @internal */
116
168
  setOptions(
117
169
  options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,
118
170
  ): void {
@@ -121,10 +173,14 @@ export class Mutation<
121
173
  this.updateGcTime(this.options.gcTime)
122
174
  }
123
175
 
176
+ /**
177
+ * The `meta` object passed in the mutation's options, if any.
178
+ */
124
179
  get meta(): MutationMeta | undefined {
125
180
  return this.options.meta
126
181
  }
127
182
 
183
+ /** @internal */
128
184
  addObserver(observer: MutationObserver<any, any, any, any>): void {
129
185
  if (!this.#observers.includes(observer)) {
130
186
  this.#observers.push(observer)
@@ -140,6 +196,7 @@ export class Mutation<
140
196
  }
141
197
  }
142
198
 
199
+ /** @internal */
143
200
  removeObserver(observer: MutationObserver<any, any, any, any>): void {
144
201
  this.#observers = this.#observers.filter((x) => x !== observer)
145
202
 
@@ -162,6 +219,27 @@ export class Mutation<
162
219
  }
163
220
  }
164
221
 
222
+ /**
223
+ * Resumes a mutation that is currently paused or was restored from a
224
+ * dehydrated, still-`pending` state.
225
+ *
226
+ * - If this mutation has an active retryer (it paused mid-attempt, e.g. due
227
+ * to the network mode or scope-based queuing), its retryer is resumed.
228
+ * - Otherwise, if the mutation's status is still `pending` (e.g. it was
229
+ * dehydrated while an attempt was in flight and never got a retryer in
230
+ * this instance), `execute` is called again with the last known variables.
231
+ * - Otherwise the mutation has already settled and this resolves immediately
232
+ * without running anything again.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
237
+ * const mutation = mutationCache.find({ mutationKey: ['addPost'] })
238
+ * await mutation?.continue()
239
+ * ```
240
+ *
241
+ * @see {@link Mutation#execute}
242
+ */
165
243
  continue(): Promise<unknown> {
166
244
  return (
167
245
  this.#retryer?.continue() ??
@@ -173,6 +251,36 @@ export class Mutation<
173
251
  )
174
252
  }
175
253
 
254
+ /**
255
+ * Runs the mutation function for the given variables through a retryer, and
256
+ * drives the mutation's state and lifecycle callbacks through to settlement.
257
+ *
258
+ * If this mutation's state is already `pending` when `execute` is called
259
+ * (i.e. it was restored, still in-flight, from a dehydrated state), the
260
+ * `onMutate` step is skipped and a `continue` action is dispatched to
261
+ * unpause it; otherwise a `pending` action is dispatched first, then the
262
+ * mutation cache's `onMutate` and the mutation's own `onMutate` option are
263
+ * awaited in that order, and the resulting context is stored.
264
+ *
265
+ * The mutation function is then run (subject to `retry`/`retryDelay`/
266
+ * `networkMode`, and to the mutation cache's scope-based serialization).
267
+ * On success, the cache's `onSuccess`/`onSettled` callbacks run before the
268
+ * mutation's own `onSuccess`/`onSettled` options, a `success` action is
269
+ * dispatched, and the resolved data is returned. On failure, the same
270
+ * cache-then-option ordering is used for `onError`/`onSettled`, but each of
271
+ * those four callbacks is individually caught so that a throwing callback
272
+ * cannot mask the original error; an `error` action is then dispatched and
273
+ * the original error is re-thrown.
274
+ *
275
+ * @example
276
+ * ```ts
277
+ * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —
278
+ * // applications normally trigger mutations through those, not this method.
279
+ * const data = await mutation.execute(variables)
280
+ * ```
281
+ *
282
+ * @see {@link Mutation#continue}
283
+ */
176
284
  async execute(variables: TVariables): Promise<TData> {
177
285
  const onContinue = () => {
178
286
  this.#dispatch({ type: 'continue' })
@@ -15,7 +15,16 @@ import type { MutationFilters } from './utils'
15
15
 
16
16
  // TYPES
17
17
 
18
+ /**
19
+ * Global callbacks that fire for every mutation handled by a `MutationCache`, regardless of which
20
+ * component or observer triggered it. They differ from the `defaultOptions` provided to a
21
+ * `QueryClient` in two ways: `defaultOptions` can be overridden by each mutation, while these
22
+ * callbacks are always called, and `onMutate` here does not allow returning a result.
23
+ *
24
+ * If a callback returns a promise, it will be awaited before the mutation continues.
25
+ */
18
26
  export interface MutationCacheConfig {
27
+ /** Called when any mutation in the cache encounters an error. */
19
28
  onError?: (
20
29
  error: DefaultError,
21
30
  variables: unknown,
@@ -23,6 +32,7 @@ export interface MutationCacheConfig {
23
32
  mutation: Mutation<unknown, unknown, unknown>,
24
33
  context: MutationFunctionContext,
25
34
  ) => Promise<unknown> | unknown
35
+ /** Called when any mutation in the cache is successful. */
26
36
  onSuccess?: (
27
37
  data: unknown,
28
38
  variables: unknown,
@@ -30,11 +40,13 @@ export interface MutationCacheConfig {
30
40
  mutation: Mutation<unknown, unknown, unknown>,
31
41
  context: MutationFunctionContext,
32
42
  ) => Promise<unknown> | unknown
43
+ /** Called before any mutation in the cache executes. */
33
44
  onMutate?: (
34
45
  variables: unknown,
35
46
  mutation: Mutation<unknown, unknown, unknown>,
36
47
  context: MutationFunctionContext,
37
48
  ) => Promise<unknown> | unknown
49
+ /** Called when any mutation in the cache is settled, either successfully or with an error. */
38
50
  onSettled?: (
39
51
  data: unknown | undefined,
40
52
  error: DefaultError | null,
@@ -78,6 +90,11 @@ interface NotifyEventMutationUpdated extends NotifyEvent {
78
90
  action: Action<any, any, any, any>
79
91
  }
80
92
 
93
+ /**
94
+ * The event passed to a `MutationCache` subscriber. Fired whenever a mutation is added or removed
95
+ * from the cache, its state is updated, or one of its observers is added, removed, or has its
96
+ * options updated.
97
+ */
81
98
  export type MutationCacheNotifyEvent =
82
99
  | NotifyEventMutationAdded
83
100
  | NotifyEventMutationRemoved
@@ -90,6 +107,20 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
90
107
 
91
108
  // CLASS
92
109
 
110
+ /**
111
+ * The `MutationCache` is the storage for mutations.
112
+ *
113
+ * Normally, you will not interact with the `MutationCache` directly and instead use a
114
+ * `QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of
115
+ * safe/known updates to the cache, such as mutations being added, removed, or updated.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const unsubscribe = mutationCache.subscribe((event) => {
120
+ * console.log(event.type, event.mutation)
121
+ * })
122
+ * ```
123
+ */
93
124
  export class MutationCache extends Subscribable<MutationCacheListener> {
94
125
  #mutations: Set<Mutation<any, any, any, any>>
95
126
  #scopes: Map<string, Array<Mutation<any, any, any, any>>>
@@ -102,6 +133,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
102
133
  this.#mutationId = 0
103
134
  }
104
135
 
136
+ /** @internal */
105
137
  build<TData, TError, TVariables, TOnMutateResult>(
106
138
  client: QueryClient,
107
139
  options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,
@@ -120,6 +152,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
120
152
  return mutation
121
153
  }
122
154
 
155
+ /** @internal */
123
156
  add(mutation: Mutation<any, any, any, any>): void {
124
157
  this.#mutations.add(mutation)
125
158
  const scope = scopeFor(mutation)
@@ -134,6 +167,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
134
167
  this.notify({ type: 'added', mutation })
135
168
  }
136
169
 
170
+ /** @internal */
137
171
  remove(mutation: Mutation<any, any, any, any>): void {
138
172
  if (this.#mutations.delete(mutation)) {
139
173
  const scope = scopeFor(mutation)
@@ -157,6 +191,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
157
191
  this.notify({ type: 'removed', mutation })
158
192
  }
159
193
 
194
+ /** @internal */
160
195
  canRun(mutation: Mutation<any, any, any, any>): boolean {
161
196
  const scope = scopeFor(mutation)
162
197
  if (typeof scope === 'string') {
@@ -174,6 +209,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
174
209
  }
175
210
  }
176
211
 
212
+ /** @internal */
177
213
  runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {
178
214
  const scope = scopeFor(mutation)
179
215
  if (typeof scope === 'string') {
@@ -187,6 +223,16 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
187
223
  }
188
224
  }
189
225
 
226
+ /**
227
+ * Removes all mutations from the cache.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * const mutationCache = queryClient.getMutationCache()
232
+ *
233
+ * mutationCache.clear()
234
+ * ```
235
+ */
190
236
  clear(): void {
191
237
  notifyManager.batch(() => {
192
238
  this.#mutations.forEach((mutation) => {
@@ -197,10 +243,38 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
197
243
  })
198
244
  }
199
245
 
246
+ /**
247
+ * Returns all mutations within the cache.
248
+ *
249
+ * This is not typically needed for most applications, but can come in handy when needing more
250
+ * information about a mutation in rare scenarios.
251
+ *
252
+ * @example
253
+ * ```ts
254
+ * const mutationCache = queryClient.getMutationCache()
255
+ *
256
+ * const mutations = mutationCache.getAll()
257
+ * ```
258
+ */
200
259
  getAll(): Array<Mutation> {
201
260
  return Array.from(this.#mutations)
202
261
  }
203
262
 
263
+ /**
264
+ * A slightly more advanced method that can be used to get an existing mutation instance from
265
+ * the cache. If the mutation does not exist, `undefined` is returned.
266
+ *
267
+ * This is not typically needed for most applications, but can come in handy when needing more
268
+ * information about a mutation in rare scenarios.
269
+ *
270
+ * @see {@link MutationCache#findAll}
271
+ * @example
272
+ * ```ts
273
+ * const mutationCache = queryClient.getMutationCache()
274
+ *
275
+ * const mutation = mutationCache.find({ mutationKey: ['addPost'] })
276
+ * ```
277
+ */
204
278
  find<
205
279
  TData = unknown,
206
280
  TError = DefaultError,
@@ -216,10 +290,26 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
216
290
  ) as Mutation<TData, TError, TVariables, TOnMutateResult> | undefined
217
291
  }
218
292
 
293
+ /**
294
+ * An even more advanced method that can be used to get existing mutation instances from the
295
+ * cache that match the given filters. If no mutations match, an empty array is returned.
296
+ *
297
+ * This is not typically needed for most applications, but can come in handy when needing more
298
+ * information about mutations in rare scenarios.
299
+ *
300
+ * @see {@link MutationCache#find}
301
+ * @example
302
+ * ```ts
303
+ * const mutationCache = queryClient.getMutationCache()
304
+ *
305
+ * const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
306
+ * ```
307
+ */
219
308
  findAll(filters: MutationFilters = {}): Array<Mutation> {
220
309
  return this.getAll().filter((mutation) => matchMutation(filters, mutation))
221
310
  }
222
311
 
312
+ /** @internal */
223
313
  notify(event: MutationCacheNotifyEvent) {
224
314
  notifyManager.batch(() => {
225
315
  this.listeners.forEach((listener) => {
@@ -228,6 +318,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
228
318
  })
229
319
  }
230
320
 
321
+ /** @internal */
231
322
  resumePausedMutations(): Promise<unknown> {
232
323
  const pausedMutations = this.getAll().filter((x) => x.state.isPaused)
233
324
 
@@ -20,6 +20,21 @@ type MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (
20
20
 
21
21
  // CLASS
22
22
 
23
+ /**
24
+ * Observes a single mutation and derives a `MutationObserverResult` from it.
25
+ * A framework hook like `useMutation` creates one `MutationObserver` per hook
26
+ * call, keeps it stable across re-renders, calls `setOptions` when the options
27
+ * passed to the hook change, subscribes to it to re-render on updates, and
28
+ * reads `getCurrentResult()` for the value to return. Calling `mutate()`
29
+ * builds a new underlying `Mutation` in the `MutationCache` and executes it.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const observer = new MutationObserver(queryClient, {
34
+ * mutationFn: (variables: { title: string }) => addPost(variables),
35
+ * })
36
+ * ```
37
+ */
23
38
  export class MutationObserver<
24
39
  TData = unknown,
25
40
  TError = DefaultError,
@@ -62,6 +77,22 @@ export class MutationObserver<
62
77
  this.reset = this.reset.bind(this)
63
78
  }
64
79
 
80
+ /**
81
+ * Updates the observer's options.
82
+ *
83
+ * If the new `mutationKey` differs from the previous one (and both were
84
+ * defined), the observer is reset, detaching it from the mutation it was
85
+ * observing. Otherwise, if the currently observed mutation is still
86
+ * `pending`, its options are updated in place as well.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * observer.setOptions({
91
+ * mutationFn: (variables: { title: string }) => addPost(variables),
92
+ * onSuccess: (data) => console.log(data),
93
+ * })
94
+ * ```
95
+ */
65
96
  setOptions(
66
97
  options: MutationObserverOptions<
67
98
  TData,
@@ -107,6 +138,7 @@ export class MutationObserver<
107
138
  }
108
139
  }
109
140
 
141
+ /** @internal */
110
142
  onMutationUpdate(
111
143
  action: Action<TData, TError, TVariables, TOnMutateResult>,
112
144
  ): void {
@@ -115,6 +147,11 @@ export class MutationObserver<
115
147
  this.#notify(action)
116
148
  }
117
149
 
150
+ /**
151
+ * Returns the observer's current result, derived from the observed
152
+ * mutation's state (or the default, `idle` state if no mutation has been
153
+ * built yet, e.g. before the first `mutate()` call or after `reset()`).
154
+ */
118
155
  getCurrentResult(): MutationObserverResult<
119
156
  TData,
120
157
  TError,
@@ -124,6 +161,22 @@ export class MutationObserver<
124
161
  return this.#currentResult
125
162
  }
126
163
 
164
+ /**
165
+ * Detaches the observer from the mutation it is currently observing (if
166
+ * any) and resets the observed result back to its default, `idle` state.
167
+ *
168
+ * This does not cancel an in-flight mutation; the mutation itself keeps
169
+ * running to completion and its own callbacks still fire, but this
170
+ * observer stops reflecting its state and a subsequent `mutate()` call
171
+ * will build a brand new mutation.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * observer.reset()
176
+ * ```
177
+ *
178
+ * @see {@link MutationObserver#mutate}
179
+ */
127
180
  reset(): void {
128
181
  // reset needs to remove the observer from the mutation because there is no way to "get it back"
129
182
  // another mutate call will yield a new mutation!
@@ -133,6 +186,24 @@ export class MutationObserver<
133
186
  this.#notify()
134
187
  }
135
188
 
189
+ /**
190
+ * Builds a new `Mutation` in the `MutationCache` using the observer's
191
+ * current options, detaches this observer from any previously observed
192
+ * mutation, attaches it to the new one, and executes it with the given
193
+ * variables.
194
+ *
195
+ * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
196
+ * invoked once the mutation settles, in addition to any callbacks defined
197
+ * on the observer's own options.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * await observer.mutate(
202
+ * { title: 'New post' },
203
+ * { onSuccess: (data) => console.log(data) },
204
+ * )
205
+ * ```
206
+ */
136
207
  mutate(
137
208
  variables: TVariables,
138
209
  options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
@@ -12,6 +12,10 @@ type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
12
12
 
13
13
  type ScheduleFunction = (callback: () => void) => void
14
14
 
15
+ /**
16
+ * Default scheduling function used by the notify manager.
17
+ * Schedules the callback with the system's `setTimeout(callback, 0)`.
18
+ */
15
19
  export const defaultScheduler: ScheduleFunction = systemSetTimeoutZero
16
20
 
17
21
  export function createNotifyManager() {
@@ -49,6 +53,12 @@ export function createNotifyManager() {
49
53
  }
50
54
 
51
55
  return {
56
+ /**
57
+ * Batches all updates scheduled inside the passed callback.
58
+ * This is mainly used internally to optimize query client updating.
59
+ * Batches can be nested; the queue is only flushed once the outermost `batch` call finishes.
60
+ * The return value of `callback` is passed through.
61
+ */
52
62
  batch: <T>(callback: () => T): T => {
53
63
  let result
54
64
  transactions++
@@ -74,6 +84,10 @@ export function createNotifyManager() {
74
84
  })
75
85
  }
76
86
  },
87
+ /**
88
+ * Schedules a function to be run on the next batch.
89
+ * By default, the batch is run with a `setTimeout`, but this can be configured via `setScheduler`.
90
+ */
77
91
  schedule,
78
92
  /**
79
93
  * Use this method to set a custom notify function.
@@ -84,11 +98,38 @@ export function createNotifyManager() {
84
98
  },
85
99
  /**
86
100
  * Use this method to set a custom function to batch notifications together into a single tick.
87
- * By default React Query will use the batch function provided by ReactDOM or React Native.
101
+ * Framework adapters use this to plug in their own batching primitive, so that a single query
102
+ * update only triggers one re-render instead of one per subscriber.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * import { notifyManager } from '@tanstack/query-core'
107
+ * import { batch } from 'solid-js'
108
+ *
109
+ * notifyManager.setBatchNotifyFunction(batch)
110
+ * ```
88
111
  */
89
112
  setBatchNotifyFunction: (fn: BatchNotifyFunction) => {
90
113
  batchNotifyFn = fn
91
114
  },
115
+ /**
116
+ * Configures a custom callback that schedules when the next batch runs.
117
+ * The default behavior is `setTimeout(callback, 0)`.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * import { notifyManager } from '@tanstack/query-core'
122
+ *
123
+ * // Schedule batches in the next microtask
124
+ * notifyManager.setScheduler(queueMicrotask)
125
+ *
126
+ * // Schedule batches before the next frame is rendered
127
+ * notifyManager.setScheduler(requestAnimationFrame)
128
+ *
129
+ * // Schedule batches some time in the future
130
+ * notifyManager.setScheduler((cb) => setTimeout(cb, 10))
131
+ * ```
132
+ */
92
133
  setScheduler: (fn: ScheduleFunction) => {
93
134
  scheduleFn = fn
94
135
  },
@@ -96,4 +137,8 @@ export function createNotifyManager() {
96
137
  }
97
138
 
98
139
  // SINGLETON
140
+
141
+ /**
142
+ * Handles scheduling and batching callbacks in TanStack Query.
143
+ */
99
144
  export const notifyManager = createNotifyManager()
@@ -3,6 +3,15 @@ import { Subscribable } from './subscribable'
3
3
  type Listener = (online: boolean) => void
4
4
  type SetupFn = (setOnline: Listener) => (() => void) | undefined
5
5
 
6
+ /**
7
+ * The `OnlineManager` manages the online state within TanStack Query. It can
8
+ * be used to change the default event listeners or to manually change the
9
+ * online state.
10
+ *
11
+ * By default, the `onlineManager` assumes an active network connection, and
12
+ * listens to the `online` and `offline` events on the `window` object to
13
+ * detect changes.
14
+ */
6
15
  export class OnlineManager extends Subscribable<Listener> {
7
16
  #online = true
8
17
  #cleanup?: () => void
@@ -45,12 +54,44 @@ export class OnlineManager extends Subscribable<Listener> {
45
54
  }
46
55
  }
47
56
 
57
+ /**
58
+ * `setEventListener` can be used to set a custom event listener that will
59
+ * be used to determine the online state. The provided `setup` function
60
+ * receives a `setOnline` callback that should be called with a `boolean`
61
+ * whenever the online state changes.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * import NetInfo from '@react-native-community/netinfo'
66
+ * import { onlineManager } from '@tanstack/query-core'
67
+ *
68
+ * onlineManager.setEventListener((setOnline) => {
69
+ * return NetInfo.addEventListener((state) => {
70
+ * setOnline(!!state.isConnected)
71
+ * })
72
+ * })
73
+ * ```
74
+ */
48
75
  setEventListener(setup: SetupFn): void {
49
76
  this.#setup = setup
50
77
  this.#cleanup?.()
51
78
  this.#cleanup = setup(this.setOnline.bind(this))
52
79
  }
53
80
 
81
+ /**
82
+ * `setOnline` can be used to manually set the online state.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * import { onlineManager } from '@tanstack/query-core'
87
+ *
88
+ * // Set to online
89
+ * onlineManager.setOnline(true)
90
+ *
91
+ * // Set to offline
92
+ * onlineManager.setOnline(false)
93
+ * ```
94
+ */
54
95
  setOnline(online: boolean): void {
55
96
  const changed = this.#online !== online
56
97
 
@@ -62,9 +103,15 @@ export class OnlineManager extends Subscribable<Listener> {
62
103
  }
63
104
  }
64
105
 
106
+ /**
107
+ * `isOnline` can be used to get the current online state.
108
+ */
65
109
  isOnline(): boolean {
66
110
  return this.#online
67
111
  }
68
112
  }
69
113
 
114
+ /**
115
+ * Singleton instance of {@link OnlineManager}, used to manage and observe the online state within TanStack Query.
116
+ */
70
117
  export const onlineManager = new OnlineManager()