style: use async await whenever applicable

This commit is contained in:
iCrawl 2017-12-20 06:25:37 +01:00
parent e38d62da47
commit 277f9dec40
No known key found for this signature in database
GPG key ID: E41A6DB922EC2CFE

View file

@ -37,23 +37,21 @@ export function activate(context: ExtensionContext) {
config = workspace.getConfiguration('discord'); config = workspace.getConfiguration('discord');
// Obtain whether or not the extension is activated. // Obtain whether or not the extension is activated.
if (config.get('enabled')) { if (config.get('enabled')) initRPC(config.get('clientID'));
initRPC(config.get('clientID'));
}
// Register the `discord.enable` command, and set the `enabled` config option to true. // Register the `discord.enable` command, and set the `enabled` config option to true.
const enabler = commands.registerCommand('discord.enable', () => { const enabler = commands.registerCommand('discord.enable', async () => {
if (rpc) destroyRPC(); if (rpc) await destroyRPC();
config.update('enabled', true); config.update('enabled', true);
initRPC(config.get('clientID')); initRPC(config.get('clientID'));
window.showInformationMessage('Enabled Discord Rich Presence for this workspace.'); window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
}); });
// Register the `discord.disable` command, and set the `enabled` config option to false. // Register the `discord.disable` command, and set the `enabled` config option to false.
const disabler = commands.registerCommand('discord.disable', () => { const disabler = commands.registerCommand('discord.disable', async () => {
if (!rpc) return; if (!rpc) return;
config.update('enabled', false); config.update('enabled', false);
destroyRPC(); await destroyRPC();
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
}); });
@ -62,9 +60,9 @@ export function activate(context: ExtensionContext) {
} }
// `Deactivate` is fired whenever the extension is deactivated. // `Deactivate` is fired whenever the extension is deactivated.
export function deactivate(context: ExtensionContext) { export async function deactivate() {
// If there's an RPC Client initalized, destroy it. // If there's an RPC Client initalized, destroy it.
destroyRPC(); await destroyRPC();
} }
// Initalize the RPC systems. // Initalize the RPC systems.
@ -101,9 +99,9 @@ function initRPC(clientID: string): void {
.add(debug.onDidStartDebugSession(() => setActivity())) .add(debug.onDidStartDebugSession(() => setActivity()))
.add(debug.onDidTerminateDebugSession(() => setActivity())); .add(debug.onDidTerminateDebugSession(() => 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', async () => {
if (!config.get('enabled')) return; if (!config.get('enabled')) return;
destroyRPC(); await destroyRPC();
// Set an interval for reconnecting. // Set an interval for reconnecting.
reconnectTimer = setInterval(() => { reconnectTimer = setInterval(() => {
reconnectCounter++; reconnectCounter++;
@ -116,10 +114,10 @@ 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(async error => {
if (reconnectTimer) { if (reconnectTimer) {
// Destroy and dispose of everything after a default of 20 reconnect attempts // Destroy and dispose of everything after a default of 20 reconnect attempts
if (reconnectCounter >= config.get('reconnectThreshold')) destroyRPC(); if (reconnectCounter >= config.get('reconnectThreshold')) await destroyRPC();
else return; else return;
} }
if (!config.get('silent')) { if (!config.get('silent')) {
@ -130,7 +128,7 @@ function initRPC(clientID: string): void {
} }
// Cleanly destroy the RPC client (if it isn't already). // Cleanly destroy the RPC client (if it isn't already).
function destroyRPC(): void { async function destroyRPC(): Promise<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. // Clear the reconnect interval.
@ -144,7 +142,7 @@ function destroyRPC(): void {
// Dispose of the event handlers. // Dispose of the event handlers.
eventHandlers.forEach(event => event.dispose()); eventHandlers.forEach(event => event.dispose());
// If there's an RPC Client initalized, destroy it. // If there's an RPC Client initalized, destroy it.
rpc.destroy(); await rpc.destroy();
// Null the RPC variable. // Null the RPC variable.
rpc = null; rpc = null;
// Null the last known file name // Null the last known file name