Skip to content

Commit 7668495

Browse files
authored
[Fix] hasSynced not resetting after a disconnectAndClear() call (#261)
1 parent 58fd059 commit 7668495

File tree

3 files changed

+16822
-22195
lines changed

3 files changed

+16822
-22195
lines changed

.changeset/fluffy-bags-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Correctly resetting hasSynced value upon a disconnectAndClear call.

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
172172
private syncStatusListenerDisposer?: () => void;
173173
protected _isReadyPromise: Promise<void>;
174174

175-
private hasSyncedWatchDisposer?: () => void;
176-
177175
protected _schema: Schema;
178176

179177
private _database: DBAdapter;
@@ -290,45 +288,20 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
290288
const version = await this.database.execute('SELECT powersync_rs_version()');
291289
this.sdkVersion = version.rows?.item(0)['powersync_rs_version()'] ?? '';
292290
await this.updateSchema(this.options.schema);
293-
this.updateHasSynced();
291+
await this.updateHasSynced();
294292
await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
295293
this.ready = true;
296294
this.iterateListeners((cb) => cb.initialized?.());
297295
}
298296

299297
protected async updateHasSynced() {
300-
const syncedSQL = 'SELECT 1 FROM ps_buckets WHERE last_applied_op > 0 LIMIT 1';
301-
302-
const abortController = new AbortController();
303-
this.hasSyncedWatchDisposer = () => abortController.abort();
304-
305-
// Abort the watch after the first sync is detected
306-
this.watch(
307-
syncedSQL,
308-
[],
309-
{
310-
onResult: (result) => {
311-
const hasSynced = !!result.rows?.length;
312-
313-
if (hasSynced != this.currentStatus.hasSynced) {
314-
this.currentStatus = new SyncStatus({ ...this.currentStatus.toJSON(), hasSynced });
315-
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
316-
}
298+
const result = await this.database.getOptional('SELECT 1 FROM ps_buckets WHERE last_applied_op > 0 LIMIT 1');
299+
const hasSynced = !!result;
317300

318-
if (hasSynced) {
319-
abortController.abort();
320-
}
321-
},
322-
onError: (ex) => {
323-
this.options.logger?.warn('Failure while watching synced state', ex);
324-
abortController.abort();
325-
}
326-
},
327-
{
328-
rawTableNames: true,
329-
signal: abortController.signal
330-
}
331-
);
301+
if (hasSynced != this.currentStatus.hasSynced) {
302+
this.currentStatus = new SyncStatus({ ...this.currentStatus.toJSON(), hasSynced });
303+
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
304+
}
332305
}
333306

334307
/**
@@ -441,6 +414,10 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
441414
await tx.execute(`DELETE FROM ${quoteIdentifier(row.name)} WHERE 1`);
442415
}
443416
});
417+
418+
// The data has been deleted - reset the sync status
419+
this.currentStatus = new SyncStatus({});
420+
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
444421
}
445422

446423
/**
@@ -453,7 +430,6 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
453430
*/
454431
async close(options: PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS) {
455432
await this.waitForReady();
456-
this.hasSyncedWatchDisposer?.();
457433

458434
const { disconnect } = options;
459435
if (disconnect) {

0 commit comments

Comments
 (0)