Skip to content

Commit 95544eb

Browse files
committed
Review feedback
1 parent 23b27ca commit 95544eb

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ export const isPowerSyncDatabaseOptionsWithSettings = (test: any): test is Power
146146
return typeof test == 'object' && isSQLOpenOptions(test.database);
147147
};
148148

149+
/**
150+
* The priority used by the core extension to indicate that a full sync was completed.
151+
*/
152+
const fullSyncPriority = 2147483647;
153+
149154
export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDBListener> {
150155
/**
151156
* Transactions should be queued in the DBAdapter, but we also want to prevent
@@ -347,27 +352,30 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
347352
'SELECT priority, last_synced_at FROM ps_sync_state ORDER BY priority DESC'
348353
);
349354
let lastCompleteSync: Date | undefined;
350-
const priorityStatus: SyncPriorityStatus[] = [];
355+
const prioritStatuses: SyncPriorityStatus[] = [];
351356

352357
for (const { priority, last_synced_at } of result) {
353358
const parsedDate = new Date(last_synced_at + 'Z');
354359

355-
if (priority === 2147483647) {
360+
if (priority == fullSyncPriority) {
356361
// This lowest-possible priority represents a complete sync.
357362
lastCompleteSync = parsedDate;
358363
} else {
359-
priorityStatus.push({ priority, hasSynced: true, lastSyncedAt: parsedDate });
364+
prioritStatuses.push({ priority, hasSynced: true, lastSyncedAt: parsedDate });
360365
}
361366
}
367+
362368

363369
const hasSynced = lastCompleteSync != null;
364-
if (hasSynced != this.currentStatus.hasSynced || priorityStatus != this.currentStatus.statusInPriority) {
365-
this.currentStatus = new SyncStatus({
366-
...this.currentStatus.toJSON(),
367-
hasSynced,
368-
lastSyncedAt: lastCompleteSync,
369-
statusInPriority: priorityStatus
370-
});
370+
const updatedStatus = new SyncStatus({
371+
...this.currentStatus.toJSON(),
372+
hasSynced,
373+
prioritStatuses,
374+
lastSyncedAt: lastCompleteSync,
375+
});
376+
377+
if (!updatedStatus.isEqual(this.currentStatus)) {
378+
this.currentStatus = updatedStatus;
371379
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
372380
}
373381
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ The next upload iteration will be delayed.`);
468468

469469
private async collectLocalBucketState(): Promise<[BucketRequest[], Map<string, BucketDescription | null>]> {
470470
const bucketEntries = await this.options.adapter.getBucketStates();
471-
const req: BucketRequest[] = Array.from(bucketEntries).map((entry) => ({
471+
const req: BucketRequest[] = bucketEntries.map((entry) => ({
472472
name: entry.bucket,
473473
after: entry.op_id
474474
}));
@@ -601,7 +601,7 @@ The next upload iteration will be delayed.`);
601601
this.logger.debug('partial checkpoint validation succeeded');
602602

603603
// All states with a higher priority can be deleted since this partial sync includes them.
604-
const priorityStates = this.syncStatus.statusInPriority.filter((s) => s.priority <= priority);
604+
const priorityStates = this.syncStatus.prioritStatuses.filter((s) => s.priority <= priority);
605605
priorityStates.push({
606606
priority,
607607
lastSyncedAt: new Date(),
@@ -610,7 +610,7 @@ The next upload iteration will be delayed.`);
610610

611611
this.updateSyncStatus({
612612
connected: true,
613-
statusInPriority: priorityStates
613+
prioritStatuses: priorityStates
614614
});
615615
}
616616
} else if (isStreamingSyncCheckpointDiff(line)) {
@@ -679,7 +679,7 @@ The next upload iteration will be delayed.`);
679679
this.updateSyncStatus({
680680
connected: true,
681681
lastSyncedAt: new Date(),
682-
statusInPriority: []
682+
prioritStatuses: []
683683
});
684684
} else if (validatedCheckpoint === targetCheckpoint) {
685685
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint!);
@@ -696,7 +696,7 @@ The next upload iteration will be delayed.`);
696696
this.updateSyncStatus({
697697
connected: true,
698698
lastSyncedAt: new Date(),
699-
statusInPriority: [],
699+
prioritStatuses: [],
700700
dataFlow: {
701701
downloading: false
702702
}
@@ -721,7 +721,7 @@ The next upload iteration will be delayed.`);
721721
...this.syncStatus.dataFlowStatus,
722722
...options.dataFlow
723723
},
724-
statusInPriority: options.statusInPriority ?? this.syncStatus.statusInPriority
724+
prioritStatuses: options.prioritStatuses ?? this.syncStatus.prioritStatuses
725725
});
726726

727727
if (!this.syncStatus.isEqual(updatedStatus)) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type SyncStatusOptions = {
1515
dataFlow?: SyncDataFlowStatus;
1616
lastSyncedAt?: Date;
1717
hasSynced?: boolean;
18-
statusInPriority?: SyncPriorityStatus[];
18+
prioritStatuses?: SyncPriorityStatus[];
1919
};
2020

2121
export class SyncStatus {
@@ -70,8 +70,8 @@ export class SyncStatus {
7070
/**
7171
* Partial sync status for involved bucket priorities.
7272
*/
73-
get statusInPriority() {
74-
return (this.options.statusInPriority ?? []).toSorted(SyncStatus.comparePriorities);
73+
get prioritStatuses() {
74+
return (this.options.prioritStatuses ?? []).toSorted(SyncStatus.comparePriorities);
7575
}
7676

7777
/**
@@ -90,8 +90,8 @@ export class SyncStatus {
9090
* @param priority The bucket priority for which the status should be reported.
9191
*/
9292
statusForPriority(priority: number): SyncPriorityStatus {
93-
// statusInPriority is sorted by ascending priorities (so higher numbers to lower numbers).
94-
for (const known of this.statusInPriority) {
93+
// prioritStatuses are sorted by ascending priorities (so higher numbers to lower numbers).
94+
for (const known of this.prioritStatuses) {
9595
// We look for the first entry that doesn't have a higher priority.
9696
if (known.priority >= priority) {
9797
return known;
@@ -122,7 +122,7 @@ export class SyncStatus {
122122
dataFlow: this.dataFlowStatus,
123123
lastSyncedAt: this.lastSyncedAt,
124124
hasSynced: this.hasSynced,
125-
statusInPriority: this.statusInPriority
125+
prioritStatuses: this.prioritStatuses
126126
};
127127
}
128128

packages/web/tests/stream.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ describe(
247247
});
248248
await another.init();
249249

250-
expect(another.currentStatus.statusInPriority).toHaveLength(1);
250+
expect(another.currentStatus.prioritStatuses).toHaveLength(1);
251251
expect(another.currentStatus.statusForPriority(0).hasSynced).toBeTruthy();
252252
await another.waitForFirstSync({priority: 0});
253253
});

0 commit comments

Comments
 (0)