Skip to content

Commit

Permalink
core: improve sync status updates
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Sep 4, 2023
1 parent aa9cd23 commit 0409a61
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/core/src/api/sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,20 @@ class Sync {

let count = 0;
this.connection.on("PushItems", async (chunk) => {
const key = await this.db.user.getEncryptionKey();
const dbLastSynced = await this.db.lastSynced();
await this.processChunk(chunk, key, dbLastSynced, true);
if (this.connection.state !== signalr.HubConnectionState.Connected)
return;

count += chunk.items.length;
sendSyncProgressEvent(this.db.eventManager, "download", count);

clearTimeout(remoteSyncTimeout);
remoteSyncTimeout = setTimeout(() => {
this.db.eventManager.publish(EVENTS.syncAborted);
}, 15000);

const key = await this.db.user.getEncryptionKey();
const dbLastSynced = await this.db.lastSynced();
await this.processChunk(chunk, key, dbLastSynced, true);
});

this.connection.on("PushCompleted", (lastSynced) => {
Expand All @@ -183,6 +191,7 @@ class Sync {
this.logger.info("Starting sync", { full, force, serverLastSynced });

this.connection.onclose((error) => {
this.db.eventManager.publish(EVENTS.syncAborted);
console.error(error);
this.logger.error(error || new Error("Connection closed."));
throw new Error("Connection closed.");
Expand Down Expand Up @@ -240,10 +249,14 @@ class Sync {
let count = 0;
this.connection.off("SendItems");
this.connection.on("SendItems", async (chunk) => {
await this.processChunk(chunk, key, dbLastSynced);
if (this.connection.state !== signalr.HubConnectionState.Connected)
return;

count += chunk.items.length;
sendSyncProgressEvent(this.db.eventManager, `download`, count);

await this.processChunk(chunk, key, dbLastSynced);

return true;
});
const serverResponse = await this.connection.invoke(
Expand Down

0 comments on commit 0409a61

Please sign in to comment.