|
9 | 9 | UpdateNotification,
|
10 | 10 | isBatchedUpdateNotification
|
11 | 11 | } from '../db/DBAdapter.js';
|
12 |
| -import { SyncStatus } from '../db/crud/SyncStatus.js'; |
| 12 | +import { SyncPriorityStatus, SyncStatus } from '../db/crud/SyncStatus.js'; |
13 | 13 | import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
|
14 | 14 | import { Schema } from '../db/schema/Schema.js';
|
15 | 15 | import { BaseObserver } from '../utils/BaseObserver.js';
|
@@ -343,14 +343,31 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
|
343 | 343 | }
|
344 | 344 |
|
345 | 345 | 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' |
348 | 348 | );
|
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[] = []; |
351 | 351 |
|
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 | + }); |
354 | 371 | this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
|
355 | 372 | }
|
356 | 373 | }
|
|
0 commit comments