@tanstack/query-core 5.103.2 → 5.104.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/build/legacy/{classPrivateFieldSet2-CV7wyte-.js → classPrivateFieldSet2-CukOTU3U.js} +5 -5
  2. package/build/legacy/{classPrivateFieldSet2-Bo0ZyOIv.cjs → classPrivateFieldSet2-CvCEk6bM.cjs} +5 -5
  3. package/build/legacy/{classPrivateMethodInitSpec-CBwa_Y7C.js → classPrivateMethodInitSpec-CgB-WYk3.js} +2 -2
  4. package/build/legacy/{classPrivateMethodInitSpec-DVhcLejn.cjs → classPrivateMethodInitSpec-Nr6mgZRs.cjs} +2 -2
  5. package/build/legacy/environmentManager.d.cts +3 -4
  6. package/build/legacy/environmentManager.d.cts.map +1 -1
  7. package/build/legacy/environmentManager.d.ts +3 -4
  8. package/build/legacy/environmentManager.d.ts.map +1 -1
  9. package/build/legacy/focusManager.cjs +1 -1
  10. package/build/legacy/focusManager.d.cts +3 -4
  11. package/build/legacy/focusManager.d.cts.map +1 -1
  12. package/build/legacy/focusManager.d.ts +3 -4
  13. package/build/legacy/focusManager.d.ts.map +1 -1
  14. package/build/legacy/focusManager.js +1 -1
  15. package/build/legacy/{hydration-Bp57N3SP.d.ts → hydration-B9t5w6NC.d.ts} +11 -4
  16. package/build/legacy/hydration-B9t5w6NC.d.ts.map +1 -0
  17. package/build/{modern/hydration-DRcloXEn.d.cts → legacy/hydration-Br9VIGXa.d.cts} +11 -4
  18. package/build/legacy/hydration-Br9VIGXa.d.cts.map +1 -0
  19. package/build/legacy/hydration.d.cts +1 -1
  20. package/build/legacy/hydration.d.ts +1 -1
  21. package/build/legacy/index.d.cts +1 -1
  22. package/build/legacy/index.d.ts +1 -1
  23. package/build/legacy/infiniteQueryBehavior.d.cts +4 -5
  24. package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -1
  25. package/build/legacy/infiniteQueryBehavior.d.ts +4 -5
  26. package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -1
  27. package/build/legacy/infiniteQueryObserver.d.cts +3 -4
  28. package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
  29. package/build/legacy/infiniteQueryObserver.d.ts +3 -4
  30. package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
  31. package/build/legacy/mutation.cjs +2 -2
  32. package/build/legacy/mutation.d.cts +1 -1
  33. package/build/legacy/mutation.d.ts +1 -1
  34. package/build/legacy/mutation.js +2 -2
  35. package/build/legacy/mutationCache.cjs +1 -1
  36. package/build/legacy/mutationCache.d.cts +1 -1
  37. package/build/legacy/mutationCache.d.ts +1 -1
  38. package/build/legacy/mutationCache.js +1 -1
  39. package/build/legacy/mutationObserver.cjs +2 -2
  40. package/build/legacy/mutationObserver.d.cts +1 -1
  41. package/build/legacy/mutationObserver.d.ts +1 -1
  42. package/build/legacy/mutationObserver.js +2 -2
  43. package/build/legacy/notifyManager.d.cts +3 -4
  44. package/build/legacy/notifyManager.d.cts.map +1 -1
  45. package/build/legacy/notifyManager.d.ts +3 -4
  46. package/build/legacy/notifyManager.d.ts.map +1 -1
  47. package/build/legacy/onlineManager.cjs +1 -1
  48. package/build/legacy/onlineManager.d.cts +3 -4
  49. package/build/legacy/onlineManager.d.cts.map +1 -1
  50. package/build/legacy/onlineManager.d.ts +3 -4
  51. package/build/legacy/onlineManager.d.ts.map +1 -1
  52. package/build/legacy/onlineManager.js +1 -1
  53. package/build/legacy/queriesObserver.cjs +2 -2
  54. package/build/legacy/queriesObserver.d.cts +4 -5
  55. package/build/legacy/queriesObserver.d.cts.map +1 -1
  56. package/build/legacy/queriesObserver.d.ts +4 -5
  57. package/build/legacy/queriesObserver.d.ts.map +1 -1
  58. package/build/legacy/queriesObserver.js +2 -2
  59. package/build/legacy/query.cjs +2 -2
  60. package/build/legacy/query.cjs.map +1 -1
  61. package/build/legacy/query.d.cts +1 -1
  62. package/build/legacy/query.d.ts +1 -1
  63. package/build/legacy/query.js +2 -2
  64. package/build/legacy/query.js.map +1 -1
  65. package/build/legacy/queryCache.cjs +1 -1
  66. package/build/legacy/queryCache.cjs.map +1 -1
  67. package/build/legacy/queryCache.d.cts +1 -1
  68. package/build/legacy/queryCache.d.ts +1 -1
  69. package/build/legacy/queryCache.js +1 -1
  70. package/build/legacy/queryCache.js.map +1 -1
  71. package/build/legacy/queryClient.cjs +8 -1
  72. package/build/legacy/queryClient.cjs.map +1 -1
  73. package/build/legacy/queryClient.d.cts +1 -1
  74. package/build/legacy/queryClient.d.ts +1 -1
  75. package/build/legacy/queryClient.js +8 -1
  76. package/build/legacy/queryClient.js.map +1 -1
  77. package/build/legacy/queryObserver.cjs +2 -2
  78. package/build/legacy/queryObserver.cjs.map +1 -1
  79. package/build/legacy/queryObserver.d.cts +1 -1
  80. package/build/legacy/queryObserver.d.ts +1 -1
  81. package/build/legacy/queryObserver.js +2 -2
  82. package/build/legacy/queryObserver.js.map +1 -1
  83. package/build/legacy/removable-0GzhW1PK.d.cts +22 -0
  84. package/build/legacy/removable-0GzhW1PK.d.cts.map +1 -0
  85. package/build/legacy/removable-0GzhW1PK.d.ts +22 -0
  86. package/build/legacy/removable-0GzhW1PK.d.ts.map +1 -0
  87. package/build/legacy/removable.cjs +10 -1
  88. package/build/legacy/removable.cjs.map +1 -1
  89. package/build/legacy/removable.d.cts +1 -1
  90. package/build/legacy/removable.d.ts +1 -1
  91. package/build/legacy/removable.js +10 -1
  92. package/build/legacy/removable.js.map +1 -1
  93. package/build/legacy/retryer.d.cts +1 -1
  94. package/build/legacy/retryer.d.ts +1 -1
  95. package/build/legacy/streamedQuery.cjs +0 -7
  96. package/build/legacy/streamedQuery.cjs.map +1 -1
  97. package/build/legacy/streamedQuery.d.cts +2 -10
  98. package/build/legacy/streamedQuery.d.cts.map +1 -1
  99. package/build/legacy/streamedQuery.d.ts +2 -10
  100. package/build/legacy/streamedQuery.d.ts.map +1 -1
  101. package/build/legacy/streamedQuery.js +0 -7
  102. package/build/legacy/streamedQuery.js.map +1 -1
  103. package/build/legacy/subscribable-CysQ0tne.d.cts +34 -0
  104. package/build/legacy/subscribable-CysQ0tne.d.cts.map +1 -0
  105. package/build/legacy/subscribable-CysQ0tne.d.ts +34 -0
  106. package/build/legacy/subscribable-CysQ0tne.d.ts.map +1 -0
  107. package/build/legacy/subscribable.cjs +22 -0
  108. package/build/legacy/subscribable.cjs.map +1 -1
  109. package/build/legacy/subscribable.d.cts +1 -1
  110. package/build/legacy/subscribable.d.ts +1 -1
  111. package/build/legacy/subscribable.js +22 -0
  112. package/build/legacy/subscribable.js.map +1 -1
  113. package/build/legacy/timeoutManager.cjs +1 -1
  114. package/build/legacy/timeoutManager.d.cts +7 -8
  115. package/build/legacy/timeoutManager.d.cts.map +1 -1
  116. package/build/legacy/timeoutManager.d.ts +7 -8
  117. package/build/legacy/timeoutManager.d.ts.map +1 -1
  118. package/build/legacy/timeoutManager.js +1 -1
  119. package/build/legacy/types.cjs.map +1 -1
  120. package/build/legacy/types.d.cts +1 -1
  121. package/build/legacy/types.d.ts +1 -1
  122. package/build/legacy/types.js.map +1 -1
  123. package/build/legacy/utils.d.cts +1 -1
  124. package/build/legacy/utils.d.ts +1 -1
  125. package/build/modern/environmentManager.d.cts +3 -4
  126. package/build/modern/environmentManager.d.cts.map +1 -1
  127. package/build/modern/environmentManager.d.ts +3 -4
  128. package/build/modern/environmentManager.d.ts.map +1 -1
  129. package/build/modern/focusManager.cjs.map +1 -1
  130. package/build/modern/focusManager.d.cts +3 -4
  131. package/build/modern/focusManager.d.cts.map +1 -1
  132. package/build/modern/focusManager.d.ts +3 -4
  133. package/build/modern/focusManager.d.ts.map +1 -1
  134. package/build/modern/focusManager.js.map +1 -1
  135. package/build/{legacy/hydration-DRcloXEn.d.cts → modern/hydration-B9t5w6NC.d.ts} +11 -4
  136. package/build/modern/hydration-B9t5w6NC.d.ts.map +1 -0
  137. package/build/modern/{hydration-Bp57N3SP.d.ts → hydration-Br9VIGXa.d.cts} +11 -4
  138. package/build/modern/hydration-Br9VIGXa.d.cts.map +1 -0
  139. package/build/modern/hydration.d.cts +1 -1
  140. package/build/modern/hydration.d.ts +1 -1
  141. package/build/modern/index.d.cts +1 -1
  142. package/build/modern/index.d.ts +1 -1
  143. package/build/modern/infiniteQueryBehavior.d.cts +4 -5
  144. package/build/modern/infiniteQueryBehavior.d.cts.map +1 -1
  145. package/build/modern/infiniteQueryBehavior.d.ts +4 -5
  146. package/build/modern/infiniteQueryBehavior.d.ts.map +1 -1
  147. package/build/modern/infiniteQueryObserver.d.cts +3 -4
  148. package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
  149. package/build/modern/infiniteQueryObserver.d.ts +3 -4
  150. package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
  151. package/build/modern/mutation.cjs.map +1 -1
  152. package/build/modern/mutation.d.cts +1 -1
  153. package/build/modern/mutation.d.ts +1 -1
  154. package/build/modern/mutation.js.map +1 -1
  155. package/build/modern/mutationCache.cjs.map +1 -1
  156. package/build/modern/mutationCache.d.cts +1 -1
  157. package/build/modern/mutationCache.d.ts +1 -1
  158. package/build/modern/mutationCache.js.map +1 -1
  159. package/build/modern/mutationObserver.cjs.map +1 -1
  160. package/build/modern/mutationObserver.d.cts +1 -1
  161. package/build/modern/mutationObserver.d.ts +1 -1
  162. package/build/modern/mutationObserver.js.map +1 -1
  163. package/build/modern/notifyManager.d.cts +3 -4
  164. package/build/modern/notifyManager.d.cts.map +1 -1
  165. package/build/modern/notifyManager.d.ts +3 -4
  166. package/build/modern/notifyManager.d.ts.map +1 -1
  167. package/build/modern/onlineManager.cjs.map +1 -1
  168. package/build/modern/onlineManager.d.cts +3 -4
  169. package/build/modern/onlineManager.d.cts.map +1 -1
  170. package/build/modern/onlineManager.d.ts +3 -4
  171. package/build/modern/onlineManager.d.ts.map +1 -1
  172. package/build/modern/onlineManager.js.map +1 -1
  173. package/build/modern/queriesObserver.cjs.map +1 -1
  174. package/build/modern/queriesObserver.d.cts +4 -5
  175. package/build/modern/queriesObserver.d.cts.map +1 -1
  176. package/build/modern/queriesObserver.d.ts +4 -5
  177. package/build/modern/queriesObserver.d.ts.map +1 -1
  178. package/build/modern/queriesObserver.js.map +1 -1
  179. package/build/modern/query.cjs.map +1 -1
  180. package/build/modern/query.d.cts +1 -1
  181. package/build/modern/query.d.ts +1 -1
  182. package/build/modern/query.js.map +1 -1
  183. package/build/modern/queryCache.cjs.map +1 -1
  184. package/build/modern/queryCache.d.cts +1 -1
  185. package/build/modern/queryCache.d.ts +1 -1
  186. package/build/modern/queryCache.js.map +1 -1
  187. package/build/modern/queryClient.cjs +7 -0
  188. package/build/modern/queryClient.cjs.map +1 -1
  189. package/build/modern/queryClient.d.cts +1 -1
  190. package/build/modern/queryClient.d.ts +1 -1
  191. package/build/modern/queryClient.js +7 -0
  192. package/build/modern/queryClient.js.map +1 -1
  193. package/build/modern/queryObserver.cjs.map +1 -1
  194. package/build/modern/queryObserver.d.cts +1 -1
  195. package/build/modern/queryObserver.d.ts +1 -1
  196. package/build/modern/queryObserver.js.map +1 -1
  197. package/build/modern/removable-0GzhW1PK.d.cts +22 -0
  198. package/build/modern/removable-0GzhW1PK.d.cts.map +1 -0
  199. package/build/modern/removable-0GzhW1PK.d.ts +22 -0
  200. package/build/modern/removable-0GzhW1PK.d.ts.map +1 -0
  201. package/build/modern/removable.cjs +9 -0
  202. package/build/modern/removable.cjs.map +1 -1
  203. package/build/modern/removable.d.cts +1 -1
  204. package/build/modern/removable.d.ts +1 -1
  205. package/build/modern/removable.js +9 -0
  206. package/build/modern/removable.js.map +1 -1
  207. package/build/modern/retryer.d.cts +1 -1
  208. package/build/modern/retryer.d.ts +1 -1
  209. package/build/modern/streamedQuery.cjs +0 -7
  210. package/build/modern/streamedQuery.cjs.map +1 -1
  211. package/build/modern/streamedQuery.d.cts +2 -10
  212. package/build/modern/streamedQuery.d.cts.map +1 -1
  213. package/build/modern/streamedQuery.d.ts +2 -10
  214. package/build/modern/streamedQuery.d.ts.map +1 -1
  215. package/build/modern/streamedQuery.js +0 -7
  216. package/build/modern/streamedQuery.js.map +1 -1
  217. package/build/modern/subscribable-CysQ0tne.d.cts +34 -0
  218. package/build/modern/subscribable-CysQ0tne.d.cts.map +1 -0
  219. package/build/modern/subscribable-CysQ0tne.d.ts +34 -0
  220. package/build/modern/subscribable-CysQ0tne.d.ts.map +1 -0
  221. package/build/modern/subscribable.cjs +22 -0
  222. package/build/modern/subscribable.cjs.map +1 -1
  223. package/build/modern/subscribable.d.cts +1 -1
  224. package/build/modern/subscribable.d.ts +1 -1
  225. package/build/modern/subscribable.js +22 -0
  226. package/build/modern/subscribable.js.map +1 -1
  227. package/build/modern/timeoutManager.cjs.map +1 -1
  228. package/build/modern/timeoutManager.d.cts +7 -8
  229. package/build/modern/timeoutManager.d.cts.map +1 -1
  230. package/build/modern/timeoutManager.d.ts +7 -8
  231. package/build/modern/timeoutManager.d.ts.map +1 -1
  232. package/build/modern/timeoutManager.js.map +1 -1
  233. package/build/modern/types.cjs.map +1 -1
  234. package/build/modern/types.d.cts +1 -1
  235. package/build/modern/types.d.ts +1 -1
  236. package/build/modern/types.js.map +1 -1
  237. package/build/modern/utils.d.cts +1 -1
  238. package/build/modern/utils.d.ts +1 -1
  239. package/package.json +3 -3
  240. package/src/query.ts +1 -2
  241. package/src/queryCache.ts +1 -2
  242. package/src/queryClient.ts +7 -0
  243. package/src/queryObserver.ts +4 -8
  244. package/src/removable.ts +9 -0
  245. package/src/streamedQuery.ts +1 -7
  246. package/src/subscribable.ts +22 -0
  247. package/src/types.ts +9 -15
  248. package/build/legacy/hydration-Bp57N3SP.d.ts.map +0 -1
  249. package/build/legacy/hydration-DRcloXEn.d.cts.map +0 -1
  250. package/build/legacy/removable-DeMjhW5M.d.cts +0 -13
  251. package/build/legacy/removable-DeMjhW5M.d.cts.map +0 -1
  252. package/build/legacy/removable-DeMjhW5M.d.ts +0 -13
  253. package/build/legacy/removable-DeMjhW5M.d.ts.map +0 -1
  254. package/build/legacy/subscribable-CbifVTKz.d.cts +0 -12
  255. package/build/legacy/subscribable-CbifVTKz.d.cts.map +0 -1
  256. package/build/legacy/subscribable-CbifVTKz.d.ts +0 -12
  257. package/build/legacy/subscribable-CbifVTKz.d.ts.map +0 -1
  258. package/build/modern/hydration-Bp57N3SP.d.ts.map +0 -1
  259. package/build/modern/hydration-DRcloXEn.d.cts.map +0 -1
  260. package/build/modern/removable-DeMjhW5M.d.cts +0 -13
  261. package/build/modern/removable-DeMjhW5M.d.cts.map +0 -1
  262. package/build/modern/removable-DeMjhW5M.d.ts +0 -13
  263. package/build/modern/removable-DeMjhW5M.d.ts.map +0 -1
  264. package/build/modern/subscribable-CbifVTKz.d.cts +0 -12
  265. package/build/modern/subscribable-CbifVTKz.d.cts.map +0 -1
  266. package/build/modern/subscribable-CbifVTKz.d.ts +0 -12
  267. package/build/modern/subscribable-CbifVTKz.d.ts.map +0 -1
@@ -1,5 +1,5 @@
1
- import { t as Subscribable } from "./subscribable-CbifVTKz.js";
2
- import { $n as Query, A as InfiniteData, C as FetchPreviousPageOptions, Ct as QueryKey, F as InfiniteQueryObserverOptions, S as FetchNextPageOptions, f as DefaultError, m as DefaultedInfiniteQueryObserverOptions, on as QueryClient, rr as QueryObserver, z as InfiniteQueryObserverResult } from "./hydration-Bp57N3SP.js";
1
+ import { t as Subscribable } from "./subscribable-CysQ0tne.js";
2
+ import { $n as Query, A as InfiniteData, C as FetchPreviousPageOptions, Ct as QueryKey, F as InfiniteQueryObserverOptions, S as FetchNextPageOptions, f as DefaultError, m as DefaultedInfiniteQueryObserverOptions, on as QueryClient, rr as QueryObserver, z as InfiniteQueryObserverResult } from "./hydration-B9t5w6NC.js";
3
3
  //#region src/infiniteQueryObserver.d.ts
4
4
  type InfiniteQueryObserverListener<TData, TError> = (result: InfiniteQueryObserverResult<TData, TError>) => void;
5
5
  /**
@@ -21,7 +21,7 @@ type InfiniteQueryObserverListener<TData, TError> = (result: InfiniteQueryObserv
21
21
  * const unsubscribe = observer.subscribe((result) => console.log(result))
22
22
  * ```
23
23
  */
24
- declare class InfiniteQueryObserver<TQueryFnData = unknown, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends QueryObserver<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey> {
24
+ export declare class InfiniteQueryObserver<TQueryFnData = unknown, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends QueryObserver<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey> {
25
25
  subscribe: Subscribable<InfiniteQueryObserverListener<TData, TError>>['subscribe'];
26
26
  getCurrentResult: ReplaceReturnType<QueryObserver<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>['getCurrentResult'], InfiniteQueryObserverResult<TData, TError>>;
27
27
  protected fetch: ReplaceReturnType<QueryObserver<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>['fetch'], Promise<InfiniteQueryObserverResult<TData, TError>>>;
@@ -83,5 +83,4 @@ declare class InfiniteQueryObserver<TQueryFnData = unknown, TError = DefaultErro
83
83
  }
84
84
  type ReplaceReturnType<TFunction extends (...args: Array<any>) => unknown, TReturn> = (...args: Parameters<TFunction>) => TReturn;
85
85
  //#endregion
86
- export { InfiniteQueryObserver };
87
86
  //# sourceMappingURL=infiniteQueryObserver.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"infiniteQueryObserver.d.ts","names":[],"sources":["../../src/infiniteQueryObserver.ts"],"mappings":";;;KAiBK,8BAA8B,OAAO,WACxC,QAAQ,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;;cAsBhC,sBACX,wBACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,8BACQ,cACR,cACA,QACA,OACA,aAAa,cAAc,aAC3B;EAGA,WAAY,aACV,8BAA8B,OAAO;EAIvC,kBAAmB,kBACjB,cACE,cACA,QACA,OACA,aAAa,cAAc,aAC3B,gCAEF,4BAA4B,OAAO;YAI3B,OAAQ,kBAChB,cACE,cACA,QACA,OACA,aAAa,cAAc,aAC3B,qBAEF,QAAQ,4BAA4B,OAAO;EAI3C,YAAA,QAAQ,aACR,SAAS,6BACP,cACA,QACA,OACA,WACA;YAMM;;;;;;;EAYV,WACE,SAAS,6BACP,cACA,QACA,OACA,WACA;;;;;;;EAaJ,oBACE,SAAS,sCACP,cACA,QACA,OACA,WACA,cAED,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;EA0BtC,cACE,UAAU,uBACT,QAAQ,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;EA2B9C,kBACE,UAAU,2BACT,QAAQ,4BAA4B,OAAO;YASpC,aACR,OAAO,MACL,cACA,QACA,aAAa,cAAc,aAC3B,YAEF,SAAS,6BACP,cACA,QACA,OACA,WACA,cAED,4BAA4B,OAAO;;KAiCnC,kBACH,sBAAsB,MAAM,wBAC5B,eACM,MAAM,WAAW,eAAe"}
1
+ {"version":3,"file":"infiniteQueryObserver.d.ts","names":[],"sources":["../../src/infiniteQueryObserver.ts"],"mappings":";;;KAiBK,8BAA8B,OAAO,WACxC,QAAQ,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;;qBAsBhC,sBACX,wBACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,8BACQ,cACR,cACA,QACA,OACA,aAAa,cAAc,aAC3B;EAGA,WAAY,aACV,8BAA8B,OAAO;EAIvC,kBAAmB,kBACjB,cACE,cACA,QACA,OACA,aAAa,cAAc,aAC3B,gCAEF,4BAA4B,OAAO;YAI3B,OAAQ,kBAChB,cACE,cACA,QACA,OACA,aAAa,cAAc,aAC3B,qBAEF,QAAQ,4BAA4B,OAAO;EAI3C,YAAA,QAAQ,aACR,SAAS,6BACP,cACA,QACA,OACA,WACA;YAMM;;;;;;;EAYV,WACE,SAAS,6BACP,cACA,QACA,OACA,WACA;;;;;;;EAaJ,oBACE,SAAS,sCACP,cACA,QACA,OACA,WACA,cAED,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;EA0BtC,cACE,UAAU,uBACT,QAAQ,4BAA4B,OAAO;;;;;;;;;;;;;;;;;;;EA2B9C,kBACE,UAAU,2BACT,QAAQ,4BAA4B,OAAO;YASpC,aACR,OAAO,MACL,cACA,QACA,aAAa,cAAc,aAC3B,YAEF,SAAS,6BACP,cACA,QACA,OACA,WACA,cAED,4BAA4B,OAAO;;KAiCnC,kBACH,sBAAsB,MAAM,wBAC5B,eACM,MAAM,WAAW,eAAe"}
@@ -1 +1 @@
1
- {"version":3,"file":"mutation.cjs","names":["Removable","#client","#mutationCache","#observers","#retryer","#dispatch","createRetryer"],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /** @internal */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /** @internal */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /** @internal */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n *\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n *\n * @see {@link Mutation#execute}\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n *\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n *\n * @see {@link Mutation#continue}\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;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O;KACjB,QAAQ;KACR,WAAW,OAAO;KAClB,aAAa,KAAK,IAAI;IACxB;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,MAAM,OAAO;KACb,cAAc;KACd,eAAe;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,sBAAA,cAAc,YAAY;GACxB,KAAKC,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAKD,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;AAEA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
1
+ {"version":3,"file":"mutation.cjs","names":["Removable","createRetryer"],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /** @internal */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /** @internal */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /** @internal */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n *\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n *\n * @see {@link Mutation#execute}\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n *\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n *\n * @see {@link Mutation#continue}\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsIA,IAAa,WAAb,cAKUA,kBAAAA,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,QACA;EACA,MAAM;EAEN,KAAK,UAAU,OAAO;EACtB,KAAK,aAAa,OAAO;EACzB,KAAK,iBAAiB,OAAO;EAC7B,KAAK,aAAa,CAAC;EACnB,KAAK,QAAQ,OAAO,SAAS,gBAAgB;EAE7C,KAAK,WAAW,OAAO,OAAO;EAC9B,KAAK,WAAW;CAClB;;CAGA,WACE,SACM;EACN,KAAK,UAAU;EAEf,KAAK,aAAa,KAAK,QAAQ,MAAM;CACvC;;;;CAKA,IAAI,OAAiC;EACnC,OAAO,KAAK,QAAQ;CACtB;;CAGA,YAAY,UAAsD;EAChE,IAAI,CAAC,KAAK,WAAW,SAAS,QAAQ,GAAG;GACvC,KAAK,WAAW,KAAK,QAAQ;GAG7B,KAAK,eAAe;GAEpB,KAAK,eAAe,OAAO;IACzB,MAAM;IACN,UAAU;IACV;GACF,CAAC;EACH;CACF;;CAGA,eAAe,UAAsD;EACnE,KAAK,aAAa,KAAK,WAAW,QAAQ,MAAM,MAAM,QAAQ;EAE9D,KAAK,WAAW;EAEhB,KAAK,eAAe,OAAO;GACzB,MAAM;GACN,UAAU;GACV;EACF,CAAC;CACH;CAEA,iBAA2B;EACzB,IAAI,CAAC,KAAK,WAAW,QAAQ;GAC3B,IAAI,KAAK,MAAM,WAAW,WACxB,KAAK,WAAW;QAEhB,KAAK,eAAe,OAAO,IAAI;EAEnC;CACF;;;;;;;;;;;;;;;;;;;;;;CAuBA,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rB,eAAe,OAAO;IACxB;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,UAAU;IACZ;IACF,KAAK,YACH,OAAO;KACL,GAAG;KACH,UAAU;IACZ;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,SAAS,OAAO;KAChB,MAAM,KAAA;KACN,cAAc;KACd,eAAe;KACf,OAAO;KACP,UAAU,OAAO;KACjB,QAAQ;KACR,WAAW,OAAO;KAClB,aAAa,KAAK,IAAI;IACxB;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,MAAM,OAAO;KACb,cAAc;KACd,eAAe;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,sBAAA,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;AAEA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
@@ -1,2 +1,2 @@
1
- import { Bn as Action, Hn as MutationState, Un as getDefaultState, Vn as Mutation } from "./hydration-DRcloXEn.cjs";
1
+ import { Bn as Action, Hn as MutationState, Un as getDefaultState, Vn as Mutation } from "./hydration-Br9VIGXa.cjs";
2
2
  export { Action, Mutation, MutationState, getDefaultState };
@@ -1,2 +1,2 @@
1
- import { Bn as Action, Hn as MutationState, Un as getDefaultState, Vn as Mutation } from "./hydration-Bp57N3SP.js";
1
+ import { Bn as Action, Hn as MutationState, Un as getDefaultState, Vn as Mutation } from "./hydration-B9t5w6NC.js";
2
2
  export { Action, Mutation, MutationState, getDefaultState };
@@ -1 +1 @@
1
- {"version":3,"file":"mutation.js","names":["#client","#mutationCache","#observers","#retryer","#dispatch"],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /** @internal */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /** @internal */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /** @internal */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n *\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n *\n * @see {@link Mutation#execute}\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n *\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n *\n * @see {@link Mutation#continue}\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsIA,IAAa,WAAb,cAKU,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,QACA;EACA,MAAM;EAEN,KAAKA,UAAU,OAAO;EACtB,KAAK,aAAa,OAAO;EACzB,KAAKC,iBAAiB,OAAO;EAC7B,KAAKC,aAAa,CAAC;EACnB,KAAK,QAAQ,OAAO,SAAS,gBAAgB;EAE7C,KAAK,WAAW,OAAO,OAAO;EAC9B,KAAK,WAAW;CAClB;;CAGA,WACE,SACM;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A,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,cAAc,YAAY;GACxB,KAAKC,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAKD,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;AAEA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
1
+ {"version":3,"file":"mutation.js","names":[],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /** @internal */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /** @internal */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /** @internal */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n *\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n *\n * @see {@link Mutation#execute}\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n *\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n *\n * @see {@link Mutation#continue}\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsIA,IAAa,WAAb,cAKU,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,Q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Ae;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;AAEA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"mutationCache.cjs","names":["Subscribable","#mutations","#scopes","#mutationId","Mutation","matchMutation","notifyManager","noop"],"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,cAAmCA,qBAAAA,aAAoC;CACrE;CACA;CACA;CAEA,YAAY,SAAqC,CAAC,GAAG;EACnD,MAAM;EADW,KAAA,SAAA;EAEjB,KAAKC,6BAAa,IAAI,IAAI;EAC1B,KAAKC,0BAAU,IAAI,IAAI;EACvB,KAAKC,cAAc;CACrB;;CAGA,MACE,QACA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,OAAO;AACjC"}
1
+ {"version":3,"file":"mutationCache.cjs","names":["Subscribable","Mutation","matchMutation","notifyManager","noop"],"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,cAAmCA,qBAAAA,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,IAAIC,iBAAAA,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,MAAM,QAAQ,SAAS,QAAQ;EAC/B,IAAI,OAAO,UAAU,UAAU;GAC7B,MAAM,kBAAkB,KAAK,QAAQ,IAAI,KAAK;GAC9C,IAAI,iBACF,gBAAgB,KAAK,QAAQ;QAE7B,KAAK,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC;EAEtC;EACA,KAAK,OAAO;GAAE,MAAM;GAAS;EAAS,CAAC;CACzC;;CAGA,OAAO,UAA8C;EACnD,IAAI,KAAK,WAAW,OAAO,QAAQ,GAAG;GACpC,MAAM,QAA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AEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,aACzBC,cAAAA,cAAc,kBAAkB,QAAQ,CAC1C;CACF;;;;;;;;;;;;;;;;CAiBA,QAAQ,UAA2B,CAAC,GAAoB;EACtD,OAAO,KAAK,OAAO,CAAC,CAAC,QAAQ,aAAaA,cAAAA,cAAc,SAAS,QAAQ,CAAC;CAC5E;;CAGA,OAAO,OAAiC;EACtC,sBAAA,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,OAAOC,sBAAAA,cAAc,YACnB,QAAQ,IACN,gBAAgB,KAAK,aAAa,SAAS,SAAS,CAAC,CAAC,MAAMC,cAAAA,IAAI,CAAC,CACnE,CACF;CACF;AACF;AAEA,SAAS,SAAS,UAAwC;CACxD,OAAO,SAAS,QAAQ,OAAO;AACjC"}
@@ -1,2 +1,2 @@
1
- import { Gn as MutationCacheConfig, Kn as MutationCacheNotifyEvent, Wn as MutationCache } from "./hydration-DRcloXEn.cjs";
1
+ import { Gn as MutationCacheConfig, Kn as MutationCacheNotifyEvent, Wn as MutationCache } from "./hydration-Br9VIGXa.cjs";
2
2
  export { MutationCache, MutationCacheConfig, MutationCacheNotifyEvent };
@@ -1,2 +1,2 @@
1
- import { Gn as MutationCacheConfig, Kn as MutationCacheNotifyEvent, Wn as MutationCache } from "./hydration-Bp57N3SP.js";
1
+ import { Gn as MutationCacheConfig, Kn as MutationCacheNotifyEvent, Wn as MutationCache } from "./hydration-B9t5w6NC.js";
2
2
  export { MutationCache, MutationCacheConfig, MutationCacheNotifyEvent };
@@ -1 +1 @@
1
- {"version":3,"file":"mutationCache.js","names":["#mutations","#scopes","#mutationId"],"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,KAAKA,6BAAa,IAAI,IAAI;EAC1B,KAAKC,0BAAU,IAAI,IAAI;EACvB,KAAKC,cAAc;CACrB;;CAGA,MACE,QACA,SACA,OACsD;EACtD,MAAM,WAAW,IAAI,SAAS;GAC5B;GACA,eAAe;GACf,YAAY,EAAE,KAAKA;GACnB,SAAS,OAAO,uBAAuB,OAAO;GAC9C;EACF,CAAC;EAED,KAAK,IAAI,QAAQ;EAEjB,OAAO;CACT;;CAGA,IAAI,UAA8C;EAChD,KAAKF,WAAW,IAAI,QAAQ;EAC5B,MAAM,QAAQ,SAAS,QAAQ;EAC/B,IAAI,OAAO,UAAU,UAAU;GAC7B,MAAM,kBAAkB,KAAKC,QAAQ,IAAI,KAAK;GAC9C,IAAI,iBACF,gBAAgB,KAAK,QAAQ;QAE7B,KAAKA,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC;EAEtC;EACA,KAAK,OAAO;GAAE,MAAM;GAAS;EAAS,CAAC;CACzC;;CAGA,OAAO,UAA8C;EACnD,IAAI,KAAKD,WAAW,OAAO,QAAQ,GAAG;GACpC,MAAM,QAAQ,SAAS,QAAQ;GAC/B,IAAI,OAAO,UAAU,UAAU;IAC7B,MAAM,kBAAkB,KAAKC,QAAQ,IAAI,KAAK;IAC9C,IAAI,iBAAiB;KACnB,IAAI,gBAAgB,SAAS,GAAG;MAC9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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 *\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,IA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AAQ,QAAQ;CAE3B;;;;;;;;;;;CAYA,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;;;;;;;;;;;;;;;;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 +1 @@
1
- {"version":3,"file":"mutationObserver.cjs","names":["Subscribable","#client","#updateResult","shallowEqualObjects","#currentMutation","hashKey","#notify","#currentResult","#mutateOptions","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,KAAKC,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAKC,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,KAAKD,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAACE,cAAAA,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAKF,QAAQ,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAU,KAAKG;GACf,UAAU;EACZ,CAAC;EAGH,IACE,aAAa,eACb,KAAK,QAAQ,eACbC,cAAAA,QAAQ,YAAY,WAAW,MAAMA,cAAAA,QAAQ,KAAK,QAAQ,WAAW,GAErE,KAAK,MAAM;OACN,IAAI,KAAKD,kBAAkB,MAAM,WAAW,WACjD,KAAKA,iBAAiB,WAAW,KAAK,OAAO;CAEjD;CAEA,cAA8B;EAC5B,IAAI,KAAK,UAAU,SAAS,KAAK,KAAKA,kBAAkB;GACtD,KAAKA,iBAAiB,YAAY,IAAI;GAEtC,KAAKF,cAAc;EACrB;CACF;CAEA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AAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAKA,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,KAAKA,eAAe,UAClB,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAKA,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,KAAKD,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 *\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,SA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KAAK,iBAAiB,QAAQ,SAAS;CAChD;CAEA,gBAAsB;EACpB,MAAM,QACJ,KAAK,kBAAkB,SACvBC,iBAAAA,gBAA4D;EAE9D,KAAK,iBAAiB;GACpB,GAAG;GACH,WAAW,MAAM,WAAW;GAC5B,WAAW,MAAM,WAAW;GAC5B,SAAS,MAAM,WAAW;GAC1B,QAAQ,MAAM,WAAW;GACzB,QAAQ,KAAK;GACb,OAAO,KAAK;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,2 +1,2 @@
1
- import { qn as MutationObserver } from "./hydration-DRcloXEn.cjs";
1
+ import { qn as MutationObserver } from "./hydration-Br9VIGXa.cjs";
2
2
  export { MutationObserver };
@@ -1,2 +1,2 @@
1
- import { qn as MutationObserver } from "./hydration-Bp57N3SP.js";
1
+ import { qn as MutationObserver } from "./hydration-B9t5w6NC.js";
2
2
  export { MutationObserver };
@@ -1 +1 @@
1
- {"version":3,"file":"mutationObserver.js","names":["#client","#updateResult","#currentMutation","#notify","#currentResult","#mutateOptions"],"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,KAAKA,UAAU;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,KAAKC,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,KAAKD,QAAQ,uBAAuB,OAAO;EAC1D,IAAI,CAAC,oBAAoB,KAAK,SAAS,WAAW,GAChD,KAAKA,QAAQ,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAU,KAAKE;GACf,UAAU;EACZ,CAAC;EAG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KC,kBAAkB,KAAK,aAAa,GAAG;IAC9C,MAAM,YAAY,KAAKD,eAAe;IACtC,MAAM,iBAAiB,KAAKA,eAAe;IAE3C,MAAM,UAAU;KACd,QAAQ,KAAKJ;KACb,MAAM,KAAK,QAAQ;KACnB,aAAa,KAAK,QAAQ;IAC5B;IAEA,IAAI,QAAQ,SAAS,WAAW;KAC9B,IAAI;MACF,KAAKK,eAAe,YAClB,OAAO,MACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAKA,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,KAAKA,eAAe,UAClB,OAAO,OACP,WACA,gBACA,OACF;KACF,SAAS,GAAG;MACV,QAAa,OAAO,CAAC;KACvB;KACA,IAAI;MACF,KAAKA,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,KAAKD,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 *\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.#current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AG;MACV,QAAa,OAAO,CAAC;KACvB;IACF;GACF;GAGA,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK,cAAc;GAC9B,CAAC;EACH,CAAC;CACH;AACF"}
@@ -8,8 +8,8 @@ type ScheduleFunction = (callback: () => void) => void;
8
8
  * Default scheduling function used by the notify manager.
9
9
  * Schedules the callback with the system's `setTimeout(callback, 0)`.
10
10
  */
11
- declare const defaultScheduler: ScheduleFunction;
12
- declare function createNotifyManager(): {
11
+ export declare const defaultScheduler: ScheduleFunction;
12
+ export declare function createNotifyManager(): {
13
13
  /**
14
14
  * Batches all updates scheduled inside the passed callback.
15
15
  * This is mainly used internally to optimize query client updating.
@@ -68,7 +68,7 @@ declare function createNotifyManager(): {
68
68
  /**
69
69
  * Handles scheduling and batching callbacks in TanStack Query.
70
70
  */
71
- declare const notifyManager: {
71
+ export declare const notifyManager: {
72
72
  /**
73
73
  * Batches all updates scheduled inside the passed callback.
74
74
  * This is mainly used internally to optimize query client updating.
@@ -125,5 +125,4 @@ declare const notifyManager: {
125
125
  readonly setScheduler: (fn: ScheduleFunction) => void;
126
126
  };
127
127
  //#endregion
128
- export { createNotifyManager, defaultScheduler, notifyManager };
129
128
  //# sourceMappingURL=notifyManager.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"notifyManager.d.cts","names":[],"sources":["../../src/notifyManager.ts"],"mappings":";KAIK;KAEA,kBAAkB;KAElB,uBAAuB;KAEvB,mBAAmB,UAAU,sBAAsB,MAAM;KAEzD,oBAAoB;;;;;cAMZ,kBAAkB;iBAEf;;;;;;;WAyCJ,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA;;;;;cAWV;;;;;;;WAlFD,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA"}
1
+ {"version":3,"file":"notifyManager.d.cts","names":[],"sources":["../../src/notifyManager.ts"],"mappings":";KAIK;KAEA,kBAAkB;KAElB,uBAAuB;KAEvB,mBAAmB,UAAU,sBAAsB,MAAM;KAEzD,oBAAoB;;;;;qBAMZ,kBAAkB;wBAEf;;;;;;;WAyCJ,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA;;;;;qBAWV;;;;;;;WAlFD,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA"}
@@ -8,8 +8,8 @@ type ScheduleFunction = (callback: () => void) => void;
8
8
  * Default scheduling function used by the notify manager.
9
9
  * Schedules the callback with the system's `setTimeout(callback, 0)`.
10
10
  */
11
- declare const defaultScheduler: ScheduleFunction;
12
- declare function createNotifyManager(): {
11
+ export declare const defaultScheduler: ScheduleFunction;
12
+ export declare function createNotifyManager(): {
13
13
  /**
14
14
  * Batches all updates scheduled inside the passed callback.
15
15
  * This is mainly used internally to optimize query client updating.
@@ -68,7 +68,7 @@ declare function createNotifyManager(): {
68
68
  /**
69
69
  * Handles scheduling and batching callbacks in TanStack Query.
70
70
  */
71
- declare const notifyManager: {
71
+ export declare const notifyManager: {
72
72
  /**
73
73
  * Batches all updates scheduled inside the passed callback.
74
74
  * This is mainly used internally to optimize query client updating.
@@ -125,5 +125,4 @@ declare const notifyManager: {
125
125
  readonly setScheduler: (fn: ScheduleFunction) => void;
126
126
  };
127
127
  //#endregion
128
- export { createNotifyManager, defaultScheduler, notifyManager };
129
128
  //# sourceMappingURL=notifyManager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"notifyManager.d.ts","names":[],"sources":["../../src/notifyManager.ts"],"mappings":";KAIK;KAEA,kBAAkB;KAElB,uBAAuB;KAEvB,mBAAmB,UAAU,sBAAsB,MAAM;KAEzD,oBAAoB;;;;;cAMZ,kBAAkB;iBAEf;;;;;;;WAyCJ,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA;;;;;cAWV;;;;;;;WAlFD,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;WAhDI,WAAA,UAAA;;;;;WAgEF,oBAAA,IAAA;;;;;;;;;;;;;;WAgBK,yBAAA,IAAA;;;;;;;;;;;;;;;;;;;WAqBV,eAAA,IAAA"}
1
+ {"version":3,"file":"notifyManager.d.ts","names":[],"sources":["../../src/notifyManager.ts"],"mappings":";KAIK;KAEA,kBAAkB;KAElB,uBAAuB;KAEvB,mBAAmB,UAAU,sBAAsB,MAAM;KAEzD,oBAAoB;;;;;qBAMZ,kBAAkB;wBAEf;;;;;;;WAyCJ,QAAA,GAAC,gBAAkB,MAAI;;;;WAgBlB,aAAA,UAAU,gBAAc,UACzB,mBAAmB,OAC5B,mBAAmB;;;;;W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