File tree Expand file tree Collapse file tree 2 files changed +24
-15
lines changed Expand file tree Collapse file tree 2 files changed +24
-15
lines changed Original file line number Diff line number Diff line change @@ -21,17 +21,14 @@ export const PersistQueryClientProvider = ({
21
21
const [ isHydrating , setIsHydrating ] = React . useState ( true )
22
22
const options = React . useRef ( persistOptions )
23
23
React . useEffect ( ( ) => {
24
- let unsubscribe : ( ( ) => void ) | undefined
24
+ const [ unsubscribe , promise ] = persistQueryClient ( {
25
+ ...options . current ,
26
+ queryClient : client ,
27
+ } )
25
28
26
- async function run ( ) {
27
- unsubscribe = await persistQueryClient ( {
28
- ...options . current ,
29
- queryClient : client ,
30
- } )
29
+ promise . then ( ( ) => {
31
30
setIsHydrating ( false )
32
- }
33
-
34
- void run ( )
31
+ } )
35
32
36
33
return unsubscribe
37
34
} , [ client ] )
Original file line number Diff line number Diff line change @@ -129,14 +129,26 @@ export function persistQueryClientSubscribe(
129
129
130
130
/**
131
131
* Restores persisted data to QueryCache and persists further changes.
132
- * (Retained for backwards compatibility)
133
132
*/
134
- export async function persistQueryClient ( props : PersistQueryClientOptions ) {
133
+ export function persistQueryClient (
134
+ props : PersistQueryClientOptions
135
+ ) : [ ( ) => void , Promise < void > ] {
136
+ let hasUnsubscribed = false
137
+ let unsubscribe = ( ) => {
138
+ hasUnsubscribed = true
139
+ }
140
+
141
+ let restorePromise = Promise . resolve ( )
142
+
135
143
if ( typeof window !== 'undefined' ) {
136
144
// Attempt restore
137
- await persistQueryClientRestore ( props )
138
-
139
- // Subscribe to changes in the query cache to trigger the save
140
- return persistQueryClientSubscribe ( props )
145
+ restorePromise = persistQueryClientRestore ( props ) . then ( ( ) => {
146
+ if ( ! hasUnsubscribed ) {
147
+ // Subscribe to changes in the query cache to trigger the save
148
+ unsubscribe = persistQueryClientSubscribe ( props )
149
+ }
150
+ } )
141
151
}
152
+
153
+ return [ unsubscribe , restorePromise ]
142
154
}
You can’t perform that action at this time.
0 commit comments