1
1
import React from 'react' ;
2
- import { useWatchedQuerySubscription } from './useWatchedQuerySubscription.js' ;
2
+ import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js' ;
3
3
import { DifferentialHookOptions , QueryResult , ReadonlyQueryResult } from './watch-types.js' ;
4
4
import { InternalHookOptions } from './watch-utils.js' ;
5
5
@@ -14,9 +14,13 @@ import { InternalHookOptions } from './watch-utils.js';
14
14
export const useWatchedQuery = < RowType = unknown > (
15
15
options : InternalHookOptions < RowType [ ] > & { options : DifferentialHookOptions < RowType > }
16
16
) : QueryResult < RowType > | ReadonlyQueryResult < RowType > => {
17
- const { query, powerSync, queryChanged, options : hookOptions } = options ;
17
+ const { query, powerSync, queryChanged, options : hookOptions , active } = options ;
18
+
19
+ function createWatchedQuery ( ) {
20
+ if ( ! active ) {
21
+ return null ;
22
+ }
18
23
19
- const createWatchedQuery = React . useCallback ( ( ) => {
20
24
const watch = hookOptions . rowComparator
21
25
? powerSync . customQuery ( query ) . differentialWatch ( {
22
26
rowComparator : hookOptions . rowComparator ,
@@ -28,26 +32,26 @@ export const useWatchedQuery = <RowType = unknown>(
28
32
throttleMs : hookOptions . throttleMs
29
33
} ) ;
30
34
return watch ;
31
- } , [ ] ) ;
35
+ }
32
36
33
37
const [ watchedQuery , setWatchedQuery ] = React . useState ( createWatchedQuery ) ;
34
38
35
39
React . useEffect ( ( ) => {
36
- watchedQuery . close ( ) ;
40
+ watchedQuery ? .close ( ) ;
37
41
setWatchedQuery ( createWatchedQuery ) ;
38
- } , [ powerSync ] ) ;
42
+ } , [ powerSync , active ] ) ;
39
43
40
44
// Indicates that the query will be re-fetched due to a change in the query.
41
45
// Used when `isFetching` hasn't been set to true yet due to React execution.
42
46
React . useEffect ( ( ) => {
43
47
if ( queryChanged ) {
44
- watchedQuery . updateSettings ( {
48
+ watchedQuery ? .updateSettings ( {
45
49
query,
46
50
throttleMs : hookOptions . throttleMs ,
47
51
reportFetching : hookOptions . reportFetching
48
52
} ) ;
49
53
}
50
54
} , [ queryChanged ] ) ;
51
55
52
- return useWatchedQuerySubscription ( watchedQuery ) ;
56
+ return useNullableWatchedQuerySubscription ( watchedQuery ) ;
53
57
} ;
0 commit comments