docs: comment out the code (#2)
Added a bunch of comments to create a better understanding of the code.
This commit is contained in:
parent
428d22d703
commit
448eee76e9
1 changed files with 28 additions and 0 deletions
|
@ -1,34 +1,53 @@
|
||||||
|
// Import the required functions & object types from various packages.
|
||||||
import { Client } from 'discord-rpc';
|
import { Client } from 'discord-rpc';
|
||||||
import { basename, extname } from 'path';
|
import { basename, extname } from 'path';
|
||||||
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
|
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
|
||||||
import { setInterval, clearInterval } from 'timers';
|
import { setInterval, clearInterval } from 'timers';
|
||||||
|
|
||||||
|
// Define the RPC variable and its type.
|
||||||
let rpc: Client;
|
let rpc: Client;
|
||||||
|
|
||||||
|
// `Activate` is fired when the extension is enabled. This SHOULD only fire once.
|
||||||
export function activate(context: ExtensionContext) {
|
export function activate(context: ExtensionContext) {
|
||||||
|
|
||||||
|
// Get the workspace's configuration for "discord".
|
||||||
const config = workspace.getConfiguration('discord');
|
const config = workspace.getConfiguration('discord');
|
||||||
|
|
||||||
|
// 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.
|
||||||
const enabler = commands.registerCommand('discord.enable', () => {
|
const enabler = commands.registerCommand('discord.enable', () => {
|
||||||
config.update('enabled', true);
|
config.update('enabled', true);
|
||||||
initRPC(config.get('clientID'));
|
initRPC(config.get('clientID'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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', () => {
|
||||||
config.update('enabled', false);
|
config.update('enabled', false);
|
||||||
rpc.setActivity({});
|
rpc.setActivity({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Push the new commands into the subscriptions.
|
||||||
context.subscriptions.push(enabler, disabler);
|
context.subscriptions.push(enabler, disabler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `Deactivate` is fired whenever the extension is deactivated.
|
||||||
export function deactivate(context: ExtensionContext) {
|
export function deactivate(context: ExtensionContext) {
|
||||||
|
|
||||||
|
// If there's an RPC Client initalized, destroy it.
|
||||||
if (rpc) rpc.destroy();
|
if (rpc) rpc.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function updates the activity (The Client's Rich Presence status).
|
||||||
function setActivity(): void {
|
function setActivity(): void {
|
||||||
|
|
||||||
|
// Do not continue if RPC isn't initalized.
|
||||||
if (!rpc) return;
|
if (!rpc) return;
|
||||||
|
|
||||||
|
// Create a JSON Object with the user's activity information.
|
||||||
const activity = {
|
const activity = {
|
||||||
details: window.activeTextEditor
|
details: window.activeTextEditor
|
||||||
? `Editing ${basename(window.activeTextEditor.document.fileName)}`
|
? `Editing ${basename(window.activeTextEditor.document.fileName)}`
|
||||||
|
@ -49,15 +68,24 @@ function setActivity(): void {
|
||||||
smallImageText: 'Visual Studio Code',
|
smallImageText: 'Visual Studio Code',
|
||||||
instance: false
|
instance: false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Update the user's activity to the `activity` variable.
|
||||||
rpc.setActivity(activity);
|
rpc.setActivity(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initalize the RPC systems.
|
||||||
function initRPC(clientID: string): void {
|
function initRPC(clientID: string): void {
|
||||||
|
|
||||||
|
// Update the RPC variable with a new RPC Client.
|
||||||
rpc = new Client({ transport: 'ipc' });
|
rpc = new Client({ transport: 'ipc' });
|
||||||
|
|
||||||
|
// Once the RPC Client is ready, set the activity.
|
||||||
rpc.once('ready', () => {
|
rpc.once('ready', () => {
|
||||||
setActivity();
|
setActivity();
|
||||||
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
|
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Log in to the RPC Client, and check whether or not it errors.
|
||||||
rpc.login(clientID).catch(error =>
|
rpc.login(clientID).catch(error =>
|
||||||
error.message.includes('ENOENT')
|
error.message.includes('ENOENT')
|
||||||
? window.showErrorMessage('No Discord Client detected!')
|
? window.showErrorMessage('No Discord Client detected!')
|
||||||
|
|
Loading…
Reference in a new issue