@tanstack/query-core 5.104.1 → 5.104.2

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 (359) hide show
  1. package/build/legacy/environmentManager.cjs +3 -1
  2. package/build/legacy/environmentManager.cjs.map +1 -1
  3. package/build/legacy/environmentManager.d.cts +6 -1
  4. package/build/legacy/environmentManager.d.cts.map +1 -1
  5. package/build/legacy/environmentManager.d.ts +6 -1
  6. package/build/legacy/environmentManager.d.ts.map +1 -1
  7. package/build/legacy/environmentManager.js +3 -1
  8. package/build/legacy/environmentManager.js.map +1 -1
  9. package/build/legacy/focusManager.cjs +4 -2
  10. package/build/legacy/focusManager.cjs.map +1 -1
  11. package/build/legacy/focusManager.d.cts +5 -3
  12. package/build/legacy/focusManager.d.cts.map +1 -1
  13. package/build/legacy/focusManager.d.ts +5 -3
  14. package/build/legacy/focusManager.d.ts.map +1 -1
  15. package/build/legacy/focusManager.js +4 -2
  16. package/build/legacy/focusManager.js.map +1 -1
  17. package/build/legacy/{hydration-B9t5w6NC.d.ts → hydration-Dfuxd5FW.d.cts} +1410 -128
  18. package/build/legacy/hydration-Dfuxd5FW.d.cts.map +1 -0
  19. package/build/legacy/{hydration-Br9VIGXa.d.cts → hydration-UAqdtMIf.d.ts} +1410 -128
  20. package/build/legacy/hydration-UAqdtMIf.d.ts.map +1 -0
  21. package/build/legacy/hydration.cjs +35 -0
  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 +35 -0
  26. package/build/legacy/hydration.js.map +1 -1
  27. package/build/legacy/index.d.cts +1 -1
  28. package/build/legacy/index.d.ts +1 -1
  29. package/build/legacy/infiniteQueryBehavior.cjs +29 -0
  30. package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
  31. package/build/legacy/infiniteQueryBehavior.d.cts +16 -1
  32. package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -1
  33. package/build/legacy/infiniteQueryBehavior.d.ts +16 -1
  34. package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -1
  35. package/build/legacy/infiniteQueryBehavior.js +29 -0
  36. package/build/legacy/infiniteQueryBehavior.js.map +1 -1
  37. package/build/legacy/infiniteQueryObserver.cjs +13 -7
  38. package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
  39. package/build/legacy/infiniteQueryObserver.d.cts +15 -9
  40. package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
  41. package/build/legacy/infiniteQueryObserver.d.ts +15 -9
  42. package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
  43. package/build/legacy/infiniteQueryObserver.js +13 -7
  44. package/build/legacy/infiniteQueryObserver.js.map +1 -1
  45. package/build/legacy/mutation.cjs +27 -10
  46. package/build/legacy/mutation.cjs.map +1 -1
  47. package/build/legacy/mutation.d.cts +1 -1
  48. package/build/legacy/mutation.d.ts +1 -1
  49. package/build/legacy/mutation.js +27 -10
  50. package/build/legacy/mutation.js.map +1 -1
  51. package/build/legacy/mutationCache.cjs +53 -12
  52. package/build/legacy/mutationCache.cjs.map +1 -1
  53. package/build/legacy/mutationCache.d.cts +1 -1
  54. package/build/legacy/mutationCache.d.ts +1 -1
  55. package/build/legacy/mutationCache.js +53 -12
  56. package/build/legacy/mutationCache.js.map +1 -1
  57. package/build/legacy/mutationObserver.cjs +27 -16
  58. package/build/legacy/mutationObserver.cjs.map +1 -1
  59. package/build/legacy/mutationObserver.d.cts +1 -1
  60. package/build/legacy/mutationObserver.d.ts +1 -1
  61. package/build/legacy/mutationObserver.js +27 -16
  62. package/build/legacy/mutationObserver.js.map +1 -1
  63. package/build/legacy/notifyManager.cjs +12 -2
  64. package/build/legacy/notifyManager.cjs.map +1 -1
  65. package/build/legacy/notifyManager.d.cts +19 -4
  66. package/build/legacy/notifyManager.d.cts.map +1 -1
  67. package/build/legacy/notifyManager.d.ts +19 -4
  68. package/build/legacy/notifyManager.d.ts.map +1 -1
  69. package/build/legacy/notifyManager.js +12 -2
  70. package/build/legacy/notifyManager.js.map +1 -1
  71. package/build/legacy/onlineManager.cjs +4 -2
  72. package/build/legacy/onlineManager.cjs.map +1 -1
  73. package/build/legacy/onlineManager.d.cts +5 -3
  74. package/build/legacy/onlineManager.d.cts.map +1 -1
  75. package/build/legacy/onlineManager.d.ts +5 -3
  76. package/build/legacy/onlineManager.d.ts.map +1 -1
  77. package/build/legacy/onlineManager.js +4 -2
  78. package/build/legacy/onlineManager.js.map +1 -1
  79. package/build/legacy/queriesObserver.cjs +15 -3
  80. package/build/legacy/queriesObserver.cjs.map +1 -1
  81. package/build/legacy/queriesObserver.d.cts +14 -5
  82. package/build/legacy/queriesObserver.d.cts.map +1 -1
  83. package/build/legacy/queriesObserver.d.ts +14 -5
  84. package/build/legacy/queriesObserver.d.ts.map +1 -1
  85. package/build/legacy/queriesObserver.js +15 -3
  86. package/build/legacy/queriesObserver.js.map +1 -1
  87. package/build/legacy/query.cjs +89 -15
  88. package/build/legacy/query.cjs.map +1 -1
  89. package/build/legacy/query.d.cts +1 -1
  90. package/build/legacy/query.d.ts +1 -1
  91. package/build/legacy/query.js +89 -15
  92. package/build/legacy/query.js.map +1 -1
  93. package/build/legacy/queryCache.cjs +34 -12
  94. package/build/legacy/queryCache.cjs.map +1 -1
  95. package/build/legacy/queryCache.d.cts +1 -1
  96. package/build/legacy/queryCache.d.ts +1 -1
  97. package/build/legacy/queryCache.js +34 -12
  98. package/build/legacy/queryCache.js.map +1 -1
  99. package/build/legacy/queryClient.cjs +104 -32
  100. package/build/legacy/queryClient.cjs.map +1 -1
  101. package/build/legacy/queryClient.d.cts +1 -1
  102. package/build/legacy/queryClient.d.ts +1 -1
  103. package/build/legacy/queryClient.js +104 -32
  104. package/build/legacy/queryClient.js.map +1 -1
  105. package/build/legacy/queryObserver.cjs +78 -6
  106. package/build/legacy/queryObserver.cjs.map +1 -1
  107. package/build/legacy/queryObserver.d.cts +1 -1
  108. package/build/legacy/queryObserver.d.ts +1 -1
  109. package/build/legacy/queryObserver.js +78 -6
  110. package/build/legacy/queryObserver.js.map +1 -1
  111. package/build/{modern/removable-0GzhW1PK.d.cts → legacy/removable-Cw9GNGyK.d.cts} +17 -1
  112. package/build/legacy/removable-Cw9GNGyK.d.cts.map +1 -0
  113. package/build/legacy/{removable-0GzhW1PK.d.cts → removable-Cw9GNGyK.d.ts} +17 -1
  114. package/build/legacy/removable-Cw9GNGyK.d.ts.map +1 -0
  115. package/build/legacy/removable.cjs +12 -0
  116. package/build/legacy/removable.cjs.map +1 -1
  117. package/build/legacy/removable.d.cts +1 -1
  118. package/build/legacy/removable.d.ts +1 -1
  119. package/build/legacy/removable.js +12 -0
  120. package/build/legacy/removable.js.map +1 -1
  121. package/build/legacy/retryer.cjs +26 -4
  122. package/build/legacy/retryer.cjs.map +1 -1
  123. package/build/legacy/retryer.d.cts +1 -1
  124. package/build/legacy/retryer.d.ts +1 -1
  125. package/build/legacy/retryer.js +26 -4
  126. package/build/legacy/retryer.js.map +1 -1
  127. package/build/legacy/streamedQuery.cjs +3 -1
  128. package/build/legacy/streamedQuery.cjs.map +1 -1
  129. package/build/legacy/streamedQuery.d.cts +4 -2
  130. package/build/legacy/streamedQuery.d.cts.map +1 -1
  131. package/build/legacy/streamedQuery.d.ts +4 -2
  132. package/build/legacy/streamedQuery.d.ts.map +1 -1
  133. package/build/legacy/streamedQuery.js +3 -1
  134. package/build/legacy/streamedQuery.js.map +1 -1
  135. package/build/{modern/subscribable-CysQ0tne.d.cts → legacy/subscribable-czlHOxMP.d.cts} +11 -1
  136. package/build/legacy/subscribable-czlHOxMP.d.cts.map +1 -0
  137. package/build/legacy/{subscribable-CysQ0tne.d.cts → subscribable-czlHOxMP.d.ts} +11 -1
  138. package/build/legacy/subscribable-czlHOxMP.d.ts.map +1 -0
  139. package/build/legacy/subscribable.cjs +10 -0
  140. package/build/legacy/subscribable.cjs.map +1 -1
  141. package/build/legacy/subscribable.d.cts +1 -1
  142. package/build/legacy/subscribable.d.ts +1 -1
  143. package/build/legacy/subscribable.js +10 -0
  144. package/build/legacy/subscribable.js.map +1 -1
  145. package/build/legacy/timeoutManager.cjs +11 -6
  146. package/build/legacy/timeoutManager.cjs.map +1 -1
  147. package/build/legacy/timeoutManager.d.cts +23 -6
  148. package/build/legacy/timeoutManager.d.cts.map +1 -1
  149. package/build/legacy/timeoutManager.d.ts +23 -6
  150. package/build/legacy/timeoutManager.d.ts.map +1 -1
  151. package/build/legacy/timeoutManager.js +11 -6
  152. package/build/legacy/timeoutManager.js.map +1 -1
  153. package/build/legacy/types.cjs.map +1 -1
  154. package/build/legacy/types.d.cts +1 -1
  155. package/build/legacy/types.d.ts +1 -1
  156. package/build/legacy/types.js.map +1 -1
  157. package/build/legacy/utils.cjs +117 -8
  158. package/build/legacy/utils.cjs.map +1 -1
  159. package/build/legacy/utils.d.cts +1 -1
  160. package/build/legacy/utils.d.ts +1 -1
  161. package/build/legacy/utils.js +117 -8
  162. package/build/legacy/utils.js.map +1 -1
  163. package/build/modern/environmentManager.cjs +3 -1
  164. package/build/modern/environmentManager.cjs.map +1 -1
  165. package/build/modern/environmentManager.d.cts +6 -1
  166. package/build/modern/environmentManager.d.cts.map +1 -1
  167. package/build/modern/environmentManager.d.ts +6 -1
  168. package/build/modern/environmentManager.d.ts.map +1 -1
  169. package/build/modern/environmentManager.js +3 -1
  170. package/build/modern/environmentManager.js.map +1 -1
  171. package/build/modern/focusManager.cjs +4 -2
  172. package/build/modern/focusManager.cjs.map +1 -1
  173. package/build/modern/focusManager.d.cts +5 -3
  174. package/build/modern/focusManager.d.cts.map +1 -1
  175. package/build/modern/focusManager.d.ts +5 -3
  176. package/build/modern/focusManager.d.ts.map +1 -1
  177. package/build/modern/focusManager.js +4 -2
  178. package/build/modern/focusManager.js.map +1 -1
  179. package/build/modern/{hydration-B9t5w6NC.d.ts → hydration-Dfuxd5FW.d.cts} +1410 -128
  180. package/build/modern/hydration-Dfuxd5FW.d.cts.map +1 -0
  181. package/build/modern/{hydration-Br9VIGXa.d.cts → hydration-UAqdtMIf.d.ts} +1410 -128
  182. package/build/modern/hydration-UAqdtMIf.d.ts.map +1 -0
  183. package/build/modern/hydration.cjs +35 -0
  184. package/build/modern/hydration.cjs.map +1 -1
  185. package/build/modern/hydration.d.cts +1 -1
  186. package/build/modern/hydration.d.ts +1 -1
  187. package/build/modern/hydration.js +35 -0
  188. package/build/modern/hydration.js.map +1 -1
  189. package/build/modern/index.d.cts +1 -1
  190. package/build/modern/index.d.ts +1 -1
  191. package/build/modern/infiniteQueryBehavior.cjs +29 -0
  192. package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
  193. package/build/modern/infiniteQueryBehavior.d.cts +16 -1
  194. package/build/modern/infiniteQueryBehavior.d.cts.map +1 -1
  195. package/build/modern/infiniteQueryBehavior.d.ts +16 -1
  196. package/build/modern/infiniteQueryBehavior.d.ts.map +1 -1
  197. package/build/modern/infiniteQueryBehavior.js +29 -0
  198. package/build/modern/infiniteQueryBehavior.js.map +1 -1
  199. package/build/modern/infiniteQueryObserver.cjs +13 -7
  200. package/build/modern/infiniteQueryObserver.cjs.map +1 -1
  201. package/build/modern/infiniteQueryObserver.d.cts +15 -9
  202. package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
  203. package/build/modern/infiniteQueryObserver.d.ts +15 -9
  204. package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
  205. package/build/modern/infiniteQueryObserver.js +13 -7
  206. package/build/modern/infiniteQueryObserver.js.map +1 -1
  207. package/build/modern/mutation.cjs +27 -10
  208. package/build/modern/mutation.cjs.map +1 -1
  209. package/build/modern/mutation.d.cts +1 -1
  210. package/build/modern/mutation.d.ts +1 -1
  211. package/build/modern/mutation.js +27 -10
  212. package/build/modern/mutation.js.map +1 -1
  213. package/build/modern/mutationCache.cjs +53 -12
  214. package/build/modern/mutationCache.cjs.map +1 -1
  215. package/build/modern/mutationCache.d.cts +1 -1
  216. package/build/modern/mutationCache.d.ts +1 -1
  217. package/build/modern/mutationCache.js +53 -12
  218. package/build/modern/mutationCache.js.map +1 -1
  219. package/build/modern/mutationObserver.cjs +23 -12
  220. package/build/modern/mutationObserver.cjs.map +1 -1
  221. package/build/modern/mutationObserver.d.cts +1 -1
  222. package/build/modern/mutationObserver.d.ts +1 -1
  223. package/build/modern/mutationObserver.js +23 -12
  224. package/build/modern/mutationObserver.js.map +1 -1
  225. package/build/modern/notifyManager.cjs +12 -2
  226. package/build/modern/notifyManager.cjs.map +1 -1
  227. package/build/modern/notifyManager.d.cts +19 -4
  228. package/build/modern/notifyManager.d.cts.map +1 -1
  229. package/build/modern/notifyManager.d.ts +19 -4
  230. package/build/modern/notifyManager.d.ts.map +1 -1
  231. package/build/modern/notifyManager.js +12 -2
  232. package/build/modern/notifyManager.js.map +1 -1
  233. package/build/modern/onlineManager.cjs +4 -2
  234. package/build/modern/onlineManager.cjs.map +1 -1
  235. package/build/modern/onlineManager.d.cts +5 -3
  236. package/build/modern/onlineManager.d.cts.map +1 -1
  237. package/build/modern/onlineManager.d.ts +5 -3
  238. package/build/modern/onlineManager.d.ts.map +1 -1
  239. package/build/modern/onlineManager.js +4 -2
  240. package/build/modern/onlineManager.js.map +1 -1
  241. package/build/modern/queriesObserver.cjs +15 -3
  242. package/build/modern/queriesObserver.cjs.map +1 -1
  243. package/build/modern/queriesObserver.d.cts +14 -5
  244. package/build/modern/queriesObserver.d.cts.map +1 -1
  245. package/build/modern/queriesObserver.d.ts +14 -5
  246. package/build/modern/queriesObserver.d.ts.map +1 -1
  247. package/build/modern/queriesObserver.js +15 -3
  248. package/build/modern/queriesObserver.js.map +1 -1
  249. package/build/modern/query.cjs +89 -15
  250. package/build/modern/query.cjs.map +1 -1
  251. package/build/modern/query.d.cts +1 -1
  252. package/build/modern/query.d.ts +1 -1
  253. package/build/modern/query.js +89 -15
  254. package/build/modern/query.js.map +1 -1
  255. package/build/modern/queryCache.cjs +34 -12
  256. package/build/modern/queryCache.cjs.map +1 -1
  257. package/build/modern/queryCache.d.cts +1 -1
  258. package/build/modern/queryCache.d.ts +1 -1
  259. package/build/modern/queryCache.js +34 -12
  260. package/build/modern/queryCache.js.map +1 -1
  261. package/build/modern/queryClient.cjs +104 -32
  262. package/build/modern/queryClient.cjs.map +1 -1
  263. package/build/modern/queryClient.d.cts +1 -1
  264. package/build/modern/queryClient.d.ts +1 -1
  265. package/build/modern/queryClient.js +104 -32
  266. package/build/modern/queryClient.js.map +1 -1
  267. package/build/modern/queryObserver.cjs +78 -6
  268. package/build/modern/queryObserver.cjs.map +1 -1
  269. package/build/modern/queryObserver.d.cts +1 -1
  270. package/build/modern/queryObserver.d.ts +1 -1
  271. package/build/modern/queryObserver.js +78 -6
  272. package/build/modern/queryObserver.js.map +1 -1
  273. package/build/modern/{removable-0GzhW1PK.d.ts → removable-Cw9GNGyK.d.cts} +17 -1
  274. package/build/modern/removable-Cw9GNGyK.d.cts.map +1 -0
  275. package/build/{legacy/removable-0GzhW1PK.d.ts → modern/removable-Cw9GNGyK.d.ts} +17 -1
  276. package/build/modern/removable-Cw9GNGyK.d.ts.map +1 -0
  277. package/build/modern/removable.cjs +12 -0
  278. package/build/modern/removable.cjs.map +1 -1
  279. package/build/modern/removable.d.cts +1 -1
  280. package/build/modern/removable.d.ts +1 -1
  281. package/build/modern/removable.js +12 -0
  282. package/build/modern/removable.js.map +1 -1
  283. package/build/modern/retryer.cjs +26 -4
  284. package/build/modern/retryer.cjs.map +1 -1
  285. package/build/modern/retryer.d.cts +1 -1
  286. package/build/modern/retryer.d.ts +1 -1
  287. package/build/modern/retryer.js +26 -4
  288. package/build/modern/retryer.js.map +1 -1
  289. package/build/modern/streamedQuery.cjs +3 -1
  290. package/build/modern/streamedQuery.cjs.map +1 -1
  291. package/build/modern/streamedQuery.d.cts +4 -2
  292. package/build/modern/streamedQuery.d.cts.map +1 -1
  293. package/build/modern/streamedQuery.d.ts +4 -2
  294. package/build/modern/streamedQuery.d.ts.map +1 -1
  295. package/build/modern/streamedQuery.js +3 -1
  296. package/build/modern/streamedQuery.js.map +1 -1
  297. package/build/modern/{subscribable-CysQ0tne.d.ts → subscribable-czlHOxMP.d.cts} +11 -1
  298. package/build/modern/subscribable-czlHOxMP.d.cts.map +1 -0
  299. package/build/{legacy/subscribable-CysQ0tne.d.ts → modern/subscribable-czlHOxMP.d.ts} +11 -1
  300. package/build/modern/subscribable-czlHOxMP.d.ts.map +1 -0
  301. package/build/modern/subscribable.cjs +10 -0
  302. package/build/modern/subscribable.cjs.map +1 -1
  303. package/build/modern/subscribable.d.cts +1 -1
  304. package/build/modern/subscribable.d.ts +1 -1
  305. package/build/modern/subscribable.js +10 -0
  306. package/build/modern/subscribable.js.map +1 -1
  307. package/build/modern/timeoutManager.cjs +11 -6
  308. package/build/modern/timeoutManager.cjs.map +1 -1
  309. package/build/modern/timeoutManager.d.cts +23 -6
  310. package/build/modern/timeoutManager.d.cts.map +1 -1
  311. package/build/modern/timeoutManager.d.ts +23 -6
  312. package/build/modern/timeoutManager.d.ts.map +1 -1
  313. package/build/modern/timeoutManager.js +11 -6
  314. package/build/modern/timeoutManager.js.map +1 -1
  315. package/build/modern/types.cjs.map +1 -1
  316. package/build/modern/types.d.cts +1 -1
  317. package/build/modern/types.d.ts +1 -1
  318. package/build/modern/types.js.map +1 -1
  319. package/build/modern/utils.cjs +117 -8
  320. package/build/modern/utils.cjs.map +1 -1
  321. package/build/modern/utils.d.cts +1 -1
  322. package/build/modern/utils.d.ts +1 -1
  323. package/build/modern/utils.js +117 -8
  324. package/build/modern/utils.js.map +1 -1
  325. package/package.json +3 -3
  326. package/src/environmentManager.ts +6 -1
  327. package/src/focusManager.ts +8 -6
  328. package/src/hydration.ts +42 -1
  329. package/src/infiniteQueryBehavior.ts +29 -0
  330. package/src/infiniteQueryObserver.ts +20 -14
  331. package/src/mutation.ts +32 -11
  332. package/src/mutationCache.ts +53 -12
  333. package/src/mutationObserver.ts +29 -14
  334. package/src/notifyManager.ts +12 -2
  335. package/src/onlineManager.ts +7 -5
  336. package/src/queriesObserver.ts +20 -5
  337. package/src/query.ts +153 -20
  338. package/src/queryCache.ts +54 -13
  339. package/src/queryClient.ts +110 -38
  340. package/src/queryObserver.ts +88 -12
  341. package/src/removable.ts +16 -0
  342. package/src/retryer.ts +57 -4
  343. package/src/streamedQuery.ts +4 -3
  344. package/src/subscribable.ts +10 -0
  345. package/src/timeoutManager.ts +23 -6
  346. package/src/types.ts +801 -27
  347. package/src/utils.ts +130 -10
  348. package/build/legacy/hydration-B9t5w6NC.d.ts.map +0 -1
  349. package/build/legacy/hydration-Br9VIGXa.d.cts.map +0 -1
  350. package/build/legacy/removable-0GzhW1PK.d.cts.map +0 -1
  351. package/build/legacy/removable-0GzhW1PK.d.ts.map +0 -1
  352. package/build/legacy/subscribable-CysQ0tne.d.cts.map +0 -1
  353. package/build/legacy/subscribable-CysQ0tne.d.ts.map +0 -1
  354. package/build/modern/hydration-B9t5w6NC.d.ts.map +0 -1
  355. package/build/modern/hydration-Br9VIGXa.d.cts.map +0 -1
  356. package/build/modern/removable-0GzhW1PK.d.cts.map +0 -1
  357. package/build/modern/removable-0GzhW1PK.d.ts.map +0 -1
  358. package/build/modern/subscribable-CysQ0tne.d.cts.map +0 -1
  359. package/build/modern/subscribable-CysQ0tne.d.ts.map +0 -1
@@ -9,7 +9,6 @@ import { Mutation } from "./mutation.js";
9
9
  * Normally, you will not interact with the `MutationCache` directly and instead use a
10
10
  * `QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of
11
11
  * safe/known updates to the cache, such as mutations being added, removed, or updated.
12
- *
13
12
  * @example
14
13
  * ```ts
15
14
  * const unsubscribe = mutationCache.subscribe((event) => {
@@ -28,7 +27,15 @@ var MutationCache = class extends Subscribable {
28
27
  this.#scopes = /* @__PURE__ */ new Map();
29
28
  this.#mutationId = 0;
30
29
  }
31
- /** @internal */
30
+ /**
31
+ * Creates a mutation and adds it to the cache.
32
+ * @internal
33
+ * @param client - The client the mutation belongs to, used to default its options.
34
+ * @param options - The mutation options. They are defaulted with {@link
35
+ * QueryClient#defaultMutationOptions}.
36
+ * @param state - The initial state of the mutation, e.g. when restoring a dehydrated mutation.
37
+ * @returns The new mutation, already added to the cache.
38
+ */
32
39
  build(client, options, state) {
33
40
  const mutation = new Mutation({
34
41
  client,
@@ -40,7 +47,11 @@ var MutationCache = class extends Subscribable {
40
47
  this.add(mutation);
41
48
  return mutation;
42
49
  }
43
- /** @internal */
50
+ /**
51
+ * Adds a mutation to the cache.
52
+ * @internal
53
+ * @param mutation - The mutation to add. If it has a `scope`, it is queued in that scope.
54
+ */
44
55
  add(mutation) {
45
56
  this.#mutations.add(mutation);
46
57
  const scope = scopeFor(mutation);
@@ -54,7 +65,11 @@ var MutationCache = class extends Subscribable {
54
65
  mutation
55
66
  });
56
67
  }
57
- /** @internal */
68
+ /**
69
+ * Removes a mutation from the cache.
70
+ * @internal
71
+ * @param mutation - The mutation to remove, also from its scope's queue.
72
+ */
58
73
  remove(mutation) {
59
74
  if (this.#mutations.delete(mutation)) {
60
75
  const scope = scopeFor(mutation);
@@ -73,7 +88,13 @@ var MutationCache = class extends Subscribable {
73
88
  mutation
74
89
  });
75
90
  }
76
- /** @internal */
91
+ /**
92
+ * Checks whether a mutation can run now, given the other mutations in its `scope`.
93
+ * @internal
94
+ * @param mutation - The mutation to check.
95
+ * @returns `true` if the mutation has no `scope`, or no other mutation in its scope is pending
96
+ * ahead of it.
97
+ */
77
98
  canRun(mutation) {
78
99
  const scope = scopeFor(mutation);
79
100
  if (typeof scope === "string") {
@@ -81,7 +102,13 @@ var MutationCache = class extends Subscribable {
81
102
  return !firstPendingMutation || firstPendingMutation === mutation;
82
103
  } else return true;
83
104
  }
84
- /** @internal */
105
+ /**
106
+ * Continues the next paused mutation in the same `scope` after a mutation finishes.
107
+ * @internal
108
+ * @param mutation - The mutation that finished.
109
+ * @returns A promise that resolves once the next paused mutation in the same scope has continued,
110
+ * or immediately if there is none.
111
+ */
85
112
  runNext(mutation) {
86
113
  const scope = scopeFor(mutation);
87
114
  if (typeof scope === "string") return (this.#scopes.get(scope)?.find((m) => m !== mutation && m.state.isPaused))?.continue() ?? Promise.resolve();
@@ -89,7 +116,6 @@ var MutationCache = class extends Subscribable {
89
116
  }
90
117
  /**
91
118
  * Removes all mutations from the cache.
92
- *
93
119
  * @example
94
120
  * ```ts
95
121
  * const mutationCache = queryClient.getMutationCache()
@@ -114,7 +140,7 @@ var MutationCache = class extends Subscribable {
114
140
  *
115
141
  * This is not typically needed for most applications, but can come in handy when needing more
116
142
  * information about a mutation in rare scenarios.
117
- *
143
+ * @returns Every mutation in the cache.
118
144
  * @example
119
145
  * ```ts
120
146
  * const mutationCache = queryClient.getMutationCache()
@@ -131,7 +157,8 @@ var MutationCache = class extends Subscribable {
131
157
  *
132
158
  * This is not typically needed for most applications, but can come in handy when needing more
133
159
  * information about a mutation in rare scenarios.
134
- *
160
+ * @param filters - The filters to match. `exact` defaults to `true`.
161
+ * @returns The first matching mutation, or `undefined`.
135
162
  * @see {@link MutationCache#findAll}
136
163
  * @example
137
164
  * ```ts
@@ -153,7 +180,8 @@ var MutationCache = class extends Subscribable {
153
180
  *
154
181
  * This is not typically needed for most applications, but can come in handy when needing more
155
182
  * information about mutations in rare scenarios.
156
- *
183
+ * @param filters - The filters to match. Without filters, every mutation is returned.
184
+ * @returns The matching mutations.
157
185
  * @see {@link MutationCache#find}
158
186
  * @example
159
187
  * ```ts
@@ -165,7 +193,11 @@ var MutationCache = class extends Subscribable {
165
193
  findAll(filters = {}) {
166
194
  return this.getAll().filter((mutation) => matchMutation(filters, mutation));
167
195
  }
168
- /** @internal */
196
+ /**
197
+ * Notifies every cache listener of an event, in one batch.
198
+ * @internal
199
+ * @param event - The event passed to every listener.
200
+ */
169
201
  notify(event) {
170
202
  notifyManager.batch(() => {
171
203
  this.listeners.forEach((listener) => {
@@ -173,12 +205,21 @@ var MutationCache = class extends Subscribable {
173
205
  });
174
206
  });
175
207
  }
176
- /** @internal */
208
+ /**
209
+ * Continues every paused mutation, e.g. when the app comes back online.
210
+ * @internal
211
+ * @returns A promise that resolves once every paused mutation has continued and settled.
212
+ */
177
213
  resumePausedMutations() {
178
214
  const pausedMutations = this.getAll().filter((x) => x.state.isPaused);
179
215
  return notifyManager.batch(() => Promise.all(pausedMutations.map((mutation) => mutation.continue().catch(noop))));
180
216
  }
181
217
  };
218
+ /**
219
+ * Returns the scope id of a mutation, used to run mutations with the same scope one after another.
220
+ * @param mutation - The mutation to read the scope of.
221
+ * @returns The mutation's `scope.id`, or `undefined` if it has no scope.
222
+ */
182
223
  function scopeFor(mutation) {
183
224
  return mutation.options.scope?.id;
184
225
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationCache.js","names":[],"sources":["../../src/mutationCache.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Mutation } from './mutation'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\nimport type { MutationObserver } from './mutationObserver'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationOptions,\n NotifyEvent,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Action, MutationState } from './mutation'\nimport type { MutationFilters } from './utils'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every mutation handled by a `MutationCache`, regardless of which\n * component or observer triggered it. They differ from the `defaultOptions` provided to a\n * `QueryClient` in two ways: `defaultOptions` can be overridden by each mutation, while these\n * callbacks are always called, and `onMutate` here does not allow returning a result.\n *\n * If a callback returns a promise, it will be awaited before the mutation continues.\n */\nexport interface MutationCacheConfig {\n /** Called when any mutation in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called when any mutation in the cache is successful. */\n onSuccess?: (\n data: unknown,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called before any mutation in the cache executes. */\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called when any mutation in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\n/**\n * The event passed to a `MutationCache` subscriber. Fired whenever a mutation is added or removed\n * from the cache, its state is updated, or one of its observers is added, removed, or has its\n * options updated.\n */\nexport type MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\n/**\n * The `MutationCache` is the storage for mutations.\n *\n * Normally, you will not interact with the `MutationCache` directly and instead use a\n * `QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as mutations being added, removed, or updated.\n *\n * @example\n * ```ts\n * const unsubscribe = mutationCache.subscribe((event) => {\n * console.log(event.type, event.mutation)\n * })\n * ```\n */\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\n this.#mutationId = 0\n }\n\n /** @internal */\n build<TData, TError, TVariables, TOnMutateResult>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> {\n const mutation = new Mutation({\n client,\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n /** @internal */\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n /** @internal */\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n /** @internal */\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n /** @internal */\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n /**\n * Removes all mutations from the cache.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * mutationCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n /**\n * Returns all mutations within the cache.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a mutation in rare scenarios.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutations = mutationCache.getAll()\n * ```\n */\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing mutation instance from\n * the cache. If the mutation does not exist, `undefined` is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a mutation in rare scenarios.\n *\n * @see {@link MutationCache#findAll}\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TOnMutateResult = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TOnMutateResult> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing mutation instances from the\n * cache that match the given filters. If no mutations 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 mutations in rare scenarios.\n *\n * @see {@link MutationCache#find}\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })\n * ```\n */\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().filter((mutation) => matchMutation(filters, mutation))\n }\n\n /** @internal */\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /** @internal */\n resumePausedMutations(): Promise<unknown> {\n const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA2HA,IAAa,gBAAb,cAAmC,aAAoC;CACrE;CACA;CACA;CAEA,YAAY,SAAqC,CAAC,GAAG;EACnD,MAAM;EADW,KAAA,SAAA;EAEjB,KAAK,6BAAa,IAAI,IAAI;EAC1B,KAAK,0BAAU,IAAI,IAAI;EACvB,KAAK,cAAc;CACrB;;CAGA,MACE,QACA,SACA,OACsD;EACtD,MAAM,WAAW,IAAI,SAAS;GAC5B;GACA,eAAe;GACf,YAAY,EAAE,KAAK;GACnB,SAAS,OAAO,uBAAuB,OAAO;GAC9C;EACF,CAAC;EAED,KAAK,IAAI,QAAQ;EAEjB,OAAO;CACT;;CAGA,IAAI,UAA8C;EAChD,KAAK,WAAW,IAAI,QAAQ;EAC5B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,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,KAAK,OAAO;KAAE,MAAM;KAAW;IAAS,CAAC;GAC3C,CAAC;GACD,KAAK,WAAW,MAAM;GACtB,KAAK,QAAQ,MAAM;EACrB,CAAC;CACH;;;;;;;;;;;;;;CAeA,SAA0B;EACxB,OAAO,MAAM,KAAK,KAAK,UAAU;CACnC;;;;;;;;;;;;;;;;CAiBA,KAME,SACkE;EAClE,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,aACzB,cAAc,kBAAkB,QAAQ,CAC1C;CACF;;;;;;;;;;;;;;;;CAiBA,QAAQ,UAA2B,CAAC,GAAoB;EACtD,OAAO,KAAK,OAAO,CAAC,CAAC,QAAQ,aAAa,cAAc,SAAS,QAAQ,CAAC;CAC5E;;CAGA,OAAO,OAAiC;EACtC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,wBAA0C;EACxC,MAAM,kBAAkB,KAAK,OAAO,CAAC,CAAC,QAAQ,MAAM,EAAE,MAAM,QAAQ;EAEpE,OAAO,cAAc,YACnB,QAAQ,IACN,gBAAgB,KAAK,aAAa,SAAS,SAAS,CAAC,CAAC,MAAM,IAAI,CAAC,CACnE,CACF;CACF;AACF;AAEA,SAAS,SAAS,UAAwC;CACxD,OAAO,SAAS,QAAQ,OAAO;AACjC"}
1
+ {"version":3,"file":"mutationCache.js","names":[],"sources":["../../src/mutationCache.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Mutation } from './mutation'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\nimport type { MutationObserver } from './mutationObserver'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationOptions,\n NotifyEvent,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Action, MutationState } from './mutation'\nimport type { MutationFilters } from './utils'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every mutation handled by a `MutationCache`, regardless of which\n * component or observer triggered it. They differ from the `defaultOptions` provided to a\n * `QueryClient` in two ways: `defaultOptions` can be overridden by each mutation, while these\n * callbacks are always called, and `onMutate` here does not allow returning a result.\n *\n * If a callback returns a promise, it will be awaited before the mutation continues.\n */\nexport interface MutationCacheConfig {\n /** Called when any mutation in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called when any mutation in the cache is successful. */\n onSuccess?: (\n data: unknown,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called before any mutation in the cache executes. */\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n /** Called when any mutation in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n variables: unknown,\n onMutateResult: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n context: MutationFunctionContext,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\n/**\n * The event passed to a `MutationCache` subscriber. Fired whenever a mutation is added or removed\n * from the cache, its state is updated, or one of its observers is added, removed, or has its\n * options updated.\n */\nexport type MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\n/**\n * The `MutationCache` is the storage for mutations.\n *\n * Normally, you will not interact with the `MutationCache` directly and instead use a\n * `QueryClient`. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as mutations being added, removed, or updated.\n * @example\n * ```ts\n * const unsubscribe = mutationCache.subscribe((event) => {\n * console.log(event.type, event.mutation)\n * })\n * ```\n */\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\n this.#mutationId = 0\n }\n\n /**\n * Creates a mutation and adds it to the cache.\n * @internal\n * @param client - The client the mutation belongs to, used to default its options.\n * @param options - The mutation options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions}.\n * @param state - The initial state of the mutation, e.g. when restoring a dehydrated mutation.\n * @returns The new mutation, already added to the cache.\n */\n build<TData, TError, TVariables, TOnMutateResult>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> {\n const mutation = new Mutation({\n client,\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n /**\n * Adds a mutation to the cache.\n * @internal\n * @param mutation - The mutation to add. If it has a `scope`, it is queued in that scope.\n */\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n /**\n * Removes a mutation from the cache.\n * @internal\n * @param mutation - The mutation to remove, also from its scope's queue.\n */\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n /**\n * Checks whether a mutation can run now, given the other mutations in its `scope`.\n * @internal\n * @param mutation - The mutation to check.\n * @returns `true` if the mutation has no `scope`, or no other mutation in its scope is pending\n * ahead of it.\n */\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n /**\n * Continues the next paused mutation in the same `scope` after a mutation finishes.\n * @internal\n * @param mutation - The mutation that finished.\n * @returns A promise that resolves once the next paused mutation in the same scope has continued,\n * or immediately if there is none.\n */\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n /**\n * Removes all mutations from the cache.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * mutationCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n /**\n * Returns all mutations within the cache.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a mutation in rare scenarios.\n * @returns Every mutation in the cache.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutations = mutationCache.getAll()\n * ```\n */\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing mutation instance from\n * the cache. If the mutation does not exist, `undefined` is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a mutation in rare scenarios.\n * @param filters - The filters to match. `exact` defaults to `true`.\n * @returns The first matching mutation, or `undefined`.\n * @see {@link MutationCache#findAll}\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TOnMutateResult = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TOnMutateResult> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TOnMutateResult> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing mutation instances from the\n * cache that match the given filters. If no mutations 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 mutations in rare scenarios.\n * @param filters - The filters to match. Without filters, every mutation is returned.\n * @returns The matching mutations.\n * @see {@link MutationCache#find}\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })\n * ```\n */\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().filter((mutation) => matchMutation(filters, mutation))\n }\n\n /**\n * Notifies every cache listener of an event, in one batch.\n * @internal\n * @param event - The event passed to every listener.\n */\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /**\n * Continues every paused mutation, e.g. when the app comes back online.\n * @internal\n * @returns A promise that resolves once every paused mutation has continued and settled.\n */\n resumePausedMutations(): Promise<unknown> {\n const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\n/**\n * Returns the scope id of a mutation, used to run mutations with the same scope one after another.\n * @param mutation - The mutation to read the scope of.\n * @returns The mutation's `scope.id`, or `undefined` if it has no scope.\n */\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0HA,IAAa,gBAAb,cAAmC,aAAoC;CACrE;CACA;CACA;CAEA,YAAY,SAAqC,CAAC,GAAG;EACnD,MAAM;EADW,KAAA,SAAA;EAEjB,KAAK,6BAAa,IAAI,IAAI;EAC1B,KAAK,0BAAU,IAAI,IAAI;EACvB,KAAK,cAAc;CACrB;;;;;;;;;;CAWA,MACE,QACA,SACA,OACsD;EACtD,MAAM,WAAW,IAAI,SAAS;GAC5B;GACA,eAAe;GACf,YAAY,EAA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KAAK,QACxB,IAAI,KAAK,CAAC,EACT,MAAM,MAAM,MAAM,YAAY,EAAE,MAAM,QAAQ,EAAA,EAE5B,SAAS,KAAK,QAAQ,QAAQ;OAEpD,OAAO,QAAQ,QAAQ;CAE3B;;;;;;;;;;CAWA,QAAc;EACZ,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,KAAK,OAAO;KAAE,MAAM;KAAW;IAAS,CAAC;GAC3C,CAAC;GACD,KAAK,WAAW,MAAM;GACtB,KAAK,QAAQ,MAAM;EACrB,CAAC;CACH;;;;;;;;;;;;;;CAeA,SAA0B;EACxB,OAAO,MAAM,KAAK,KAAK,UAAU;CACnC;;;;;;;;;;;;;;;;;CAkBA,KAME,SACkE;EAClE,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,aACzB,cAAc,kBAAkB,QAAQ,CAC1C;CACF;;;;;;;;;;;;;;;;;CAkBA,QAAQ,UAA2B,CAAC,GAAoB;EACtD,OAAO,KAAK,OAAO,CAAC,CAAC,QAAQ,aAAa,cAAc,SAAS,QAAQ,CAAC;CAC5E;;;;;;CAOA,OAAO,OAAiC;EACtC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;;;;;CAOA,wBAA0C;EACxC,MAAM,kBAAkB,KAAK,OAAO,CAAC,CAAC,QAAQ,MAAM,EAAE,MAAM,QAAQ;EAEpE,OAAO,cAAc,YACnB,QAAQ,IACN,gBAAgB,KAAK,aAAa,SAAS,SAAS,CAAC,CAAC,MAAM,IAAI,CAAC,CACnE,CACF;CACF;AACF;;;;;;AAOA,SAAS,SAAS,UAAwC;CACxD,OAAO,SAAS,QAAQ,OAAO;AACjC"}
@@ -11,7 +11,6 @@ const require_mutation = require("./mutation.cjs");
11
11
  * passed to the hook change, subscribes to it to re-render on updates, and
12
12
  * reads `getCurrentResult()` for the value to return. Calling `mutate()`
13
13
  * builds a new underlying `Mutation` in the `MutationCache` and executes it.
14
- *
15
14
  * @example
16
15
  * ```ts
17
16
  * const observer = new MutationObserver(queryClient, {
@@ -31,6 +30,10 @@ var MutationObserver = class extends require_subscribable.Subscribable {
31
30
  this.bindMethods();
32
31
  this.#updateResult();
33
32
  }
33
+ /**
34
+ * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when
35
+ * destructured from it.
36
+ */
34
37
  bindMethods() {
35
38
  this.mutate = this.mutate.bind(this);
36
39
  this.reset = this.reset.bind(this);
@@ -42,7 +45,8 @@ var MutationObserver = class extends require_subscribable.Subscribable {
42
45
  * defined), the observer is reset, detaching it from the mutation it was
43
46
  * observing. Otherwise, if the currently observed mutation is still
44
47
  * `pending`, its options are updated in place as well.
45
- *
48
+ * @param options - The new mutation observer options. They are defaulted with {@link
49
+ * QueryClient#defaultMutationOptions} before being applied.
46
50
  * @example
47
51
  * ```ts
48
52
  * observer.setOptions({
@@ -71,7 +75,12 @@ var MutationObserver = class extends require_subscribable.Subscribable {
71
75
  onUnsubscribe() {
72
76
  if (!this.hasListeners()) this.#currentMutation?.removeObserver(this);
73
77
  }
74
- /** @internal */
78
+ /**
79
+ * Updates the observer's result when the observed mutation changes, and notifies its listeners.
80
+ * @internal
81
+ * @param action - The action that updated the observed mutation, passed on to the observer's
82
+ * callbacks.
83
+ */
75
84
  onMutationUpdate(action) {
76
85
  this.#updateResult();
77
86
  this.#notify(action);
@@ -80,6 +89,7 @@ var MutationObserver = class extends require_subscribable.Subscribable {
80
89
  * Returns the observer's current result, derived from the observed
81
90
  * mutation's state (or the default, `idle` state if no mutation has been
82
91
  * built yet, e.g. before the first `mutate()` call or after `reset()`).
92
+ * @returns The observer's latest result.
83
93
  */
84
94
  getCurrentResult() {
85
95
  return this.#currentResult;
@@ -92,13 +102,11 @@ var MutationObserver = class extends require_subscribable.Subscribable {
92
102
  * running to completion and its own callbacks still fire, but this
93
103
  * observer stops reflecting its state and a subsequent `mutate()` call
94
104
  * will build a brand new mutation.
95
- *
105
+ * @see {@link MutationObserver#mutate}
96
106
  * @example
97
107
  * ```ts
98
108
  * observer.reset()
99
109
  * ```
100
- *
101
- * @see {@link MutationObserver#mutate}
102
110
  */
103
111
  reset() {
104
112
  this.#currentMutation?.removeObserver(this);
@@ -115,7 +123,9 @@ var MutationObserver = class extends require_subscribable.Subscribable {
115
123
  * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
116
124
  * invoked once the mutation settles, in addition to any callbacks defined
117
125
  * on the observer's own options.
118
- *
126
+ * @param variables - The variables passed to the `mutationFn`.
127
+ * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.
128
+ * @returns A promise that resolves with the mutation's data, or rejects with its error.
119
129
  * @example
120
130
  * ```ts
121
131
  * await observer.mutate(
@@ -145,7 +155,8 @@ var MutationObserver = class extends require_subscribable.Subscribable {
145
155
  }
146
156
  #notify(action) {
147
157
  require_notifyManager.notifyManager.batch(() => {
148
- if (this.#mutateOptions && this.hasListeners()) {
158
+ const mutateOptions = this.#mutateOptions;
159
+ if (mutateOptions && this.hasListeners()) {
149
160
  const variables = this.#currentResult.variables;
150
161
  const onMutateResult = this.#currentResult.context;
151
162
  const context = {
@@ -155,23 +166,23 @@ var MutationObserver = class extends require_subscribable.Subscribable {
155
166
  };
156
167
  if (action?.type === "success") {
157
168
  try {
158
- this.#mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
169
+ mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
159
170
  } catch (e) {
160
171
  Promise.reject(e);
161
172
  }
162
173
  try {
163
- this.#mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
174
+ mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
164
175
  } catch (e) {
165
176
  Promise.reject(e);
166
177
  }
167
178
  } else if (action?.type === "error") {
168
179
  try {
169
- this.#mutateOptions.onError?.(action.error, variables, onMutateResult, context);
180
+ mutateOptions.onError?.(action.error, variables, onMutateResult, context);
170
181
  } catch (e) {
171
182
  Promise.reject(e);
172
183
  }
173
184
  try {
174
- this.#mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
185
+ mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
175
186
  } catch (e) {
176
187
  Promise.reject(e);
177
188
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.cjs","names":["Subscribable","shallowEqualObjects","hashKey","getDefaultState"],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n *\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n *\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /** @internal */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n *\n * @example\n * ```ts\n * observer.reset()\n * ```\n *\n * @see {@link MutationObserver#mutate}\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n *\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n this.#mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n this.#mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqCA,IAAa,mBAAb,cAKUA,qBAAAA,aAER;CAGA;CACA,iBAKI,KAAA;CACJ;CACA;CAEA,YACE,QACA,SAMA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAK,cAAc;CACrB;CAEA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;CAkBA,WACE,SAMA;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAACC,cAAAA,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAK,QAAQ,iBAAiB,CAAC,CAA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EACd;CACF;CAEA,QAAQ,QAAmE;EACzE,sBAAA,cAAc,YAAY;GAExB,IAAI,KAAK,kBAAkB,KAAK,aAAa,GAAG;IAC9C,MAAM,YAAY,KAAK,eAAe;IACtC,MAAM,iBAAiB,KAAK,eAAe;IAE3C,MAAM,UAAU;KACd,QAAQ,KAAK;KACb,MAAM,KAAK,QAAQ;KACnB,aAAa,KAAK,QAAQ;IAC5B;IAEA,IAAI,QAAQ,SAAS,WAAW;KAC9B,IAAI;MACF,KAAK,eAAe,YAClB,OAAO,MACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAK,eAAe,YAClB,OAAO,MACP,MACA,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF,OAAO,IAAI,QAAQ,SAAS,SAAS;KACnC,IAAI;MACF,KAAK,eAAe,UAClB,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAK,eAAe,YAClB,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF;GACF;GAGA,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK,cAAc;GAC9B,CAAC;EACH,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"mutationObserver.cjs","names":["Subscribable","shallowEqualObjects","hashKey","getDefaultState"],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKUA,qBAAAA,aAER;CAGA;CACA,iBAKI,KAAA;CACJ;CACA;CAEA,YACE,QACA,SAMA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAK,cAAc;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAACC,cAAAA,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAK,QAAQ,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAU,KAAK;GACf,UAAU;EACZ,CAAC;EAGH,IACE,aAAa,eACb,KAAK,QAAQ,eACbC,cAAAA,QAAQ,YAAY,WAAW,MAAMA,cA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M,YAAY,KAAK,eAAe;IACtC,MAAM,iBAAiB,KAAK,eAAe;IAE3C,MAAM,UAAU;KACd,QAAQ,KAAK;KACb,MAAM,KAAK,QAAQ;KACnB,aAAa,KAAK,QAAQ;IAC5B;IAEA,IAAI,QAAQ,SAAS,WAAW;KAC9B,IAAI;MACF,cAAc,YACZ,OAAO,MACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,cAAc,YACZ,OAAO,MACP,MACA,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF,OAAO,IAAI,QAAQ,SAAS,SAAS;KACnC,IAAI;MACF,cAAc,UACZ,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,cAAc,YACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF;GACF;GAGA,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK,cAAc;GAC9B,CAAC;EACH,CAAC;CACH;AACF"}
@@ -1,2 +1,2 @@
1
- import { qn as MutationObserver } from "./hydration-Br9VIGXa.cjs";
1
+ import { qn as MutationObserver } from "./hydration-Dfuxd5FW.cjs";
2
2
  export { MutationObserver };
@@ -1,2 +1,2 @@
1
- import { qn as MutationObserver } from "./hydration-B9t5w6NC.js";
1
+ import { qn as MutationObserver } from "./hydration-UAqdtMIf.js";
2
2
  export { MutationObserver };
@@ -10,7 +10,6 @@ import { getDefaultState } from "./mutation.js";
10
10
  * passed to the hook change, subscribes to it to re-render on updates, and
11
11
  * reads `getCurrentResult()` for the value to return. Calling `mutate()`
12
12
  * builds a new underlying `Mutation` in the `MutationCache` and executes it.
13
- *
14
13
  * @example
15
14
  * ```ts
16
15
  * const observer = new MutationObserver(queryClient, {
@@ -30,6 +29,10 @@ var MutationObserver = class extends Subscribable {
30
29
  this.bindMethods();
31
30
  this.#updateResult();
32
31
  }
32
+ /**
33
+ * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when
34
+ * destructured from it.
35
+ */
33
36
  bindMethods() {
34
37
  this.mutate = this.mutate.bind(this);
35
38
  this.reset = this.reset.bind(this);
@@ -41,7 +44,8 @@ var MutationObserver = class extends Subscribable {
41
44
  * defined), the observer is reset, detaching it from the mutation it was
42
45
  * observing. Otherwise, if the currently observed mutation is still
43
46
  * `pending`, its options are updated in place as well.
44
- *
47
+ * @param options - The new mutation observer options. They are defaulted with {@link
48
+ * QueryClient#defaultMutationOptions} before being applied.
45
49
  * @example
46
50
  * ```ts
47
51
  * observer.setOptions({
@@ -70,7 +74,12 @@ var MutationObserver = class extends Subscribable {
70
74
  onUnsubscribe() {
71
75
  if (!this.hasListeners()) this.#currentMutation?.removeObserver(this);
72
76
  }
73
- /** @internal */
77
+ /**
78
+ * Updates the observer's result when the observed mutation changes, and notifies its listeners.
79
+ * @internal
80
+ * @param action - The action that updated the observed mutation, passed on to the observer's
81
+ * callbacks.
82
+ */
74
83
  onMutationUpdate(action) {
75
84
  this.#updateResult();
76
85
  this.#notify(action);
@@ -79,6 +88,7 @@ var MutationObserver = class extends Subscribable {
79
88
  * Returns the observer's current result, derived from the observed
80
89
  * mutation's state (or the default, `idle` state if no mutation has been
81
90
  * built yet, e.g. before the first `mutate()` call or after `reset()`).
91
+ * @returns The observer's latest result.
82
92
  */
83
93
  getCurrentResult() {
84
94
  return this.#currentResult;
@@ -91,13 +101,11 @@ var MutationObserver = class extends Subscribable {
91
101
  * running to completion and its own callbacks still fire, but this
92
102
  * observer stops reflecting its state and a subsequent `mutate()` call
93
103
  * will build a brand new mutation.
94
- *
104
+ * @see {@link MutationObserver#mutate}
95
105
  * @example
96
106
  * ```ts
97
107
  * observer.reset()
98
108
  * ```
99
- *
100
- * @see {@link MutationObserver#mutate}
101
109
  */
102
110
  reset() {
103
111
  this.#currentMutation?.removeObserver(this);
@@ -114,7 +122,9 @@ var MutationObserver = class extends Subscribable {
114
122
  * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
115
123
  * invoked once the mutation settles, in addition to any callbacks defined
116
124
  * on the observer's own options.
117
- *
125
+ * @param variables - The variables passed to the `mutationFn`.
126
+ * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.
127
+ * @returns A promise that resolves with the mutation's data, or rejects with its error.
118
128
  * @example
119
129
  * ```ts
120
130
  * await observer.mutate(
@@ -144,7 +154,8 @@ var MutationObserver = class extends Subscribable {
144
154
  }
145
155
  #notify(action) {
146
156
  notifyManager.batch(() => {
147
- if (this.#mutateOptions && this.hasListeners()) {
157
+ const mutateOptions = this.#mutateOptions;
158
+ if (mutateOptions && this.hasListeners()) {
148
159
  const variables = this.#currentResult.variables;
149
160
  const onMutateResult = this.#currentResult.context;
150
161
  const context = {
@@ -154,23 +165,23 @@ var MutationObserver = class extends Subscribable {
154
165
  };
155
166
  if (action?.type === "success") {
156
167
  try {
157
- this.#mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
168
+ mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
158
169
  } catch (e) {
159
170
  Promise.reject(e);
160
171
  }
161
172
  try {
162
- this.#mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
173
+ mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
163
174
  } catch (e) {
164
175
  Promise.reject(e);
165
176
  }
166
177
  } else if (action?.type === "error") {
167
178
  try {
168
- this.#mutateOptions.onError?.(action.error, variables, onMutateResult, context);
179
+ mutateOptions.onError?.(action.error, variables, onMutateResult, context);
169
180
  } catch (e) {
170
181
  Promise.reject(e);
171
182
  }
172
183
  try {
173
- this.#mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
184
+ mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
174
185
  } catch (e) {
175
186
  Promise.reject(e);
176
187
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.js","names":[],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n *\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n *\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /** @internal */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n *\n * @example\n * ```ts\n * observer.reset()\n * ```\n *\n * @see {@link MutationObserver#mutate}\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n *\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n this.#mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n this.#mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqCA,IAAa,mBAAb,cAKU,aAER;CAGA;CACA,iBAKI,KAAA;CACJ;CACA;CAEA,YACE,QACA,SAMA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAK,cAAc;CACrB;CAEA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;CAkBA,WACE,SAMA;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAAC,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAK,QAAQ,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAU,KAAK;GACf,UAAU;EACZ,CAAC;EAGH,IACE,aAAa,eACb,KAAK,QAAQ,eACb,QAAQ,YAAY,WAAW,MAAM,QAAQ,KAAK,QAAQ,WAAW,GAErE,KAAK,MAAM;OACN,IAAI,KAAK,kBAAkB,MAAM,WAAW,WACjD,KAAK,iBAAiB,WAAW,KAAK,OAAO;CAEjD;CAEA,cAA8B;EAC5B,IAAI,KAAK,UAA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B,OAAO,MACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAK,eAAe,YAClB,OAAO,MACP,MACA,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF,OAAO,IAAI,QAAQ,SAAS,SAAS;KACnC,IAAI;MACF,KAAK,eAAe,UAClB,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAK,eAAe,YAClB,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF;GACF;GAGA,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK,cAAc;GAC9B,CAAC;EACH,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"mutationObserver.js","names":[],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKU,aAER;CAGA;CACA,iBAKI,KAAA;CACJ;CACA;CAEA,YACE,QACA,SAMA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAK,cAAc;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAAC,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAK,QAAQ,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAU,KAAK;GACf,UAAU;EACZ,CAAC;EAGH,IACE,aAAa,eACb,KAAK,QAAQ,eACb,QAAQ,YAAY,WAAW,MAAM,QAAQ,KAAK,QAAQ,WAAW,GAErE,KAAK,MAAM;OACN,IAAI,KAAK,kBAAkB,MAAM,WAAW,WACjD,KAAK,iBAAiB,WAAW,KAAK,OAAO;CAEjD;CAEA,cAAuC;EACrC,IAAI,KAAK,UAAU,SAAS,KAAK,KAAK,kBAAkB;GACtD,KAAK,iBAAiB,YAAY,IAAI;GAEtC,KAAK,cAAc;EACrB;CACF;CAEA,gBAAyC;EACvC,IAAI,CAAC,KAAK,aAAa,GACrB,KAAK,kBAAkB,eAAe,IAAI;CAE9C;;;;;;;CAQA,iBACE,QACM;EACN,KAAK,cAAc;EAEnB,KA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AAS,SAAS;KACnC,IAAI;MACF,cAAc,UACZ,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,cAAc,YACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;IACF;GACF;GAGA,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK,cAAc;GAC9B,CAAC;EACH,CAAC;CACH;AACF"}
@@ -5,6 +5,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
5
5
  * Schedules the callback with the system's `setTimeout(callback, 0)`.
6
6
  */
7
7
  const defaultScheduler = require("./timeoutManager.cjs").systemSetTimeoutZero;
8
+ /**
9
+ * Creates a notify manager, which schedules and batches notifications to subscribers. Query uses a
10
+ * single instance of it, exported as {@link notifyManager}.
11
+ * @returns A new notify manager.
12
+ */
8
13
  function createNotifyManager() {
9
14
  let queue = [];
10
15
  let transactions = 0;
@@ -38,6 +43,8 @@ function createNotifyManager() {
38
43
  * This is mainly used internally to optimize query client updating.
39
44
  * Batches can be nested; the queue is only flushed once the outermost `batch` call finishes.
40
45
  * The return value of `callback` is passed through.
46
+ * @param callback - The function to run in the batch.
47
+ * @returns The return value of `callback`.
41
48
  */
42
49
  batch: (callback) => {
43
50
  let result;
@@ -52,6 +59,8 @@ function createNotifyManager() {
52
59
  },
53
60
  /**
54
61
  * All calls to the wrapped function will be batched.
62
+ * @param callback - The function to wrap.
63
+ * @returns A function that schedules a call to `callback` with the given arguments.
55
64
  */
56
65
  batchCalls: (callback) => {
57
66
  return (...args) => {
@@ -68,6 +77,7 @@ function createNotifyManager() {
68
77
  /**
69
78
  * Use this method to set a custom notify function.
70
79
  * This can be used to for example wrap notifications with `React.act` while running tests.
80
+ * @param fn - Receives each notification callback and must call it.
71
81
  */
72
82
  setNotifyFunction: (fn) => {
73
83
  notifyFn = fn;
@@ -76,7 +86,7 @@ function createNotifyManager() {
76
86
  * Use this method to set a custom function to batch notifications together into a single tick.
77
87
  * Framework adapters use this to plug in their own batching primitive, so that a single query
78
88
  * update only triggers one re-render instead of one per subscriber.
79
- *
89
+ * @param fn - Receives a function that runs a batch of notifications and must call it.
80
90
  * @example
81
91
  * ```ts
82
92
  * import { notifyManager } from '@tanstack/query-core'
@@ -91,7 +101,7 @@ function createNotifyManager() {
91
101
  /**
92
102
  * Configures a custom callback that schedules when the next batch runs.
93
103
  * The default behavior is `setTimeout(callback, 0)`.
94
- *
104
+ * @param fn - Receives a callback that runs the next batch, and schedules it.
95
105
  * @example
96
106
  * ```ts
97
107
  * import { notifyManager } from '@tanstack/query-core'