11import React from 'react' ;
2- import { useWatchedQuerySubscription } from './useWatchedQuerySubscription.js' ;
2+ import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js' ;
33import { DifferentialHookOptions , QueryResult , ReadonlyQueryResult } from './watch-types.js' ;
44import { InternalHookOptions } from './watch-utils.js' ;
55
@@ -14,9 +14,13 @@ import { InternalHookOptions } from './watch-utils.js';
1414export const useWatchedQuery = < RowType = unknown > (
1515 options : InternalHookOptions < RowType [ ] > & { options : DifferentialHookOptions < RowType > }
1616) : 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+ }
1823
19- const createWatchedQuery = React . useCallback ( ( ) => {
2024 const watch = hookOptions . rowComparator
2125 ? powerSync . customQuery ( query ) . differentialWatch ( {
2226 rowComparator : hookOptions . rowComparator ,
@@ -28,26 +32,26 @@ export const useWatchedQuery = <RowType = unknown>(
2832 throttleMs : hookOptions . throttleMs
2933 } ) ;
3034 return watch ;
31- } , [ ] ) ;
35+ }
3236
3337 const [ watchedQuery , setWatchedQuery ] = React . useState ( createWatchedQuery ) ;
3438
3539 React . useEffect ( ( ) => {
36- watchedQuery . close ( ) ;
40+ watchedQuery ? .close ( ) ;
3741 setWatchedQuery ( createWatchedQuery ) ;
38- } , [ powerSync ] ) ;
42+ } , [ powerSync , active ] ) ;
3943
4044 // Indicates that the query will be re-fetched due to a change in the query.
4145 // Used when `isFetching` hasn't been set to true yet due to React execution.
4246 React . useEffect ( ( ) => {
4347 if ( queryChanged ) {
44- watchedQuery . updateSettings ( {
48+ watchedQuery ? .updateSettings ( {
4549 query,
4650 throttleMs : hookOptions . throttleMs ,
4751 reportFetching : hookOptions . reportFetching
4852 } ) ;
4953 }
5054 } , [ queryChanged ] ) ;
5155
52- return useWatchedQuerySubscription ( watchedQuery ) ;
56+ return useNullableWatchedQuerySubscription ( watchedQuery ) ;
5357} ;
0 commit comments