Skip to content

Commit 14c2e90

Browse files
committed
Rember partial sync completions
1 parent 14c3404 commit 14c2e90

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

packages/common/src/client/AbstractPowerSyncDatabase.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
UpdateNotification,
1010
isBatchedUpdateNotification
1111
} from '../db/DBAdapter.js';
12-
import { SyncStatus } from '../db/crud/SyncStatus.js';
12+
import { SyncPriorityStatus, SyncStatus } from '../db/crud/SyncStatus.js';
1313
import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
1414
import { Schema } from '../db/schema/Schema.js';
1515
import { BaseObserver } from '../utils/BaseObserver.js';
@@ -343,14 +343,31 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
343343
}
344344

345345
protected async updateHasSynced() {
346-
const result = await this.database.get<{ synced_at: string | null }>(
347-
'SELECT powersync_last_synced_at() as synced_at'
346+
const result = await this.database.getAll<{ priority: number; last_synced_at: string }>(
347+
'SELECT priority, last_synced_at FROM ps_sync_state ORDER BY priority DESC'
348348
);
349-
const hasSynced = result.synced_at != null;
350-
const syncedAt = result.synced_at != null ? new Date(result.synced_at! + 'Z') : undefined;
349+
let lastCompleteSync: Date | undefined;
350+
const priorityStatus: SyncPriorityStatus[] = [];
351351

352-
if (hasSynced != this.currentStatus.hasSynced) {
353-
this.currentStatus = new SyncStatus({ ...this.currentStatus.toJSON(), hasSynced, lastSyncedAt: syncedAt });
352+
for (const { priority, last_synced_at } of result) {
353+
const parsedDate = new Date(last_synced_at + 'Z');
354+
355+
if (priority === 2147483647) {
356+
// This lowest-possible priority represents a complete sync.
357+
lastCompleteSync = parsedDate;
358+
} else {
359+
priorityStatus.push({ priority, hasSynced: true, lastSyncedAt: parsedDate });
360+
}
361+
}
362+
363+
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+
});
354371
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
355372
}
356373
}

0 commit comments

Comments
 (0)