@tanstack/query-core 5.101.4 → 5.102.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/classPrivateFieldSet2-Bo0ZyOIv.cjs +56 -0
- package/build/legacy/classPrivateFieldSet2-CV7wyte-.js +27 -0
- package/build/legacy/classPrivateMethodInitSpec-CBwa_Y7C.js +7 -0
- package/build/legacy/classPrivateMethodInitSpec-DVhcLejn.cjs +12 -0
- package/build/legacy/environmentManager.cjs +25 -46
- package/build/legacy/environmentManager.cjs.map +1 -1
- package/build/legacy/environmentManager.d.cts +18 -2
- package/build/legacy/environmentManager.d.cts.map +1 -0
- package/build/legacy/environmentManager.d.ts +18 -2
- package/build/legacy/environmentManager.d.ts.map +1 -0
- package/build/legacy/environmentManager.js +23 -22
- package/build/legacy/environmentManager.js.map +1 -1
- package/build/legacy/focusManager.cjs +63 -104
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +18 -2
- package/build/legacy/focusManager.d.cts.map +1 -0
- package/build/legacy/focusManager.d.ts +18 -2
- package/build/legacy/focusManager.d.ts.map +1 -0
- package/build/legacy/focusManager.js +60 -76
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/hydration-D1E7ZbjZ.d.ts +1374 -0
- package/build/legacy/hydration-D1E7ZbjZ.d.ts.map +1 -0
- package/build/legacy/hydration-NBquRCHe.d.cts +1374 -0
- package/build/legacy/hydration-NBquRCHe.d.cts.map +1 -0
- package/build/legacy/hydration.cjs +114 -194
- package/build/legacy/hydration.cjs.map +1 -1
- package/build/legacy/hydration.d.cts +2 -7
- package/build/legacy/hydration.d.ts +2 -7
- package/build/legacy/hydration.js +108 -167
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.cjs +59 -111
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +10 -141
- package/build/legacy/index.d.ts +10 -141
- package/build/legacy/index.js +16 -66
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs +92 -135
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +14 -3
- package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -0
- package/build/legacy/infiniteQueryBehavior.d.ts +14 -3
- package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -0
- package/build/legacy/infiniteQueryBehavior.js +89 -115
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.cjs +59 -88
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +20 -1
- package/build/legacy/infiniteQueryObserver.d.cts.map +1 -0
- package/build/legacy/infiniteQueryObserver.d.ts +20 -1
- package/build/legacy/infiniteQueryObserver.d.ts.map +1 -0
- package/build/legacy/infiniteQueryObserver.js +57 -64
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +236 -329
- package/build/legacy/mutation.cjs.map +1 -1
- package/build/legacy/mutation.d.cts +2 -4
- package/build/legacy/mutation.d.ts +2 -4
- package/build/legacy/mutation.js +231 -299
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs +121 -166
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +2 -2
- package/build/legacy/mutationCache.d.ts +2 -2
- package/build/legacy/mutationCache.js +117 -130
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs +134 -187
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.d.cts +2 -1
- package/build/legacy/mutationObserver.d.ts +2 -1
- package/build/legacy/mutationObserver.js +132 -160
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/notifyManager.cjs +76 -110
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.d.cts +48 -3
- package/build/legacy/notifyManager.d.cts.map +1 -0
- package/build/legacy/notifyManager.d.ts +48 -3
- package/build/legacy/notifyManager.d.ts.map +1 -0
- package/build/legacy/notifyManager.js +73 -84
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/onlineManager.cjs +57 -93
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +17 -2
- package/build/legacy/onlineManager.d.cts.map +1 -0
- package/build/legacy/onlineManager.d.ts +17 -2
- package/build/legacy/onlineManager.d.ts.map +1 -0
- package/build/legacy/onlineManager.js +54 -65
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.cjs +203 -262
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +23 -2
- package/build/legacy/queriesObserver.d.cts.map +1 -0
- package/build/legacy/queriesObserver.d.ts +23 -2
- package/build/legacy/queriesObserver.d.ts.map +1 -0
- package/build/legacy/queriesObserver.js +199 -233
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +409 -491
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +2 -9
- package/build/legacy/query.d.ts +2 -9
- package/build/legacy/query.js +402 -467
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs +99 -128
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +2 -3
- package/build/legacy/queryCache.d.ts +2 -3
- package/build/legacy/queryCache.js +96 -100
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient.cjs +295 -345
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +2 -1
- package/build/legacy/queryClient.d.ts +2 -1
- package/build/legacy/queryClient.js +292 -318
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.cjs +350 -502
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +2 -1
- package/build/legacy/queryObserver.d.ts +2 -1
- package/build/legacy/queryObserver.js +345 -480
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/removable-DeMjhW5M.d.cts +13 -0
- package/build/legacy/removable-DeMjhW5M.d.cts.map +1 -0
- package/build/legacy/removable-DeMjhW5M.d.ts +13 -0
- package/build/legacy/removable-DeMjhW5M.d.ts.map +1 -0
- package/build/legacy/removable.cjs +31 -67
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.d.cts +2 -1
- package/build/legacy/removable.d.ts +2 -1
- package/build/legacy/removable.js +28 -39
- package/build/legacy/removable.js.map +1 -1
- package/build/legacy/retryer.cjs +127 -155
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +2 -7
- package/build/legacy/retryer.d.ts +2 -7
- package/build/legacy/retryer.js +121 -127
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +49 -78
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +33 -1
- package/build/legacy/streamedQuery.d.cts.map +1 -0
- package/build/legacy/streamedQuery.d.ts +33 -1
- package/build/legacy/streamedQuery.d.ts.map +1 -0
- package/build/legacy/streamedQuery.js +47 -54
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/subscribable-CbifVTKz.d.cts +12 -0
- package/build/legacy/subscribable-CbifVTKz.d.cts.map +1 -0
- package/build/legacy/subscribable-CbifVTKz.d.ts +12 -0
- package/build/legacy/subscribable-CbifVTKz.d.ts.map +1 -0
- package/build/legacy/subscribable.cjs +22 -48
- package/build/legacy/subscribable.cjs.map +1 -1
- package/build/legacy/subscribable.d.cts +2 -1
- package/build/legacy/subscribable.d.ts +2 -1
- package/build/legacy/subscribable.js +21 -25
- package/build/legacy/subscribable.js.map +1 -1
- package/build/legacy/timeoutManager.cjs +64 -105
- package/build/legacy/timeoutManager.cjs.map +1 -1
- package/build/legacy/timeoutManager.d.cts +60 -7
- package/build/legacy/timeoutManager.d.cts.map +1 -0
- package/build/legacy/timeoutManager.d.ts +60 -7
- package/build/legacy/timeoutManager.d.ts.map +1 -0
- package/build/legacy/timeoutManager.js +60 -76
- package/build/legacy/timeoutManager.js.map +1 -1
- package/build/legacy/types.cjs +10 -35
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +2 -94
- package/build/legacy/types.d.ts +2 -94
- package/build/legacy/types.js +7 -10
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/utils.cjs +175 -300
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +2 -30
- package/build/legacy/utils.d.ts +2 -30
- package/build/legacy/utils.js +149 -252
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/environmentManager.cjs +25 -46
- package/build/modern/environmentManager.cjs.map +1 -1
- package/build/modern/environmentManager.d.cts +18 -2
- package/build/modern/environmentManager.d.cts.map +1 -0
- package/build/modern/environmentManager.d.ts +18 -2
- package/build/modern/environmentManager.d.ts.map +1 -0
- package/build/modern/environmentManager.js +23 -20
- package/build/modern/environmentManager.js.map +1 -1
- package/build/modern/focusManager.cjs +56 -90
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +18 -2
- package/build/modern/focusManager.d.cts.map +1 -0
- package/build/modern/focusManager.d.ts +18 -2
- package/build/modern/focusManager.d.ts.map +1 -0
- package/build/modern/focusManager.js +53 -63
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/hydration-D1E7ZbjZ.d.ts +1374 -0
- package/build/modern/hydration-D1E7ZbjZ.d.ts.map +1 -0
- package/build/modern/hydration-NBquRCHe.d.cts +1374 -0
- package/build/modern/hydration-NBquRCHe.d.cts.map +1 -0
- package/build/modern/hydration.cjs +105 -189
- package/build/modern/hydration.cjs.map +1 -1
- package/build/modern/hydration.d.cts +2 -7
- package/build/modern/hydration.d.ts +2 -7
- package/build/modern/hydration.js +99 -160
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.cjs +59 -111
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +10 -141
- package/build/modern/index.d.ts +10 -141
- package/build/modern/index.js +16 -64
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.cjs +89 -131
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +14 -3
- package/build/modern/infiniteQueryBehavior.d.cts.map +1 -0
- package/build/modern/infiniteQueryBehavior.d.ts +14 -3
- package/build/modern/infiniteQueryBehavior.d.ts.map +1 -0
- package/build/modern/infiniteQueryBehavior.js +86 -109
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.cjs +58 -87
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +20 -1
- package/build/modern/infiniteQueryObserver.d.cts.map +1 -0
- package/build/modern/infiniteQueryObserver.d.ts +20 -1
- package/build/modern/infiniteQueryObserver.d.ts.map +1 -0
- package/build/modern/infiniteQueryObserver.js +56 -61
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +221 -303
- package/build/modern/mutation.cjs.map +1 -1
- package/build/modern/mutation.d.cts +2 -4
- package/build/modern/mutation.d.ts +2 -4
- package/build/modern/mutation.js +216 -274
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs +111 -145
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +2 -2
- package/build/modern/mutationCache.d.ts +2 -2
- package/build/modern/mutationCache.js +107 -117
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs +115 -162
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.d.cts +2 -1
- package/build/modern/mutationObserver.d.ts +2 -1
- package/build/modern/mutationObserver.js +113 -136
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/notifyManager.cjs +76 -110
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.d.cts +48 -3
- package/build/modern/notifyManager.d.cts.map +1 -0
- package/build/modern/notifyManager.d.ts +48 -3
- package/build/modern/notifyManager.d.ts.map +1 -0
- package/build/modern/notifyManager.js +73 -82
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/onlineManager.cjs +51 -80
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +17 -2
- package/build/modern/onlineManager.d.cts.map +1 -0
- package/build/modern/onlineManager.d.ts +17 -2
- package/build/modern/onlineManager.d.ts.map +1 -0
- package/build/modern/onlineManager.js +48 -53
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.cjs +185 -237
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +23 -2
- package/build/modern/queriesObserver.d.cts.map +1 -0
- package/build/modern/queriesObserver.d.ts +23 -2
- package/build/modern/queriesObserver.d.ts.map +1 -0
- package/build/modern/queriesObserver.js +181 -209
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +384 -464
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +2 -9
- package/build/modern/query.d.ts +2 -9
- package/build/modern/query.js +377 -441
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs +97 -119
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +2 -3
- package/build/modern/queryCache.d.ts +2 -3
- package/build/modern/queryCache.js +94 -92
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient.cjs +281 -318
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +2 -1
- package/build/modern/queryClient.d.ts +2 -1
- package/build/modern/queryClient.js +278 -299
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.cjs +329 -475
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +2 -1
- package/build/modern/queryObserver.d.ts +2 -1
- package/build/modern/queryObserver.js +324 -454
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/removable-DeMjhW5M.d.cts +13 -0
- package/build/modern/removable-DeMjhW5M.d.cts.map +1 -0
- package/build/modern/removable-DeMjhW5M.d.ts +13 -0
- package/build/modern/removable-DeMjhW5M.d.ts.map +1 -0
- package/build/modern/removable.cjs +27 -56
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.d.cts +2 -1
- package/build/modern/removable.d.ts +2 -1
- package/build/modern/removable.js +24 -29
- package/build/modern/removable.js.map +1 -1
- package/build/modern/retryer.cjs +121 -151
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +2 -7
- package/build/modern/retryer.d.ts +2 -7
- package/build/modern/retryer.js +115 -121
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +49 -78
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +33 -1
- package/build/modern/streamedQuery.d.cts.map +1 -0
- package/build/modern/streamedQuery.d.ts +33 -1
- package/build/modern/streamedQuery.d.ts.map +1 -0
- package/build/modern/streamedQuery.js +47 -52
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/subscribable-CbifVTKz.d.cts +12 -0
- package/build/modern/subscribable-CbifVTKz.d.cts.map +1 -0
- package/build/modern/subscribable-CbifVTKz.d.ts +12 -0
- package/build/modern/subscribable-CbifVTKz.d.ts.map +1 -0
- package/build/modern/subscribable.cjs +22 -48
- package/build/modern/subscribable.cjs.map +1 -1
- package/build/modern/subscribable.d.cts +2 -1
- package/build/modern/subscribable.d.ts +2 -1
- package/build/modern/subscribable.js +21 -23
- package/build/modern/subscribable.js.map +1 -1
- package/build/modern/timeoutManager.cjs +59 -93
- package/build/modern/timeoutManager.cjs.map +1 -1
- package/build/modern/timeoutManager.d.cts +60 -7
- package/build/modern/timeoutManager.d.cts.map +1 -0
- package/build/modern/timeoutManager.d.ts +60 -7
- package/build/modern/timeoutManager.d.ts.map +1 -0
- package/build/modern/timeoutManager.js +55 -65
- package/build/modern/timeoutManager.js.map +1 -1
- package/build/modern/types.cjs +10 -35
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +2 -94
- package/build/modern/types.d.ts +2 -94
- package/build/modern/types.js +8 -9
- package/build/modern/types.js.map +1 -1
- package/build/modern/utils.cjs +175 -300
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +2 -30
- package/build/modern/utils.d.ts +2 -30
- package/build/modern/utils.js +149 -250
- package/build/modern/utils.js.map +1 -1
- package/package.json +3 -5
- package/src/hydration.ts +62 -44
- package/src/index.ts +1 -0
- package/src/mutation.ts +14 -6
- package/src/mutationObserver.ts +8 -0
- package/src/queriesObserver.ts +14 -8
- package/src/query.ts +14 -6
- package/src/queryClient.ts +90 -2
- package/src/queryObserver.ts +41 -64
- package/src/retryer.ts +20 -12
- package/src/types.ts +70 -58
- package/build/legacy/_tsup-dts-rollup.d.cts +0 -2188
- package/build/legacy/_tsup-dts-rollup.d.ts +0 -2188
- package/build/legacy/chunk-PXG64RU4.js +0 -25
- package/build/legacy/chunk-PXG64RU4.js.map +0 -1
- package/build/legacy/thenable.cjs +0 -76
- package/build/legacy/thenable.cjs.map +0 -1
- package/build/legacy/thenable.d.cts +0 -6
- package/build/legacy/thenable.d.ts +0 -6
- package/build/legacy/thenable.js +0 -52
- package/build/legacy/thenable.js.map +0 -1
- package/build/modern/_tsup-dts-rollup.d.cts +0 -2188
- package/build/modern/_tsup-dts-rollup.d.ts +0 -2188
- package/build/modern/thenable.cjs +0 -75
- package/build/modern/thenable.cjs.map +0 -1
- package/build/modern/thenable.d.cts +0 -6
- package/build/modern/thenable.d.ts +0 -6
- package/build/modern/thenable.js +0 -49
- package/build/modern/thenable.js.map +0 -1
- package/src/thenable.ts +0 -111
package/src/hydration.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { tryResolveSync } from './thenable'
|
|
2
1
|
import { noop } from './utils'
|
|
3
2
|
import type {
|
|
4
3
|
DefaultError,
|
|
@@ -16,8 +15,24 @@ import type { Mutation, MutationState } from './mutation'
|
|
|
16
15
|
|
|
17
16
|
// TYPES
|
|
18
17
|
type TransformerFn = (data: any) => any
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
|
|
19
|
+
function tryResolveSync(promise: PromiseLike<unknown>) {
|
|
20
|
+
let data: unknown
|
|
21
|
+
|
|
22
|
+
const thenResult = promise.then((result) => {
|
|
23
|
+
data = result
|
|
24
|
+
return result
|
|
25
|
+
}, noop) as Promise<unknown> | undefined
|
|
26
|
+
|
|
27
|
+
// .catch can be unavailable on certain kinds of thenable's
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
29
|
+
thenResult?.catch?.(noop)
|
|
30
|
+
|
|
31
|
+
if (data !== undefined) {
|
|
32
|
+
return { data }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return undefined
|
|
21
36
|
}
|
|
22
37
|
|
|
23
38
|
export interface DehydrateOptions {
|
|
@@ -71,51 +86,57 @@ function dehydrateMutation(mutation: Mutation): DehydratedMutation {
|
|
|
71
86
|
}
|
|
72
87
|
}
|
|
73
88
|
|
|
89
|
+
function dehydratePromise(
|
|
90
|
+
query: Query,
|
|
91
|
+
serializeData?: TransformerFn,
|
|
92
|
+
shouldRedactErrors?: (error: unknown) => boolean,
|
|
93
|
+
): Promise<unknown> | undefined {
|
|
94
|
+
const promise = query.promise?.then(serializeData).catch((error) => {
|
|
95
|
+
if (shouldRedactErrors?.(error) === false) {
|
|
96
|
+
// Reject original error if it should not be redacted
|
|
97
|
+
return Promise.reject(error)
|
|
98
|
+
}
|
|
99
|
+
// If not in production, log original error before rejecting redacted error
|
|
100
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
101
|
+
console.error(
|
|
102
|
+
`A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
return Promise.reject(new Error('redacted'))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// Avoid unhandled promise rejections
|
|
109
|
+
// We need the promise we dehydrate to reject to get the correct result into
|
|
110
|
+
// the query cache, but we also want to avoid unhandled promise rejections
|
|
111
|
+
// in whatever environment the prefetches are happening in.
|
|
112
|
+
promise?.catch(noop)
|
|
113
|
+
|
|
114
|
+
return promise
|
|
115
|
+
}
|
|
116
|
+
|
|
74
117
|
// Most config is not dehydrated but instead meant to configure again when
|
|
75
118
|
// consuming the de/rehydrated data, typically with useQuery on the client.
|
|
76
119
|
// Sometimes it might make sense to prefetch data on the server and include
|
|
77
120
|
// in the html-payload, but not consume it on the initial render.
|
|
78
|
-
function dehydrateQuery(
|
|
121
|
+
export function dehydrateQuery(
|
|
79
122
|
query: Query,
|
|
80
|
-
serializeData
|
|
81
|
-
shouldRedactErrors
|
|
123
|
+
serializeData?: TransformerFn,
|
|
124
|
+
shouldRedactErrors?: (error: unknown) => boolean,
|
|
82
125
|
): DehydratedQuery {
|
|
83
|
-
const dehydratePromise = () => {
|
|
84
|
-
const promise = query.promise?.then(serializeData).catch((error) => {
|
|
85
|
-
if (!shouldRedactErrors(error)) {
|
|
86
|
-
// Reject original error if it should not be redacted
|
|
87
|
-
return Promise.reject(error)
|
|
88
|
-
}
|
|
89
|
-
// If not in production, log original error before rejecting redacted error
|
|
90
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
91
|
-
console.error(
|
|
92
|
-
`A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
return Promise.reject(new Error('redacted'))
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
// Avoid unhandled promise rejections
|
|
99
|
-
// We need the promise we dehydrate to reject to get the correct result into
|
|
100
|
-
// the query cache, but we also want to avoid unhandled promise rejections
|
|
101
|
-
// in whatever environment the prefetches are happening in.
|
|
102
|
-
promise?.catch(noop)
|
|
103
|
-
|
|
104
|
-
return promise
|
|
105
|
-
}
|
|
106
|
-
|
|
107
126
|
return {
|
|
108
127
|
dehydratedAt: Date.now(),
|
|
109
128
|
state: {
|
|
110
129
|
...query.state,
|
|
111
130
|
...(query.state.data !== undefined && {
|
|
112
|
-
data: serializeData
|
|
131
|
+
data: serializeData
|
|
132
|
+
? serializeData(query.state.data)
|
|
133
|
+
: query.state.data,
|
|
113
134
|
}),
|
|
114
135
|
},
|
|
115
136
|
queryKey: query.queryKey,
|
|
116
137
|
queryHash: query.queryHash,
|
|
117
138
|
...(query.state.status === 'pending' && {
|
|
118
|
-
promise: dehydratePromise(),
|
|
139
|
+
promise: dehydratePromise(query, serializeData, shouldRedactErrors),
|
|
119
140
|
}),
|
|
120
141
|
...(query.meta && { meta: query.meta }),
|
|
121
142
|
...(query.queryType && { queryType: query.queryType }),
|
|
@@ -130,10 +151,6 @@ export function defaultShouldDehydrateQuery(query: Query) {
|
|
|
130
151
|
return query.state.status === 'success'
|
|
131
152
|
}
|
|
132
153
|
|
|
133
|
-
function defaultShouldRedactErrors(_: unknown) {
|
|
134
|
-
return true
|
|
135
|
-
}
|
|
136
|
-
|
|
137
154
|
export function dehydrate(
|
|
138
155
|
client: QueryClient,
|
|
139
156
|
options: DehydrateOptions = {},
|
|
@@ -157,13 +174,10 @@ export function dehydrate(
|
|
|
157
174
|
|
|
158
175
|
const shouldRedactErrors =
|
|
159
176
|
options.shouldRedactErrors ??
|
|
160
|
-
client.getDefaultOptions().dehydrate?.shouldRedactErrors
|
|
161
|
-
defaultShouldRedactErrors
|
|
177
|
+
client.getDefaultOptions().dehydrate?.shouldRedactErrors
|
|
162
178
|
|
|
163
179
|
const serializeData =
|
|
164
|
-
options.serializeData ??
|
|
165
|
-
client.getDefaultOptions().dehydrate?.serializeData ??
|
|
166
|
-
defaultTransformerFn
|
|
180
|
+
options.serializeData ?? client.getDefaultOptions().dehydrate?.serializeData
|
|
167
181
|
|
|
168
182
|
const queries = client
|
|
169
183
|
.getQueryCache()
|
|
@@ -190,8 +204,7 @@ export function hydrate(
|
|
|
190
204
|
const queryCache = client.getQueryCache()
|
|
191
205
|
const deserializeData =
|
|
192
206
|
options?.defaultOptions?.deserializeData ??
|
|
193
|
-
client.getDefaultOptions().hydrate?.deserializeData
|
|
194
|
-
defaultTransformerFn
|
|
207
|
+
client.getDefaultOptions().hydrate?.deserializeData
|
|
195
208
|
|
|
196
209
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
197
210
|
const mutations = (dehydratedState as DehydratedState).mutations || []
|
|
@@ -222,7 +235,12 @@ export function hydrate(
|
|
|
222
235
|
}) => {
|
|
223
236
|
const syncData = promise ? tryResolveSync(promise) : undefined
|
|
224
237
|
const rawData = state.data === undefined ? syncData?.data : state.data
|
|
225
|
-
const data =
|
|
238
|
+
const data =
|
|
239
|
+
rawData === undefined
|
|
240
|
+
? rawData
|
|
241
|
+
: deserializeData
|
|
242
|
+
? deserializeData(rawData)
|
|
243
|
+
: rawData
|
|
226
244
|
|
|
227
245
|
let query = queryCache.get(queryHash)
|
|
228
246
|
const existingQueryIsPending = query?.state.status === 'pending'
|
package/src/index.ts
CHANGED
package/src/mutation.ts
CHANGED
|
@@ -165,8 +165,11 @@ export class Mutation<
|
|
|
165
165
|
continue(): Promise<unknown> {
|
|
166
166
|
return (
|
|
167
167
|
this.#retryer?.continue() ??
|
|
168
|
-
// continuing a mutation assumes that variables are set, mutation must have been dehydrated before
|
|
169
|
-
|
|
168
|
+
// continuing a mutation assumes that variables are set, mutation must have been dehydrated before.
|
|
169
|
+
// a settled mutation has no retryer to continue and must not run again
|
|
170
|
+
(this.state.status === 'pending'
|
|
171
|
+
? this.execute(this.state.variables!)
|
|
172
|
+
: Promise.resolve())
|
|
170
173
|
)
|
|
171
174
|
}
|
|
172
175
|
|
|
@@ -181,7 +184,7 @@ export class Mutation<
|
|
|
181
184
|
mutationKey: this.options.mutationKey,
|
|
182
185
|
} satisfies MutationFunctionContext
|
|
183
186
|
|
|
184
|
-
this.#retryer = createRetryer({
|
|
187
|
+
const retryer = (this.#retryer = createRetryer({
|
|
185
188
|
fn: () => {
|
|
186
189
|
if (!this.options.mutationFn) {
|
|
187
190
|
return Promise.reject(new Error('No mutationFn found'))
|
|
@@ -200,10 +203,10 @@ export class Mutation<
|
|
|
200
203
|
retryDelay: this.options.retryDelay,
|
|
201
204
|
networkMode: this.options.networkMode,
|
|
202
205
|
canRun: () => this.#mutationCache.canRun(this),
|
|
203
|
-
})
|
|
206
|
+
}))
|
|
204
207
|
|
|
205
208
|
const restored = this.state.status === 'pending'
|
|
206
|
-
const isPaused = !
|
|
209
|
+
const isPaused = !retryer.canStart()
|
|
207
210
|
|
|
208
211
|
try {
|
|
209
212
|
if (restored) {
|
|
@@ -232,7 +235,7 @@ export class Mutation<
|
|
|
232
235
|
})
|
|
233
236
|
}
|
|
234
237
|
}
|
|
235
|
-
const data = await
|
|
238
|
+
const data = await retryer.start()
|
|
236
239
|
|
|
237
240
|
// Notify cache callback
|
|
238
241
|
await this.#mutationCache.config.onSuccess?.(
|
|
@@ -324,6 +327,11 @@ export class Mutation<
|
|
|
324
327
|
this.#dispatch({ type: 'error', error: error as TError })
|
|
325
328
|
throw error
|
|
326
329
|
} finally {
|
|
330
|
+
// The settled retryer's promise would otherwise pin this mutation's
|
|
331
|
+
// result, variables and context for as long as the cache keeps it
|
|
332
|
+
if (this.#retryer === retryer) {
|
|
333
|
+
this.#retryer = undefined
|
|
334
|
+
}
|
|
327
335
|
this.#mutationCache.runNext(this)
|
|
328
336
|
}
|
|
329
337
|
}
|
package/src/mutationObserver.ts
CHANGED
|
@@ -93,6 +93,14 @@ export class MutationObserver<
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
protected onSubscribe(): void {
|
|
97
|
+
if (this.listeners.size === 1 && this.#currentMutation) {
|
|
98
|
+
this.#currentMutation.addObserver(this)
|
|
99
|
+
|
|
100
|
+
this.#updateResult()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
protected onUnsubscribe(): void {
|
|
97
105
|
if (!this.hasListeners()) {
|
|
98
106
|
this.#currentMutation?.removeObserver(this)
|
package/src/queriesObserver.ts
CHANGED
|
@@ -40,7 +40,7 @@ export class QueriesObserver<
|
|
|
40
40
|
#queries: Array<QueryObserverOptions>
|
|
41
41
|
#options?: QueriesObserverOptions<TCombinedResult>
|
|
42
42
|
#observers: Array<QueryObserver>
|
|
43
|
-
#combinedResult
|
|
43
|
+
#combinedResult!: TCombinedResult
|
|
44
44
|
#lastCombine?: CombineFn<TCombinedResult>
|
|
45
45
|
#lastResult?: Array<QueryObserverResult>
|
|
46
46
|
#lastQueryHashes?: Array<string>
|
|
@@ -200,14 +200,19 @@ export class QueriesObserver<
|
|
|
200
200
|
result: Array<QueryObserverResult>,
|
|
201
201
|
matches: Array<QueryObserverMatch>,
|
|
202
202
|
) {
|
|
203
|
+
const trackedProps = new Set<keyof QueryObserverResult>()
|
|
204
|
+
|
|
203
205
|
return matches.map((match, index) => {
|
|
204
206
|
const observerResult = result[index]!
|
|
205
207
|
return !match.defaultedQueryOptions.notifyOnChangeProps
|
|
206
208
|
? match.observer.trackResult(observerResult, (accessedProp) => {
|
|
207
209
|
// track property on all observers to ensure proper (synchronized) tracking (#7000)
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
210
|
+
if (!trackedProps.has(accessedProp)) {
|
|
211
|
+
trackedProps.add(accessedProp)
|
|
212
|
+
matches.forEach((m) => {
|
|
213
|
+
m.observer.trackProp(accessedProp)
|
|
214
|
+
})
|
|
215
|
+
}
|
|
211
216
|
})
|
|
212
217
|
: observerResult
|
|
213
218
|
})
|
|
@@ -227,7 +232,6 @@ export class QueriesObserver<
|
|
|
227
232
|
queryHashes.some((hash, i) => hash !== lastHashes[i]))
|
|
228
233
|
|
|
229
234
|
if (
|
|
230
|
-
!this.#combinedResult ||
|
|
231
235
|
this.#result !== this.#lastResult ||
|
|
232
236
|
queryHashesChanged ||
|
|
233
237
|
combine !== this.#lastCombine
|
|
@@ -251,7 +255,7 @@ export class QueriesObserver<
|
|
|
251
255
|
|
|
252
256
|
#shouldSkipCombine(): boolean {
|
|
253
257
|
return (
|
|
254
|
-
this.#options?.combine
|
|
258
|
+
!this.#options?.combine ||
|
|
255
259
|
this.#observers.some((observer, index) => {
|
|
256
260
|
return (
|
|
257
261
|
observer.options.suspense && this.#result[index]?.data === undefined
|
|
@@ -305,12 +309,14 @@ export class QueriesObserver<
|
|
|
305
309
|
|
|
306
310
|
#notify(): void {
|
|
307
311
|
if (this.hasListeners()) {
|
|
308
|
-
const newTracked = this.#trackResult(this.#result, this.#observerMatches)
|
|
309
312
|
const shouldSkipCombine = this.#shouldSkipCombine()
|
|
310
313
|
const previousResult = this.#combinedResult
|
|
311
314
|
const newResult = shouldSkipCombine
|
|
312
315
|
? previousResult
|
|
313
|
-
: this.#combineResult(
|
|
316
|
+
: this.#combineResult(
|
|
317
|
+
this.#trackResult(this.#result, this.#observerMatches),
|
|
318
|
+
this.#options?.combine,
|
|
319
|
+
)
|
|
314
320
|
|
|
315
321
|
if (shouldSkipCombine || previousResult !== newResult) {
|
|
316
322
|
notifyManager.batch(() => {
|
package/src/query.ts
CHANGED
|
@@ -360,8 +360,9 @@ export class Query<
|
|
|
360
360
|
}
|
|
361
361
|
|
|
362
362
|
removeObserver(observer: QueryObserver<any, any, any, any, any>): void {
|
|
363
|
-
|
|
364
|
-
|
|
363
|
+
const index = this.observers.indexOf(observer)
|
|
364
|
+
if (index !== -1) {
|
|
365
|
+
this.observers.splice(index, 1)
|
|
365
366
|
|
|
366
367
|
if (!this.observers.length) {
|
|
367
368
|
// If the transport layer does not support cancellation
|
|
@@ -533,7 +534,7 @@ export class Query<
|
|
|
533
534
|
}
|
|
534
535
|
|
|
535
536
|
// Try to fetch the data
|
|
536
|
-
this.#retryer = createRetryer({
|
|
537
|
+
const retryer = (this.#retryer = createRetryer({
|
|
537
538
|
initialPromise: fetchOptions?.initialPromise as
|
|
538
539
|
| Promise<TData>
|
|
539
540
|
| undefined,
|
|
@@ -560,10 +561,10 @@ export class Query<
|
|
|
560
561
|
retryDelay: context.options.retryDelay,
|
|
561
562
|
networkMode: context.options.networkMode,
|
|
562
563
|
canRun: () => true,
|
|
563
|
-
})
|
|
564
|
+
}))
|
|
564
565
|
|
|
565
566
|
try {
|
|
566
|
-
const data = await
|
|
567
|
+
const data = await retryer.start()
|
|
567
568
|
// this is more of a runtime guard
|
|
568
569
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
569
570
|
if (data === undefined) {
|
|
@@ -619,6 +620,11 @@ export class Query<
|
|
|
619
620
|
|
|
620
621
|
throw error // rethrow the error for further handling
|
|
621
622
|
} finally {
|
|
623
|
+
// The settled retryer's promise would otherwise pin this fetch's raw
|
|
624
|
+
// result (a second copy after structural sharing) for the query's lifetime
|
|
625
|
+
if (this.#retryer === retryer) {
|
|
626
|
+
this.#retryer = undefined
|
|
627
|
+
}
|
|
622
628
|
// Schedule query gc after fetching
|
|
623
629
|
this.scheduleGc()
|
|
624
630
|
}
|
|
@@ -698,7 +704,9 @@ export class Query<
|
|
|
698
704
|
this.state = reducer(this.state)
|
|
699
705
|
|
|
700
706
|
notifyManager.batch(() => {
|
|
701
|
-
|
|
707
|
+
// Keep the current iteration stable if an observer unsubscribes
|
|
708
|
+
// synchronously while it is being notified.
|
|
709
|
+
this.observers.slice().forEach((observer) => {
|
|
702
710
|
observer.onQueryUpdate()
|
|
703
711
|
})
|
|
704
712
|
|
package/src/queryClient.ts
CHANGED
|
@@ -24,6 +24,7 @@ import type {
|
|
|
24
24
|
InferDataFromTag,
|
|
25
25
|
InferErrorFromTag,
|
|
26
26
|
InfiniteData,
|
|
27
|
+
InfiniteQueryExecuteOptions,
|
|
27
28
|
InvalidateOptions,
|
|
28
29
|
InvalidateQueryFilters,
|
|
29
30
|
MutationKey,
|
|
@@ -31,6 +32,7 @@ import type {
|
|
|
31
32
|
MutationOptions,
|
|
32
33
|
OmitKeyof,
|
|
33
34
|
QueryClientConfig,
|
|
35
|
+
QueryExecuteOptions,
|
|
34
36
|
QueryKey,
|
|
35
37
|
QueryObserverOptions,
|
|
36
38
|
QueryOptions,
|
|
@@ -135,6 +137,9 @@ export class QueryClient {
|
|
|
135
137
|
.data
|
|
136
138
|
}
|
|
137
139
|
|
|
140
|
+
/**
|
|
141
|
+
* @deprecated Use queryClient.query({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.
|
|
142
|
+
*/
|
|
138
143
|
ensureQueryData<
|
|
139
144
|
TQueryFnData,
|
|
140
145
|
TError = DefaultError,
|
|
@@ -260,13 +265,15 @@ export class QueryClient {
|
|
|
260
265
|
const queryCache = this.#queryCache
|
|
261
266
|
|
|
262
267
|
return notifyManager.batch(() => {
|
|
263
|
-
queryCache.findAll(filters)
|
|
268
|
+
const matched = queryCache.findAll(filters)
|
|
269
|
+
const queriesToRefetch = new Set(matched)
|
|
270
|
+
matched.forEach((query) => {
|
|
264
271
|
query.reset()
|
|
265
272
|
})
|
|
266
273
|
return this.refetchQueries(
|
|
267
274
|
{
|
|
268
275
|
type: 'active',
|
|
269
|
-
|
|
276
|
+
predicate: (query) => queriesToRefetch.has(query),
|
|
270
277
|
},
|
|
271
278
|
options,
|
|
272
279
|
)
|
|
@@ -336,6 +343,52 @@ export class QueryClient {
|
|
|
336
343
|
return Promise.all(promises).then(noop)
|
|
337
344
|
}
|
|
338
345
|
|
|
346
|
+
async query<
|
|
347
|
+
TQueryFnData,
|
|
348
|
+
TError = DefaultError,
|
|
349
|
+
TData = TQueryFnData,
|
|
350
|
+
TQueryData = TQueryFnData,
|
|
351
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
352
|
+
TPageParam = never,
|
|
353
|
+
>(
|
|
354
|
+
options: QueryExecuteOptions<
|
|
355
|
+
TQueryFnData,
|
|
356
|
+
TError,
|
|
357
|
+
TData,
|
|
358
|
+
TQueryData,
|
|
359
|
+
TQueryKey,
|
|
360
|
+
TPageParam
|
|
361
|
+
>,
|
|
362
|
+
): Promise<TData> {
|
|
363
|
+
const defaultedOptions = this.defaultQueryOptions(options)
|
|
364
|
+
|
|
365
|
+
// https://github.com/tannerlinsley/react-query/issues/652
|
|
366
|
+
if (defaultedOptions.retry === undefined) {
|
|
367
|
+
defaultedOptions.retry = false
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const query = this.#queryCache.build(this, defaultedOptions)
|
|
371
|
+
|
|
372
|
+
const isStale = query.isStaleByTime(
|
|
373
|
+
resolveStaleTime(defaultedOptions.staleTime, query),
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
const queryData = isStale
|
|
377
|
+
? await query.fetch(defaultedOptions)
|
|
378
|
+
: (query.state.data as TQueryData)
|
|
379
|
+
|
|
380
|
+
const select = defaultedOptions.select
|
|
381
|
+
|
|
382
|
+
if (select) {
|
|
383
|
+
return select(queryData)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return queryData as unknown as TData
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* @deprecated Use queryClient.query(options) instead. This method will be removed in the next major version.
|
|
391
|
+
*/
|
|
339
392
|
fetchQuery<
|
|
340
393
|
TQueryFnData,
|
|
341
394
|
TError = DefaultError,
|
|
@@ -367,6 +420,9 @@ export class QueryClient {
|
|
|
367
420
|
: Promise.resolve(query.state.data as TData)
|
|
368
421
|
}
|
|
369
422
|
|
|
423
|
+
/**
|
|
424
|
+
* @deprecated Use queryClient.query(options) instead. You can swallow errors with `.catch(noop)`. This method will be removed in the next major version.
|
|
425
|
+
*/
|
|
370
426
|
prefetchQuery<
|
|
371
427
|
TQueryFnData = unknown,
|
|
372
428
|
TError = DefaultError,
|
|
@@ -378,6 +434,32 @@ export class QueryClient {
|
|
|
378
434
|
return this.fetchQuery(options).then(noop).catch(noop)
|
|
379
435
|
}
|
|
380
436
|
|
|
437
|
+
infiniteQuery<
|
|
438
|
+
TQueryFnData,
|
|
439
|
+
TError = DefaultError,
|
|
440
|
+
TData = InfiniteData<TQueryFnData>,
|
|
441
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
442
|
+
TPageParam = unknown,
|
|
443
|
+
>(
|
|
444
|
+
options: InfiniteQueryExecuteOptions<
|
|
445
|
+
TQueryFnData,
|
|
446
|
+
TError,
|
|
447
|
+
TData,
|
|
448
|
+
TQueryKey,
|
|
449
|
+
TPageParam
|
|
450
|
+
>,
|
|
451
|
+
): Promise<
|
|
452
|
+
Array<TData> extends Array<InfiniteData<TQueryFnData>>
|
|
453
|
+
? InfiniteData<TQueryFnData, TPageParam>
|
|
454
|
+
: TData
|
|
455
|
+
> {
|
|
456
|
+
options._type = 'infinite'
|
|
457
|
+
return this.query(options as any)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* @deprecated Use queryClient.infiniteQuery(options) instead. This method will be removed in the next major version.
|
|
462
|
+
*/
|
|
381
463
|
fetchInfiniteQuery<
|
|
382
464
|
TQueryFnData,
|
|
383
465
|
TError = DefaultError,
|
|
@@ -397,6 +479,9 @@ export class QueryClient {
|
|
|
397
479
|
return this.fetchQuery(options as any)
|
|
398
480
|
}
|
|
399
481
|
|
|
482
|
+
/**
|
|
483
|
+
* @deprecated Use queryClient.infiniteQuery(options) instead. You can swallow errors with `.catch(noop)`. This method will be removed in the next major version.
|
|
484
|
+
*/
|
|
400
485
|
prefetchInfiniteQuery<
|
|
401
486
|
TQueryFnData,
|
|
402
487
|
TError = DefaultError,
|
|
@@ -415,6 +500,9 @@ export class QueryClient {
|
|
|
415
500
|
return this.fetchInfiniteQuery(options).then(noop).catch(noop)
|
|
416
501
|
}
|
|
417
502
|
|
|
503
|
+
/**
|
|
504
|
+
* @deprecated Use queryClient.infiniteQuery({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.
|
|
505
|
+
*/
|
|
418
506
|
ensureInfiniteQueryData<
|
|
419
507
|
TQueryFnData,
|
|
420
508
|
TError = DefaultError,
|
package/src/queryObserver.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { environmentManager } from './environmentManager'
|
|
|
3
3
|
import { notifyManager } from './notifyManager'
|
|
4
4
|
import { fetchState } from './query'
|
|
5
5
|
import { Subscribable } from './subscribable'
|
|
6
|
-
import { pendingThenable } from './thenable'
|
|
7
6
|
import {
|
|
8
7
|
isValidTimeout,
|
|
9
8
|
noop,
|
|
@@ -17,7 +16,6 @@ import { timeoutManager } from './timeoutManager'
|
|
|
17
16
|
import type { ManagedTimerId } from './timeoutManager'
|
|
18
17
|
import type { FetchOptions, Query, QueryState } from './query'
|
|
19
18
|
import type { QueryClient } from './queryClient'
|
|
20
|
-
import type { PendingThenable, Thenable } from './thenable'
|
|
21
19
|
import type {
|
|
22
20
|
DefaultError,
|
|
23
21
|
DefaultedQueryObserverOptions,
|
|
@@ -56,7 +54,6 @@ export class QueryObserver<
|
|
|
56
54
|
TQueryData,
|
|
57
55
|
TQueryKey
|
|
58
56
|
>
|
|
59
|
-
#currentThenable: Thenable<TData>
|
|
60
57
|
#selectError: TError | null
|
|
61
58
|
#selectFn?: (data: TQueryData) => TData
|
|
62
59
|
#selectResult?: TData
|
|
@@ -82,7 +79,6 @@ export class QueryObserver<
|
|
|
82
79
|
|
|
83
80
|
this.#client = client
|
|
84
81
|
this.#selectError = null
|
|
85
|
-
this.#currentThenable = pendingThenable()
|
|
86
82
|
|
|
87
83
|
this.bindMethods()
|
|
88
84
|
this.setOptions(options)
|
|
@@ -268,19 +264,6 @@ export class QueryObserver<
|
|
|
268
264
|
get: (target, key) => {
|
|
269
265
|
this.trackProp(key as keyof QueryObserverResult)
|
|
270
266
|
onPropTracked?.(key as keyof QueryObserverResult)
|
|
271
|
-
if (key === 'promise') {
|
|
272
|
-
this.trackProp('data')
|
|
273
|
-
if (
|
|
274
|
-
!this.options.experimental_prefetchInRender &&
|
|
275
|
-
this.#currentThenable.status === 'pending'
|
|
276
|
-
) {
|
|
277
|
-
this.#currentThenable.reject(
|
|
278
|
-
new Error(
|
|
279
|
-
'experimental_prefetchInRender feature flag is not enabled',
|
|
280
|
-
),
|
|
281
|
-
)
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
267
|
return Reflect.get(target, key)
|
|
285
268
|
},
|
|
286
269
|
})
|
|
@@ -317,7 +300,42 @@ export class QueryObserver<
|
|
|
317
300
|
.getQueryCache()
|
|
318
301
|
.build(this.#client, defaultedOptions)
|
|
319
302
|
|
|
320
|
-
|
|
303
|
+
let unsubscribe = () => {}
|
|
304
|
+
let resolveEarly:
|
|
305
|
+
| ((result: QueryObserverResult<TData, TError>) => void)
|
|
306
|
+
| undefined
|
|
307
|
+
|
|
308
|
+
const cachePromise = new Promise<QueryObserverResult<TData, TError>>(
|
|
309
|
+
(resolve) => {
|
|
310
|
+
resolveEarly = resolve
|
|
311
|
+
unsubscribe = this.#client.getQueryCache().subscribe((event) => {
|
|
312
|
+
if (
|
|
313
|
+
event.type === 'updated' &&
|
|
314
|
+
event.query.queryHash === query.queryHash &&
|
|
315
|
+
query.state.data !== undefined
|
|
316
|
+
) {
|
|
317
|
+
unsubscribe()
|
|
318
|
+
resolve(this.createResult(query, defaultedOptions))
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
},
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
return Promise.race([
|
|
325
|
+
query
|
|
326
|
+
.fetch()
|
|
327
|
+
.then(() => {
|
|
328
|
+
const result = this.createResult(query, defaultedOptions)
|
|
329
|
+
// Settle the subscriber promise so both branches always settle.
|
|
330
|
+
// This value is ignored by Promise.race since the fetch branch already won.
|
|
331
|
+
resolveEarly?.(result)
|
|
332
|
+
return result
|
|
333
|
+
})
|
|
334
|
+
.finally(() => {
|
|
335
|
+
unsubscribe()
|
|
336
|
+
}),
|
|
337
|
+
cachePromise,
|
|
338
|
+
])
|
|
321
339
|
}
|
|
322
340
|
|
|
323
341
|
protected fetch(
|
|
@@ -545,6 +563,10 @@ export class QueryObserver<
|
|
|
545
563
|
this.#selectError = selectError as TError
|
|
546
564
|
}
|
|
547
565
|
}
|
|
566
|
+
} else if (data === undefined) {
|
|
567
|
+
// a stored select error belongs to previously selected data; once that
|
|
568
|
+
// data is gone (query switch or reset), it must not leak into this result
|
|
569
|
+
this.#selectError = null
|
|
548
570
|
}
|
|
549
571
|
|
|
550
572
|
if (this.#selectError) {
|
|
@@ -552,6 +574,7 @@ export class QueryObserver<
|
|
|
552
574
|
data = this.#selectResult
|
|
553
575
|
errorUpdatedAt = Date.now()
|
|
554
576
|
status = 'error'
|
|
577
|
+
isPlaceholderData = false
|
|
555
578
|
}
|
|
556
579
|
|
|
557
580
|
const isFetching = newState.fetchStatus === 'fetching'
|
|
@@ -588,57 +611,11 @@ export class QueryObserver<
|
|
|
588
611
|
isRefetchError: isError && hasData,
|
|
589
612
|
isStale: isStale(query, options),
|
|
590
613
|
refetch: this.refetch,
|
|
591
|
-
promise: this.#currentThenable,
|
|
592
614
|
isEnabled: resolveQueryBoolean(options.enabled, query) !== false,
|
|
593
615
|
}
|
|
594
616
|
|
|
595
617
|
const nextResult = result as QueryObserverResult<TData, TError>
|
|
596
618
|
|
|
597
|
-
if (this.options.experimental_prefetchInRender) {
|
|
598
|
-
const hasResultData = nextResult.data !== undefined
|
|
599
|
-
const isErrorWithoutData = nextResult.status === 'error' && !hasResultData
|
|
600
|
-
const finalizeThenableIfPossible = (thenable: PendingThenable<TData>) => {
|
|
601
|
-
if (isErrorWithoutData) {
|
|
602
|
-
thenable.reject(nextResult.error)
|
|
603
|
-
} else if (hasResultData) {
|
|
604
|
-
thenable.resolve(nextResult.data as TData)
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
* Create a new thenable and result promise when the results have changed
|
|
610
|
-
*/
|
|
611
|
-
const recreateThenable = () => {
|
|
612
|
-
const pending =
|
|
613
|
-
(this.#currentThenable =
|
|
614
|
-
nextResult.promise =
|
|
615
|
-
pendingThenable())
|
|
616
|
-
|
|
617
|
-
finalizeThenableIfPossible(pending)
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
const prevThenable = this.#currentThenable
|
|
621
|
-
switch (prevThenable.status) {
|
|
622
|
-
case 'pending':
|
|
623
|
-
// Finalize the previous thenable if it was pending
|
|
624
|
-
// and we are still observing the same query
|
|
625
|
-
if (query.queryHash === prevQuery.queryHash) {
|
|
626
|
-
finalizeThenableIfPossible(prevThenable)
|
|
627
|
-
}
|
|
628
|
-
break
|
|
629
|
-
case 'fulfilled':
|
|
630
|
-
if (isErrorWithoutData || nextResult.data !== prevThenable.value) {
|
|
631
|
-
recreateThenable()
|
|
632
|
-
}
|
|
633
|
-
break
|
|
634
|
-
case 'rejected':
|
|
635
|
-
if (!isErrorWithoutData || nextResult.error !== prevThenable.reason) {
|
|
636
|
-
recreateThenable()
|
|
637
|
-
}
|
|
638
|
-
break
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
619
|
return nextResult
|
|
643
620
|
}
|
|
644
621
|
|