diff --git a/.changeset/itchy-jobs-return.md b/.changeset/itchy-jobs-return.md new file mode 100644 index 00000000..ea27bd8b --- /dev/null +++ b/.changeset/itchy-jobs-return.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/powersync-sdk-common': patch +--- + +Fixed regression where `waitForReady` would not trigger or resolve if not invoked before `init` diff --git a/apps/supabase-todolist b/apps/supabase-todolist index 304c1611..e6a1d045 160000 --- a/apps/supabase-todolist +++ b/apps/supabase-todolist @@ -1 +1 @@ -Subproject commit 304c161193bf5d038e2fb5cad34c2f24c6b561e0 +Subproject commit e6a1d045ab8244041651757402adc562a6699a4f diff --git a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts index df91958f..b307c135 100644 --- a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts +++ b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts @@ -72,17 +72,18 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver void; - protected _isReadyPromise: Promise | null; + protected _isReadyPromise: Promise; constructor(protected options: PowerSyncDatabaseOptions) { super(); - this._isReadyPromise = null; this.bucketStorageAdapter = this.generateBucketStorageAdapter(); this.closed = true; this.currentStatus = null; this.options = { ...DEFAULT_POWERSYNC_DB_OPTIONS, ...options }; this.ready = false; this.sdkVersion = ''; + // Start async init + this._isReadyPromise = this.initialize(); } get schema() { @@ -111,34 +112,37 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver { - const l = this.registerListener({ - initialized: () => { - this.ready = true; - resolve(); - l?.(); - } - }); - })) - ); + await this._isReadyPromise; } - abstract _init(): Promise; + /** + * Allows for extended implementations to execute custom initialization + * logic as part of the total init process + */ + abstract _initialize(): Promise; /** - * This performs the total initialization process. + * Entry point for executing initialization logic. + * This is to be automatically executed in the constructor. */ - async init() { - await this._init(); + protected async initialize() { + await this._initialize(); await this.bucketStorageAdapter.init(); await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]); const version = await this.options.database.execute('SELECT powersync_rs_version()'); this.sdkVersion = version.rows?.item(0)['powersync_rs_version()'] ?? ''; + this.ready = true; this.iterateListeners((cb) => cb.initialized?.()); } + /** + * Wait for initialization to complete. + * While initializing is automatic, this helps to catch and report initialization errors. + */ + async init() { + return this.waitForReady(); + } + /** * Connects to stream of events from PowerSync instance */ diff --git a/packages/powersync-sdk-react-native/src/db/PowerSyncDatabase.ts b/packages/powersync-sdk-react-native/src/db/PowerSyncDatabase.ts index 933ebaf6..0e92198a 100644 --- a/packages/powersync-sdk-react-native/src/db/PowerSyncDatabase.ts +++ b/packages/powersync-sdk-react-native/src/db/PowerSyncDatabase.ts @@ -9,7 +9,7 @@ import { ReactNativeRemote } from '../sync/stream/ReactNativeRemote'; import { ReactNativeStreamingSyncImplementation } from '../sync/stream/ReactNativeStreamingSyncImplementation'; export class PowerSyncDatabase extends AbstractPowerSyncDatabase { - async _init(): Promise {} + async _initialize(): Promise {} protected generateBucketStorageAdapter(): BucketStorageAdapter { return new SqliteBucketStorage(this.database, AbstractPowerSyncDatabase.transactionMutex); diff --git a/yarn.lock b/yarn.lock index 2cab165c..f8c1e147 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2066,13 +2066,6 @@ "@types/yargs" "^17.0.8" chalk "^4.0.0" -"@journeyapps/react-native-quick-sqlite@0.1.0": - version "0.1.0" - resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.0.tgz#51f38f04c477cd8f457465aec48d097d7df85011" - integrity sha512-uF1R2RGFXhuY1vvjABAR47kuUSATJmf4RWLmTBHtBa8pLkPh/DKqbvCGhO9lsCC8JDzUfY0+xhsCmnQ4t5trow== - dependencies: - lodash "^4.17.21" - "@journeyapps/react-native-quick-sqlite@0.1.1": version "0.1.1" resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.1.tgz#94145dba13b177f6aa42552754e56ecc3b2e7f17"