@tanstack/query-core 5.101.4 → 5.102.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 (370) hide show
  1. package/build/legacy/classPrivateFieldSet2-Bo0ZyOIv.cjs +56 -0
  2. package/build/legacy/classPrivateFieldSet2-CV7wyte-.js +27 -0
  3. package/build/legacy/classPrivateMethodInitSpec-CBwa_Y7C.js +7 -0
  4. package/build/legacy/classPrivateMethodInitSpec-DVhcLejn.cjs +12 -0
  5. package/build/legacy/environmentManager.cjs +25 -46
  6. package/build/legacy/environmentManager.cjs.map +1 -1
  7. package/build/legacy/environmentManager.d.cts +18 -2
  8. package/build/legacy/environmentManager.d.cts.map +1 -0
  9. package/build/legacy/environmentManager.d.ts +18 -2
  10. package/build/legacy/environmentManager.d.ts.map +1 -0
  11. package/build/legacy/environmentManager.js +23 -22
  12. package/build/legacy/environmentManager.js.map +1 -1
  13. package/build/legacy/focusManager.cjs +63 -104
  14. package/build/legacy/focusManager.cjs.map +1 -1
  15. package/build/legacy/focusManager.d.cts +18 -2
  16. package/build/legacy/focusManager.d.cts.map +1 -0
  17. package/build/legacy/focusManager.d.ts +18 -2
  18. package/build/legacy/focusManager.d.ts.map +1 -0
  19. package/build/legacy/focusManager.js +60 -76
  20. package/build/legacy/focusManager.js.map +1 -1
  21. package/build/legacy/hydration-D1E7ZbjZ.d.ts +1374 -0
  22. package/build/legacy/hydration-D1E7ZbjZ.d.ts.map +1 -0
  23. package/build/legacy/hydration-NBquRCHe.d.cts +1374 -0
  24. package/build/legacy/hydration-NBquRCHe.d.cts.map +1 -0
  25. package/build/legacy/hydration.cjs +114 -194
  26. package/build/legacy/hydration.cjs.map +1 -1
  27. package/build/legacy/hydration.d.cts +2 -7
  28. package/build/legacy/hydration.d.ts +2 -7
  29. package/build/legacy/hydration.js +108 -167
  30. package/build/legacy/hydration.js.map +1 -1
  31. package/build/legacy/index.cjs +59 -111
  32. package/build/legacy/index.cjs.map +1 -1
  33. package/build/legacy/index.d.cts +10 -141
  34. package/build/legacy/index.d.ts +10 -141
  35. package/build/legacy/index.js +16 -66
  36. package/build/legacy/index.js.map +1 -1
  37. package/build/legacy/infiniteQueryBehavior.cjs +92 -135
  38. package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
  39. package/build/legacy/infiniteQueryBehavior.d.cts +14 -3
  40. package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -0
  41. package/build/legacy/infiniteQueryBehavior.d.ts +14 -3
  42. package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -0
  43. package/build/legacy/infiniteQueryBehavior.js +89 -115
  44. package/build/legacy/infiniteQueryBehavior.js.map +1 -1
  45. package/build/legacy/infiniteQueryObserver.cjs +59 -88
  46. package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
  47. package/build/legacy/infiniteQueryObserver.d.cts +20 -1
  48. package/build/legacy/infiniteQueryObserver.d.cts.map +1 -0
  49. package/build/legacy/infiniteQueryObserver.d.ts +20 -1
  50. package/build/legacy/infiniteQueryObserver.d.ts.map +1 -0
  51. package/build/legacy/infiniteQueryObserver.js +57 -64
  52. package/build/legacy/infiniteQueryObserver.js.map +1 -1
  53. package/build/legacy/mutation.cjs +236 -329
  54. package/build/legacy/mutation.cjs.map +1 -1
  55. package/build/legacy/mutation.d.cts +2 -4
  56. package/build/legacy/mutation.d.ts +2 -4
  57. package/build/legacy/mutation.js +231 -299
  58. package/build/legacy/mutation.js.map +1 -1
  59. package/build/legacy/mutationCache.cjs +121 -166
  60. package/build/legacy/mutationCache.cjs.map +1 -1
  61. package/build/legacy/mutationCache.d.cts +2 -2
  62. package/build/legacy/mutationCache.d.ts +2 -2
  63. package/build/legacy/mutationCache.js +117 -130
  64. package/build/legacy/mutationCache.js.map +1 -1
  65. package/build/legacy/mutationObserver.cjs +134 -187
  66. package/build/legacy/mutationObserver.cjs.map +1 -1
  67. package/build/legacy/mutationObserver.d.cts +2 -1
  68. package/build/legacy/mutationObserver.d.ts +2 -1
  69. package/build/legacy/mutationObserver.js +132 -160
  70. package/build/legacy/mutationObserver.js.map +1 -1
  71. package/build/legacy/notifyManager.cjs +76 -110
  72. package/build/legacy/notifyManager.cjs.map +1 -1
  73. package/build/legacy/notifyManager.d.cts +48 -3
  74. package/build/legacy/notifyManager.d.cts.map +1 -0
  75. package/build/legacy/notifyManager.d.ts +48 -3
  76. package/build/legacy/notifyManager.d.ts.map +1 -0
  77. package/build/legacy/notifyManager.js +73 -84
  78. package/build/legacy/notifyManager.js.map +1 -1
  79. package/build/legacy/onlineManager.cjs +57 -93
  80. package/build/legacy/onlineManager.cjs.map +1 -1
  81. package/build/legacy/onlineManager.d.cts +17 -2
  82. package/build/legacy/onlineManager.d.cts.map +1 -0
  83. package/build/legacy/onlineManager.d.ts +17 -2
  84. package/build/legacy/onlineManager.d.ts.map +1 -0
  85. package/build/legacy/onlineManager.js +54 -65
  86. package/build/legacy/onlineManager.js.map +1 -1
  87. package/build/legacy/queriesObserver.cjs +203 -262
  88. package/build/legacy/queriesObserver.cjs.map +1 -1
  89. package/build/legacy/queriesObserver.d.cts +23 -2
  90. package/build/legacy/queriesObserver.d.cts.map +1 -0
  91. package/build/legacy/queriesObserver.d.ts +23 -2
  92. package/build/legacy/queriesObserver.d.ts.map +1 -0
  93. package/build/legacy/queriesObserver.js +199 -233
  94. package/build/legacy/queriesObserver.js.map +1 -1
  95. package/build/legacy/query.cjs +409 -491
  96. package/build/legacy/query.cjs.map +1 -1
  97. package/build/legacy/query.d.cts +2 -9
  98. package/build/legacy/query.d.ts +2 -9
  99. package/build/legacy/query.js +402 -467
  100. package/build/legacy/query.js.map +1 -1
  101. package/build/legacy/queryCache.cjs +99 -128
  102. package/build/legacy/queryCache.cjs.map +1 -1
  103. package/build/legacy/queryCache.d.cts +2 -3
  104. package/build/legacy/queryCache.d.ts +2 -3
  105. package/build/legacy/queryCache.js +96 -100
  106. package/build/legacy/queryCache.js.map +1 -1
  107. package/build/legacy/queryClient.cjs +295 -345
  108. package/build/legacy/queryClient.cjs.map +1 -1
  109. package/build/legacy/queryClient.d.cts +2 -1
  110. package/build/legacy/queryClient.d.ts +2 -1
  111. package/build/legacy/queryClient.js +292 -318
  112. package/build/legacy/queryClient.js.map +1 -1
  113. package/build/legacy/queryObserver.cjs +350 -502
  114. package/build/legacy/queryObserver.cjs.map +1 -1
  115. package/build/legacy/queryObserver.d.cts +2 -1
  116. package/build/legacy/queryObserver.d.ts +2 -1
  117. package/build/legacy/queryObserver.js +345 -480
  118. package/build/legacy/queryObserver.js.map +1 -1
  119. package/build/legacy/removable-DeMjhW5M.d.cts +13 -0
  120. package/build/legacy/removable-DeMjhW5M.d.cts.map +1 -0
  121. package/build/legacy/removable-DeMjhW5M.d.ts +13 -0
  122. package/build/legacy/removable-DeMjhW5M.d.ts.map +1 -0
  123. package/build/legacy/removable.cjs +31 -67
  124. package/build/legacy/removable.cjs.map +1 -1
  125. package/build/legacy/removable.d.cts +2 -1
  126. package/build/legacy/removable.d.ts +2 -1
  127. package/build/legacy/removable.js +28 -39
  128. package/build/legacy/removable.js.map +1 -1
  129. package/build/legacy/retryer.cjs +127 -155
  130. package/build/legacy/retryer.cjs.map +1 -1
  131. package/build/legacy/retryer.d.cts +2 -7
  132. package/build/legacy/retryer.d.ts +2 -7
  133. package/build/legacy/retryer.js +121 -127
  134. package/build/legacy/retryer.js.map +1 -1
  135. package/build/legacy/streamedQuery.cjs +49 -78
  136. package/build/legacy/streamedQuery.cjs.map +1 -1
  137. package/build/legacy/streamedQuery.d.cts +33 -1
  138. package/build/legacy/streamedQuery.d.cts.map +1 -0
  139. package/build/legacy/streamedQuery.d.ts +33 -1
  140. package/build/legacy/streamedQuery.d.ts.map +1 -0
  141. package/build/legacy/streamedQuery.js +47 -54
  142. package/build/legacy/streamedQuery.js.map +1 -1
  143. package/build/legacy/subscribable-CbifVTKz.d.cts +12 -0
  144. package/build/legacy/subscribable-CbifVTKz.d.cts.map +1 -0
  145. package/build/legacy/subscribable-CbifVTKz.d.ts +12 -0
  146. package/build/legacy/subscribable-CbifVTKz.d.ts.map +1 -0
  147. package/build/legacy/subscribable.cjs +22 -48
  148. package/build/legacy/subscribable.cjs.map +1 -1
  149. package/build/legacy/subscribable.d.cts +2 -1
  150. package/build/legacy/subscribable.d.ts +2 -1
  151. package/build/legacy/subscribable.js +21 -25
  152. package/build/legacy/subscribable.js.map +1 -1
  153. package/build/legacy/timeoutManager.cjs +64 -105
  154. package/build/legacy/timeoutManager.cjs.map +1 -1
  155. package/build/legacy/timeoutManager.d.cts +60 -7
  156. package/build/legacy/timeoutManager.d.cts.map +1 -0
  157. package/build/legacy/timeoutManager.d.ts +60 -7
  158. package/build/legacy/timeoutManager.d.ts.map +1 -0
  159. package/build/legacy/timeoutManager.js +60 -76
  160. package/build/legacy/timeoutManager.js.map +1 -1
  161. package/build/legacy/types.cjs +10 -35
  162. package/build/legacy/types.cjs.map +1 -1
  163. package/build/legacy/types.d.cts +2 -94
  164. package/build/legacy/types.d.ts +2 -94
  165. package/build/legacy/types.js +7 -10
  166. package/build/legacy/types.js.map +1 -1
  167. package/build/legacy/utils.cjs +175 -300
  168. package/build/legacy/utils.cjs.map +1 -1
  169. package/build/legacy/utils.d.cts +2 -30
  170. package/build/legacy/utils.d.ts +2 -30
  171. package/build/legacy/utils.js +149 -252
  172. package/build/legacy/utils.js.map +1 -1
  173. package/build/modern/environmentManager.cjs +25 -46
  174. package/build/modern/environmentManager.cjs.map +1 -1
  175. package/build/modern/environmentManager.d.cts +18 -2
  176. package/build/modern/environmentManager.d.cts.map +1 -0
  177. package/build/modern/environmentManager.d.ts +18 -2
  178. package/build/modern/environmentManager.d.ts.map +1 -0
  179. package/build/modern/environmentManager.js +23 -20
  180. package/build/modern/environmentManager.js.map +1 -1
  181. package/build/modern/focusManager.cjs +56 -90
  182. package/build/modern/focusManager.cjs.map +1 -1
  183. package/build/modern/focusManager.d.cts +18 -2
  184. package/build/modern/focusManager.d.cts.map +1 -0
  185. package/build/modern/focusManager.d.ts +18 -2
  186. package/build/modern/focusManager.d.ts.map +1 -0
  187. package/build/modern/focusManager.js +53 -63
  188. package/build/modern/focusManager.js.map +1 -1
  189. package/build/modern/hydration-D1E7ZbjZ.d.ts +1374 -0
  190. package/build/modern/hydration-D1E7ZbjZ.d.ts.map +1 -0
  191. package/build/modern/hydration-NBquRCHe.d.cts +1374 -0
  192. package/build/modern/hydration-NBquRCHe.d.cts.map +1 -0
  193. package/build/modern/hydration.cjs +105 -189
  194. package/build/modern/hydration.cjs.map +1 -1
  195. package/build/modern/hydration.d.cts +2 -7
  196. package/build/modern/hydration.d.ts +2 -7
  197. package/build/modern/hydration.js +99 -160
  198. package/build/modern/hydration.js.map +1 -1
  199. package/build/modern/index.cjs +59 -111
  200. package/build/modern/index.cjs.map +1 -1
  201. package/build/modern/index.d.cts +10 -141
  202. package/build/modern/index.d.ts +10 -141
  203. package/build/modern/index.js +16 -64
  204. package/build/modern/index.js.map +1 -1
  205. package/build/modern/infiniteQueryBehavior.cjs +89 -131
  206. package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
  207. package/build/modern/infiniteQueryBehavior.d.cts +14 -3
  208. package/build/modern/infiniteQueryBehavior.d.cts.map +1 -0
  209. package/build/modern/infiniteQueryBehavior.d.ts +14 -3
  210. package/build/modern/infiniteQueryBehavior.d.ts.map +1 -0
  211. package/build/modern/infiniteQueryBehavior.js +86 -109
  212. package/build/modern/infiniteQueryBehavior.js.map +1 -1
  213. package/build/modern/infiniteQueryObserver.cjs +58 -87
  214. package/build/modern/infiniteQueryObserver.cjs.map +1 -1
  215. package/build/modern/infiniteQueryObserver.d.cts +20 -1
  216. package/build/modern/infiniteQueryObserver.d.cts.map +1 -0
  217. package/build/modern/infiniteQueryObserver.d.ts +20 -1
  218. package/build/modern/infiniteQueryObserver.d.ts.map +1 -0
  219. package/build/modern/infiniteQueryObserver.js +56 -61
  220. package/build/modern/infiniteQueryObserver.js.map +1 -1
  221. package/build/modern/mutation.cjs +221 -303
  222. package/build/modern/mutation.cjs.map +1 -1
  223. package/build/modern/mutation.d.cts +2 -4
  224. package/build/modern/mutation.d.ts +2 -4
  225. package/build/modern/mutation.js +216 -274
  226. package/build/modern/mutation.js.map +1 -1
  227. package/build/modern/mutationCache.cjs +111 -145
  228. package/build/modern/mutationCache.cjs.map +1 -1
  229. package/build/modern/mutationCache.d.cts +2 -2
  230. package/build/modern/mutationCache.d.ts +2 -2
  231. package/build/modern/mutationCache.js +107 -117
  232. package/build/modern/mutationCache.js.map +1 -1
  233. package/build/modern/mutationObserver.cjs +115 -162
  234. package/build/modern/mutationObserver.cjs.map +1 -1
  235. package/build/modern/mutationObserver.d.cts +2 -1
  236. package/build/modern/mutationObserver.d.ts +2 -1
  237. package/build/modern/mutationObserver.js +113 -136
  238. package/build/modern/mutationObserver.js.map +1 -1
  239. package/build/modern/notifyManager.cjs +76 -110
  240. package/build/modern/notifyManager.cjs.map +1 -1
  241. package/build/modern/notifyManager.d.cts +48 -3
  242. package/build/modern/notifyManager.d.cts.map +1 -0
  243. package/build/modern/notifyManager.d.ts +48 -3
  244. package/build/modern/notifyManager.d.ts.map +1 -0
  245. package/build/modern/notifyManager.js +73 -82
  246. package/build/modern/notifyManager.js.map +1 -1
  247. package/build/modern/onlineManager.cjs +51 -80
  248. package/build/modern/onlineManager.cjs.map +1 -1
  249. package/build/modern/onlineManager.d.cts +17 -2
  250. package/build/modern/onlineManager.d.cts.map +1 -0
  251. package/build/modern/onlineManager.d.ts +17 -2
  252. package/build/modern/onlineManager.d.ts.map +1 -0
  253. package/build/modern/onlineManager.js +48 -53
  254. package/build/modern/onlineManager.js.map +1 -1
  255. package/build/modern/queriesObserver.cjs +185 -237
  256. package/build/modern/queriesObserver.cjs.map +1 -1
  257. package/build/modern/queriesObserver.d.cts +23 -2
  258. package/build/modern/queriesObserver.d.cts.map +1 -0
  259. package/build/modern/queriesObserver.d.ts +23 -2
  260. package/build/modern/queriesObserver.d.ts.map +1 -0
  261. package/build/modern/queriesObserver.js +181 -209
  262. package/build/modern/queriesObserver.js.map +1 -1
  263. package/build/modern/query.cjs +384 -464
  264. package/build/modern/query.cjs.map +1 -1
  265. package/build/modern/query.d.cts +2 -9
  266. package/build/modern/query.d.ts +2 -9
  267. package/build/modern/query.js +377 -441
  268. package/build/modern/query.js.map +1 -1
  269. package/build/modern/queryCache.cjs +97 -119
  270. package/build/modern/queryCache.cjs.map +1 -1
  271. package/build/modern/queryCache.d.cts +2 -3
  272. package/build/modern/queryCache.d.ts +2 -3
  273. package/build/modern/queryCache.js +94 -92
  274. package/build/modern/queryCache.js.map +1 -1
  275. package/build/modern/queryClient.cjs +281 -318
  276. package/build/modern/queryClient.cjs.map +1 -1
  277. package/build/modern/queryClient.d.cts +2 -1
  278. package/build/modern/queryClient.d.ts +2 -1
  279. package/build/modern/queryClient.js +278 -299
  280. package/build/modern/queryClient.js.map +1 -1
  281. package/build/modern/queryObserver.cjs +329 -475
  282. package/build/modern/queryObserver.cjs.map +1 -1
  283. package/build/modern/queryObserver.d.cts +2 -1
  284. package/build/modern/queryObserver.d.ts +2 -1
  285. package/build/modern/queryObserver.js +324 -454
  286. package/build/modern/queryObserver.js.map +1 -1
  287. package/build/modern/removable-DeMjhW5M.d.cts +13 -0
  288. package/build/modern/removable-DeMjhW5M.d.cts.map +1 -0
  289. package/build/modern/removable-DeMjhW5M.d.ts +13 -0
  290. package/build/modern/removable-DeMjhW5M.d.ts.map +1 -0
  291. package/build/modern/removable.cjs +27 -56
  292. package/build/modern/removable.cjs.map +1 -1
  293. package/build/modern/removable.d.cts +2 -1
  294. package/build/modern/removable.d.ts +2 -1
  295. package/build/modern/removable.js +24 -29
  296. package/build/modern/removable.js.map +1 -1
  297. package/build/modern/retryer.cjs +121 -151
  298. package/build/modern/retryer.cjs.map +1 -1
  299. package/build/modern/retryer.d.cts +2 -7
  300. package/build/modern/retryer.d.ts +2 -7
  301. package/build/modern/retryer.js +115 -121
  302. package/build/modern/retryer.js.map +1 -1
  303. package/build/modern/streamedQuery.cjs +49 -78
  304. package/build/modern/streamedQuery.cjs.map +1 -1
  305. package/build/modern/streamedQuery.d.cts +33 -1
  306. package/build/modern/streamedQuery.d.cts.map +1 -0
  307. package/build/modern/streamedQuery.d.ts +33 -1
  308. package/build/modern/streamedQuery.d.ts.map +1 -0
  309. package/build/modern/streamedQuery.js +47 -52
  310. package/build/modern/streamedQuery.js.map +1 -1
  311. package/build/modern/subscribable-CbifVTKz.d.cts +12 -0
  312. package/build/modern/subscribable-CbifVTKz.d.cts.map +1 -0
  313. package/build/modern/subscribable-CbifVTKz.d.ts +12 -0
  314. package/build/modern/subscribable-CbifVTKz.d.ts.map +1 -0
  315. package/build/modern/subscribable.cjs +22 -48
  316. package/build/modern/subscribable.cjs.map +1 -1
  317. package/build/modern/subscribable.d.cts +2 -1
  318. package/build/modern/subscribable.d.ts +2 -1
  319. package/build/modern/subscribable.js +21 -23
  320. package/build/modern/subscribable.js.map +1 -1
  321. package/build/modern/timeoutManager.cjs +59 -93
  322. package/build/modern/timeoutManager.cjs.map +1 -1
  323. package/build/modern/timeoutManager.d.cts +60 -7
  324. package/build/modern/timeoutManager.d.cts.map +1 -0
  325. package/build/modern/timeoutManager.d.ts +60 -7
  326. package/build/modern/timeoutManager.d.ts.map +1 -0
  327. package/build/modern/timeoutManager.js +55 -65
  328. package/build/modern/timeoutManager.js.map +1 -1
  329. package/build/modern/types.cjs +10 -35
  330. package/build/modern/types.cjs.map +1 -1
  331. package/build/modern/types.d.cts +2 -94
  332. package/build/modern/types.d.ts +2 -94
  333. package/build/modern/types.js +8 -9
  334. package/build/modern/types.js.map +1 -1
  335. package/build/modern/utils.cjs +175 -300
  336. package/build/modern/utils.cjs.map +1 -1
  337. package/build/modern/utils.d.cts +2 -30
  338. package/build/modern/utils.d.ts +2 -30
  339. package/build/modern/utils.js +149 -250
  340. package/build/modern/utils.js.map +1 -1
  341. package/package.json +3 -5
  342. package/src/hydration.ts +62 -44
  343. package/src/index.ts +1 -0
  344. package/src/mutation.ts +14 -6
  345. package/src/mutationObserver.ts +8 -0
  346. package/src/queriesObserver.ts +14 -8
  347. package/src/query.ts +14 -6
  348. package/src/queryClient.ts +90 -2
  349. package/src/queryObserver.ts +41 -64
  350. package/src/retryer.ts +20 -12
  351. package/src/types.ts +70 -58
  352. package/build/legacy/_tsup-dts-rollup.d.cts +0 -2188
  353. package/build/legacy/_tsup-dts-rollup.d.ts +0 -2188
  354. package/build/legacy/chunk-PXG64RU4.js +0 -25
  355. package/build/legacy/chunk-PXG64RU4.js.map +0 -1
  356. package/build/legacy/thenable.cjs +0 -76
  357. package/build/legacy/thenable.cjs.map +0 -1
  358. package/build/legacy/thenable.d.cts +0 -6
  359. package/build/legacy/thenable.d.ts +0 -6
  360. package/build/legacy/thenable.js +0 -52
  361. package/build/legacy/thenable.js.map +0 -1
  362. package/build/modern/_tsup-dts-rollup.d.cts +0 -2188
  363. package/build/modern/_tsup-dts-rollup.d.ts +0 -2188
  364. package/build/modern/thenable.cjs +0 -75
  365. package/build/modern/thenable.cjs.map +0 -1
  366. package/build/modern/thenable.d.cts +0 -6
  367. package/build/modern/thenable.d.ts +0 -6
  368. package/build/modern/thenable.js +0 -49
  369. package/build/modern/thenable.js.map +0 -1
  370. package/src/thenable.ts +0 -111
@@ -1,472 +1,342 @@
1
- // src/queryObserver.ts
2
- import { focusManager } from "./focusManager.js";
1
+ import { timeoutManager } from "./timeoutManager.js";
2
+ import { isValidTimeout, noop, replaceData, resolveQueryBoolean, resolveStaleTime, shallowEqualObjects, timeUntilStale } from "./utils.js";
3
3
  import { environmentManager } from "./environmentManager.js";
4
+ import { Subscribable } from "./subscribable.js";
5
+ import { focusManager } from "./focusManager.js";
4
6
  import { notifyManager } from "./notifyManager.js";
5
7
  import { fetchState } from "./query.js";
6
- import { Subscribable } from "./subscribable.js";
7
- import { pendingThenable } from "./thenable.js";
8
- import {
9
- isValidTimeout,
10
- noop,
11
- replaceData,
12
- resolveQueryBoolean,
13
- resolveStaleTime,
14
- shallowEqualObjects,
15
- timeUntilStale
16
- } from "./utils.js";
17
- import { timeoutManager } from "./timeoutManager.js";
8
+ //#region src/queryObserver.ts
18
9
  var QueryObserver = class extends Subscribable {
19
- constructor(client, options) {
20
- super();
21
- this.options = options;
22
- this.#client = client;
23
- this.#selectError = null;
24
- this.#currentThenable = pendingThenable();
25
- this.bindMethods();
26
- this.setOptions(options);
27
- }
28
- #client;
29
- #currentQuery = void 0;
30
- #currentQueryInitialState = void 0;
31
- #currentResult = void 0;
32
- #currentResultState;
33
- #currentResultOptions;
34
- #currentThenable;
35
- #selectError;
36
- #selectFn;
37
- #selectResult;
38
- // This property keeps track of the last query with defined data.
39
- // It will be used to pass the previous data and query to the placeholder function between renders.
40
- #lastQueryWithDefinedData;
41
- #staleTimeoutId;
42
- #refetchIntervalId;
43
- #currentRefetchInterval;
44
- #trackedProps = /* @__PURE__ */ new Set();
45
- bindMethods() {
46
- this.refetch = this.refetch.bind(this);
47
- }
48
- onSubscribe() {
49
- if (this.listeners.size === 1) {
50
- this.#currentQuery.addObserver(this);
51
- if (shouldFetchOnMount(this.#currentQuery, this.options)) {
52
- this.#executeFetch();
53
- } else {
54
- this.updateResult();
55
- }
56
- this.#updateTimers();
57
- }
58
- }
59
- onUnsubscribe() {
60
- if (!this.hasListeners()) {
61
- this.destroy();
62
- }
63
- }
64
- shouldFetchOnReconnect() {
65
- return shouldFetchOn(
66
- this.#currentQuery,
67
- this.options,
68
- this.options.refetchOnReconnect
69
- );
70
- }
71
- shouldFetchOnWindowFocus() {
72
- return shouldFetchOn(
73
- this.#currentQuery,
74
- this.options,
75
- this.options.refetchOnWindowFocus
76
- );
77
- }
78
- destroy() {
79
- this.listeners = /* @__PURE__ */ new Set();
80
- this.#clearStaleTimeout();
81
- this.#clearRefetchInterval();
82
- this.#currentQuery.removeObserver(this);
83
- }
84
- setOptions(options) {
85
- const prevOptions = this.options;
86
- const prevQuery = this.#currentQuery;
87
- this.options = this.#client.defaultQueryOptions(options);
88
- if (this.options.enabled !== void 0 && typeof this.options.enabled !== "boolean" && typeof this.options.enabled !== "function" && typeof resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== "boolean") {
89
- throw new Error(
90
- "Expected enabled to be a boolean or a callback that returns a boolean"
91
- );
92
- }
93
- this.#updateQuery();
94
- this.#currentQuery.setOptions(this.options);
95
- if (prevOptions._defaulted && !shallowEqualObjects(this.options, prevOptions)) {
96
- this.#client.getQueryCache().notify({
97
- type: "observerOptionsUpdated",
98
- query: this.#currentQuery,
99
- observer: this
100
- });
101
- }
102
- const mounted = this.hasListeners();
103
- if (mounted && shouldFetchOptionally(
104
- this.#currentQuery,
105
- prevQuery,
106
- this.options,
107
- prevOptions
108
- )) {
109
- this.#executeFetch();
110
- }
111
- this.updateResult();
112
- if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || resolveStaleTime(this.options.staleTime, this.#currentQuery) !== resolveStaleTime(prevOptions.staleTime, this.#currentQuery))) {
113
- this.#updateStaleTimeout();
114
- }
115
- const nextRefetchInterval = this.#computeRefetchInterval();
116
- if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || nextRefetchInterval !== this.#currentRefetchInterval)) {
117
- this.#updateRefetchInterval(nextRefetchInterval);
118
- }
119
- }
120
- getOptimisticResult(options) {
121
- const query = this.#client.getQueryCache().build(this.#client, options);
122
- const result = this.createResult(query, options);
123
- if (shouldAssignObserverCurrentProperties(this, result)) {
124
- this.#currentResult = result;
125
- this.#currentResultOptions = this.options;
126
- this.#currentResultState = this.#currentQuery.state;
127
- }
128
- return result;
129
- }
130
- getCurrentResult() {
131
- return this.#currentResult;
132
- }
133
- trackResult(result, onPropTracked) {
134
- return new Proxy(result, {
135
- get: (target, key) => {
136
- this.trackProp(key);
137
- onPropTracked?.(key);
138
- if (key === "promise") {
139
- this.trackProp("data");
140
- if (!this.options.experimental_prefetchInRender && this.#currentThenable.status === "pending") {
141
- this.#currentThenable.reject(
142
- new Error(
143
- "experimental_prefetchInRender feature flag is not enabled"
144
- )
145
- );
146
- }
147
- }
148
- return Reflect.get(target, key);
149
- }
150
- });
151
- }
152
- trackProp(key) {
153
- this.#trackedProps.add(key);
154
- }
155
- getCurrentQuery() {
156
- return this.#currentQuery;
157
- }
158
- refetch({ ...options } = {}) {
159
- return this.fetch({
160
- ...options
161
- });
162
- }
163
- fetchOptimistic(options) {
164
- const defaultedOptions = this.#client.defaultQueryOptions(options);
165
- const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
166
- return query.fetch().then(() => this.createResult(query, defaultedOptions));
167
- }
168
- fetch(fetchOptions) {
169
- return this.#executeFetch({
170
- ...fetchOptions,
171
- cancelRefetch: fetchOptions.cancelRefetch ?? true
172
- }).then(() => {
173
- this.updateResult();
174
- return this.#currentResult;
175
- });
176
- }
177
- #executeFetch(fetchOptions) {
178
- this.#updateQuery();
179
- let promise = this.#currentQuery.fetch(
180
- this.options,
181
- fetchOptions
182
- );
183
- if (!fetchOptions?.throwOnError) {
184
- promise = promise.catch(noop);
185
- }
186
- return promise;
187
- }
188
- #updateStaleTimeout() {
189
- this.#clearStaleTimeout();
190
- const staleTime = resolveStaleTime(
191
- this.options.staleTime,
192
- this.#currentQuery
193
- );
194
- if (environmentManager.isServer() || this.#currentResult.isStale || !isValidTimeout(staleTime)) {
195
- return;
196
- }
197
- const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime);
198
- const timeout = time + 1;
199
- this.#staleTimeoutId = timeoutManager.setTimeout(() => {
200
- if (!this.#currentResult.isStale) {
201
- this.updateResult();
202
- }
203
- }, timeout);
204
- }
205
- #computeRefetchInterval() {
206
- return (typeof this.options.refetchInterval === "function" ? this.options.refetchInterval(this.#currentQuery) : this.options.refetchInterval) ?? false;
207
- }
208
- #updateRefetchInterval(nextInterval) {
209
- this.#clearRefetchInterval();
210
- this.#currentRefetchInterval = nextInterval;
211
- if (environmentManager.isServer() || resolveQueryBoolean(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
212
- return;
213
- }
214
- this.#refetchIntervalId = timeoutManager.setInterval(() => {
215
- if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
216
- this.#executeFetch();
217
- }
218
- }, this.#currentRefetchInterval);
219
- }
220
- #updateTimers() {
221
- this.#updateStaleTimeout();
222
- this.#updateRefetchInterval(this.#computeRefetchInterval());
223
- }
224
- #clearStaleTimeout() {
225
- if (this.#staleTimeoutId !== void 0) {
226
- timeoutManager.clearTimeout(this.#staleTimeoutId);
227
- this.#staleTimeoutId = void 0;
228
- }
229
- }
230
- #clearRefetchInterval() {
231
- if (this.#refetchIntervalId !== void 0) {
232
- timeoutManager.clearInterval(this.#refetchIntervalId);
233
- this.#refetchIntervalId = void 0;
234
- }
235
- }
236
- createResult(query, options) {
237
- const prevQuery = this.#currentQuery;
238
- const prevOptions = this.options;
239
- const prevResult = this.#currentResult;
240
- const prevResultState = this.#currentResultState;
241
- const prevResultOptions = this.#currentResultOptions;
242
- const queryChange = query !== prevQuery;
243
- const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
244
- const { state } = query;
245
- let newState = { ...state };
246
- let isPlaceholderData = false;
247
- let data;
248
- if (options._optimisticResults) {
249
- const mounted = this.hasListeners();
250
- const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
251
- const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
252
- if (fetchOnMount || fetchOptionally) {
253
- newState = {
254
- ...newState,
255
- ...fetchState(state.data, query.options)
256
- };
257
- }
258
- if (options._optimisticResults === "isRestoring") {
259
- newState.fetchStatus = "idle";
260
- }
261
- }
262
- let { error, errorUpdatedAt, status } = newState;
263
- data = newState.data;
264
- let skipSelect = false;
265
- if (options.placeholderData !== void 0 && data === void 0 && status === "pending") {
266
- let placeholderData;
267
- if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
268
- placeholderData = prevResult.data;
269
- skipSelect = true;
270
- } else {
271
- placeholderData = typeof options.placeholderData === "function" ? options.placeholderData(
272
- this.#lastQueryWithDefinedData?.state.data,
273
- this.#lastQueryWithDefinedData
274
- ) : options.placeholderData;
275
- }
276
- if (placeholderData !== void 0) {
277
- status = "success";
278
- data = replaceData(
279
- prevResult?.data,
280
- placeholderData,
281
- options
282
- );
283
- isPlaceholderData = true;
284
- }
285
- }
286
- if (options.select && data !== void 0 && !skipSelect) {
287
- if (prevResult && data === prevResultState?.data && options.select === this.#selectFn) {
288
- data = this.#selectResult;
289
- } else {
290
- try {
291
- this.#selectFn = options.select;
292
- data = options.select(data);
293
- data = replaceData(prevResult?.data, data, options);
294
- this.#selectResult = data;
295
- this.#selectError = null;
296
- } catch (selectError) {
297
- this.#selectError = selectError;
298
- }
299
- }
300
- }
301
- if (this.#selectError) {
302
- error = this.#selectError;
303
- data = this.#selectResult;
304
- errorUpdatedAt = Date.now();
305
- status = "error";
306
- }
307
- const isFetching = newState.fetchStatus === "fetching";
308
- const isPending = status === "pending";
309
- const isError = status === "error";
310
- const isLoading = isPending && isFetching;
311
- const hasData = data !== void 0;
312
- const result = {
313
- status,
314
- fetchStatus: newState.fetchStatus,
315
- isPending,
316
- isSuccess: status === "success",
317
- isError,
318
- isInitialLoading: isLoading,
319
- isLoading,
320
- data,
321
- dataUpdatedAt: newState.dataUpdatedAt,
322
- error,
323
- errorUpdatedAt,
324
- failureCount: newState.fetchFailureCount,
325
- failureReason: newState.fetchFailureReason,
326
- errorUpdateCount: newState.errorUpdateCount,
327
- isFetched: query.isFetched(),
328
- isFetchedAfterMount: newState.dataUpdateCount > queryInitialState.dataUpdateCount || newState.errorUpdateCount > queryInitialState.errorUpdateCount,
329
- isFetching,
330
- isRefetching: isFetching && !isPending,
331
- isLoadingError: isError && !hasData,
332
- isPaused: newState.fetchStatus === "paused",
333
- isPlaceholderData,
334
- isRefetchError: isError && hasData,
335
- isStale: isStale(query, options),
336
- refetch: this.refetch,
337
- promise: this.#currentThenable,
338
- isEnabled: resolveQueryBoolean(options.enabled, query) !== false
339
- };
340
- const nextResult = result;
341
- if (this.options.experimental_prefetchInRender) {
342
- const hasResultData = nextResult.data !== void 0;
343
- const isErrorWithoutData = nextResult.status === "error" && !hasResultData;
344
- const finalizeThenableIfPossible = (thenable) => {
345
- if (isErrorWithoutData) {
346
- thenable.reject(nextResult.error);
347
- } else if (hasResultData) {
348
- thenable.resolve(nextResult.data);
349
- }
350
- };
351
- const recreateThenable = () => {
352
- const pending = this.#currentThenable = nextResult.promise = pendingThenable();
353
- finalizeThenableIfPossible(pending);
354
- };
355
- const prevThenable = this.#currentThenable;
356
- switch (prevThenable.status) {
357
- case "pending":
358
- if (query.queryHash === prevQuery.queryHash) {
359
- finalizeThenableIfPossible(prevThenable);
360
- }
361
- break;
362
- case "fulfilled":
363
- if (isErrorWithoutData || nextResult.data !== prevThenable.value) {
364
- recreateThenable();
365
- }
366
- break;
367
- case "rejected":
368
- if (!isErrorWithoutData || nextResult.error !== prevThenable.reason) {
369
- recreateThenable();
370
- }
371
- break;
372
- }
373
- }
374
- return nextResult;
375
- }
376
- updateResult() {
377
- const prevResult = this.#currentResult;
378
- const nextResult = this.createResult(this.#currentQuery, this.options);
379
- this.#currentResultState = this.#currentQuery.state;
380
- this.#currentResultOptions = this.options;
381
- if (this.#currentResultState.data !== void 0) {
382
- this.#lastQueryWithDefinedData = this.#currentQuery;
383
- }
384
- if (shallowEqualObjects(nextResult, prevResult)) {
385
- return;
386
- }
387
- this.#currentResult = nextResult;
388
- const shouldNotifyListeners = () => {
389
- if (!prevResult) {
390
- return true;
391
- }
392
- const { notifyOnChangeProps } = this.options;
393
- const notifyOnChangePropsValue = typeof notifyOnChangeProps === "function" ? notifyOnChangeProps() : notifyOnChangeProps;
394
- if (notifyOnChangePropsValue === "all" || !notifyOnChangePropsValue && !this.#trackedProps.size) {
395
- return true;
396
- }
397
- const includedProps = new Set(
398
- notifyOnChangePropsValue ?? this.#trackedProps
399
- );
400
- if (this.options.throwOnError) {
401
- includedProps.add("error");
402
- }
403
- return Object.keys(this.#currentResult).some((key) => {
404
- const typedKey = key;
405
- const changed = this.#currentResult[typedKey] !== prevResult[typedKey];
406
- return changed && includedProps.has(typedKey);
407
- });
408
- };
409
- this.#notify({ listeners: shouldNotifyListeners() });
410
- }
411
- #updateQuery() {
412
- const query = this.#client.getQueryCache().build(this.#client, this.options);
413
- if (query === this.#currentQuery) {
414
- return;
415
- }
416
- const prevQuery = this.#currentQuery;
417
- this.#currentQuery = query;
418
- this.#currentQueryInitialState = query.state;
419
- if (this.hasListeners()) {
420
- prevQuery?.removeObserver(this);
421
- query.addObserver(this);
422
- }
423
- }
424
- onQueryUpdate() {
425
- this.updateResult();
426
- if (this.hasListeners()) {
427
- this.#updateTimers();
428
- }
429
- }
430
- #notify(notifyOptions) {
431
- notifyManager.batch(() => {
432
- if (notifyOptions.listeners) {
433
- this.listeners.forEach((listener) => {
434
- listener(this.#currentResult);
435
- });
436
- }
437
- this.#client.getQueryCache().notify({
438
- query: this.#currentQuery,
439
- type: "observerResultsUpdated"
440
- });
441
- });
442
- }
10
+ #client;
11
+ #currentQuery = void 0;
12
+ #currentQueryInitialState = void 0;
13
+ #currentResult = void 0;
14
+ #currentResultState;
15
+ #currentResultOptions;
16
+ #selectError;
17
+ #selectFn;
18
+ #selectResult;
19
+ #lastQueryWithDefinedData;
20
+ #staleTimeoutId;
21
+ #refetchIntervalId;
22
+ #currentRefetchInterval;
23
+ #trackedProps = /* @__PURE__ */ new Set();
24
+ constructor(client, options) {
25
+ super();
26
+ this.options = options;
27
+ this.#client = client;
28
+ this.#selectError = null;
29
+ this.bindMethods();
30
+ this.setOptions(options);
31
+ }
32
+ bindMethods() {
33
+ this.refetch = this.refetch.bind(this);
34
+ }
35
+ onSubscribe() {
36
+ if (this.listeners.size === 1) {
37
+ this.#currentQuery.addObserver(this);
38
+ if (shouldFetchOnMount(this.#currentQuery, this.options)) this.#executeFetch();
39
+ else this.updateResult();
40
+ this.#updateTimers();
41
+ }
42
+ }
43
+ onUnsubscribe() {
44
+ if (!this.hasListeners()) this.destroy();
45
+ }
46
+ shouldFetchOnReconnect() {
47
+ return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnReconnect);
48
+ }
49
+ shouldFetchOnWindowFocus() {
50
+ return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
51
+ }
52
+ destroy() {
53
+ this.listeners = /* @__PURE__ */ new Set();
54
+ this.#clearStaleTimeout();
55
+ this.#clearRefetchInterval();
56
+ this.#currentQuery.removeObserver(this);
57
+ }
58
+ setOptions(options) {
59
+ const prevOptions = this.options;
60
+ const prevQuery = this.#currentQuery;
61
+ this.options = this.#client.defaultQueryOptions(options);
62
+ if (this.options.enabled !== void 0 && typeof this.options.enabled !== "boolean" && typeof this.options.enabled !== "function" && typeof resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== "boolean") throw new Error("Expected enabled to be a boolean or a callback that returns a boolean");
63
+ this.#updateQuery();
64
+ this.#currentQuery.setOptions(this.options);
65
+ if (prevOptions._defaulted && !shallowEqualObjects(this.options, prevOptions)) this.#client.getQueryCache().notify({
66
+ type: "observerOptionsUpdated",
67
+ query: this.#currentQuery,
68
+ observer: this
69
+ });
70
+ const mounted = this.hasListeners();
71
+ if (mounted && shouldFetchOptionally(this.#currentQuery, prevQuery, this.options, prevOptions)) this.#executeFetch();
72
+ this.updateResult();
73
+ if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || resolveStaleTime(this.options.staleTime, this.#currentQuery) !== resolveStaleTime(prevOptions.staleTime, this.#currentQuery))) this.#updateStaleTimeout();
74
+ const nextRefetchInterval = this.#computeRefetchInterval();
75
+ if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || nextRefetchInterval !== this.#currentRefetchInterval)) this.#updateRefetchInterval(nextRefetchInterval);
76
+ }
77
+ getOptimisticResult(options) {
78
+ const query = this.#client.getQueryCache().build(this.#client, options);
79
+ const result = this.createResult(query, options);
80
+ if (shouldAssignObserverCurrentProperties(this, result)) {
81
+ this.#currentResult = result;
82
+ this.#currentResultOptions = this.options;
83
+ this.#currentResultState = this.#currentQuery.state;
84
+ }
85
+ return result;
86
+ }
87
+ getCurrentResult() {
88
+ return this.#currentResult;
89
+ }
90
+ trackResult(result, onPropTracked) {
91
+ return new Proxy(result, { get: (target, key) => {
92
+ this.trackProp(key);
93
+ onPropTracked?.(key);
94
+ return Reflect.get(target, key);
95
+ } });
96
+ }
97
+ trackProp(key) {
98
+ this.#trackedProps.add(key);
99
+ }
100
+ getCurrentQuery() {
101
+ return this.#currentQuery;
102
+ }
103
+ refetch({ ...options } = {}) {
104
+ return this.fetch({ ...options });
105
+ }
106
+ fetchOptimistic(options) {
107
+ const defaultedOptions = this.#client.defaultQueryOptions(options);
108
+ const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
109
+ let unsubscribe = () => {};
110
+ let resolveEarly;
111
+ const cachePromise = new Promise((resolve) => {
112
+ resolveEarly = resolve;
113
+ unsubscribe = this.#client.getQueryCache().subscribe((event) => {
114
+ if (event.type === "updated" && event.query.queryHash === query.queryHash && query.state.data !== void 0) {
115
+ unsubscribe();
116
+ resolve(this.createResult(query, defaultedOptions));
117
+ }
118
+ });
119
+ });
120
+ return Promise.race([query.fetch().then(() => {
121
+ const result = this.createResult(query, defaultedOptions);
122
+ resolveEarly?.(result);
123
+ return result;
124
+ }).finally(() => {
125
+ unsubscribe();
126
+ }), cachePromise]);
127
+ }
128
+ fetch(fetchOptions) {
129
+ return this.#executeFetch({
130
+ ...fetchOptions,
131
+ cancelRefetch: fetchOptions.cancelRefetch ?? true
132
+ }).then(() => {
133
+ this.updateResult();
134
+ return this.#currentResult;
135
+ });
136
+ }
137
+ #executeFetch(fetchOptions) {
138
+ this.#updateQuery();
139
+ let promise = this.#currentQuery.fetch(this.options, fetchOptions);
140
+ if (!fetchOptions?.throwOnError) promise = promise.catch(noop);
141
+ return promise;
142
+ }
143
+ #updateStaleTimeout() {
144
+ this.#clearStaleTimeout();
145
+ const staleTime = resolveStaleTime(this.options.staleTime, this.#currentQuery);
146
+ if (environmentManager.isServer() || this.#currentResult.isStale || !isValidTimeout(staleTime)) return;
147
+ const timeout = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime) + 1;
148
+ this.#staleTimeoutId = timeoutManager.setTimeout(() => {
149
+ if (!this.#currentResult.isStale) this.updateResult();
150
+ }, timeout);
151
+ }
152
+ #computeRefetchInterval() {
153
+ return (typeof this.options.refetchInterval === "function" ? this.options.refetchInterval(this.#currentQuery) : this.options.refetchInterval) ?? false;
154
+ }
155
+ #updateRefetchInterval(nextInterval) {
156
+ this.#clearRefetchInterval();
157
+ this.#currentRefetchInterval = nextInterval;
158
+ if (environmentManager.isServer() || resolveQueryBoolean(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) return;
159
+ this.#refetchIntervalId = timeoutManager.setInterval(() => {
160
+ if (this.options.refetchIntervalInBackground || focusManager.isFocused()) this.#executeFetch();
161
+ }, this.#currentRefetchInterval);
162
+ }
163
+ #updateTimers() {
164
+ this.#updateStaleTimeout();
165
+ this.#updateRefetchInterval(this.#computeRefetchInterval());
166
+ }
167
+ #clearStaleTimeout() {
168
+ if (this.#staleTimeoutId !== void 0) {
169
+ timeoutManager.clearTimeout(this.#staleTimeoutId);
170
+ this.#staleTimeoutId = void 0;
171
+ }
172
+ }
173
+ #clearRefetchInterval() {
174
+ if (this.#refetchIntervalId !== void 0) {
175
+ timeoutManager.clearInterval(this.#refetchIntervalId);
176
+ this.#refetchIntervalId = void 0;
177
+ }
178
+ }
179
+ createResult(query, options) {
180
+ const prevQuery = this.#currentQuery;
181
+ const prevOptions = this.options;
182
+ const prevResult = this.#currentResult;
183
+ const prevResultState = this.#currentResultState;
184
+ const prevResultOptions = this.#currentResultOptions;
185
+ const queryInitialState = query !== prevQuery ? query.state : this.#currentQueryInitialState;
186
+ const { state } = query;
187
+ let newState = { ...state };
188
+ let isPlaceholderData = false;
189
+ let data;
190
+ if (options._optimisticResults) {
191
+ const mounted = this.hasListeners();
192
+ const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
193
+ const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
194
+ if (fetchOnMount || fetchOptionally) newState = {
195
+ ...newState,
196
+ ...fetchState(state.data, query.options)
197
+ };
198
+ if (options._optimisticResults === "isRestoring") newState.fetchStatus = "idle";
199
+ }
200
+ let { error, errorUpdatedAt, status } = newState;
201
+ data = newState.data;
202
+ let skipSelect = false;
203
+ if (options.placeholderData !== void 0 && data === void 0 && status === "pending") {
204
+ let placeholderData;
205
+ if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
206
+ placeholderData = prevResult.data;
207
+ skipSelect = true;
208
+ } else placeholderData = typeof options.placeholderData === "function" ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
209
+ if (placeholderData !== void 0) {
210
+ status = "success";
211
+ data = replaceData(prevResult?.data, placeholderData, options);
212
+ isPlaceholderData = true;
213
+ }
214
+ }
215
+ if (options.select && data !== void 0 && !skipSelect) {
216
+ if (prevResult && data === prevResultState?.data && options.select === this.#selectFn) data = this.#selectResult;
217
+ else try {
218
+ this.#selectFn = options.select;
219
+ data = options.select(data);
220
+ data = replaceData(prevResult?.data, data, options);
221
+ this.#selectResult = data;
222
+ this.#selectError = null;
223
+ } catch (selectError) {
224
+ this.#selectError = selectError;
225
+ }
226
+ } else if (data === void 0) this.#selectError = null;
227
+ if (this.#selectError) {
228
+ error = this.#selectError;
229
+ data = this.#selectResult;
230
+ errorUpdatedAt = Date.now();
231
+ status = "error";
232
+ isPlaceholderData = false;
233
+ }
234
+ const isFetching = newState.fetchStatus === "fetching";
235
+ const isPending = status === "pending";
236
+ const isError = status === "error";
237
+ const isLoading = isPending && isFetching;
238
+ const hasData = data !== void 0;
239
+ return {
240
+ status,
241
+ fetchStatus: newState.fetchStatus,
242
+ isPending,
243
+ isSuccess: status === "success",
244
+ isError,
245
+ isInitialLoading: isLoading,
246
+ isLoading,
247
+ data,
248
+ dataUpdatedAt: newState.dataUpdatedAt,
249
+ error,
250
+ errorUpdatedAt,
251
+ failureCount: newState.fetchFailureCount,
252
+ failureReason: newState.fetchFailureReason,
253
+ errorUpdateCount: newState.errorUpdateCount,
254
+ isFetched: query.isFetched(),
255
+ isFetchedAfterMount: newState.dataUpdateCount > queryInitialState.dataUpdateCount || newState.errorUpdateCount > queryInitialState.errorUpdateCount,
256
+ isFetching,
257
+ isRefetching: isFetching && !isPending,
258
+ isLoadingError: isError && !hasData,
259
+ isPaused: newState.fetchStatus === "paused",
260
+ isPlaceholderData,
261
+ isRefetchError: isError && hasData,
262
+ isStale: isStale(query, options),
263
+ refetch: this.refetch,
264
+ isEnabled: resolveQueryBoolean(options.enabled, query) !== false
265
+ };
266
+ }
267
+ updateResult() {
268
+ const prevResult = this.#currentResult;
269
+ const nextResult = this.createResult(this.#currentQuery, this.options);
270
+ this.#currentResultState = this.#currentQuery.state;
271
+ this.#currentResultOptions = this.options;
272
+ if (this.#currentResultState.data !== void 0) this.#lastQueryWithDefinedData = this.#currentQuery;
273
+ if (shallowEqualObjects(nextResult, prevResult)) return;
274
+ this.#currentResult = nextResult;
275
+ const shouldNotifyListeners = () => {
276
+ if (!prevResult) return true;
277
+ const { notifyOnChangeProps } = this.options;
278
+ const notifyOnChangePropsValue = typeof notifyOnChangeProps === "function" ? notifyOnChangeProps() : notifyOnChangeProps;
279
+ if (notifyOnChangePropsValue === "all" || !notifyOnChangePropsValue && !this.#trackedProps.size) return true;
280
+ const includedProps = new Set(notifyOnChangePropsValue ?? this.#trackedProps);
281
+ if (this.options.throwOnError) includedProps.add("error");
282
+ return Object.keys(this.#currentResult).some((key) => {
283
+ const typedKey = key;
284
+ return this.#currentResult[typedKey] !== prevResult[typedKey] && includedProps.has(typedKey);
285
+ });
286
+ };
287
+ this.#notify({ listeners: shouldNotifyListeners() });
288
+ }
289
+ #updateQuery() {
290
+ const query = this.#client.getQueryCache().build(this.#client, this.options);
291
+ if (query === this.#currentQuery) return;
292
+ const prevQuery = this.#currentQuery;
293
+ this.#currentQuery = query;
294
+ this.#currentQueryInitialState = query.state;
295
+ if (this.hasListeners()) {
296
+ prevQuery?.removeObserver(this);
297
+ query.addObserver(this);
298
+ }
299
+ }
300
+ onQueryUpdate() {
301
+ this.updateResult();
302
+ if (this.hasListeners()) this.#updateTimers();
303
+ }
304
+ #notify(notifyOptions) {
305
+ notifyManager.batch(() => {
306
+ if (notifyOptions.listeners) this.listeners.forEach((listener) => {
307
+ listener(this.#currentResult);
308
+ });
309
+ this.#client.getQueryCache().notify({
310
+ query: this.#currentQuery,
311
+ type: "observerResultsUpdated"
312
+ });
313
+ });
314
+ }
443
315
  };
444
316
  function shouldLoadOnMount(query, options) {
445
- return resolveQueryBoolean(options.enabled, query) !== false && query.state.data === void 0 && !(query.state.status === "error" && resolveQueryBoolean(options.retryOnMount, query) === false);
317
+ return resolveQueryBoolean(options.enabled, query) !== false && query.state.data === void 0 && !(query.state.status === "error" && resolveQueryBoolean(options.retryOnMount, query) === false);
446
318
  }
447
319
  function shouldFetchOnMount(query, options) {
448
- return shouldLoadOnMount(query, options) || query.state.data !== void 0 && shouldFetchOn(query, options, options.refetchOnMount);
320
+ return shouldLoadOnMount(query, options) || query.state.data !== void 0 && shouldFetchOn(query, options, options.refetchOnMount);
449
321
  }
450
322
  function shouldFetchOn(query, options, field) {
451
- if (resolveQueryBoolean(options.enabled, query) !== false && resolveStaleTime(options.staleTime, query) !== "static") {
452
- const value = typeof field === "function" ? field(query) : field;
453
- return value === "always" || value !== false && isStale(query, options);
454
- }
455
- return false;
323
+ if (resolveQueryBoolean(options.enabled, query) !== false && resolveStaleTime(options.staleTime, query) !== "static") {
324
+ const value = typeof field === "function" ? field(query) : field;
325
+ return value === "always" || value !== false && isStale(query, options);
326
+ }
327
+ return false;
456
328
  }
457
329
  function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
458
- return (query !== prevQuery || resolveQueryBoolean(prevOptions.enabled, query) === false) && (!options.suspense || query.state.status !== "error") && isStale(query, options);
330
+ return (query !== prevQuery || resolveQueryBoolean(prevOptions.enabled, query) === false) && (!options.suspense || query.state.status !== "error") && isStale(query, options);
459
331
  }
460
332
  function isStale(query, options) {
461
- return resolveQueryBoolean(options.enabled, query) !== false && query.isStaleByTime(resolveStaleTime(options.staleTime, query));
333
+ return resolveQueryBoolean(options.enabled, query) !== false && query.isStaleByTime(resolveStaleTime(options.staleTime, query));
462
334
  }
463
335
  function shouldAssignObserverCurrentProperties(observer, optimisticResult) {
464
- if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
465
- return true;
466
- }
467
- return false;
336
+ if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) return true;
337
+ return false;
468
338
  }
469
- export {
470
- QueryObserver
471
- };
339
+ //#endregion
340
+ export { QueryObserver };
341
+
472
342
  //# sourceMappingURL=queryObserver.js.map