@tanstack/react-query 4.24.9 → 5.0.0-alpha.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 (199) hide show
  1. package/build/lib/HydrationBoundary.d.ts +9 -0
  2. package/build/lib/{Hydrate.esm.js → HydrationBoundary.esm.js} +14 -17
  3. package/build/lib/HydrationBoundary.esm.js.map +1 -0
  4. package/build/lib/{Hydrate.js → HydrationBoundary.js} +17 -24
  5. package/build/lib/HydrationBoundary.js.map +1 -0
  6. package/build/lib/{Hydrate.mjs → HydrationBoundary.mjs} +14 -17
  7. package/build/lib/HydrationBoundary.mjs.map +1 -0
  8. package/build/lib/QueryClientProvider.d.ts +4 -19
  9. package/build/lib/QueryClientProvider.esm.js +11 -46
  10. package/build/lib/QueryClientProvider.esm.js.map +1 -1
  11. package/build/lib/QueryClientProvider.js +14 -52
  12. package/build/lib/QueryClientProvider.js.map +1 -1
  13. package/build/lib/QueryClientProvider.mjs +11 -46
  14. package/build/lib/QueryClientProvider.mjs.map +1 -1
  15. package/build/lib/QueryErrorResetBoundary.esm.js +7 -2
  16. package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
  17. package/build/lib/QueryErrorResetBoundary.js +10 -8
  18. package/build/lib/QueryErrorResetBoundary.js.map +1 -1
  19. package/build/lib/QueryErrorResetBoundary.mjs +7 -2
  20. package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
  21. package/build/lib/__tests__/useQuery.types.test.d.ts +1 -2
  22. package/build/lib/__tests__/utils.d.ts +7 -10
  23. package/build/lib/errorBoundaryUtils.d.ts +3 -3
  24. package/build/lib/errorBoundaryUtils.esm.js +3 -3
  25. package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
  26. package/build/lib/errorBoundaryUtils.js +6 -9
  27. package/build/lib/errorBoundaryUtils.js.map +1 -1
  28. package/build/lib/errorBoundaryUtils.mjs +3 -3
  29. package/build/lib/errorBoundaryUtils.mjs.map +1 -1
  30. package/build/lib/index.d.ts +4 -5
  31. package/build/lib/index.esm.js +3 -4
  32. package/build/lib/index.esm.js.map +1 -1
  33. package/build/lib/index.js +6 -9
  34. package/build/lib/index.js.map +1 -1
  35. package/build/lib/index.mjs +3 -4
  36. package/build/lib/index.mjs.map +1 -1
  37. package/build/lib/isRestoring.esm.js.map +1 -1
  38. package/build/lib/isRestoring.js +3 -6
  39. package/build/lib/isRestoring.js.map +1 -1
  40. package/build/lib/isRestoring.mjs.map +1 -1
  41. package/build/lib/suspense.esm.js +5 -5
  42. package/build/lib/suspense.esm.js.map +1 -1
  43. package/build/lib/suspense.js +5 -7
  44. package/build/lib/suspense.js.map +1 -1
  45. package/build/lib/suspense.mjs +5 -5
  46. package/build/lib/suspense.mjs.map +1 -1
  47. package/build/lib/types.d.ts +14 -22
  48. package/build/lib/useBaseQuery.d.ts +2 -2
  49. package/build/lib/useBaseQuery.esm.js +15 -18
  50. package/build/lib/useBaseQuery.esm.js.map +1 -1
  51. package/build/lib/useBaseQuery.js +18 -24
  52. package/build/lib/useBaseQuery.js.map +1 -1
  53. package/build/lib/useBaseQuery.mjs +15 -18
  54. package/build/lib/useBaseQuery.mjs.map +1 -1
  55. package/build/lib/useInfiniteQuery.d.ts +2 -4
  56. package/build/lib/useInfiniteQuery.esm.js +4 -4
  57. package/build/lib/useInfiniteQuery.esm.js.map +1 -1
  58. package/build/lib/useInfiniteQuery.js +3 -5
  59. package/build/lib/useInfiniteQuery.js.map +1 -1
  60. package/build/lib/useInfiniteQuery.mjs +4 -4
  61. package/build/lib/useInfiniteQuery.mjs.map +1 -1
  62. package/build/lib/useIsFetching.d.ts +2 -7
  63. package/build/lib/useIsFetching.esm.js +5 -9
  64. package/build/lib/useIsFetching.esm.js.map +1 -1
  65. package/build/lib/useIsFetching.js +7 -14
  66. package/build/lib/useIsFetching.js.map +1 -1
  67. package/build/lib/useIsFetching.mjs +5 -9
  68. package/build/lib/useIsFetching.mjs.map +1 -1
  69. package/build/lib/useMutation.d.ts +2 -5
  70. package/build/lib/useMutation.esm.js +12 -14
  71. package/build/lib/useMutation.esm.js.map +1 -1
  72. package/build/lib/useMutation.js +14 -19
  73. package/build/lib/useMutation.js.map +1 -1
  74. package/build/lib/useMutation.mjs +12 -14
  75. package/build/lib/useMutation.mjs.map +1 -1
  76. package/build/lib/useMutationState.d.ts +8 -0
  77. package/build/lib/useMutationState.esm.js +37 -0
  78. package/build/lib/useMutationState.esm.js.map +1 -0
  79. package/build/lib/useMutationState.js +59 -0
  80. package/build/lib/useMutationState.js.map +1 -0
  81. package/build/lib/useMutationState.mjs +37 -0
  82. package/build/lib/useMutationState.mjs.map +1 -0
  83. package/build/lib/useQueries.d.ts +9 -7
  84. package/build/lib/useQueries.esm.js +16 -27
  85. package/build/lib/useQueries.esm.js.map +1 -1
  86. package/build/lib/useQueries.js +19 -33
  87. package/build/lib/useQueries.js.map +1 -1
  88. package/build/lib/useQueries.mjs +16 -27
  89. package/build/lib/useQueries.mjs.map +1 -1
  90. package/build/lib/useQuery.d.ts +9 -21
  91. package/build/lib/useQuery.esm.js +5 -4
  92. package/build/lib/useQuery.esm.js.map +1 -1
  93. package/build/lib/useQuery.js +4 -5
  94. package/build/lib/useQuery.js.map +1 -1
  95. package/build/lib/useQuery.mjs +5 -4
  96. package/build/lib/useQuery.mjs.map +1 -1
  97. package/build/lib/utils.d.ts +1 -1
  98. package/build/lib/utils.esm.js +5 -6
  99. package/build/lib/utils.esm.js.map +1 -1
  100. package/build/lib/utils.js +5 -8
  101. package/build/lib/utils.js.map +1 -1
  102. package/build/lib/utils.mjs +5 -6
  103. package/build/lib/utils.mjs.map +1 -1
  104. package/build/umd/index.development.js +1007 -1826
  105. package/build/umd/index.development.js.map +1 -1
  106. package/build/umd/index.production.js +1 -1
  107. package/build/umd/index.production.js.map +1 -1
  108. package/package.json +6 -18
  109. package/src/{Hydrate.tsx → HydrationBoundary.tsx} +17 -18
  110. package/src/QueryClientProvider.tsx +11 -65
  111. package/src/__tests__/{Hydrate.test.tsx → HydrationBoundary.test.tsx} +89 -77
  112. package/src/__tests__/QueryClientProvider.test.tsx +33 -147
  113. package/src/__tests__/QueryResetErrorBoundary.test.tsx +118 -85
  114. package/src/__tests__/ssr-hydration.test.tsx +27 -33
  115. package/src/__tests__/ssr.test.tsx +23 -9
  116. package/src/__tests__/suspense.test.tsx +194 -171
  117. package/src/__tests__/useInfiniteQuery.test.tsx +249 -494
  118. package/src/__tests__/useInfiniteQuery.type.test.tsx +131 -0
  119. package/src/__tests__/useIsFetching.test.tsx +65 -108
  120. package/src/__tests__/useMutation.test.tsx +200 -268
  121. package/src/__tests__/useMutationState.test.tsx +284 -0
  122. package/src/__tests__/useQueries.test.tsx +43 -347
  123. package/src/__tests__/useQuery.test.tsx +890 -686
  124. package/src/__tests__/useQuery.types.test.tsx +23 -24
  125. package/src/__tests__/utils.tsx +14 -23
  126. package/src/errorBoundaryUtils.ts +5 -10
  127. package/src/index.ts +4 -7
  128. package/src/types.ts +33 -38
  129. package/src/useBaseQuery.ts +7 -7
  130. package/src/useInfiniteQuery.ts +12 -77
  131. package/src/useIsFetching.ts +8 -20
  132. package/src/useMutation.ts +8 -66
  133. package/src/useMutationState.ts +81 -0
  134. package/src/useQueries.ts +29 -21
  135. package/src/useQuery.ts +27 -104
  136. package/src/utils.ts +5 -5
  137. package/build/lib/Hydrate.d.ts +0 -10
  138. package/build/lib/Hydrate.esm.js.map +0 -1
  139. package/build/lib/Hydrate.js.map +0 -1
  140. package/build/lib/Hydrate.mjs.map +0 -1
  141. package/build/lib/reactBatchedUpdates.d.ts +0 -2
  142. package/build/lib/reactBatchedUpdates.esm.js +0 -6
  143. package/build/lib/reactBatchedUpdates.esm.js.map +0 -1
  144. package/build/lib/reactBatchedUpdates.js +0 -30
  145. package/build/lib/reactBatchedUpdates.js.map +0 -1
  146. package/build/lib/reactBatchedUpdates.mjs +0 -6
  147. package/build/lib/reactBatchedUpdates.mjs.map +0 -1
  148. package/build/lib/reactBatchedUpdates.native.d.ts +0 -2
  149. package/build/lib/reactBatchedUpdates.native.esm.js +0 -2
  150. package/build/lib/reactBatchedUpdates.native.esm.js.map +0 -1
  151. package/build/lib/reactBatchedUpdates.native.js +0 -13
  152. package/build/lib/reactBatchedUpdates.native.js.map +0 -1
  153. package/build/lib/reactBatchedUpdates.native.mjs +0 -2
  154. package/build/lib/reactBatchedUpdates.native.mjs.map +0 -1
  155. package/build/lib/setBatchUpdatesFn.esm.js +0 -5
  156. package/build/lib/setBatchUpdatesFn.esm.js.map +0 -1
  157. package/build/lib/setBatchUpdatesFn.js +0 -7
  158. package/build/lib/setBatchUpdatesFn.js.map +0 -1
  159. package/build/lib/setBatchUpdatesFn.mjs +0 -5
  160. package/build/lib/setBatchUpdatesFn.mjs.map +0 -1
  161. package/build/lib/useIsMutating.d.ts +0 -7
  162. package/build/lib/useIsMutating.esm.js +0 -16
  163. package/build/lib/useIsMutating.esm.js.map +0 -1
  164. package/build/lib/useIsMutating.js +0 -40
  165. package/build/lib/useIsMutating.js.map +0 -1
  166. package/build/lib/useIsMutating.mjs +0 -16
  167. package/build/lib/useIsMutating.mjs.map +0 -1
  168. package/build/lib/useSyncExternalStore.d.ts +0 -2
  169. package/build/lib/useSyncExternalStore.esm.js +0 -7
  170. package/build/lib/useSyncExternalStore.esm.js.map +0 -1
  171. package/build/lib/useSyncExternalStore.js +0 -11
  172. package/build/lib/useSyncExternalStore.js.map +0 -1
  173. package/build/lib/useSyncExternalStore.mjs +0 -7
  174. package/build/lib/useSyncExternalStore.mjs.map +0 -1
  175. package/build/lib/useSyncExternalStore.native.d.ts +0 -2
  176. package/build/lib/useSyncExternalStore.native.esm.js +0 -2
  177. package/build/lib/useSyncExternalStore.native.esm.js.map +0 -1
  178. package/build/lib/useSyncExternalStore.native.js +0 -13
  179. package/build/lib/useSyncExternalStore.native.js.map +0 -1
  180. package/build/lib/useSyncExternalStore.native.mjs +0 -2
  181. package/build/lib/useSyncExternalStore.native.mjs.map +0 -1
  182. package/codemods/v4/key-transformation.js +0 -138
  183. package/codemods/v4/replace-import-specifier.js +0 -25
  184. package/codemods/v4/utils/index.js +0 -166
  185. package/codemods/v4/utils/replacers/key-replacer.js +0 -160
  186. package/codemods/v4/utils/transformers/query-cache-transformer.js +0 -115
  187. package/codemods/v4/utils/transformers/query-client-transformer.js +0 -49
  188. package/codemods/v4/utils/transformers/use-query-like-transformer.js +0 -32
  189. package/codemods/v4/utils/unprocessable-key-error.js +0 -8
  190. package/src/__tests__/useIsMutating.test.tsx +0 -259
  191. package/src/reactBatchedUpdates.native.ts +0 -4
  192. package/src/reactBatchedUpdates.ts +0 -2
  193. package/src/setBatchUpdatesFn.ts +0 -4
  194. package/src/useIsMutating.ts +0 -39
  195. package/src/useSyncExternalStore.native.ts +0 -5
  196. package/src/useSyncExternalStore.ts +0 -4
  197. /package/build/lib/__tests__/{Hydrate.test.d.ts → HydrationBoundary.test.d.ts} +0 -0
  198. /package/build/lib/__tests__/{useIsMutating.test.d.ts → useInfiniteQuery.type.test.d.ts} +0 -0
  199. /package/build/lib/{setBatchUpdatesFn.d.ts → __tests__/useMutationState.test.d.ts} +0 -0
@@ -1,160 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-var-requires
2
- const UnprocessableKeyError = require('../unprocessable-key-error')
3
-
4
- module.exports = ({ jscodeshift, root, filePath, keyName = 'queryKey' }) => {
5
- const isArrayExpression = node =>
6
- jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
7
-
8
- const isStringLiteral = node =>
9
- jscodeshift.match(node, { type: jscodeshift.StringLiteral.name }) ||
10
- jscodeshift.match(node, { type: jscodeshift.Literal.name })
11
-
12
- const isTemplateLiteral = node =>
13
- jscodeshift.match(node, { type: jscodeshift.TemplateLiteral.name })
14
-
15
- const findVariableDeclaration = node => {
16
- const declarations = root
17
- .find(jscodeshift.VariableDeclarator, {
18
- id: {
19
- type: jscodeshift.Identifier.name,
20
- name: node.name,
21
- },
22
- })
23
- .paths()
24
-
25
- return declarations.length > 0 ? declarations[0] : null
26
- }
27
-
28
- const createKeyValue = node => {
29
- // When the node is a string literal we convert it into an array of strings.
30
- if (isStringLiteral(node)) {
31
- return jscodeshift.arrayExpression([
32
- jscodeshift.stringLiteral(node.value),
33
- ])
34
- }
35
-
36
- // When the node is a template literal we convert it into an array of template literals.
37
- if (isTemplateLiteral(node)) {
38
- return jscodeshift.arrayExpression([
39
- jscodeshift.templateLiteral(node.quasis, node.expressions),
40
- ])
41
- }
42
-
43
- if (jscodeshift.match(node, { type: jscodeshift.Identifier.name })) {
44
- // When the node is an identifier at first, we try to find its declaration, because we will try
45
- // to guess its type.
46
- const variableDeclaration = findVariableDeclaration(node)
47
-
48
- if (!variableDeclaration) {
49
- throw new UnprocessableKeyError(
50
- `In file ${filePath} at line ${node.loc.start.line} the type of identifier \`${node.name}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`
51
- )
52
- }
53
-
54
- const initializer = variableDeclaration.value.init
55
-
56
- // When it's a string, we just wrap it into an array expression.
57
- if (isStringLiteral(initializer) || isTemplateLiteral(initializer)) {
58
- return jscodeshift.arrayExpression([node])
59
- }
60
- }
61
-
62
- throw new UnprocessableKeyError(
63
- `In file ${filePath} at line ${node.loc.start.line} the type of the \`${keyName}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`
64
- )
65
- }
66
-
67
- const createKeyProperty = node =>
68
- jscodeshift.property(
69
- 'init',
70
- jscodeshift.identifier(keyName),
71
- createKeyValue(node)
72
- )
73
-
74
- const getPropertyFromObjectExpression = (objectExpression, propertyName) =>
75
- objectExpression.properties.find(
76
- property => property.key.name === propertyName
77
- ) ?? null
78
-
79
- const buildWithTypeArguments = (node, builder) => {
80
- const newNode = builder(node)
81
-
82
- if (node.typeParameters) {
83
- newNode.typeArguments = node.typeParameters
84
- }
85
-
86
- return newNode
87
- }
88
-
89
- return ({ node }) => {
90
- // When the node doesn't have the 'original' property, that means the codemod has been already applied,
91
- // so we don't need to do any changes.
92
- if (!node.original) {
93
- return node
94
- }
95
-
96
- const methodArguments = node.arguments
97
-
98
- // The method call doesn't have any arguments, we have nothing to do in this case.
99
- if (methodArguments.length === 0) {
100
- return node
101
- }
102
-
103
- try {
104
- const [firstArgument, ...restOfTheArguments] = methodArguments
105
-
106
- if (
107
- jscodeshift.match(firstArgument, {
108
- type: jscodeshift.ObjectExpression.name,
109
- })
110
- ) {
111
- const originalKey = getPropertyFromObjectExpression(
112
- firstArgument,
113
- keyName
114
- )
115
-
116
- if (!originalKey) {
117
- throw new UnprocessableKeyError(
118
- `In file ${filePath} at line ${node.loc.start.line} the \`${keyName}\` couldn't be found. Did you forget to add it?`
119
- )
120
- }
121
-
122
- const restOfTheProperties = firstArgument.properties.filter(
123
- item => item.key.name !== keyName
124
- )
125
-
126
- return buildWithTypeArguments(node, originalNode =>
127
- jscodeshift.callExpression(originalNode.original.callee, [
128
- jscodeshift.objectExpression([
129
- createKeyProperty(originalKey.value),
130
- ...restOfTheProperties,
131
- ]),
132
- ...restOfTheArguments,
133
- ])
134
- )
135
- }
136
-
137
- // When the node is an array expression we just simply return it because we want query keys to be arrays.
138
- if (isArrayExpression(firstArgument)) {
139
- return node
140
- }
141
-
142
- return buildWithTypeArguments(node, originalNode =>
143
- jscodeshift.callExpression(originalNode.original.callee, [
144
- createKeyValue(firstArgument),
145
- ...restOfTheArguments,
146
- ])
147
- )
148
- } catch (error) {
149
- if (error.name === 'UnprocessableKeyError') {
150
- if (process.env.NODE_ENV !== 'test') {
151
- console.warn(error.message)
152
- }
153
-
154
- return node
155
- }
156
-
157
- throw error
158
- }
159
- }
160
- }
@@ -1,115 +0,0 @@
1
- module.exports = ({ jscodeshift, utils, root }) => {
2
- const isGetQueryCacheMethodCall = (
3
- initializer,
4
- importIdentifiers,
5
- knownQueryClientIds
6
- ) => {
7
- const isKnownQueryClient = node =>
8
- utils.isIdentifier(node) && knownQueryClientIds.includes(node.name)
9
-
10
- const isGetQueryCacheIdentifier = node =>
11
- utils.isIdentifier(node) && node.name === 'getQueryCache'
12
-
13
- const isValidInitializer = node =>
14
- utils.isCallExpression(node) && utils.isMemberExpression(node.callee)
15
-
16
- if (isValidInitializer(initializer)) {
17
- const instance = initializer.callee.object
18
-
19
- return (
20
- isGetQueryCacheIdentifier(initializer.callee.property) &&
21
- (isKnownQueryClient(instance) ||
22
- utils.isFunctionCallOf(
23
- instance,
24
- utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
25
- ))
26
- )
27
- }
28
-
29
- return false
30
- }
31
-
32
- const findQueryCacheInstantiations = (
33
- importIdentifiers,
34
- knownQueryClientIds
35
- ) =>
36
- root.find(jscodeshift.VariableDeclarator, {}).filter(node => {
37
- if (node.value.init) {
38
- const initializer = node.value.init
39
-
40
- return (
41
- utils.isClassInstantiationOf(
42
- initializer,
43
- utils.getSelectorByImports(importIdentifiers, 'QueryCache')
44
- ) ||
45
- isGetQueryCacheMethodCall(
46
- initializer,
47
- importIdentifiers,
48
- knownQueryClientIds
49
- )
50
- )
51
- }
52
-
53
- return false
54
- })
55
-
56
- const filterQueryCacheMethodCalls = node =>
57
- utils.isIdentifier(node) && ['find', 'findAll'].includes(node.name)
58
-
59
- const findQueryCacheMethodCalls = importIdentifiers => {
60
- /**
61
- * Here we collect all query client instantiations. We have to make aware of them because the query cache can be
62
- * accessed by the query client as well.
63
- */
64
- const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
65
- importIdentifiers
66
- )
67
- /**
68
- * Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
69
- * instances, to locate the possible usages we need to be aware of the identifier names.
70
- */
71
- const queryCacheIdentifiers = findQueryCacheInstantiations(
72
- importIdentifiers,
73
- queryClientIdentifiers
74
- )
75
- .paths()
76
- .map(node => node.value.id.name)
77
-
78
- return (
79
- utils
80
- // First, we need to find all method calls.
81
- .findAllMethodCalls()
82
- // Then we narrow the collection to all `fetch` and `fetchAll` methods.
83
- .filter(node => filterQueryCacheMethodCalls(node.value.callee.property))
84
- .filter(node => {
85
- const object = node.value.callee.object
86
-
87
- // If the method is called on a `QueryCache` instance, we keep it in the collection.
88
- if (utils.isIdentifier(object)) {
89
- return queryCacheIdentifiers.includes(object.name)
90
- }
91
-
92
- // If the method is called on a `QueryClient` instance, we keep it in the collection.
93
- if (utils.isCallExpression(object)) {
94
- return isGetQueryCacheMethodCall(
95
- object,
96
- importIdentifiers,
97
- queryClientIdentifiers
98
- )
99
- }
100
-
101
- return false
102
- })
103
- )
104
- }
105
-
106
- const execute = replacer => {
107
- findQueryCacheMethodCalls(
108
- utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient'])
109
- ).replaceWith(replacer)
110
- }
111
-
112
- return {
113
- execute,
114
- }
115
- }
@@ -1,49 +0,0 @@
1
- module.exports = ({ jscodeshift, utils, root }) => {
2
- const filterQueryClientMethodCalls = (node, methods) =>
3
- utils.isIdentifier(node) && methods.includes(node.name)
4
-
5
- const findQueryClientMethodCalls = (importIdentifiers, methods) => {
6
- /**
7
- * Here we collect all query client instantiations. We have to make aware of them because some method calls might
8
- * be invoked on these instances.
9
- */
10
- const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
11
- importIdentifiers
12
- )
13
-
14
- return (
15
- utils
16
- // First, we need to find all method calls.
17
- .findAllMethodCalls()
18
- // Then we narrow the collection to `QueryClient` methods.
19
- .filter(node =>
20
- filterQueryClientMethodCalls(node.value.callee.property, methods)
21
- )
22
- .filter(node => {
23
- const object = node.value.callee.object
24
-
25
- // If the method is called on a `QueryClient` instance, we keep it in the collection.
26
- if (utils.isIdentifier(object)) {
27
- return queryClientIdentifiers.includes(object.name)
28
- }
29
-
30
- // If the method is called on the return value of `useQueryClient` hook, we keep it in the collection.
31
- return utils.isFunctionCallOf(
32
- object,
33
- utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
34
- )
35
- })
36
- )
37
- }
38
-
39
- const execute = (methods, replacer) => {
40
- findQueryClientMethodCalls(
41
- utils.locateImports(['QueryClient', 'useQueryClient']),
42
- methods
43
- ).replaceWith(replacer)
44
- }
45
-
46
- return {
47
- execute,
48
- }
49
- }
@@ -1,32 +0,0 @@
1
- module.exports = ({ jscodeshift, utils, root }) => {
2
- const filterUseQueryLikeHookCalls = (node, importIdentifiers, hooks) => {
3
- for (const hook of hooks) {
4
- const selector = utils.getSelectorByImports(importIdentifiers, hook)
5
-
6
- if (utils.isFunctionCallOf(node, selector)) {
7
- return true
8
- }
9
- }
10
-
11
- return false
12
- }
13
-
14
- const findUseQueryLikeHookCalls = (importIdentifiers, hooks) =>
15
- root
16
- // First, we need to find all call expressions.
17
- .find(jscodeshift.CallExpression, {})
18
- // Then we narrow the collection to the `useQuery` like hook calls.
19
- .filter(node =>
20
- filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks)
21
- )
22
-
23
- const execute = (hooks, replacer) => {
24
- findUseQueryLikeHookCalls(utils.locateImports(hooks), hooks).replaceWith(
25
- replacer
26
- )
27
- }
28
-
29
- return {
30
- execute,
31
- }
32
- }
@@ -1,8 +0,0 @@
1
- class UnprocessableKeyError extends Error {
2
- constructor(message) {
3
- super(message)
4
- this.name = 'UnprocessableKeyError'
5
- }
6
- }
7
-
8
- module.exports = UnprocessableKeyError
@@ -1,259 +0,0 @@
1
- import { fireEvent, waitFor } from '@testing-library/react'
2
- import * as React from 'react'
3
- import { useIsMutating } from '../useIsMutating'
4
- import { useMutation } from '../useMutation'
5
- import {
6
- createQueryClient,
7
- renderWithClient,
8
- setActTimeout,
9
- sleep,
10
- } from './utils'
11
- import { ErrorBoundary } from 'react-error-boundary'
12
- import { QueryClient } from '@tanstack/query-core'
13
- import * as MutationCacheModule from '../../../query-core/src/mutationCache'
14
-
15
- describe('useIsMutating', () => {
16
- it('should return the number of fetching mutations', async () => {
17
- const isMutatings: number[] = []
18
- const queryClient = createQueryClient()
19
-
20
- function IsMutating() {
21
- const isMutating = useIsMutating()
22
- isMutatings.push(isMutating)
23
- return null
24
- }
25
-
26
- function Mutations() {
27
- const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
28
- await sleep(150)
29
- return 'data'
30
- })
31
- const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
32
- await sleep(50)
33
- return 'data'
34
- })
35
-
36
- React.useEffect(() => {
37
- mutate1()
38
- setActTimeout(() => {
39
- mutate2()
40
- }, 50)
41
- }, [mutate1, mutate2])
42
-
43
- return null
44
- }
45
-
46
- function Page() {
47
- return (
48
- <div>
49
- <IsMutating />
50
- <Mutations />
51
- </div>
52
- )
53
- }
54
-
55
- renderWithClient(queryClient, <Page />)
56
- await waitFor(() => expect(isMutatings).toEqual([0, 1, 2, 1, 0]))
57
- })
58
-
59
- it('should filter correctly by mutationKey', async () => {
60
- const isMutatings: number[] = []
61
- const queryClient = createQueryClient()
62
-
63
- function IsMutating() {
64
- const isMutating = useIsMutating(['mutation1'])
65
- isMutatings.push(isMutating)
66
- return null
67
- }
68
-
69
- function Page() {
70
- const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
71
- await sleep(100)
72
- return 'data'
73
- })
74
- const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
75
- await sleep(100)
76
- return 'data'
77
- })
78
-
79
- React.useEffect(() => {
80
- mutate1()
81
- mutate2()
82
- }, [mutate1, mutate2])
83
-
84
- return <IsMutating />
85
- }
86
-
87
- renderWithClient(queryClient, <Page />)
88
- await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 0]))
89
- })
90
-
91
- it('should filter correctly by predicate', async () => {
92
- const isMutatings: number[] = []
93
- const queryClient = createQueryClient()
94
-
95
- function IsMutating() {
96
- const isMutating = useIsMutating({
97
- predicate: (mutation) =>
98
- mutation.options.mutationKey?.[0] === 'mutation1',
99
- })
100
- isMutatings.push(isMutating)
101
- return null
102
- }
103
-
104
- function Page() {
105
- const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
106
- await sleep(100)
107
- return 'data'
108
- })
109
- const { mutate: mutate2 } = useMutation(['mutation2'], async () => {
110
- await sleep(100)
111
- return 'data'
112
- })
113
-
114
- React.useEffect(() => {
115
- mutate1()
116
- mutate2()
117
- }, [mutate1, mutate2])
118
-
119
- return <IsMutating />
120
- }
121
-
122
- renderWithClient(queryClient, <Page />)
123
- await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 0]))
124
- })
125
-
126
- it('should not change state if unmounted', async () => {
127
- // We have to mock the MutationCache to not unsubscribe
128
- // the listener when the component is unmounted
129
- class MutationCacheMock extends MutationCacheModule.MutationCache {
130
- subscribe(listener: any) {
131
- super.subscribe(listener)
132
- return () => void 0
133
- }
134
- }
135
-
136
- const MutationCacheSpy = jest
137
- .spyOn(MutationCacheModule, 'MutationCache')
138
- .mockImplementation((fn) => {
139
- return new MutationCacheMock(fn)
140
- })
141
-
142
- const queryClient = createQueryClient()
143
-
144
- function IsMutating() {
145
- useIsMutating()
146
- return null
147
- }
148
-
149
- function Page() {
150
- const [mounted, setMounted] = React.useState(true)
151
- const { mutate: mutate1 } = useMutation(['mutation1'], async () => {
152
- await sleep(10)
153
- return 'data'
154
- })
155
-
156
- React.useEffect(() => {
157
- mutate1()
158
- }, [mutate1])
159
-
160
- return (
161
- <div>
162
- <button onClick={() => setMounted(false)}>unmount</button>
163
- {mounted && <IsMutating />}
164
- </div>
165
- )
166
- }
167
-
168
- const { getByText } = renderWithClient(queryClient, <Page />)
169
- fireEvent.click(getByText('unmount'))
170
-
171
- // Should not display the console error
172
- // "Warning: Can't perform a React state update on an unmounted component"
173
-
174
- await sleep(20)
175
- MutationCacheSpy.mockRestore()
176
- })
177
-
178
- describe('with custom context', () => {
179
- it('should return the number of fetching mutations', async () => {
180
- const context = React.createContext<QueryClient | undefined>(undefined)
181
-
182
- const isMutatings: number[] = []
183
- const queryClient = new QueryClient()
184
-
185
- function IsMutating() {
186
- const isMutating = useIsMutating(undefined, { context })
187
- isMutatings.push(isMutating)
188
- return null
189
- }
190
-
191
- function Page() {
192
- const { mutate: mutate1 } = useMutation(
193
- ['mutation1'],
194
- async () => {
195
- await sleep(150)
196
- return 'data'
197
- },
198
- { context },
199
- )
200
- const { mutate: mutate2 } = useMutation(
201
- ['mutation2'],
202
- async () => {
203
- await sleep(50)
204
- return 'data'
205
- },
206
- { context },
207
- )
208
-
209
- React.useEffect(() => {
210
- mutate1()
211
- setActTimeout(() => {
212
- mutate2()
213
- }, 50)
214
- }, [mutate1, mutate2])
215
-
216
- return <IsMutating />
217
- }
218
-
219
- renderWithClient(queryClient, <Page />, { context })
220
- await waitFor(() => expect(isMutatings).toEqual([0, 1, 1, 2, 2, 1, 0]))
221
- })
222
-
223
- it('should throw if the context is not passed to useIsMutating', async () => {
224
- const context = React.createContext<QueryClient | undefined>(undefined)
225
-
226
- const isMutatings: number[] = []
227
- const queryClient = new QueryClient()
228
-
229
- function IsMutating() {
230
- const isMutating = useIsMutating(undefined)
231
- isMutatings.push(isMutating)
232
- return null
233
- }
234
-
235
- function Page() {
236
- const { mutate } = useMutation(['mutation'], async () => 'data', {
237
- useErrorBoundary: true,
238
- context,
239
- })
240
-
241
- React.useEffect(() => {
242
- mutate()
243
- }, [mutate])
244
-
245
- return <IsMutating />
246
- }
247
-
248
- const rendered = renderWithClient(
249
- queryClient,
250
- <ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
251
- <Page />
252
- </ErrorBoundary>,
253
- { context },
254
- )
255
-
256
- await waitFor(() => rendered.getByText('error boundary'))
257
- })
258
- })
259
- })
@@ -1,4 +0,0 @@
1
- // @ts-ignore
2
- // eslint-disable-next-line import/no-unresolved
3
- import { unstable_batchedUpdates } from 'react-native'
4
- export { unstable_batchedUpdates }
@@ -1,2 +0,0 @@
1
- import * as ReactDOM from 'react-dom'
2
- export const unstable_batchedUpdates = ReactDOM.unstable_batchedUpdates
@@ -1,4 +0,0 @@
1
- import { notifyManager } from '@tanstack/query-core'
2
- import { unstable_batchedUpdates } from './reactBatchedUpdates'
3
-
4
- notifyManager.setBatchNotifyFunction(unstable_batchedUpdates)
@@ -1,39 +0,0 @@
1
- import * as React from 'react'
2
- import { useSyncExternalStore } from './useSyncExternalStore'
3
-
4
- import type { MutationKey, MutationFilters } from '@tanstack/query-core'
5
- import { notifyManager, parseMutationFilterArgs } from '@tanstack/query-core'
6
- import type { ContextOptions } from './types'
7
- import { useQueryClient } from './QueryClientProvider'
8
-
9
- interface Options extends ContextOptions {}
10
-
11
- export function useIsMutating(
12
- filters?: MutationFilters,
13
- options?: Options,
14
- ): number
15
- export function useIsMutating(
16
- mutationKey?: MutationKey,
17
- filters?: Omit<MutationFilters, 'mutationKey'>,
18
- options?: Options,
19
- ): number
20
- export function useIsMutating(
21
- arg1?: MutationKey | MutationFilters,
22
- arg2?: Omit<MutationFilters, 'mutationKey'> | Options,
23
- arg3?: Options,
24
- ): number {
25
- const [filters, options = {}] = parseMutationFilterArgs(arg1, arg2, arg3)
26
-
27
- const queryClient = useQueryClient({ context: options.context })
28
- const mutationCache = queryClient.getMutationCache()
29
-
30
- return useSyncExternalStore(
31
- React.useCallback(
32
- (onStoreChange) =>
33
- mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),
34
- [mutationCache],
35
- ),
36
- () => queryClient.isMutating(filters),
37
- () => queryClient.isMutating(filters),
38
- )
39
- }
@@ -1,5 +0,0 @@
1
- // Temporary workaround due to an issue with react-native uSES - https://github.com/TanStack/query/pull/3601
2
- // @ts-ignore
3
- import { useSyncExternalStore } from 'use-sync-external-store/shim/index.native.js'
4
-
5
- export { useSyncExternalStore }
@@ -1,4 +0,0 @@
1
- // Temporary workaround due to an issue with react-native uSES - https://github.com/TanStack/query/pull/3601
2
- import { useSyncExternalStore as uSES } from 'use-sync-external-store/shim/index.js'
3
-
4
- export const useSyncExternalStore = uSES