@tanstack/query-core 5.0.0-alpha.6 → 5.0.0-alpha.61

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 (226) hide show
  1. package/build/lib/{focusManager.mjs → focusManager.cjs} +9 -6
  2. package/build/lib/{focusManager.mjs.map → focusManager.cjs.map} +1 -1
  3. package/build/lib/focusManager.d.ts +2 -1
  4. package/build/lib/focusManager.d.ts.map +1 -0
  5. package/build/lib/focusManager.js +20 -39
  6. package/build/lib/focusManager.js.map +1 -1
  7. package/build/lib/{hydration.mjs → hydration.cjs} +11 -20
  8. package/build/lib/hydration.cjs.map +1 -0
  9. package/build/lib/hydration.d.ts +3 -6
  10. package/build/lib/hydration.d.ts.map +1 -0
  11. package/build/lib/hydration.js +7 -28
  12. package/build/lib/hydration.js.map +1 -1
  13. package/build/lib/index.cjs +40 -0
  14. package/build/lib/index.cjs.map +1 -0
  15. package/build/lib/index.d.ts +5 -3
  16. package/build/lib/index.d.ts.map +1 -0
  17. package/build/lib/index.js +13 -39
  18. package/build/lib/index.js.map +1 -1
  19. package/build/lib/{infiniteQueryBehavior.mjs → infiniteQueryBehavior.cjs} +8 -4
  20. package/build/lib/{infiniteQueryBehavior.mjs.map → infiniteQueryBehavior.cjs.map} +1 -1
  21. package/build/lib/infiniteQueryBehavior.d.ts +1 -0
  22. package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
  23. package/build/lib/infiniteQueryBehavior.js +7 -12
  24. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  25. package/build/lib/{infiniteQueryObserver.mjs → infiniteQueryObserver.cjs} +11 -9
  26. package/build/lib/{infiniteQueryObserver.mjs.map → infiniteQueryObserver.cjs.map} +1 -1
  27. package/build/lib/infiniteQueryObserver.d.ts +2 -1
  28. package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
  29. package/build/lib/infiniteQueryObserver.js +10 -13
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/{mutation.mjs → mutation.cjs} +13 -10
  32. package/build/lib/mutation.cjs.map +1 -0
  33. package/build/lib/mutation.d.ts +2 -1
  34. package/build/lib/mutation.d.ts.map +1 -0
  35. package/build/lib/mutation.js +107 -135
  36. package/build/lib/mutation.js.map +1 -1
  37. package/build/lib/{mutationCache.mjs → mutationCache.cjs} +21 -18
  38. package/build/lib/mutationCache.cjs.map +1 -0
  39. package/build/lib/mutationCache.d.ts +3 -2
  40. package/build/lib/mutationCache.d.ts.map +1 -0
  41. package/build/lib/mutationCache.js +33 -47
  42. package/build/lib/mutationCache.js.map +1 -1
  43. package/build/lib/{mutationObserver.mjs → mutationObserver.cjs} +13 -11
  44. package/build/lib/mutationObserver.cjs.map +1 -0
  45. package/build/lib/mutationObserver.d.ts +2 -1
  46. package/build/lib/mutationObserver.d.ts.map +1 -0
  47. package/build/lib/mutationObserver.js +59 -91
  48. package/build/lib/mutationObserver.js.map +1 -1
  49. package/build/lib/{notifyManager.mjs → notifyManager.cjs} +8 -5
  50. package/build/lib/{notifyManager.mjs.map → notifyManager.cjs.map} +1 -1
  51. package/build/lib/notifyManager.d.ts +4 -3
  52. package/build/lib/notifyManager.d.ts.map +1 -0
  53. package/build/lib/notifyManager.js +4 -7
  54. package/build/lib/notifyManager.js.map +1 -1
  55. package/build/lib/{onlineManager.mjs → onlineManager.cjs} +16 -10
  56. package/build/lib/onlineManager.cjs.map +1 -0
  57. package/build/lib/onlineManager.d.ts +2 -1
  58. package/build/lib/onlineManager.d.ts.map +1 -0
  59. package/build/lib/onlineManager.js +26 -41
  60. package/build/lib/onlineManager.js.map +1 -1
  61. package/build/lib/{queriesObserver.mjs → queriesObserver.cjs} +51 -26
  62. package/build/lib/queriesObserver.cjs.map +1 -0
  63. package/build/lib/queriesObserver.d.ts +14 -6
  64. package/build/lib/queriesObserver.d.ts.map +1 -0
  65. package/build/lib/queriesObserver.js +99 -107
  66. package/build/lib/queriesObserver.js.map +1 -1
  67. package/build/lib/{query.mjs → query.cjs} +22 -20
  68. package/build/lib/query.cjs.map +1 -0
  69. package/build/lib/query.d.ts +3 -2
  70. package/build/lib/query.d.ts.map +1 -0
  71. package/build/lib/query.js +173 -224
  72. package/build/lib/query.js.map +1 -1
  73. package/build/lib/{queryCache.mjs → queryCache.cjs} +28 -23
  74. package/build/lib/queryCache.cjs.map +1 -0
  75. package/build/lib/queryCache.d.ts +3 -3
  76. package/build/lib/queryCache.d.ts.map +1 -0
  77. package/build/lib/queryCache.js +31 -37
  78. package/build/lib/queryCache.js.map +1 -1
  79. package/build/lib/{queryClient.mjs → queryClient.cjs} +50 -42
  80. package/build/lib/queryClient.cjs.map +1 -0
  81. package/build/lib/queryClient.d.ts +2 -2
  82. package/build/lib/queryClient.d.ts.map +1 -0
  83. package/build/lib/queryClient.js +98 -134
  84. package/build/lib/queryClient.js.map +1 -1
  85. package/build/lib/{queryObserver.mjs → queryObserver.cjs} +40 -50
  86. package/build/lib/queryObserver.cjs.map +1 -0
  87. package/build/lib/queryObserver.d.ts +6 -7
  88. package/build/lib/queryObserver.d.ts.map +1 -0
  89. package/build/lib/queryObserver.js +208 -323
  90. package/build/lib/queryObserver.js.map +1 -1
  91. package/build/lib/{removable.mjs → removable.cjs} +7 -5
  92. package/build/lib/{removable.mjs.map → removable.cjs.map} +1 -1
  93. package/build/lib/removable.d.ts +1 -0
  94. package/build/lib/removable.d.ts.map +1 -0
  95. package/build/lib/removable.js +9 -18
  96. package/build/lib/removable.js.map +1 -1
  97. package/build/lib/{retryer.mjs → retryer.cjs} +15 -8
  98. package/build/lib/{retryer.mjs.map → retryer.cjs.map} +1 -1
  99. package/build/lib/retryer.d.ts +5 -4
  100. package/build/lib/retryer.d.ts.map +1 -0
  101. package/build/lib/retryer.js +21 -25
  102. package/build/lib/retryer.js.map +1 -1
  103. package/build/lib/{subscribable.mjs → subscribable.cjs} +8 -6
  104. package/build/lib/subscribable.cjs.map +1 -0
  105. package/build/lib/subscribable.d.ts +3 -2
  106. package/build/lib/subscribable.d.ts.map +1 -0
  107. package/build/lib/subscribable.js +5 -7
  108. package/build/lib/subscribable.js.map +1 -1
  109. package/build/lib/tests/focusManager.test.d.ts +1 -0
  110. package/build/lib/tests/focusManager.test.d.ts.map +1 -0
  111. package/build/lib/tests/hydration.test.d.ts +1 -0
  112. package/build/lib/tests/hydration.test.d.ts.map +1 -0
  113. package/build/lib/tests/infiniteQueryBehavior.test.d.ts +1 -0
  114. package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
  115. package/build/lib/tests/infiniteQueryObserver.test.d.ts +1 -0
  116. package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
  117. package/build/lib/tests/mutationCache.test.d.ts +1 -0
  118. package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
  119. package/build/lib/tests/mutationObserver.test.d.ts +1 -0
  120. package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
  121. package/build/lib/tests/mutations.test.d.ts +1 -0
  122. package/build/lib/tests/mutations.test.d.ts.map +1 -0
  123. package/build/lib/tests/notifyManager.test.d.ts +1 -0
  124. package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
  125. package/build/lib/tests/onlineManager.test.d.ts +1 -0
  126. package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
  127. package/build/lib/tests/queriesObserver.test.d.ts +1 -0
  128. package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
  129. package/build/lib/tests/query.test.d.ts +1 -0
  130. package/build/lib/tests/query.test.d.ts.map +1 -0
  131. package/build/lib/tests/queryCache.test.d.ts +1 -0
  132. package/build/lib/tests/queryCache.test.d.ts.map +1 -0
  133. package/build/lib/tests/queryClient.test.d.ts +1 -0
  134. package/build/lib/tests/queryClient.test.d.ts.map +1 -0
  135. package/build/lib/tests/queryObserver.test.d.ts +1 -0
  136. package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
  137. package/build/lib/tests/utils.d.ts +3 -2
  138. package/build/lib/tests/utils.d.ts.map +1 -0
  139. package/build/lib/tests/utils.test.d.ts +1 -0
  140. package/build/lib/tests/utils.test.d.ts.map +1 -0
  141. package/build/lib/types.d.ts +34 -46
  142. package/build/lib/types.d.ts.map +1 -0
  143. package/build/lib/{utils.mjs → utils.cjs} +23 -2
  144. package/build/lib/{utils.mjs.map → utils.cjs.map} +1 -1
  145. package/build/lib/utils.d.ts +4 -3
  146. package/build/lib/utils.d.ts.map +1 -0
  147. package/build/lib/utils.js +2 -23
  148. package/build/lib/utils.js.map +1 -1
  149. package/package.json +13 -9
  150. package/src/hydration.ts +18 -37
  151. package/src/index.ts +3 -4
  152. package/src/mutation.ts +1 -1
  153. package/src/mutationCache.ts +4 -4
  154. package/src/mutationObserver.ts +1 -1
  155. package/src/onlineManager.ts +8 -4
  156. package/src/queriesObserver.ts +75 -25
  157. package/src/query.ts +4 -4
  158. package/src/queryCache.ts +5 -8
  159. package/src/queryClient.ts +20 -20
  160. package/src/queryObserver.ts +47 -55
  161. package/src/subscribable.ts +5 -5
  162. package/src/tests/hydration.test.tsx +7 -3
  163. package/src/tests/query.test.tsx +1 -1
  164. package/src/tests/queryCache.test.tsx +1 -19
  165. package/src/tests/queryClient.test.tsx +39 -3
  166. package/src/tests/queryObserver.test.tsx +121 -0
  167. package/src/types.ts +14 -21
  168. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +0 -13
  169. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
  170. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -16
  171. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  172. package/build/lib/focusManager.esm.js +0 -87
  173. package/build/lib/focusManager.esm.js.map +0 -1
  174. package/build/lib/hydration.esm.js +0 -100
  175. package/build/lib/hydration.esm.js.map +0 -1
  176. package/build/lib/hydration.mjs.map +0 -1
  177. package/build/lib/index.esm.js +0 -14
  178. package/build/lib/index.esm.js.map +0 -1
  179. package/build/lib/index.mjs +0 -14
  180. package/build/lib/index.mjs.map +0 -1
  181. package/build/lib/infiniteQueryBehavior.esm.js +0 -127
  182. package/build/lib/infiniteQueryBehavior.esm.js.map +0 -1
  183. package/build/lib/infiniteQueryObserver.esm.js +0 -76
  184. package/build/lib/infiniteQueryObserver.esm.js.map +0 -1
  185. package/build/lib/mutation.esm.js +0 -259
  186. package/build/lib/mutation.esm.js.map +0 -1
  187. package/build/lib/mutation.mjs.map +0 -1
  188. package/build/lib/mutationCache.esm.js +0 -95
  189. package/build/lib/mutationCache.esm.js.map +0 -1
  190. package/build/lib/mutationCache.mjs.map +0 -1
  191. package/build/lib/mutationObserver.esm.js +0 -126
  192. package/build/lib/mutationObserver.esm.js.map +0 -1
  193. package/build/lib/mutationObserver.mjs.map +0 -1
  194. package/build/lib/notifyManager.esm.js +0 -89
  195. package/build/lib/notifyManager.esm.js.map +0 -1
  196. package/build/lib/onlineManager.esm.js +0 -88
  197. package/build/lib/onlineManager.esm.js.map +0 -1
  198. package/build/lib/onlineManager.mjs.map +0 -1
  199. package/build/lib/queriesObserver.esm.js +0 -165
  200. package/build/lib/queriesObserver.esm.js.map +0 -1
  201. package/build/lib/queriesObserver.mjs.map +0 -1
  202. package/build/lib/query.esm.js +0 -469
  203. package/build/lib/query.esm.js.map +0 -1
  204. package/build/lib/query.mjs.map +0 -1
  205. package/build/lib/queryCache.esm.js +0 -108
  206. package/build/lib/queryCache.esm.js.map +0 -1
  207. package/build/lib/queryCache.mjs.map +0 -1
  208. package/build/lib/queryClient.esm.js +0 -317
  209. package/build/lib/queryClient.esm.js.map +0 -1
  210. package/build/lib/queryClient.mjs.map +0 -1
  211. package/build/lib/queryObserver.esm.js +0 -541
  212. package/build/lib/queryObserver.esm.js.map +0 -1
  213. package/build/lib/queryObserver.mjs.map +0 -1
  214. package/build/lib/removable.esm.js +0 -36
  215. package/build/lib/removable.esm.js.map +0 -1
  216. package/build/lib/retryer.esm.js +0 -151
  217. package/build/lib/retryer.esm.js.map +0 -1
  218. package/build/lib/subscribable.esm.js +0 -26
  219. package/build/lib/subscribable.esm.js.map +0 -1
  220. package/build/lib/subscribable.mjs.map +0 -1
  221. package/build/lib/utils.esm.js +0 -227
  222. package/build/lib/utils.esm.js.map +0 -1
  223. package/build/umd/index.development.js +0 -2683
  224. package/build/umd/index.development.js.map +0 -1
  225. package/build/umd/index.production.js +0 -2
  226. package/build/umd/index.production.js.map +0 -1
@@ -1,6 +1,5 @@
1
1
  import { sleep, queryKey, createQueryClient } from './utils'
2
- import { QueryClient } from '..'
3
- import { QueryCache, QueryObserver } from '..'
2
+ import { QueryClient, QueryCache, QueryObserver } from '..'
4
3
  import { waitFor } from '@testing-library/react'
5
4
  import { vi } from 'vitest'
6
5
 
@@ -329,23 +328,6 @@ describe('queryCache', () => {
329
328
  })
330
329
  })
331
330
 
332
- describe('QueryCacheConfig.createStore', () => {
333
- test('should call createStore', async () => {
334
- const createStore = vi.fn().mockImplementation(() => new Map())
335
- new QueryCache({ createStore })
336
- expect(createStore).toHaveBeenCalledWith()
337
- })
338
-
339
- test('should use created store', async () => {
340
- const store = new Map()
341
- const spy = vi.spyOn(store, 'get')
342
-
343
- new QueryCache({ createStore: () => store }).get('key')
344
-
345
- expect(spy).toHaveBeenCalledTimes(1)
346
- })
347
- })
348
-
349
331
  describe('QueryCache.add', () => {
350
332
  test('should not try to add a query already added to the cache', async () => {
351
333
  const key = queryKey()
@@ -1,15 +1,24 @@
1
1
  import { waitFor } from '@testing-library/react'
2
2
  import '@testing-library/jest-dom'
3
3
 
4
- import { sleep, queryKey, createQueryClient } from './utils'
4
+ import {
5
+ sleep,
6
+ queryKey,
7
+ createQueryClient,
8
+ mockNavigatorOnLine,
9
+ } from './utils'
5
10
  import type {
6
11
  QueryCache,
7
12
  QueryClient,
8
13
  QueryFunction,
9
14
  QueryObserverOptions,
10
15
  } from '..'
11
- import { MutationObserver, QueryObserver } from '..'
12
- import { focusManager, onlineManager } from '..'
16
+ import {
17
+ MutationObserver,
18
+ QueryObserver,
19
+ focusManager,
20
+ onlineManager,
21
+ } from '..'
13
22
  import { noop } from '../utils'
14
23
  import { vi } from 'vitest'
15
24
 
@@ -1020,6 +1029,33 @@ describe('queryClient', () => {
1020
1029
  }
1021
1030
  expect(error).toEqual('error')
1022
1031
  })
1032
+
1033
+ test('should resolve Promise immediately if query is paused', async () => {
1034
+ const key1 = queryKey()
1035
+ const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
1036
+ await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
1037
+ const onlineMock = mockNavigatorOnLine(false)
1038
+
1039
+ await queryClient.refetchQueries({ queryKey: key1 })
1040
+
1041
+ // if we reach this point, the test succeeds because the Promise was resolved immediately
1042
+ expect(queryFn1).toHaveBeenCalledTimes(1)
1043
+ onlineMock.mockRestore()
1044
+ })
1045
+
1046
+ test('should refetch if query we are offline but query networkMode is always', async () => {
1047
+ const key1 = queryKey()
1048
+ queryClient.setQueryDefaults(key1, { networkMode: 'always' })
1049
+ const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
1050
+ await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
1051
+ const onlineMock = mockNavigatorOnLine(false)
1052
+
1053
+ await queryClient.refetchQueries({ queryKey: key1 })
1054
+
1055
+ // initial fetch + refetch (even though we are offline)
1056
+ expect(queryFn1).toHaveBeenCalledTimes(2)
1057
+ onlineMock.mockRestore()
1058
+ })
1023
1059
  })
1024
1060
 
1025
1061
  describe('invalidateQueries', () => {
@@ -691,6 +691,127 @@ describe('queryObserver', () => {
691
691
  expect(observer.getCurrentResult().isPlaceholderData).toBe(false)
692
692
  })
693
693
 
694
+ test('should pass the correct previous queryKey (from prevQuery) to placeholderData function params with select', async () => {
695
+ const results: QueryObserverResult[] = []
696
+ const keys: Array<readonly unknown[] | null> = []
697
+
698
+ const key1 = queryKey()
699
+ const key2 = queryKey()
700
+
701
+ const data1 = { value: 'data1' }
702
+ const data2 = { value: 'data2' }
703
+
704
+ const observer = new QueryObserver(queryClient, {
705
+ queryKey: key1,
706
+ queryFn: () => data1,
707
+ placeholderData: (prev, prevQuery) => {
708
+ keys.push(prevQuery?.queryKey || null)
709
+ return prev
710
+ },
711
+ select: (data) => data.value,
712
+ })
713
+
714
+ const unsubscribe = observer.subscribe((result) => {
715
+ results.push(result)
716
+ })
717
+
718
+ await sleep(1)
719
+
720
+ observer.setOptions({
721
+ queryKey: key2,
722
+ queryFn: () => data2,
723
+ placeholderData: (prev, prevQuery) => {
724
+ keys.push(prevQuery?.queryKey || null)
725
+ return prev
726
+ },
727
+ select: (data) => data.value,
728
+ })
729
+
730
+ await sleep(1)
731
+ unsubscribe()
732
+ expect(results.length).toBe(4)
733
+ expect(keys.length).toBe(3)
734
+ expect(keys[0]).toBe(null) // First Query - status: 'pending', fetchStatus: 'idle'
735
+ expect(keys[1]).toBe(null) // First Query - status: 'pending', fetchStatus: 'fetching'
736
+ expect(keys[2]).toBe(key1) // Second Query - status: 'pending', fetchStatus: 'fetching'
737
+
738
+ expect(results[0]).toMatchObject({
739
+ data: undefined,
740
+ status: 'pending',
741
+ fetchStatus: 'fetching',
742
+ }) // Initial fetch
743
+ expect(results[1]).toMatchObject({
744
+ data: 'data1',
745
+ status: 'success',
746
+ fetchStatus: 'idle',
747
+ }) // Successful fetch
748
+ expect(results[2]).toMatchObject({
749
+ data: 'data1',
750
+ status: 'success',
751
+ fetchStatus: 'fetching',
752
+ }) // Fetch for new key, but using previous data as placeholder
753
+ expect(results[3]).toMatchObject({
754
+ data: 'data2',
755
+ status: 'success',
756
+ fetchStatus: 'idle',
757
+ }) // Successful fetch for new key
758
+ })
759
+
760
+ test('should pass the correct previous data to placeholderData function params when select function is used in conjunction', async () => {
761
+ const results: QueryObserverResult[] = []
762
+
763
+ const key1 = queryKey()
764
+ const key2 = queryKey()
765
+
766
+ const data1 = { value: 'data1' }
767
+ const data2 = { value: 'data2' }
768
+
769
+ const observer = new QueryObserver(queryClient, {
770
+ queryKey: key1,
771
+ queryFn: () => data1,
772
+ placeholderData: (prev) => prev,
773
+ select: (data) => data.value,
774
+ })
775
+
776
+ const unsubscribe = observer.subscribe((result) => {
777
+ results.push(result)
778
+ })
779
+
780
+ await sleep(1)
781
+
782
+ observer.setOptions({
783
+ queryKey: key2,
784
+ queryFn: () => data2,
785
+ placeholderData: (prev) => prev,
786
+ select: (data) => data.value,
787
+ })
788
+
789
+ await sleep(1)
790
+ unsubscribe()
791
+
792
+ expect(results.length).toBe(4)
793
+ expect(results[0]).toMatchObject({
794
+ data: undefined,
795
+ status: 'pending',
796
+ fetchStatus: 'fetching',
797
+ }) // Initial fetch
798
+ expect(results[1]).toMatchObject({
799
+ data: 'data1',
800
+ status: 'success',
801
+ fetchStatus: 'idle',
802
+ }) // Successful fetch
803
+ expect(results[2]).toMatchObject({
804
+ data: 'data1',
805
+ status: 'success',
806
+ fetchStatus: 'fetching',
807
+ }) // Fetch for new key, but using previous data as placeholder
808
+ expect(results[3]).toMatchObject({
809
+ data: 'data2',
810
+ status: 'success',
811
+ fetchStatus: 'idle',
812
+ }) // Successful fetch for new key
813
+ })
814
+
694
815
  test('setOptions should notify cache listeners', async () => {
695
816
  const key = queryKey()
696
817
 
package/src/types.ts CHANGED
@@ -1,12 +1,11 @@
1
1
  /* istanbul ignore file */
2
2
 
3
3
  import type { MutationState } from './mutation'
4
- import type { QueryBehavior, Query } from './query'
4
+ import type { FetchDirection, QueryBehavior, Query } from './query'
5
5
  import type { RetryValue, RetryDelayValue } from './retryer'
6
6
  import type { QueryFilters, QueryTypeFilter } from './utils'
7
7
  import type { QueryCache } from './queryCache'
8
8
  import type { MutationCache } from './mutationCache'
9
- import type { FetchDirection } from './query'
10
9
 
11
10
  export interface Register {
12
11
  // defaultError: Error
@@ -49,8 +48,14 @@ export type InitialDataFunction<T> = () => T | undefined
49
48
 
50
49
  type NonFunctionGuard<T> = T extends Function ? never : T
51
50
 
52
- export type PlaceholderDataFunction<TQueryData> = (
51
+ export type PlaceholderDataFunction<
52
+ TQueryFnData = unknown,
53
+ TError = DefaultError,
54
+ TQueryData = TQueryFnData,
55
+ TQueryKey extends QueryKey = QueryKey,
56
+ > = (
53
57
  previousData: TQueryData | undefined,
58
+ previousQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined,
54
59
  ) => TQueryData | undefined
55
60
 
56
61
  export type QueriesPlaceholderDataFunction<TQueryData> = () =>
@@ -154,7 +159,7 @@ export interface InfiniteQueryPageParamsOptions<
154
159
  getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
155
160
  }
156
161
 
157
- export type ThrowErrors<
162
+ export type ThrowOnError<
158
163
  TQueryFnData,
159
164
  TError,
160
165
  TQueryData,
@@ -202,7 +207,7 @@ export interface QueryObserverOptions<
202
207
  | ((
203
208
  data: TData | undefined,
204
209
  query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
205
- ) => number | false)
210
+ ) => number | false | undefined)
206
211
  /**
207
212
  * If set to `true`, the query will continue to refetch while their tab/window is in the background.
208
213
  * Defaults to `false`.
@@ -259,18 +264,6 @@ export interface QueryObserverOptions<
259
264
  * By default, access to properties will be tracked, and the component will only re-render when one of the tracked properties change.
260
265
  */
261
266
  notifyOnChangeProps?: Array<keyof InfiniteQueryObserverResult> | 'all'
262
- /**
263
- * This callback will fire any time the query successfully fetches new data.
264
- */
265
- onSuccess?: (data: TData) => void
266
- /**
267
- * This callback will fire if the query encounters an error and will be passed the error.
268
- */
269
- onError?: (err: TError) => void
270
- /**
271
- * This callback will fire any time the query is either successfully fetched or errors and be passed either the data or error.
272
- */
273
- onSettled?: (data: TData | undefined, error: TError | null) => void
274
267
  /**
275
268
  * Whether errors should be thrown instead of setting the `error` property.
276
269
  * If set to `true` or `suspense` is `true`, all errors will be thrown to the error boundary.
@@ -278,7 +271,7 @@ export interface QueryObserverOptions<
278
271
  * If set to a function, it will be passed the error and the query, and it should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`).
279
272
  * Defaults to `false`.
280
273
  */
281
- throwErrors?: ThrowErrors<TQueryFnData, TError, TQueryData, TQueryKey>
274
+ throwOnError?: ThrowOnError<TQueryFnData, TError, TQueryData, TQueryKey>
282
275
  /**
283
276
  * This option can be used to transform or select a part of the data returned by the query function.
284
277
  */
@@ -309,7 +302,7 @@ export type DefaultedQueryObserverOptions<
309
302
  TQueryKey extends QueryKey = QueryKey,
310
303
  > = WithRequired<
311
304
  QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
312
- 'throwErrors' | 'refetchOnReconnect'
305
+ 'throwOnError' | 'refetchOnReconnect'
313
306
  >
314
307
 
315
308
  export interface InfiniteQueryObserverOptions<
@@ -345,7 +338,7 @@ export type DefaultedInfiniteQueryObserverOptions<
345
338
  TQueryKey,
346
339
  TPageParam
347
340
  >,
348
- 'throwErrors' | 'refetchOnReconnect'
341
+ 'throwOnError' | 'refetchOnReconnect'
349
342
  >
350
343
 
351
344
  export interface FetchQueryOptions<
@@ -648,7 +641,7 @@ export interface MutationObserverOptions<
648
641
  TVariables = void,
649
642
  TContext = unknown,
650
643
  > extends MutationOptions<TData, TError, TVariables, TContext> {
651
- throwErrors?: boolean | ((error: TError) => boolean)
644
+ throwOnError?: boolean | ((error: TError) => boolean)
652
645
  }
653
646
 
654
647
  export interface MutateOptions<
@@ -1,13 +0,0 @@
1
- var id = 0;
2
- function _classPrivateFieldLooseKey(name) {
3
- return "__private_" + id++ + "_" + name;
4
- }
5
- function _classPrivateFieldLooseBase(receiver, privateKey) {
6
- if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
7
- throw new TypeError("attempted to use private field on non-instance");
8
- }
9
- return receiver;
10
- }
11
-
12
- export { _classPrivateFieldLooseBase as classPrivateFieldLooseBase, _classPrivateFieldLooseKey as classPrivateFieldLooseKey };
13
- //# sourceMappingURL=_rollupPluginBabelHelpers.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_rollupPluginBabelHelpers.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- var id = 0;
4
- function _classPrivateFieldLooseKey(name) {
5
- return "__private_" + id++ + "_" + name;
6
- }
7
- function _classPrivateFieldLooseBase(receiver, privateKey) {
8
- if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
9
- throw new TypeError("attempted to use private field on non-instance");
10
- }
11
- return receiver;
12
- }
13
-
14
- exports.classPrivateFieldLooseBase = _classPrivateFieldLooseBase;
15
- exports.classPrivateFieldLooseKey = _classPrivateFieldLooseKey;
16
- //# sourceMappingURL=_rollupPluginBabelHelpers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_rollupPluginBabelHelpers.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
@@ -1,87 +0,0 @@
1
- import { classPrivateFieldLooseBase as _classPrivateFieldLooseBase, classPrivateFieldLooseKey as _classPrivateFieldLooseKey } from './_virtual/_rollupPluginBabelHelpers.esm.js';
2
- import { Subscribable } from './subscribable.esm.js';
3
- import { isServer } from './utils.esm.js';
4
-
5
- var _focused = /*#__PURE__*/_classPrivateFieldLooseKey("focused");
6
- var _cleanup = /*#__PURE__*/_classPrivateFieldLooseKey("cleanup");
7
- var _setup = /*#__PURE__*/_classPrivateFieldLooseKey("setup");
8
- class FocusManager extends Subscribable {
9
- constructor() {
10
- super();
11
- Object.defineProperty(this, _focused, {
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, _cleanup, {
16
- writable: true,
17
- value: void 0
18
- });
19
- Object.defineProperty(this, _setup, {
20
- writable: true,
21
- value: void 0
22
- });
23
- _classPrivateFieldLooseBase(this, _setup)[_setup] = onFocus => {
24
- // addEventListener does not exist in React Native, but window does
25
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
26
- if (!isServer && window.addEventListener) {
27
- const listener = () => onFocus();
28
- // Listen to visibilitychange
29
- window.addEventListener('visibilitychange', listener, false);
30
- return () => {
31
- // Be sure to unsubscribe if a new handler is set
32
- window.removeEventListener('visibilitychange', listener);
33
- };
34
- }
35
- return;
36
- };
37
- }
38
- onSubscribe() {
39
- if (!_classPrivateFieldLooseBase(this, _cleanup)[_cleanup]) {
40
- this.setEventListener(_classPrivateFieldLooseBase(this, _setup)[_setup]);
41
- }
42
- }
43
- onUnsubscribe() {
44
- if (!this.hasListeners()) {
45
- var _classPrivateFieldLoo, _classPrivateFieldLoo2;
46
- (_classPrivateFieldLoo = (_classPrivateFieldLoo2 = _classPrivateFieldLooseBase(this, _cleanup))[_cleanup]) == null ? void 0 : _classPrivateFieldLoo.call(_classPrivateFieldLoo2);
47
- _classPrivateFieldLooseBase(this, _cleanup)[_cleanup] = undefined;
48
- }
49
- }
50
- setEventListener(setup) {
51
- var _classPrivateFieldLoo3, _classPrivateFieldLoo4;
52
- _classPrivateFieldLooseBase(this, _setup)[_setup] = setup;
53
- (_classPrivateFieldLoo3 = (_classPrivateFieldLoo4 = _classPrivateFieldLooseBase(this, _cleanup))[_cleanup]) == null ? void 0 : _classPrivateFieldLoo3.call(_classPrivateFieldLoo4);
54
- _classPrivateFieldLooseBase(this, _cleanup)[_cleanup] = setup(focused => {
55
- if (typeof focused === 'boolean') {
56
- this.setFocused(focused);
57
- } else {
58
- this.onFocus();
59
- }
60
- });
61
- }
62
- setFocused(focused) {
63
- _classPrivateFieldLooseBase(this, _focused)[_focused] = focused;
64
- if (focused) {
65
- this.onFocus();
66
- }
67
- }
68
- onFocus() {
69
- this.listeners.forEach(listener => {
70
- listener();
71
- });
72
- }
73
- isFocused() {
74
- var _globalThis$document;
75
- if (typeof _classPrivateFieldLooseBase(this, _focused)[_focused] === 'boolean') {
76
- return _classPrivateFieldLooseBase(this, _focused)[_focused];
77
- }
78
-
79
- // document global can be unavailable in react native
80
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
81
- return ((_globalThis$document = globalThis.document) == null ? void 0 : _globalThis$document.visibilityState) !== 'hidden';
82
- }
83
- }
84
- const focusManager = new FocusManager();
85
-
86
- export { FocusManager, focusManager };
87
- //# sourceMappingURL=focusManager.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"focusManager.esm.js","sources":["../../src/focusManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\nimport { isServer } from './utils'\n\ntype SetupFn = (\n setFocused: (focused?: boolean) => void,\n) => (() => void) | undefined\n\nexport class FocusManager extends Subscribable {\n #focused?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onFocus) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!isServer && window.addEventListener) {\n const listener = () => onFocus()\n // Listen to visibilitychange\n window.addEventListener('visibilitychange', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('visibilitychange', listener)\n }\n }\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((focused) => {\n if (typeof focused === 'boolean') {\n this.setFocused(focused)\n } else {\n this.onFocus()\n }\n })\n }\n\n setFocused(focused?: boolean): void {\n this.#focused = focused\n\n if (focused) {\n this.onFocus()\n }\n }\n\n onFocus(): void {\n this.listeners.forEach((listener) => {\n listener()\n })\n }\n\n isFocused(): boolean {\n if (typeof this.#focused === 'boolean') {\n return this.#focused\n }\n\n // document global can be unavailable in react native\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return globalThis.document?.visibilityState !== 'hidden'\n }\n}\n\nexport const focusManager = new FocusManager()\n"],"names":["FocusManager","Subscribable","constructor","onFocus","isServer","window","addEventListener","listener","removeEventListener","onSubscribe","setEventListener","onUnsubscribe","hasListeners","undefined","setup","focused","setFocused","listeners","forEach","isFocused","globalThis","document","visibilityState","focusManager"],"m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;AAClC,IAAA,2BAAA,CAAA,IAAI,wBAAYA,OAAO,CAAA;AAEvB,IAAA,IAAIA,OAAO,EAAE;MACX,IAAI,CAACZ,OAAO,EAAE,CAAA;AAChB,KAAA;AACF,GAAA;AAEAA,EAAAA,OAAO,GAAS;AACd,IAAA,IAAI,CAACc,SAAS,CAACC,OAAO,CAAEX,QAAQ,IAAK;AACnCA,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAC,CAAC,CAAA;AACJ,GAAA;AAEAY,EAAAA,SAAS,GAAY;AAAA,IAAA,IAAA,oBAAA,CAAA;AACnB,IAAA,IAAI,OAAO,2BAAA,CAAA,IAAI,EAAS,QAAA,CAAA,CAAA,QAAA,CAAA,KAAK,SAAS,EAAE;AACtC,MAAA,OAAA,2BAAA,CAAO,IAAI,EAAA,QAAA,CAAA,CAAA,QAAA,CAAA,CAAA;AACb,KAAA;;AAEA;AACA;IACA,OAAO,CAAA,CAAA,oBAAA,GAAAC,UAAU,CAACC,QAAQ,qBAAnB,oBAAqBC,CAAAA,eAAe,MAAK,QAAQ,CAAA;AAC1D,GAAA;AACF,CAAA;AAEaC,MAAAA,YAAY,GAAG,IAAIvB,YAAY;;;;"}
@@ -1,100 +0,0 @@
1
- // TYPES
2
-
3
- // FUNCTIONS
4
-
5
- function dehydrateMutation(mutation) {
6
- return {
7
- mutationKey: mutation.options.mutationKey,
8
- state: mutation.state
9
- };
10
- }
11
-
12
- // Most config is not dehydrated but instead meant to configure again when
13
- // consuming the de/rehydrated data, typically with useQuery on the client.
14
- // Sometimes it might make sense to prefetch data on the server and include
15
- // in the html-payload, but not consume it on the initial render.
16
- function dehydrateQuery(query) {
17
- return {
18
- state: query.state,
19
- queryKey: query.queryKey,
20
- queryHash: query.queryHash
21
- };
22
- }
23
- function defaultShouldDehydrateMutation(mutation) {
24
- return mutation.state.isPaused;
25
- }
26
- function defaultShouldDehydrateQuery(query) {
27
- return query.state.status === 'success';
28
- }
29
- function dehydrate(client, options = {}) {
30
- const mutations = [];
31
- const queries = [];
32
- if (options.dehydrateMutations !== false) {
33
- const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
34
- client.getMutationCache().getAll().forEach(mutation => {
35
- if (shouldDehydrateMutation(mutation)) {
36
- mutations.push(dehydrateMutation(mutation));
37
- }
38
- });
39
- }
40
- if (options.dehydrateQueries !== false) {
41
- const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
42
- client.getQueryCache().getAll().forEach(query => {
43
- if (shouldDehydrateQuery(query)) {
44
- queries.push(dehydrateQuery(query));
45
- }
46
- });
47
- }
48
- return {
49
- mutations,
50
- queries
51
- };
52
- }
53
- function hydrate(client, dehydratedState, options) {
54
- if (typeof dehydratedState !== 'object' || dehydratedState === null) {
55
- return;
56
- }
57
- const mutationCache = client.getMutationCache();
58
- const queryCache = client.getQueryCache();
59
-
60
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
61
- const mutations = dehydratedState.mutations || [];
62
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
63
- const queries = dehydratedState.queries || [];
64
- mutations.forEach(dehydratedMutation => {
65
- var _options$defaultOptio;
66
- mutationCache.build(client, {
67
- ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
68
- mutationKey: dehydratedMutation.mutationKey
69
- }, dehydratedMutation.state);
70
- });
71
- queries.forEach(dehydratedQuery => {
72
- var _options$defaultOptio2;
73
- const query = queryCache.get(dehydratedQuery.queryHash);
74
-
75
- // Reset fetch status to idle in the dehydrated state to avoid
76
- // query being stuck in fetching state upon hydration
77
- const dehydratedQueryState = {
78
- ...dehydratedQuery.state,
79
- fetchStatus: 'idle'
80
- };
81
-
82
- // Do not hydrate if an existing query exists with newer data
83
- if (query) {
84
- if (query.state.dataUpdatedAt < dehydratedQueryState.dataUpdatedAt) {
85
- query.setState(dehydratedQueryState);
86
- }
87
- return;
88
- }
89
-
90
- // Restore query
91
- queryCache.build(client, {
92
- ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
93
- queryKey: dehydratedQuery.queryKey,
94
- queryHash: dehydratedQuery.queryHash
95
- }, dehydratedQueryState);
96
- });
97
- }
98
-
99
- export { defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate };
100
- //# sourceMappingURL=hydration.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hydration.esm.js","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach((dehydratedQuery) => {\n const query = queryCache.get(dehydratedQuery.queryHash)\n\n // Reset fetch status to idle in the dehydrated state to avoid\n // query being stuck in fetching state upon hydration\n const dehydratedQueryState = {\n ...dehydratedQuery.state,\n fetchStatus: 'idle' as const,\n }\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < dehydratedQueryState.dataUpdatedAt) {\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey: dehydratedQuery.queryKey,\n queryHash: dehydratedQuery.queryHash,\n },\n dehydratedQueryState,\n )\n })\n}\n"],"names":["dehydrateMutation","mutation","mutationKey","options","state","dehydrateQuery","query","queryKey","queryHash","defaultShouldDehydrateMutation","isPaused","defaultShouldDehydrateQuery","status","dehydrate","client","mutations","queries","dehydrateMutations","shouldDehydrateMutation","getMutationCache","getAll","forEach","push","dehydrateQueries","shouldDehydrateQuery","getQueryCache","hydrate","dehydratedState","mutationCache","queryCache","dehydratedMutation","build","defaultOptions","dehydratedQuery","get","dehydratedQueryState","fetchStatus","dataUpdatedAt","setState"],"mappings":"AAUA;;AAoCA;;AAEA,SAASA,iBAAiB,CAACC,QAAkB,EAAsB;EACjE,OAAO;AACLC,IAAAA,WAAW,EAAED,QAAQ,CAACE,OAAO,CAACD,WAAW;IACzCE,KAAK,EAAEH,QAAQ,CAACG,KAAAA;GACjB,CAAA;AACH,CAAA;;AAEA;AACA;AACA;AACA;AACA,SAASC,cAAc,CAACC,KAAY,EAAmB;EACrD,OAAO;IACLF,KAAK,EAAEE,KAAK,CAACF,KAAK;IAClBG,QAAQ,EAAED,KAAK,CAACC,QAAQ;IACxBC,SAAS,EAAEF,KAAK,CAACE,SAAAA;GAClB,CAAA;AACH,CAAA;AAEO,SAASC,8BAA8B,CAACR,QAAkB,EAAE;AACjE,EAAA,OAAOA,QAAQ,CAACG,KAAK,CAACM,QAAQ,CAAA;AAChC,CAAA;AAEO,SAASC,2BAA2B,CAACL,KAAY,EAAE;AACxD,EAAA,OAAOA,KAAK,CAACF,KAAK,CAACQ,MAAM,KAAK,SAAS,CAAA;AACzC,CAAA;AAEO,SAASC,SAAS,CACvBC,MAAmB,EACnBX,OAAyB,GAAG,EAAE,EACb;EACjB,MAAMY,SAA+B,GAAG,EAAE,CAA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E6B,cAAc,KAAA,IAAA,GAAA,KAAA,CAAA,GAAvB,uBAAyBhB,OAAO,CAAA;MACnCT,QAAQ,EAAE0B,eAAe,CAAC1B,QAAQ;MAClCC,SAAS,EAAEyB,eAAe,CAACzB,SAAAA;KAC5B,EACD2B,oBAAoB,CACrB,CAAA;AACH,GAAC,CAAC,CAAA;AACJ;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"hydration.mjs","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach((dehydratedQuery) => {\n const query = queryCache.get(dehydratedQuery.queryHash)\n\n // Reset fetch status to idle in the dehydrated state to avoid\n // query being stuck in fetching state upon hydration\n const dehydratedQueryState = {\n ...dehydratedQuery.state,\n fetchStatus: 'idle' as const,\n }\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < dehydratedQueryState.dataUpdatedAt) {\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey: dehydratedQuery.queryKey,\n queryHash: dehydratedQuery.queryHash,\n },\n dehydratedQueryState,\n )\n })\n}\n"],"names":["dehydrateMutation","mutation","mutationKey","options","state","dehydrateQuery","query","queryKey","queryHash","defaultShouldDehydrateMutation","isPaused","defaultShouldDehydrateQuery","status","dehydrate","client","mutations","queries","dehydrateMutations","shouldDehydrateMutation","getMutationCache","getAll","forEach","push","dehydrateQueries","shouldDehydrateQuery","getQueryCache","hydrate","dehydratedState","mutationCache","queryCache","dehydratedMutation","build","defaultOptions","dehydratedQuery","get","dehydratedQueryState","fetchStatus","dataUpdatedAt","setState"],"mappings":"AAUA;;AAoCA;;AAEA,SAASA,iBAAiB,CAACC,QAAkB,EAAsB;EACjE,OAAO;AACLC,IAAAA,WAAW,EAAED,QAAQ,CAACE,OAAO,CAACD,WAAW;IACzCE,KAAK,EAAEH,QAAQ,CAACG,KAAAA;GACjB,CAAA;AACH,CAAA;;AAEA;AACA;AACA;AACA;AACA,SAASC,cAAc,CAACC,KAAY,EAAmB;EACrD,OAAO;IACLF,KAAK,EAAEE,KAAK,CAACF,KAAK;IAClBG,QAAQ,EAAED,KAAK,CAACC,QAAQ;IACxBC,SAAS,EAAEF,KAAK,CAACE,SAAAA;GAClB,CAAA;AACH,CAAA;AAEO,SAASC,8BAA8B,CAACR,QAAkB,EAAE;AACjE,EAAA,OAAOA,QAAQ,CAACG,KAAK,CAACM,QAAQ,CAAA;AAChC,CAAA;AAEO,SAASC,2BAA2B,CAACL,KAAY,EAAE;AACxD,EAAA,OAAOA,KAAK,CAACF,KAAK,CAACQ,MAAM,KAAK,SAAS,CAAA;AACzC,CAAA;AAEO,SAASC,SAAS,CACvBC,MAAmB,EACnBX,OAAyB,GAAG,EAAE,EACb;EACjB,MAAMY,SAA+B,GAAG,EAAE,CAAA;EAC1C,MAAMC,OAA0B,GAAG,EAA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Z,SAAS,IAAI,EAAE,CAAA;AACtE;AACA,EAAA,MAAMC,OAAO,GAAIW,eAAe,CAAqBX,OAAO,IAAI,EAAE,CAAA;AAElED,EAAAA,SAAS,CAACM,OAAO,CAAES,kBAAkB,IAAK;AACxCF,IAAAA,aAAa,CAACG,KAAK,CACjBjB,MAAM,EACN;AACE,MAAA,GAAGX,OAAO,EAAE6B,cAAc,EAAEjB,SAAS;MACrCb,WAAW,EAAE4B,kBAAkB,CAAC5B,WAAAA;AAClC,KAAC,EACD4B,kBAAkB,CAAC1B,KAAK,CACzB,CAAA;AACH,GAAC,CAAC,CAAA;AAEFY,EAAAA,OAAO,CAACK,OAAO,CAAEY,eAAe,IAAK;IACnC,MAAM3B,KAAK,GAAGuB,UAAU,CAACK,GAAG,CAACD,eAAe,CAACzB,SAAS,CAAC,CAAA;;AAEvD;AACA;AACA,IAAA,MAAM2B,oBAAoB,GAAG;MAC3B,GAAGF,eAAe,CAAC7B,KAAK;AACxBgC,MAAAA,WAAW,EAAE,MAAA;KACd,CAAA;;AAED;AACA,IAAA,IAAI9B,KAAK,EAAE;MACT,IAAIA,KAAK,CAACF,KAAK,CAACiC,aAAa,GAAGF,oBAAoB,CAACE,aAAa,EAAE;AAClE/B,QAAAA,KAAK,CAACgC,QAAQ,CAACH,oBAAoB,CAAC,CAAA;AACtC,OAAA;AACA,MAAA,OAAA;AACF,KAAA;;AAEA;AACAN,IAAAA,UAAU,CAACE,KAAK,CACdjB,MAAM,EACN;AACE,MAAA,GAAGX,OAAO,EAAE6B,cAAc,EAAEhB,OAAO;MACnCT,QAAQ,EAAE0B,eAAe,CAAC1B,QAAQ;MAClCC,SAAS,EAAEyB,eAAe,CAACzB,SAAAA;KAC5B,EACD2B,oBAAoB,CACrB,CAAA;AACH,GAAC,CAAC,CAAA;AACJ;;;;"}
@@ -1,14 +0,0 @@
1
- export { CancelledError, isCancelledError } from './retryer.esm.js';
2
- export { QueryCache } from './queryCache.esm.js';
3
- export { QueryClient } from './queryClient.esm.js';
4
- export { QueryObserver } from './queryObserver.esm.js';
5
- export { QueriesObserver } from './queriesObserver.esm.js';
6
- export { InfiniteQueryObserver } from './infiniteQueryObserver.esm.js';
7
- export { MutationCache } from './mutationCache.esm.js';
8
- export { MutationObserver } from './mutationObserver.esm.js';
9
- export { notifyManager } from './notifyManager.esm.js';
10
- export { focusManager } from './focusManager.esm.js';
11
- export { onlineManager } from './onlineManager.esm.js';
12
- export { hashKey, isServer, keepPreviousData, matchQuery, replaceEqualDeep } from './utils.esm.js';
13
- export { defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate } from './hydration.esm.js';
14
- //# sourceMappingURL=index.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
@@ -1,14 +0,0 @@
1
- export { CancelledError, isCancelledError } from './retryer.mjs';
2
- export { QueryCache } from './queryCache.mjs';
3
- export { QueryClient } from './queryClient.mjs';
4
- export { QueryObserver } from './queryObserver.mjs';
5
- export { QueriesObserver } from './queriesObserver.mjs';
6
- export { InfiniteQueryObserver } from './infiniteQueryObserver.mjs';
7
- export { MutationCache } from './mutationCache.mjs';
8
- export { MutationObserver } from './mutationObserver.mjs';
9
- export { notifyManager } from './notifyManager.mjs';
10
- export { focusManager } from './focusManager.mjs';
11
- export { onlineManager } from './onlineManager.mjs';
12
- export { hashKey, isServer, keepPreviousData, matchQuery, replaceEqualDeep } from './utils.mjs';
13
- export { defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate } from './hydration.mjs';
14
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}