Skip to content

Commit 36a9d21

Browse files
Merge pull request #26 from powersync-ja/test_init
[Fix] Init Regression
2 parents 52bec03 + ba982eb commit 36a9d21

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

.changeset/itchy-jobs-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@journeyapps/powersync-sdk-common': patch
3+
---
4+
5+
Fixed regression where `waitForReady` would not trigger or resolve if not invoked before `init`

apps/supabase-todolist

packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

packages/powersync-sdk-react-native/src/db/PowerSyncDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ReactNativeRemote } from '../sync/stream/ReactNativeRemote';
99
import { ReactNativeStreamingSyncImplementation } from '../sync/stream/ReactNativeStreamingSyncImplementation';
1010

1111
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
12-
async _init(): Promise<void> {}
12+
async _initialize(): Promise<void> {}
1313

1414
protected generateBucketStorageAdapter(): BucketStorageAdapter {
1515
return new SqliteBucketStorage(this.database, AbstractPowerSyncDatabase.transactionMutex);

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,13 +2066,6 @@
20662066
"@types/yargs" "^17.0.8"
20672067
chalk "^4.0.0"
20682068

2069-
"@journeyapps/react-native-quick-sqlite@0.1.0":
2070-
version "0.1.0"
2071-
resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.0.tgz#51f38f04c477cd8f457465aec48d097d7df85011"
2072-
integrity sha512-uF1R2RGFXhuY1vvjABAR47kuUSATJmf4RWLmTBHtBa8pLkPh/DKqbvCGhO9lsCC8JDzUfY0+xhsCmnQ4t5trow==
2073-
dependencies:
2074-
lodash "^4.17.21"
2075-
20762069
"@journeyapps/react-native-quick-sqlite@0.1.1":
20772070
version "0.1.1"
20782071
resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.1.tgz#94145dba13b177f6aa42552754e56ecc3b2e7f17"

0 commit comments

Comments
 (0)