Skip to content

Commit

Permalink
Merge pull request #26 from powersync-ja/test_init
Browse files Browse the repository at this point in the history
[Fix] Init Regression
  • Loading branch information
stevensJourney authored Nov 6, 2023
2 parents 52bec03 + ba982eb commit 36a9d21
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-jobs-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@journeyapps/powersync-sdk-common': patch
---

Fixed regression where `waitForReady` would not trigger or resolve if not invoked before `init`
2 changes: 1 addition & 1 deletion apps/supabase-todolist
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
private abortController: AbortController | null;
protected bucketStorageAdapter: BucketStorageAdapter;
private syncStatusListenerDisposer?: () => void;
protected _isReadyPromise: Promise<void> | null;
protected _isReadyPromise: Promise<void>;

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() {
Expand Down Expand Up @@ -111,34 +112,37 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
return;
}

return (
this._isReadyPromise ||
(this._isReadyPromise = new Promise((resolve) => {
const l = this.registerListener({
initialized: () => {
this.ready = true;
resolve();
l?.();
}
});
}))
);
await this._isReadyPromise;
}

abstract _init(): Promise<void>;
/**
* Allows for extended implementations to execute custom initialization
* logic as part of the total init process
*/
abstract _initialize(): Promise<void>;

/**
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ReactNativeRemote } from '../sync/stream/ReactNativeRemote';
import { ReactNativeStreamingSyncImplementation } from '../sync/stream/ReactNativeStreamingSyncImplementation';

export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
async _init(): Promise<void> {}
async _initialize(): Promise<void> {}

protected generateBucketStorageAdapter(): BucketStorageAdapter {
return new SqliteBucketStorage(this.database, AbstractPowerSyncDatabase.transactionMutex);
Expand Down
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2066,13 +2066,6 @@
"@types/yargs" "^17.0.8"
chalk "^4.0.0"

"@journeyapps/[email protected]":
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/[email protected]":
version "0.1.1"
resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.1.tgz#94145dba13b177f6aa42552754e56ecc3b2e7f17"
Expand Down

0 comments on commit 36a9d21

Please sign in to comment.