Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: make sync hub connection more reliable

Abdullah Atta73b09847b8c2
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 2564b3ae7..37018ccd2 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -135,7 +135,6 @@ class Sync {
}
})
.withHubProtocol(new MessagePackHubProtocol({ ignoreUndefined: true }))
- .withAutomaticReconnect()
.build();
EV.subscribe(EVENTS.userLoggedOut, async () => {
@@ -143,6 +142,10 @@ class Sync {
this.autoSync.stop();
});
+ this.connection.onclose((error) => {
+ this.logger.error(error || new Error("Connection closed."));
+ });
+
this.connection.on("SyncItem", async (syncStatus) => {
await this.onSyncItem(syncStatus);
sendSyncProgressEvent(
@@ -167,11 +170,6 @@ class Sync {
async start(full, force, serverLastSynced) {
this.logger.info("Starting sync", { full, force, serverLastSynced });
- this.connection.onclose((error) => {
- this.logger.error(error || new Error("Connection closed."));
- throw new Error("Connection closed.");
- });
-
const { lastSynced, oldLastSynced } = await this.init(force);
this.logger.info("Initialized sync", { lastSynced, oldLastSynced });
@@ -405,12 +403,15 @@ class Sync {
}
async checkConnection() {
- try {
- if (this.connection.state !== signalr.HubConnectionState.Connected) {
- if (this.connection.state !== signalr.HubConnectionState.Disconnected) {
- await this.connection.stop();
- }
+ const CONNECTED_STATES = [
+ signalr.HubConnectionState.Connected,
+ signalr.HubConnectionState.Connecting,
+ signalr.HubConnectionState.Reconnecting,
+ signalr.HubConnectionState.Disconnecting
+ ];
+ try {
+ if (!CONNECTED_STATES.includes(this.connection.state)) {
await promiseTimeout(15000, this.connection.start());
}
} catch (e) {