fix: move code around to clear the interval properly

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

View file

@ -43,7 +43,6 @@ export function activate(context: ExtensionContext) {
const disabler = commands.registerCommand('discord.disable', () => { const disabler = commands.registerCommand('discord.disable', () => {
if (!rpc) return; if (!rpc) return;
config.update('enabled', false); config.update('enabled', false);
eventHandler.dispose();
rpc.setActivity({}); rpc.setActivity({});
destroyRPC(); destroyRPC();
}); });
@ -76,7 +75,6 @@ function initRPC(clientID: string): void {
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.
rpc.transport.once('close', () => { rpc.transport.once('close', () => {
eventHandler.dispose();
destroyRPC(); destroyRPC();
// Set an interval for reconnecting. // Set an interval for reconnecting.
reconnect = setInterval(() => { reconnect = setInterval(() => {
@ -89,15 +87,9 @@ function initRPC(clientID: string): void {
// 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) { if (reconnect) {
if (reconnectCounter >= 20) { // Destroy and dispose of everything after 20 reconnect attempts
clearInterval(reconnect); if (reconnectCounter >= 20) destroyRPC();
reconnect = null; else return;
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}`);
@ -108,6 +100,12 @@ function initRPC(clientID: string): void {
function destroyRPC(): void { function destroyRPC(): void {
// Do not continue if RPC isn't initalized. // Do not continue if RPC isn't initalized.
if (!rpc) return; if (!rpc) return;
// Clear the reconnect interval.
if (reconnect) clearInterval(reconnect);
// Null reconnect variable.
reconnect = null;
// Dispose of the event handler.
eventHandler.dispose();
// If there's an RPC Client initalized, destroy it. // If there's an RPC Client initalized, destroy it.
rpc.destroy(); rpc.destroy();
// Null the RPC variable. // Null the RPC variable.