@@ -2,12 +2,30 @@ import type { DB, QueryResult } from '@op-engineering/op-sqlite';
22import type { Logger } from '../common/logger' ;
33
44/**
5- * Extracts the number of changes from a CloudSync query result
5+ * Extracts the number of received rows from a CloudSync query result.
6+ *
7+ * The result row contains a JSON string:
8+ * {"status":"synced|syncing|out-of-sync","localVersion":N,"serverVersion":N,"rowsReceived":N}
9+ *
10+ * We only use rowsReceived since polling is for downloading remote changes.
11+ * The status field relates to upload state and is not relevant here.
612 */
713const extractChangesFromResult = ( result : QueryResult | undefined ) : number => {
814 const firstRow = result ?. rows ?. [ 0 ] ;
9- const value = firstRow ? Object . values ( firstRow ) [ 0 ] : 0 ;
10- return typeof value === 'number' ? value : 0 ;
15+ if ( ! firstRow ) return 0 ;
16+
17+ const raw = Object . values ( firstRow ) [ 0 ] ;
18+
19+ if ( typeof raw === 'string' ) {
20+ try {
21+ const parsed = JSON . parse ( raw ) ;
22+ return typeof parsed . rowsReceived === 'number' ? parsed . rowsReceived : 0 ;
23+ } catch {
24+ return 0 ;
25+ }
26+ }
27+
28+ return 0 ;
1129} ;
1230
1331/**
@@ -95,14 +113,14 @@ export async function executeSync(
95113
96114 // Wait before next attempt (except on last attempt)
97115 if ( attempt < maxAttempts - 1 ) {
98- await new Promise ( ( resolve ) => setTimeout ( resolve , attemptDelay ) ) ;
116+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , attemptDelay ) ) ;
99117 }
100118 }
101119 }
102120
103121 /** LOG RESULT */
104122 if ( changes > 0 ) {
105- logger . info ( `✅ Sync completed: ${ changes } changes synced ` ) ;
123+ logger . info ( `✅ Sync completed: ${ changes } changes downloaded ` ) ;
106124 } else {
107125 logger . info ( '✅ Sync completed: no changes' ) ;
108126 }
0 commit comments