refactor: commands merged into a single toggle

This commit is contained in:
iCrawl 2017-11-24 04:17:12 +01:00
parent 78172bf133
commit 510a4ab32c
No known key found for this signature in database
GPG key ID: E41A6DB922EC2CFE
2 changed files with 16 additions and 21 deletions

View file

@ -23,13 +23,8 @@
"contributes": {
"commands": [
{
"command": "discord.enable",
"title": "Enable Visual Studio Code Discord in the current workspace",
"category": "VS-Discord"
},
{
"command": "discord.disable",
"title": "Disable Visual Studio Code Discord in the current workspace",
"command": "discord.toggle",
"title": "Wether Visual Studio Code Discord in the current workspace is enabled or not",
"category": "VS-Discord"
}
],

View file

@ -32,23 +32,22 @@ export function activate(context: ExtensionContext) {
initRPC(config.get('clientID'));
}
// Register the `discord.enable` command, and set the `enabled` config option to true.
const enabler = commands.registerCommand('discord.enable', () => {
if (rpc) return;
config.update('enabled', true);
initRPC(config.get('clientID'));
});
// Register the `discord.disable` command, and set the `enabled` config option to false.
const disabler = commands.registerCommand('discord.disable', () => {
if (!rpc) return;
config.update('enabled', false);
rpc.setActivity({});
destroyRPC();
// Register the `discord.toggle` command.
const toggler = commands.registerCommand('discord.toggle', () => {
if (rpc) {
config.update('enabled', false);
rpc.setActivity({});
destroyRPC();
window.showInformationMessage('Disabled Rich Presence for Discord.');
} else {
config.update('enabled', true);
initRPC(config.get('clientID'));
window.showInformationMessage('Enabled Rich Presence for Discord.');
}
});
// Push the new commands into the subscriptions.
context.subscriptions.push(enabler, disabler);
context.subscriptions.push(toggler);
}
// `Deactivate` is fired whenever the extension is deactivated.
@ -75,6 +74,7 @@ function initRPC(clientID: string): void {
eventHandler = workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
// Make sure to listen to the close event and dispose and destroy everything accordingly.
rpc.transport.once('close', () => {
if (!config.get('enabled')) return;
destroyRPC();
// Set an interval for reconnecting.
reconnect = setInterval(() => {