@@ -17,6 +17,7 @@ import {
17
17
willFetch ,
18
18
} from './suspense'
19
19
import { noop } from './utils'
20
+ import { usePauseManager } from './PauseManagerProvider'
20
21
import type {
21
22
QueryClient ,
22
23
QueryKey ,
@@ -53,6 +54,7 @@ export function useBaseQuery<
53
54
const client = useQueryClient ( queryClient )
54
55
const isRestoring = useIsRestoring ( )
55
56
const errorResetBoundary = useQueryErrorResetBoundary ( )
57
+ const pauseManager = usePauseManager ( )
56
58
const defaultedOptions = client . defaultQueryOptions ( options )
57
59
58
60
; ( client . getDefaultOptions ( ) . queries as any ) ?. _experimental_beforeQuery ?.(
@@ -97,17 +99,37 @@ export function useBaseQuery<
97
99
React . useSyncExternalStore (
98
100
React . useCallback (
99
101
( onStoreChange ) => {
100
- const unsubscribe = shouldSubscribe
101
- ? observer . subscribe ( notifyManager . batchCalls ( onStoreChange ) )
102
- : noop
102
+ if ( ! shouldSubscribe ) {
103
+ return noop
104
+ }
105
+
106
+ const subscribe = ( ) =>
107
+ observer . subscribe ( notifyManager . batchCalls ( onStoreChange ) )
108
+ let unsubscribe : ReturnType < typeof subscribe >
109
+
110
+ if ( pauseManager ) {
111
+ let unsubscribeObserver : ( ( ) => void ) | undefined
112
+ const onPausedChange = ( paused : boolean ) => {
113
+ unsubscribeObserver ?.( )
114
+ unsubscribeObserver = paused ? undefined : subscribe ( )
115
+ }
116
+ const unsubscribeVisibility = pauseManager . subscribe ( onPausedChange )
117
+ onPausedChange ( pauseManager . isPaused ( ) )
118
+ unsubscribe = ( ) => {
119
+ unsubscribeVisibility ( )
120
+ unsubscribeObserver ?.( )
121
+ }
122
+ } else {
123
+ unsubscribe = subscribe ( )
124
+ }
103
125
104
126
// Update result to make sure we did not miss any query updates
105
127
// between creating the observer and subscribing to it.
106
128
observer . updateResult ( )
107
129
108
130
return unsubscribe
109
131
} ,
110
- [ observer , shouldSubscribe ] ,
132
+ [ observer , shouldSubscribe , pauseManager ] ,
111
133
) ,
112
134
( ) => observer . getCurrentResult ( ) ,
113
135
( ) => observer . getCurrentResult ( ) ,
0 commit comments