feat: implement direct syncing using sse (experimental)

This commit is contained in:
thecodrr
2020-08-24 13:07:16 +05:00
parent d7e3e1d1e7
commit 10689d9ef9
2 changed files with 24 additions and 2 deletions

View File

@@ -52,7 +52,7 @@ export default class Sync {
return await response.json();
}
async start() {
async _performChecks() {
let user = await this._db.user.get();
let token = await this._db.user.token();
if (!user || !token) throw new Error("You need to login to sync.");
@@ -62,6 +62,13 @@ export default class Sync {
await this._db.conflicts.check();
let lastSynced = user.notesnook.lastSynced || 0;
return { user, lastSynced, token };
}
async start() {
let { user, lastSynced, token } = await this._performChecks();
let serverResponse = await this._fetch(lastSynced, token);
// we prepare local data before merging so we always have correct data
@@ -82,6 +89,20 @@ export default class Sync {
}
}
async eventMerge(serverResponse) {
let { user, lastSynced } = await this._performChecks();
// merge the server response
await this._merger.merge(serverResponse, lastSynced);
// check for conflicts and throw
await this._db.conflicts.check();
// update our lastSynced time
if (lastSynced) {
await this._db.user.set({ notesnook: { ...user.notesnook, lastSynced } });
}
}
async _send(data, token) {
let response = await fetch(`${HOST}/sync`, {
method: "POST",