Skip to content

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

core: fail fast if sync connection fails

Abdullah Attaca0d7469660f
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 496f03256..cd223e70e 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -405,13 +405,30 @@ class Sync {
async checkConnection() {
try {
if (this.connection.state !== signalr.HubConnectionState.Connected) {
- if (this.connection.state !== signalr.HubConnectionState.Disconnected)
+ if (this.connection.state !== signalr.HubConnectionState.Disconnected) {
await this.connection.stop();
+ }
- await this.connection.start();
+ await promiseTimeout(15000, this.connection.start());
}
} catch (e) {
+ this.connection.stop();
this.logger.warn(e.message);
+ throw new Error(
+ "Could not connect to the Sync server. Please try again."
+ );
}
}
}
+
+function promiseTimeout(ms, promise) {
+ // Create a promise that rejects in <ms> milliseconds
+ let timeout = new Promise((resolve, reject) => {
+ let id = setTimeout(() => {
+ clearTimeout(id);
+ reject(new Error("Sync timed out in " + ms + "ms."));
+ }, ms);
+ });
+ // Returns a race between our timeout and the passed in promise
+ return Promise.race([promise, timeout]);
+}