fix: proper reconnect logic with a counter

This commit is contained in:
iCrawl 2017-11-24 03:45:23 +01:00
parent fd5a243683
commit fad1abd81c
No known key found for this signature in database
GPG key ID: E41A6DB922EC2CFE

View file

@ -19,6 +19,8 @@ let eventHandler: Disposable;
let config; let config;
// Define the reconnect timer and its type. // Define the reconnect timer and its type.
let reconnect: NodeJS.Timer; let reconnect: NodeJS.Timer;
// Define the reconnect counter and its type.
let reconnectCounter = 0;
// `Activate` is fired when the extension is enabled. This SHOULD only fire once. // `Activate` is fired when the extension is enabled. This SHOULD only fire once.
export function activate(context: ExtensionContext) { export function activate(context: ExtensionContext) {
@ -69,6 +71,7 @@ function initRPC(clientID: string): void {
// Null reconnect variable. // Null reconnect variable.
reconnect = null; reconnect = null;
} }
reconnectCounter = 0;
setActivity(); setActivity();
eventHandler = workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity()); eventHandler = workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
// Make sure to listen to the close event and dispose and destroy everything accordingly. // Make sure to listen to the close event and dispose and destroy everything accordingly.
@ -76,13 +79,26 @@ function initRPC(clientID: string): void {
eventHandler.dispose(); eventHandler.dispose();
destroyRPC(); destroyRPC();
// Set an interval for reconnecting. // Set an interval for reconnecting.
reconnect = setInterval(() => initRPC(config.get('clientID')), 5000); reconnect = setInterval(() => {
reconnectCounter++;
initRPC(config.get('clientID'));
}, 5000);
}); });
}); });
// Log in to the RPC Client, and check whether or not it errors. // Log in to the RPC Client, and check whether or not it errors.
rpc.login(clientID).catch(error => { rpc.login(clientID).catch(error => {
if (reconnect && !error.message.includes('ENOENT')) return; if (reconnect) {
if (reconnectCounter >= 20) {
clearInterval(reconnect);
reconnect = null;
eventHandler.dispose();
destroyRPC();
rpc = null;
} else {
return;
}
}
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!'); if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
else window.showErrorMessage(`Couldn't connect to discord via rpc: ${error.message}`); else window.showErrorMessage(`Couldn't connect to discord via rpc: ${error.message}`);
}); });