Skip to content

Commit f1ceacf

Browse files
committed
fix: parse JSON response from cloudsync_network_sync
1 parent 8ee38de commit f1ceacf

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

src/core/sync/executeSync.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ import type { DB, QueryResult } from '@op-engineering/op-sqlite';
22
import 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
*/
713
const 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
/**
@@ -68,7 +86,7 @@ export async function executeSync(
6886
]);
6987

7088
changes = extractChangesFromResult(result);
71-
logger.info(`🔄 Sync result: ${changes} changes`);
89+
logger.info(`🔄 Sync result: ${changes} changes downloaded`);
7290
} else {
7391
/** JS RETRY MODE */
7492
// Retry/delay in JS thread - better for foreground (doesn't block write connection)
@@ -87,24 +105,26 @@ export async function executeSync(
87105
}
88106

89107
changes = extractChangesFromResult(result);
90-
logger.info(`🔄 Sync attempt ${attempt + 1} result: ${changes} changes`);
108+
logger.info(
109+
`🔄 Sync attempt ${attempt + 1} result: ${changes} changes downloaded`
110+
);
91111

92112
if (changes > 0) {
93113
break;
94114
}
95115

96116
// Wait before next attempt (except on last attempt)
97117
if (attempt < maxAttempts - 1) {
98-
await new Promise((resolve) => setTimeout(resolve, attemptDelay));
118+
await new Promise<void>((resolve) => setTimeout(resolve, attemptDelay));
99119
}
100120
}
101121
}
102122

103123
/** LOG RESULT */
104124
if (changes > 0) {
105-
logger.info(`✅ Sync completed: ${changes} changes synced`);
125+
logger.info(`✅ Sync completed: ${changes} changes downloaded`);
106126
} else {
107-
logger.info('✅ Sync completed: no changes');
127+
logger.info('✅ Sync completed: no changes downloaded');
108128
}
109129

110130
return changes;

0 commit comments

Comments
 (0)