mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
fix: do not allow multiple concurrent syncs
This commit is contained in:
@@ -42,6 +42,7 @@ export default class Sync {
|
|||||||
this._collector = new Collector(this._db);
|
this._collector = new Collector(this._db);
|
||||||
this._merger = new Merger(this._db);
|
this._merger = new Merger(this._db);
|
||||||
this._tokenManager = new TokenManager(this._db);
|
this._tokenManager = new TokenManager(this._db);
|
||||||
|
this._isSyncing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _fetch(lastSynced, token) {
|
async _fetch(lastSynced, token) {
|
||||||
@@ -52,6 +53,7 @@ export default class Sync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _performChecks() {
|
async _performChecks() {
|
||||||
|
if (this._isSyncing) throw new Error("Sync already running.");
|
||||||
let lastSynced = (await this._db.context.read("lastSynced")) || 0;
|
let lastSynced = (await this._db.context.read("lastSynced")) || 0;
|
||||||
let token = await this._tokenManager.getAccessToken();
|
let token = await this._tokenManager.getAccessToken();
|
||||||
if (!token) throw new Error("You need to login to sync.");
|
if (!token) throw new Error("You need to login to sync.");
|
||||||
@@ -65,28 +67,35 @@ export default class Sync {
|
|||||||
|
|
||||||
async start(full, force) {
|
async start(full, force) {
|
||||||
if (force) await this._db.context.write("lastSynced", 0);
|
if (force) await this._db.context.write("lastSynced", 0);
|
||||||
|
|
||||||
let { lastSynced, token } = await this._performChecks();
|
let { lastSynced, token } = await this._performChecks();
|
||||||
|
|
||||||
if (full) var serverResponse = await this._fetch(lastSynced, token);
|
try {
|
||||||
|
this._isSyncing = true;
|
||||||
|
|
||||||
// we prepare local data before merging so we always have correct data
|
if (full) var serverResponse = await this._fetch(lastSynced, token);
|
||||||
const data = await this._collector.collect(lastSynced);
|
|
||||||
|
|
||||||
if (full) {
|
// we prepare local data before merging so we always have correct data
|
||||||
// merge the server response
|
const data = await this._collector.collect(lastSynced);
|
||||||
await this._merger.merge(serverResponse, lastSynced);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check for conflicts and throw
|
if (full) {
|
||||||
await this._db.conflicts.check();
|
// merge the server response
|
||||||
|
await this._merger.merge(serverResponse, lastSynced);
|
||||||
|
}
|
||||||
|
|
||||||
// send the data back to server
|
// check for conflicts and throw
|
||||||
lastSynced = await this._send(data, token);
|
await this._db.conflicts.check();
|
||||||
|
|
||||||
// update our lastSynced time
|
// send the data back to server
|
||||||
if (lastSynced) {
|
lastSynced = await this._send(data, token);
|
||||||
await this._db.context.write("lastSynced", lastSynced);
|
|
||||||
|
// update our lastSynced time
|
||||||
|
if (lastSynced) {
|
||||||
|
await this._db.context.write("lastSynced", lastSynced);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
this._isSyncing = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user