@@ -10,7 +10,11 @@ export type InternalHookOptions<DataType> = {
1010 active : boolean ;
1111} ;
1212
13- export const checkQueryChanged = < T > ( query : WatchCompatibleQuery < T > , options : AdditionalOptions ) => {
13+ interface WatchCompatibleQueryWithParams < T > extends WatchCompatibleQuery < T > {
14+ stringifiedParameters ?: string ;
15+ }
16+
17+ export const checkQueryChanged = < T > ( query : WatchCompatibleQueryWithParams < T > , options : AdditionalOptions ) => {
1418 let _compiled : CompiledQuery ;
1519 try {
1620 _compiled = query . compile ( ) ;
@@ -19,7 +23,7 @@ export const checkQueryChanged = <T>(query: WatchCompatibleQuery<T>, options: Ad
1923 }
2024 const compiled = _compiled ! ;
2125
22- const stringifiedParams = JSON . stringify ( compiled . parameters ) ;
26+ const stringifiedParams = query . stringifiedParameters ?? JSON . stringify ( compiled . parameters ) ;
2327 const stringifiedOptions = JSON . stringify ( options ) ;
2428
2529 const previousQueryRef = React . useRef ( { sqlStatement : compiled . sql , stringifiedParams, stringifiedOptions } ) ;
@@ -45,15 +49,18 @@ export const constructCompatibleQuery = <RowType>(
4549 options : AdditionalOptions
4650) => {
4751 const powerSync = usePowerSync ( ) ;
52+ const stringifiedParameters = React . useMemo ( ( ) => JSON . stringify ( parameters ) , [ parameters ] ) ;
4853
49- const parsedQuery = React . useMemo < WatchCompatibleQuery < RowType [ ] > > ( ( ) => {
54+ const parsedQuery = React . useMemo < WatchCompatibleQueryWithParams < RowType [ ] > > ( ( ) => {
5055 if ( typeof query == 'string' ) {
5156 return {
5257 compile : ( ) => ( {
5358 sql : query ,
5459 parameters
5560 } ) ,
56- execute : ( ) => powerSync . getAll ( query , parameters )
61+ execute : ( ) => powerSync . getAll ( query , parameters ) ,
62+ // Setting this is a small optimization that avoids checkQueryChanged recomputing the JSON representation.
63+ stringifiedParameters
5764 } ;
5865 } else {
5966 return {
@@ -66,9 +73,11 @@ export const constructCompatibleQuery = <RowType>(
6673 } ;
6774 } ,
6875 execute : ( ) => query . execute ( )
76+ // Note that we can't set stringifiedParameters here because we only know parameters after the query has been
77+ // compiled.
6978 } ;
7079 }
71- } , [ query , powerSync , ... parameters ] ) ;
80+ } , [ query , powerSync , stringifiedParameters ] ) ;
7281
7382 const queryChanged = checkQueryChanged ( parsedQuery , options ) ;
7483
0 commit comments