@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.
- package/build/lib/HydrationBoundary.d.ts +9 -0
- package/build/lib/{Hydrate.esm.js → HydrationBoundary.esm.js} +14 -17
- package/build/lib/HydrationBoundary.esm.js.map +1 -0
- package/build/lib/{Hydrate.js → HydrationBoundary.js} +17 -24
- package/build/lib/HydrationBoundary.js.map +1 -0
- package/build/lib/{Hydrate.mjs → HydrationBoundary.mjs} +14 -17
- package/build/lib/HydrationBoundary.mjs.map +1 -0
- package/build/lib/QueryClientProvider.d.ts +4 -19
- package/build/lib/QueryClientProvider.esm.js +11 -46
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +14 -52
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +11 -46
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.esm.js +7 -2
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +10 -8
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +7 -2
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -2
- package/build/lib/__tests__/utils.d.ts +7 -10
- package/build/lib/errorBoundaryUtils.d.ts +3 -3
- package/build/lib/errorBoundaryUtils.esm.js +3 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +6 -9
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +3 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +4 -5
- package/build/lib/index.esm.js +3 -4
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +6 -9
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +3 -4
- package/build/lib/index.mjs.map +1 -1
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +3 -6
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -7
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +5 -5
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +14 -22
- package/build/lib/useBaseQuery.d.ts +2 -2
- package/build/lib/useBaseQuery.esm.js +15 -18
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +18 -24
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +15 -18
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +2 -4
- package/build/lib/useInfiniteQuery.esm.js +4 -4
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +3 -5
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +4 -4
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +2 -7
- package/build/lib/useIsFetching.esm.js +5 -9
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +7 -14
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +5 -9
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +2 -5
- package/build/lib/useMutation.esm.js +12 -14
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +14 -19
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +12 -14
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +8 -0
- package/build/lib/useMutationState.esm.js +37 -0
- package/build/lib/useMutationState.esm.js.map +1 -0
- package/build/lib/useMutationState.js +59 -0
- package/build/lib/useMutationState.js.map +1 -0
- package/build/lib/useMutationState.mjs +37 -0
- package/build/lib/useMutationState.mjs.map +1 -0
- package/build/lib/useQueries.d.ts +9 -7
- package/build/lib/useQueries.esm.js +16 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +19 -33
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +16 -27
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +9 -21
- package/build/lib/useQuery.esm.js +5 -4
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +4 -5
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +5 -4
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -1
- package/build/lib/utils.esm.js +5 -6
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +5 -8
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +5 -6
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +1007 -1826
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +6 -18
- package/src/{Hydrate.tsx → HydrationBoundary.tsx} +17 -18
- package/src/QueryClientProvider.tsx +11 -65
- package/src/__tests__/{Hydrate.test.tsx → HydrationBoundary.test.tsx} +89 -77
- package/src/__tests__/QueryClientProvider.test.tsx +33 -147
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +118 -85
- package/src/__tests__/ssr-hydration.test.tsx +27 -33
- package/src/__tests__/ssr.test.tsx +23 -9
- package/src/__tests__/suspense.test.tsx +194 -171
- package/src/__tests__/useInfiniteQuery.test.tsx +249 -494
- package/src/__tests__/useInfiniteQuery.type.test.tsx +131 -0
- package/src/__tests__/useIsFetching.test.tsx +65 -108
- package/src/__tests__/useMutation.test.tsx +200 -268
- package/src/__tests__/useMutationState.test.tsx +284 -0
- package/src/__tests__/useQueries.test.tsx +43 -347
- package/src/__tests__/useQuery.test.tsx +890 -686
- package/src/__tests__/useQuery.types.test.tsx +23 -24
- package/src/__tests__/utils.tsx +14 -23
- package/src/errorBoundaryUtils.ts +5 -10
- package/src/index.ts +4 -7
- package/src/types.ts +33 -38
- package/src/useBaseQuery.ts +7 -7
- package/src/useInfiniteQuery.ts +12 -77
- package/src/useIsFetching.ts +8 -20
- package/src/useMutation.ts +8 -66
- package/src/useMutationState.ts +81 -0
- package/src/useQueries.ts +29 -21
- package/src/useQuery.ts +27 -104
- package/src/utils.ts +5 -5
- package/build/lib/Hydrate.d.ts +0 -10
- package/build/lib/Hydrate.esm.js.map +0 -1
- package/build/lib/Hydrate.js.map +0 -1
- package/build/lib/Hydrate.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.esm.js +0 -6
- package/build/lib/reactBatchedUpdates.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.js +0 -30
- package/build/lib/reactBatchedUpdates.js.map +0 -1
- package/build/lib/reactBatchedUpdates.mjs +0 -6
- package/build/lib/reactBatchedUpdates.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.native.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.js +0 -13
- package/build/lib/reactBatchedUpdates.native.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.mjs +0 -2
- package/build/lib/reactBatchedUpdates.native.mjs.map +0 -1
- package/build/lib/setBatchUpdatesFn.esm.js +0 -5
- package/build/lib/setBatchUpdatesFn.esm.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.js +0 -7
- package/build/lib/setBatchUpdatesFn.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.mjs +0 -5
- package/build/lib/setBatchUpdatesFn.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -7
- package/build/lib/useIsMutating.esm.js +0 -16
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -40
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -16
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.d.ts +0 -2
- package/build/lib/useSyncExternalStore.esm.js +0 -7
- package/build/lib/useSyncExternalStore.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.js +0 -11
- package/build/lib/useSyncExternalStore.js.map +0 -1
- package/build/lib/useSyncExternalStore.mjs +0 -7
- package/build/lib/useSyncExternalStore.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.native.d.ts +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.js +0 -13
- package/build/lib/useSyncExternalStore.native.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.mjs +0 -2
- package/build/lib/useSyncExternalStore.native.mjs.map +0 -1
- package/codemods/v4/key-transformation.js +0 -138
- package/codemods/v4/replace-import-specifier.js +0 -25
- package/codemods/v4/utils/index.js +0 -166
- package/codemods/v4/utils/replacers/key-replacer.js +0 -160
- package/codemods/v4/utils/transformers/query-cache-transformer.js +0 -115
- package/codemods/v4/utils/transformers/query-client-transformer.js +0 -49
- package/codemods/v4/utils/transformers/use-query-like-transformer.js +0 -32
- package/codemods/v4/utils/unprocessable-key-error.js +0 -8
- package/src/__tests__/useIsMutating.test.tsx +0 -259
- package/src/reactBatchedUpdates.native.ts +0 -4
- package/src/reactBatchedUpdates.ts +0 -2
- package/src/setBatchUpdatesFn.ts +0 -4
- package/src/useIsMutating.ts +0 -39
- package/src/useSyncExternalStore.native.ts +0 -5
- package/src/useSyncExternalStore.ts +0 -4
- /package/build/lib/__tests__/{Hydrate.test.d.ts → HydrationBoundary.test.d.ts} +0 -0
- /package/build/lib/__tests__/{useIsMutating.test.d.ts → useInfiniteQuery.type.test.d.ts} +0 -0
- /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,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
|
-
})
|
package/src/setBatchUpdatesFn.ts
DELETED
package/src/useIsMutating.ts
DELETED
|
@@ -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
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|