fix: generate id per sync instead of using connection id

This commit is contained in:
thecodrr
2022-03-30 16:52:09 +05:00
parent 854b45f8fc
commit 4db686d7f8

View File

@@ -15,6 +15,7 @@ import Conflicts from "./conflicts";
import { SyncQueue } from "./syncqueue"; import { SyncQueue } from "./syncqueue";
import { AutoSync } from "./auto-sync"; import { AutoSync } from "./auto-sync";
import { toChunks } from "../../utils/array"; import { toChunks } from "../../utils/array";
import id from "../../utils/id";
const ITEM_TYPE_MAP = { const ITEM_TYPE_MAP = {
attachments: "attachment", attachments: "attachment",
@@ -26,7 +27,7 @@ const ITEM_TYPE_MAP = {
/** /**
* @typedef {{ * @typedef {{
* id: string, * syncId: string,
* item: string, * item: string,
* itemType: string, * itemType: string,
* lastSynced: number, * lastSynced: number,
@@ -43,6 +44,7 @@ const ITEM_TYPE_MAP = {
* lastSynced: number, * lastSynced: number,
* current: number, * current: number,
* total: number, * total: number,
* syncId: string
* }} BatchedSyncTransferItem * }} BatchedSyncTransferItem
*/ */
@@ -90,6 +92,7 @@ class Sync {
this.queue = new SyncQueue(db.storage); this.queue = new SyncQueue(db.storage);
this.merger = new Merger(db); this.merger = new Merger(db);
this.autoSync = new AutoSync(db, 1000); this.autoSync = new AutoSync(db, 1000);
this.syncId = null;
const tokenManager = new TokenManager(db.storage); const tokenManager = new TokenManager(db.storage);
this.connection = new signalr.HubConnectionBuilder() this.connection = new signalr.HubConnectionBuilder()
@@ -125,6 +128,8 @@ class Sync {
throw new Error("Connection closed."); throw new Error("Connection closed.");
}); });
this.syncId = this.getSyncId();
const { lastSynced, oldLastSynced } = await this.init(force); const { lastSynced, oldLastSynced } = await this.init(force);
const serverResponse = full ? await this.fetch(lastSynced) : null; const serverResponse = full ? await this.fetch(lastSynced) : null;
@@ -259,6 +264,7 @@ class Sync {
total, total,
items, items,
types, types,
syncId: this.syncId,
}); });
if (result) { if (result) {
@@ -267,11 +273,16 @@ class Sync {
this.db.eventManager, this.db.eventManager,
"upload", "upload",
total, total,
index + ids.length index + ids.length,
items
); );
} }
} }
return await this.connection.invoke("SyncCompleted", lastSynced); return await this.connection.invoke(
"SyncCompleted",
lastSynced,
this.syncId
);
} }
async stop(lastSynced) { async stop(lastSynced) {
@@ -324,10 +335,12 @@ class Sync {
const { current, id: syncId, item: itemJSON, itemType, total } = syncStatus; const { current, id: syncId, item: itemJSON, itemType, total } = syncStatus;
const item = JSON.parse(itemJSON); const item = JSON.parse(itemJSON);
if (syncId) {
this.runningSyncs[syncId] = { this.runningSyncs[syncId] = {
...this.runningSyncs[syncId], ...this.runningSyncs[syncId],
[item.id]: true, [item.id]: true,
}; };
}
await this.merger.mergeItem(itemType, item); await this.merger.mergeItem(itemType, item);
sendSyncProgressEvent(this.db.eventManager, "download", total, current); sendSyncProgressEvent(this.db.eventManager, "download", total, current);
@@ -344,4 +357,8 @@ class Sync {
const result = await this.connection.invoke("SyncItem", batch); const result = await this.connection.invoke("SyncItem", batch);
return result === 1; return result === 1;
} }
getSyncId() {
return id();
}
} }