Skip to content

Commit 0606ac2

Browse files
authored
Implementation of 'connecting' sync status from Dart SDK (#434)
1 parent 829bf47 commit 0606ac2

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

.changeset/swift-seahorses-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': minor
3+
---
4+
5+
add 'connecting' flag to SyncStatus

packages/common/src/client/AbstractPowerSyncDatabase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
232232
return this.currentStatus?.connected || false;
233233
}
234234

235+
get connecting() {
236+
return this.currentStatus?.connecting || false;
237+
}
238+
235239
/**
236240
* Opens the DBAdapter given open options using a default open factory
237241
*/

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export abstract class AbstractStreamingSyncImplementation
156156

157157
this.syncStatus = new SyncStatus({
158158
connected: false,
159+
connecting: false,
159160
lastSyncedAt: undefined,
160161
dataFlow: {
161162
uploading: false,
@@ -347,7 +348,7 @@ The next upload iteration will be delayed.`);
347348
this.streamingSyncPromise = undefined;
348349

349350
this.abortController = null;
350-
this.updateSyncStatus({ connected: false });
351+
this.updateSyncStatus({ connected: false, connecting: false });
351352
}
352353

353354
/**
@@ -382,6 +383,7 @@ The next upload iteration will be delayed.`);
382383
this.crudUpdateListener = undefined;
383384
this.updateSyncStatus({
384385
connected: false,
386+
connecting: false,
385387
dataFlow: {
386388
downloading: false
387389
}
@@ -395,6 +397,7 @@ The next upload iteration will be delayed.`);
395397
* - Close any sync stream ReadableStreams (which will also close any established network requests)
396398
*/
397399
while (true) {
400+
this.updateSyncStatus({ connecting: true });
398401
try {
399402
if (signal?.aborted) {
400403
break;
@@ -423,6 +426,8 @@ The next upload iteration will be delayed.`);
423426
} else {
424427
this.logger.error(ex);
425428
}
429+
430+
// On error, wait a little before retrying
426431
await this.delayRetry();
427432
} finally {
428433
if (!signal.aborted) {
@@ -431,15 +436,14 @@ The next upload iteration will be delayed.`);
431436
}
432437

433438
this.updateSyncStatus({
434-
connected: false
439+
connected: false,
440+
connecting: true // May be unnecessary
435441
});
436-
437-
// On error, wait a little before retrying
438442
}
439443
}
440444

441445
// Mark as disconnected if here
442-
this.updateSyncStatus({ connected: false });
446+
this.updateSyncStatus({ connected: false, connecting: false });
443447
}
444448

445449
protected async streamingSyncIteration(
@@ -647,6 +651,7 @@ The next upload iteration will be delayed.`);
647651
protected updateSyncStatus(options: SyncStatusOptions) {
648652
const updatedStatus = new SyncStatus({
649653
connected: options.connected ?? this.syncStatus.connected,
654+
connecting: !options.connected && (options.connecting ?? this.syncStatus.connecting),
650655
lastSyncedAt: options.lastSyncedAt ?? this.syncStatus.lastSyncedAt,
651656
dataFlow: {
652657
...this.syncStatus.dataFlowStatus,

packages/common/src/db/crud/SyncStatus.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type SyncDataFlowStatus = Partial<{
55

66
export type SyncStatusOptions = {
77
connected?: boolean;
8+
connecting?: boolean;
89
dataFlow?: SyncDataFlowStatus;
910
lastSyncedAt?: Date;
1011
hasSynced?: boolean;
@@ -20,6 +21,10 @@ export class SyncStatus {
2021
return this.options.connected ?? false;
2122
}
2223

24+
get connecting() {
25+
return this.options.connecting ?? false;
26+
}
27+
2328
/**
2429
* Time that a last sync has fully completed, if any.
2530
* Currently this is reset to null after a restart.
@@ -61,12 +66,13 @@ export class SyncStatus {
6166

6267
getMessage() {
6368
const dataFlow = this.dataFlowStatus;
64-
return `SyncStatus<connected: ${this.connected} lastSyncedAt: ${this.lastSyncedAt} hasSynced: ${this.hasSynced}. Downloading: ${dataFlow.downloading}. Uploading: ${dataFlow.uploading}`;
69+
return `SyncStatus<connected: ${this.connected} connecting: ${this.connecting} lastSyncedAt: ${this.lastSyncedAt} hasSynced: ${this.hasSynced}. Downloading: ${dataFlow.downloading}. Uploading: ${dataFlow.uploading}`;
6570
}
6671

6772
toJSON(): SyncStatusOptions {
6873
return {
6974
connected: this.connected,
75+
connecting: this.connecting,
7076
dataFlow: this.dataFlowStatus,
7177
lastSyncedAt: this.lastSyncedAt,
7278
hasSynced: this.hasSynced

0 commit comments

Comments
 (0)