diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js index ed00a8326..7fc5b47d6 100644 --- a/packages/core/api/sync/index.js +++ b/packages/core/api/sync/index.js @@ -31,6 +31,7 @@ import { EV, EVENTS, sendAttachmentsProgressEvent, + sendSyncProgressEvent, } from "../../common"; import Constants from "../../utils/constants"; import http from "../../utils/http"; @@ -60,10 +61,11 @@ export default class Sync { accessTokenFactory: () => this._db.user.tokenManager.getAccessToken(), }) .build(); - this._connection.on("SyncItem", async (type, item) => { + this._connection.on("SyncItem", async (type, item, current, total) => { this.stopAutoSync(); await this._realtimeMerger.mergeItem(type, JSON.parse(item)); EV.publish(EVENTS.appRefreshRequested); + sendSyncProgressEvent("download", total, current); await this.startAutoSync(); }); @@ -221,7 +223,9 @@ export default class Sync { await this._connection.send("FetchItems", lastSynced); // TODO progress 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(); @@ -330,6 +334,7 @@ export default class Sync { } async sendItemsToServer(type, array, dateSynced) { + let index = 0; for (const item of array) { if (!item) return; const result = await this._connection.invoke( @@ -339,6 +344,7 @@ export default class Sync { dateSynced ); if (result !== 1) throw new Error(`Failed to sync item: ${item.id}.`); + sendSyncProgressEvent("upload", array.length, ++index); } } } diff --git a/packages/core/common.js b/packages/core/common.js index ad33183ea..ec34bd63e 100644 --- a/packages/core/common.js +++ b/packages/core/common.js @@ -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 CHECK_IDS = { @@ -41,6 +49,7 @@ export const EVENTS = { userSignedUp: "user:signedUp", userSessionExpired: "user:sessionExpired", databaseSyncRequested: "db:syncRequested", + syncProgress: "sync:progress", databaseMigrated: "db:migrated", databaseUpdated: "db:updated", databaseCollectionInitiated: "db:collectionInitiated",