@@ -28,6 +28,7 @@ import {
2828 StreamingSyncImplementation ,
2929 StreamingSyncImplementationListener
3030} from './sync/stream/AbstractStreamingSyncImplementation.js' ;
31+ import { runOnSchemaChange } from './runOnSchemaChange.js' ;
3132
3233export interface DisconnectAndClearOptions {
3334 /** When set to false, data in local-only tables is preserved. */
@@ -103,6 +104,7 @@ export interface WatchOnChangeHandler {
103104
104105export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
105106 initialized : ( ) => void ;
107+ schemaChanged : ( schema : Schema ) => void ;
106108}
107109
108110export interface PowerSyncCloseOptions {
@@ -360,7 +362,10 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
360362 this . options . logger ?. warn ( 'Schema validation failed. Unexpected behaviour could occur' , ex ) ;
361363 }
362364 this . _schema = schema ;
365+
363366 await this . database . execute ( 'SELECT powersync_replace_schema(?)' , [ JSON . stringify ( this . schema . toJSON ( ) ) ] ) ;
367+ await this . database . refreshSchema ( ) ;
368+ this . iterateListeners ( async ( cb ) => cb . schemaChanged ?.( schema ) ) ;
364369 }
365370
366371 /**
@@ -758,10 +763,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
758763 throw new Error ( 'onResult is required' ) ;
759764 }
760765
761- ( async ( ) => {
766+ const watchQuery = async ( abortSignal : AbortSignal ) => {
762767 try {
763768 const resolvedTables = await this . resolveTables ( sql , parameters , options ) ;
764-
765769 // Fetch initial data
766770 const result = await this . executeReadOnly ( sql , parameters ) ;
767771 onResult ( result ) ;
@@ -780,13 +784,17 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
780784 } ,
781785 {
782786 ...( options ?? { } ) ,
783- tables : resolvedTables
787+ tables : resolvedTables ,
788+ // Override the abort signal since we intercept it
789+ signal : abortSignal
784790 }
785791 ) ;
786792 } catch ( error ) {
787793 onError ?.( error ) ;
788794 }
789- } ) ( ) ;
795+ } ;
796+
797+ runOnSchemaChange ( watchQuery , this , options ) ;
790798 }
791799
792800 /**
@@ -796,19 +804,20 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
796804 */
797805 watchWithAsyncGenerator ( sql : string , parameters ?: any [ ] , options ?: SQLWatchOptions ) : AsyncIterable < QueryResult > {
798806 return new EventIterator < QueryResult > ( ( eventOptions ) => {
799- ( async ( ) => {
800- const resolvedTables = await this . resolveTables ( sql , parameters , options ) ;
807+ const handler : WatchHandler = {
808+ onResult : ( result ) => {
809+ eventOptions . push ( result ) ;
810+ } ,
811+ onError : ( error ) => {
812+ eventOptions . fail ( error ) ;
813+ }
814+ } ;
801815
802- // Fetch initial data
803- eventOptions . push ( await this . executeReadOnly ( sql , parameters ) ) ;
816+ this . watchWithCallback ( sql , parameters , handler , options ) ;
804817
805- for await ( const event of this . onChangeWithAsyncGenerator ( {
806- ...( options ?? { } ) ,
807- tables : resolvedTables
808- } ) ) {
809- eventOptions . push ( await this . executeReadOnly ( sql , parameters ) ) ;
810- }
811- } ) ( ) ;
818+ options ?. signal ?. addEventListener ( 'abort' , ( ) => {
819+ eventOptions . stop ( ) ;
820+ } ) ;
812821 } ) ;
813822 }
814823
0 commit comments