@@ -72,17 +72,18 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
7272 private abortController : AbortController | null ;
7373 protected bucketStorageAdapter : BucketStorageAdapter ;
7474 private syncStatusListenerDisposer ?: ( ) => void ;
75- protected _isReadyPromise : Promise < void > | null ;
75+ protected _isReadyPromise : Promise < void > ;
7676
7777 constructor ( protected options : PowerSyncDatabaseOptions ) {
7878 super ( ) ;
79- this . _isReadyPromise = null ;
8079 this . bucketStorageAdapter = this . generateBucketStorageAdapter ( ) ;
8180 this . closed = true ;
8281 this . currentStatus = null ;
8382 this . options = { ...DEFAULT_POWERSYNC_DB_OPTIONS , ...options } ;
8483 this . ready = false ;
8584 this . sdkVersion = '' ;
85+ // Start async init
86+ this . _isReadyPromise = this . initialize ( ) ;
8687 }
8788
8889 get schema ( ) {
@@ -111,34 +112,37 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
111112 return ;
112113 }
113114
114- return (
115- this . _isReadyPromise ||
116- ( this . _isReadyPromise = new Promise ( ( resolve ) => {
117- const l = this . registerListener ( {
118- initialized : ( ) => {
119- this . ready = true ;
120- resolve ( ) ;
121- l ?.( ) ;
122- }
123- } ) ;
124- } ) )
125- ) ;
115+ await this . _isReadyPromise ;
126116 }
127117
128- abstract _init ( ) : Promise < void > ;
118+ /**
119+ * Allows for extended implementations to execute custom initialization
120+ * logic as part of the total init process
121+ */
122+ abstract _initialize ( ) : Promise < void > ;
129123
130124 /**
131- * This performs the total initialization process.
125+ * Entry point for executing initialization logic.
126+ * This is to be automatically executed in the constructor.
132127 */
133- async init ( ) {
134- await this . _init ( ) ;
128+ protected async initialize ( ) {
129+ await this . _initialize ( ) ;
135130 await this . bucketStorageAdapter . init ( ) ;
136131 await this . database . execute ( 'SELECT powersync_replace_schema(?)' , [ JSON . stringify ( this . schema . toJSON ( ) ) ] ) ;
137132 const version = await this . options . database . execute ( 'SELECT powersync_rs_version()' ) ;
138133 this . sdkVersion = version . rows ?. item ( 0 ) [ 'powersync_rs_version()' ] ?? '' ;
134+ this . ready = true ;
139135 this . iterateListeners ( ( cb ) => cb . initialized ?.( ) ) ;
140136 }
141137
138+ /**
139+ * Wait for initialization to complete.
140+ * While initializing is automatic, this helps to catch and report initialization errors.
141+ */
142+ async init ( ) {
143+ return this . waitForReady ( ) ;
144+ }
145+
142146 /**
143147 * Connects to stream of events from PowerSync instance
144148 */
0 commit comments