@@ -34,6 +34,7 @@ import {
3434} from './sync/stream/AbstractStreamingSyncImplementation.js' ;
3535import { WatchedQuery , WatchedQueryOptions } from './watched/WatchedQuery.js' ;
3636import { OnChangeQueryProcessor , WatchedQueryComparator } from './watched/processors/OnChangeQueryProcessor.js' ;
37+ import { FalsyComparator } from './watched/processors/comparators.js' ;
3738
3839export interface DisconnectAndClearOptions {
3940 /** When set to false, data in local-only tables is preserved. */
@@ -71,6 +72,18 @@ export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatab
7172 database : SQLOpenOptions ;
7273}
7374
75+ export interface WatchComparatorOptions < DataType > {
76+ mode : 'comparison' ;
77+ comparator ?: WatchedQueryComparator < DataType > ;
78+ }
79+
80+ export type WatchProcessorOptions < DataType > = WatchComparatorOptions < DataType > ;
81+
82+ export interface IncrementalWatchOptions < DataType > {
83+ watch : WatchedQueryOptions < DataType > ;
84+ processor ?: WatchProcessorOptions < DataType > ;
85+ }
86+
7487export interface SQLWatchOptions {
7588 signal ?: AbortSignal ;
7689 tables ?: string [ ] ;
@@ -92,7 +105,7 @@ export interface SQLWatchOptions {
92105 * Optional comparator which will be used to compare the results of the query.
93106 * The watched query will only yield results if the comparator returns false.
94107 */
95- comparator ?: WatchedQueryComparator < QueryResult > ;
108+ processor ?: WatchProcessorOptions < QueryResult > ;
96109}
97110
98111export interface WatchOnChangeEvent {
@@ -109,16 +122,6 @@ export interface WatchOnChangeHandler {
109122 onError ?: ( error : Error ) => void ;
110123}
111124
112- export interface ComparatorWatchOptions < DataType > {
113- mode : 'comparison' ;
114- comparator ?: WatchedQueryComparator < DataType > ;
115- }
116-
117- export interface IncrementalWatchOptions < DataType > {
118- watch : WatchedQueryOptions < DataType > ;
119- processor ?: ComparatorWatchOptions < DataType > ;
120- }
121-
122125export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
123126 initialized : ( ) => void ;
124127 schemaChanged : ( schema : Schema ) => void ;
@@ -916,25 +919,27 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
916919 throw new Error ( 'onResult is required' ) ;
917920 }
918921
919- const watch = new OnChangeQueryProcessor ( {
920- db : this ,
921- // Comparisons are disabled if no comparator is provided
922- comparator : options ?. comparator ,
923- watchOptions : {
924- placeholderData : null ,
922+ // Uses shared incremental watch logic under the hook, but maintains the same external API as the old watch method.
923+ const watchedQuery = this . incrementalWatch ( {
924+ watch : {
925925 query : {
926926 compile : ( ) => ( {
927927 sql : sql ,
928928 parameters : parameters ?? [ ]
929929 } ) ,
930930 execute : ( ) => this . executeReadOnly ( sql , parameters )
931931 } ,
932- throttleMs : options ?. throttleMs ?? DEFAULT_WATCH_THROTTLE_MS ,
933- reportFetching : false
932+ placeholderData : null ,
933+ reportFetching : false ,
934+ throttleMs : options ?. throttleMs ?? DEFAULT_WATCH_THROTTLE_MS
935+ } ,
936+ processor : options ?. processor ?? {
937+ mode : 'comparison' ,
938+ comparator : FalsyComparator
934939 }
935940 } ) ;
936941
937- const dispose = watch . subscribe ( {
942+ const dispose = watchedQuery . subscribe ( {
938943 onData : ( data ) => {
939944 if ( ! data ) {
940945 // This should not happen. We only use null for the initial data.
@@ -949,7 +954,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
949954
950955 options ?. signal ?. addEventListener ( 'abort' , ( ) => {
951956 dispose ( ) ;
952- watch . close ( ) ;
957+ watchedQuery . close ( ) ;
953958 } ) ;
954959 }
955960
0 commit comments