mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
core: fail fast if sync connection fails
this adds reslience to the sync logic in cases where network isn't responding or is very slow to respond. Previously there were cases where the sync would get stuck in Connecting state. This will no longer happen.
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user