feat: send progress events during sync

This commit is contained in:
thecodrr
2022-02-08 15:09:39 +05:00
parent bb675e9da2
commit fb0c30ecca
2 changed files with 17 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ import {
EV, EV,
EVENTS, EVENTS,
sendAttachmentsProgressEvent, sendAttachmentsProgressEvent,
sendSyncProgressEvent,
} from "../../common"; } from "../../common";
import Constants from "../../utils/constants"; import Constants from "../../utils/constants";
import http from "../../utils/http"; import http from "../../utils/http";
@@ -60,10 +61,11 @@ export default class Sync {
accessTokenFactory: () => this._db.user.tokenManager.getAccessToken(), accessTokenFactory: () => this._db.user.tokenManager.getAccessToken(),
}) })
.build(); .build();
this._connection.on("SyncItem", async (type, item) => { this._connection.on("SyncItem", async (type, item, current, total) => {
this.stopAutoSync(); this.stopAutoSync();
await this._realtimeMerger.mergeItem(type, JSON.parse(item)); await this._realtimeMerger.mergeItem(type, JSON.parse(item));
EV.publish(EVENTS.appRefreshRequested); EV.publish(EVENTS.appRefreshRequested);
sendSyncProgressEvent("download", total, current);
await this.startAutoSync(); await this.startAutoSync();
}); });
@@ -221,7 +223,9 @@ export default class Sync {
await this._connection.send("FetchItems", lastSynced); await this._connection.send("FetchItems", lastSynced);
// TODO progress // TODO progress
var serverResponse = await new Promise((resolve) => { var serverResponse = await new Promise((resolve) => {
this._connection.on("SyncCompleted", (result) => resolve(result)); this._connection.on("SyncCompleted", (synced, lastSynced) =>
resolve({ synced, lastSynced })
);
}); });
await this._db.conflicts.check(); await this._db.conflicts.check();
@@ -330,6 +334,7 @@ export default class Sync {
} }
async sendItemsToServer(type, array, dateSynced) { async sendItemsToServer(type, array, dateSynced) {
let index = 0;
for (const item of array) { for (const item of array) {
if (!item) return; if (!item) return;
const result = await this._connection.invoke( const result = await this._connection.invoke(
@@ -339,6 +344,7 @@ export default class Sync {
dateSynced dateSynced
); );
if (result !== 1) throw new Error(`Failed to sync item: ${item.id}.`); if (result !== 1) throw new Error(`Failed to sync item: ${item.id}.`);
sendSyncProgressEvent("upload", array.length, ++index);
} }
} }
} }

View File

@@ -19,6 +19,14 @@ export function sendAttachmentsProgressEvent(type, groupId, total, current) {
}); });
} }
export function sendSyncProgressEvent(type, total, current) {
EV.publish(EVENTS.syncProgress, {
type,
total,
current: current === undefined ? total : current,
});
}
export const CLIENT_ID = "notesnook"; export const CLIENT_ID = "notesnook";
export const CHECK_IDS = { export const CHECK_IDS = {
@@ -41,6 +49,7 @@ export const EVENTS = {
userSignedUp: "user:signedUp", userSignedUp: "user:signedUp",
userSessionExpired: "user:sessionExpired", userSessionExpired: "user:sessionExpired",
databaseSyncRequested: "db:syncRequested", databaseSyncRequested: "db:syncRequested",
syncProgress: "sync:progress",
databaseMigrated: "db:migrated", databaseMigrated: "db:migrated",
databaseUpdated: "db:updated", databaseUpdated: "db:updated",
databaseCollectionInitiated: "db:collectionInitiated", databaseCollectionInitiated: "db:collectionInitiated",