From 7be2adf048de3cd4b1043ad74009c0f684544e0c Mon Sep 17 00:00:00 2001 From: Frangu Vlad Date: Tue, 20 Nov 2018 22:32:26 +0200 Subject: [PATCH] fix: errors on boot (#121) * fix: Reconnects not working if Discord wasn't open before VSCode * misc: Cleanup * fix: Hide and re-show the status bar item on enable/disable * nit: Crawls request * fix: Use if + .catch on the promise * nit: Crawl's request --- src/client/RPCClient.ts | 28 +++++++++++++++------------- src/extension.ts | 3 +++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/client/RPCClient.ts b/src/client/RPCClient.ts index 62ad7cd..94799ab 100644 --- a/src/client/RPCClient.ts +++ b/src/client/RPCClient.ts @@ -44,35 +44,37 @@ export default class RPCClient implements Disposable { this._rpc.once('ready', () => { Logger.log('Successfully connected to Discord.'); this.statusBarIcon.text = '$(globe) Connected to Discord'; - this.statusBarIcon.tooltip = 'Connected to Discord'; - setTimeout(() => this.statusBarIcon.text = '$(globe)', 5000); + setTimeout(() => { + this.statusBarIcon.text = '$(globe)'; + this.statusBarIcon.tooltip = 'Connected to Discord'; + }, 10000); if (activityTimer) clearInterval(activityTimer); this.setActivity(); - this._rpc.transport.once('close', async () => { - if (!this.config.get('enabled')) return; - await this.dispose(); - this.statusBarIcon.text = '$(plug) Reconnect to Discord'; - this.statusBarIcon.command = 'discord.reconnect'; - }); - activityTimer = setInterval(() => { this.config = workspace.getConfiguration('discord'); this.setActivity(this.config.get('workspaceElapsedTime')); }, 10000); }); + + this._rpc.transport.once('close', async () => { + if (!this.config.get('enabled')) return; + await this.dispose(); + this.statusBarIcon.text = '$(plug) Reconnect to Discord'; + this.statusBarIcon.command = 'discord.reconnect'; + }); await this._rpc.login({ clientId: this._clientId }); } public async dispose() { this._activity.dispose(); - if (this._rpc) { + try { await this._rpc.destroy(); - this._rpc = null; - } + } catch {} + this._rpc = null; + clearInterval(activityTimer); - this.statusBarIcon.hide(); } } diff --git a/src/extension.ts b/src/extension.ts index 5cd3e69..3642a2a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -25,6 +25,7 @@ export async function activate(context: ExtensionContext) { await rpc.login(); } catch (error) { Logger.log(`Encountered following error after trying to login:\n${error}`); + await rpc.dispose(); if (!config.get('silent')) { if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!'); else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.toString()}`); @@ -39,6 +40,7 @@ export async function activate(context: ExtensionContext) { config.update('enabled', true); rpc.config = workspace.getConfiguration('discord'); rpc.statusBarIcon.text = '$(pulse) Connecting to Discord...'; + rpc.statusBarIcon.show(); await rpc.login(); window.showInformationMessage('Enabled Discord Rich Presence for this workspace.'); }); @@ -47,6 +49,7 @@ export async function activate(context: ExtensionContext) { config.update('enabled', false); rpc.config = workspace.getConfiguration('discord'); await rpc.dispose(); + rpc.statusBarIcon.hide(); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); });