@@ -35,7 +35,9 @@ export interface WatchOnChangeEvent {
3535 changedTables : string [ ] ;
3636}
3737
38- export interface PowerSyncDBListener extends StreamingSyncImplementationListener { }
38+ export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
39+ initialized : ( ) => void ;
40+ }
3941
4042const POWERSYNC_TABLE_MATCH = / ( ^ p s _ d a t a _ _ | ^ p s _ d a t a _ l o c a l _ _ ) / ;
4143
@@ -71,27 +73,25 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
7173 private syncStatusListenerDisposer ?: ( ) => void ;
7274 protected initialized : Promise < void > ;
7375
74- /**
75- * Initializes PowerSync
76- */
77- public init : ( ) => Promise < void > ;
78-
7976 constructor ( protected options : PowerSyncDatabaseOptions ) {
8077 super ( ) ;
8178 this . currentStatus = null ;
8279 this . closed = true ;
8380 this . options = { ...DEFAULT_POWERSYNC_DB_OPTIONS , ...options } ;
8481 this . bucketStorageAdapter = this . generateBucketStorageAdapter ( ) ;
8582 this . sdkVersion = '' ;
86- // This ensures the `initialized` promise is pending even before `init` is called
87- this . initialized = new Promise ( ( resolve , reject ) => {
88- this . init = async ( ) => {
89- try {
90- resolve ( await this . initFn ( ) ) ;
91- } catch ( ex ) {
92- reject ( ex ) ;
83+ /**
84+ * This will resolve once the DB has been initialized
85+ * This allows methods such as `execute` to await initialization before
86+ * executing statements
87+ */
88+ this . initialized = new Promise ( ( resolve ) => {
89+ const l = this . registerListener ( {
90+ initialized : ( ) => {
91+ resolve ( ) ;
92+ l ?.( ) ;
9393 }
94- } ;
94+ } ) ;
9595 } ) ;
9696 }
9797
@@ -118,12 +118,13 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
118118 /**
119119 * This performs the total initialization process.
120120 */
121- private async initFn ( ) {
121+ async init ( ) {
122122 await this . _init ( ) ;
123123 await this . bucketStorageAdapter . init ( ) ;
124124 await this . database . execute ( 'SELECT powersync_replace_schema(?)' , [ JSON . stringify ( this . schema . toJSON ( ) ) ] ) ;
125125 const version = await this . options . database . execute ( 'SELECT powersync_rs_version()' ) ;
126126 this . sdkVersion = version . rows ?. item ( 0 ) [ 'powersync_rs_version()' ] ?? '' ;
127+ this . iterateListeners ( ( cb ) => cb . initialized ?.( ) ) ;
127128 }
128129
129130 /**
0 commit comments