refactor: rework activity logic
This commit is contained in:
parent
8fcb7e2691
commit
ab046317f5
3 changed files with 53 additions and 35 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "discord-vscode",
|
"name": "discord-vscode",
|
||||||
"displayName": "Discord Presence",
|
"displayName": "Discord Presence",
|
||||||
"version": "5.1.0",
|
"version": "5.1.1",
|
||||||
"description": "Update your discord status with a rich presence.",
|
"description": "Update your discord status with a rich presence.",
|
||||||
"private": true,
|
"private": true,
|
||||||
"author": {
|
"author": {
|
||||||
|
|
|
@ -31,6 +31,7 @@ interface ActivityPayload {
|
||||||
matchSecret?: string;
|
matchSecret?: string;
|
||||||
joinSecret?: string;
|
joinSecret?: string;
|
||||||
spectateSecret?: string;
|
spectateSecret?: string;
|
||||||
|
buttons?: { label: string; url: string }[];
|
||||||
instance?: boolean;
|
instance?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,10 +73,6 @@ export async function activity(previous: ActivityPayload = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (window.activeTextEditor) {
|
if (window.activeTextEditor) {
|
||||||
if (window.activeTextEditor.document.languageId === 'Log') {
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
const largeImageKey = resolveFileIcon(window.activeTextEditor.document);
|
const largeImageKey = resolveFileIcon(window.activeTextEditor.document);
|
||||||
const largeImageText = config[CONFIG_KEYS.LargeImage]
|
const largeImageText = config[CONFIG_KEYS.LargeImage]
|
||||||
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey))
|
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey))
|
||||||
|
|
|
@ -24,6 +24,14 @@ const rpc = new Client({ transport: 'ipc' });
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
||||||
let state = {};
|
let state = {};
|
||||||
|
let interval: NodeJS.Timeout;
|
||||||
|
let listeners: { dispose(): any }[] = [];
|
||||||
|
|
||||||
|
export function cleanUp() {
|
||||||
|
listeners.forEach((listener) => listener.dispose());
|
||||||
|
listeners = [];
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
|
||||||
async function sendActivity() {
|
async function sendActivity() {
|
||||||
state = {
|
state = {
|
||||||
|
@ -32,41 +40,39 @@ async function sendActivity() {
|
||||||
rpc.setActivity(state);
|
rpc.setActivity(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(context: ExtensionContext) {
|
async function login() {
|
||||||
rpc.once('ready', () => {
|
rpc.once('ready', () => {
|
||||||
log(LogLevel.Info, 'Successfully connected to Discord');
|
log(LogLevel.Info, 'Successfully connected to Discord');
|
||||||
|
|
||||||
statusBarIcon.text = '$(globe) Connected to Discord';
|
statusBarIcon.text = '$(globe) Connected to Discord';
|
||||||
statusBarIcon.tooltip = 'Connected to Discord';
|
statusBarIcon.tooltip = 'Connected to Discord';
|
||||||
|
|
||||||
|
void sendActivity();
|
||||||
|
interval = setInterval(() => void sendActivity(), 5000);
|
||||||
const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity());
|
const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity());
|
||||||
const onChangeTextDocument = workspace.onDidChangeTextDocument(throttle(() => sendActivity(), 1000));
|
const onChangeTextDocument = workspace.onDidChangeTextDocument(throttle(() => sendActivity(), 1000));
|
||||||
const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity());
|
const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity());
|
||||||
const onTerminateDebugSession = debug.onDidTerminateDebugSession(() => sendActivity());
|
const onTerminateDebugSession = debug.onDidTerminateDebugSession(() => sendActivity());
|
||||||
|
|
||||||
context.subscriptions.push(
|
listeners.push(onChangeActiveTextEditor, onChangeTextDocument, onStartDebugSession, onTerminateDebugSession);
|
||||||
onChangeActiveTextEditor,
|
|
||||||
onChangeTextDocument,
|
|
||||||
onStartDebugSession,
|
|
||||||
onTerminateDebugSession,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await rpc.login({ clientId: CLIENT_ID });
|
await rpc.login({ clientId: CLIENT_ID });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(LogLevel.Error, `Encountered following error while trying to login:\n${error as string}`);
|
log(LogLevel.Error, `Encountered following error while trying to login:\n${error as string}`);
|
||||||
rpc.dispose();
|
cleanUp();
|
||||||
|
await rpc.destroy();
|
||||||
if (!config[CONFIG_KEYS.SuppressNotifications]) {
|
if (!config[CONFIG_KEYS.SuppressNotifications]) {
|
||||||
if (error?.message?.includes('ENOENT')) void window.showErrorMessage('No Discord client detected');
|
if (error?.message?.includes('ENOENT')) void window.showErrorMessage('No Discord client detected');
|
||||||
else void window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error as string}`);
|
else void window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error as string}`);
|
||||||
}
|
}
|
||||||
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
||||||
rpc.statusBarIcon.command = 'discord.reconnect';
|
statusBarIcon.command = 'discord.reconnect';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function activate(context: ExtensionContext) {
|
export async function activate(context: ExtensionContext) {
|
||||||
log(LogLevel.Info, 'Discord Presence activated');
|
log(LogLevel.Info, 'Discord Presence activated');
|
||||||
|
|
||||||
let isWorkspaceExcluded = false;
|
let isWorkspaceExcluded = false;
|
||||||
|
@ -80,44 +86,59 @@ export function activate(context: ExtensionContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const enabler = commands.registerCommand('discord.enable', () => {
|
const enable = async (update = true) => {
|
||||||
rpc.destroy();
|
if (update) {
|
||||||
void config.update('enabled', true);
|
void config.update('enabled', true);
|
||||||
|
}
|
||||||
|
cleanUp();
|
||||||
|
await rpc.destroy();
|
||||||
statusBarIcon.text = '$(pulse) Connecting to Discord...';
|
statusBarIcon.text = '$(pulse) Connecting to Discord...';
|
||||||
statusBarIcon.show();
|
statusBarIcon.show();
|
||||||
void login(context);
|
await login();
|
||||||
|
};
|
||||||
|
|
||||||
|
const disable = async (update = true) => {
|
||||||
|
if (update) {
|
||||||
|
void config.update('enabled', false);
|
||||||
|
}
|
||||||
|
cleanUp();
|
||||||
|
await rpc.destroy();
|
||||||
|
statusBarIcon.hide();
|
||||||
|
};
|
||||||
|
|
||||||
|
const enabler = commands.registerCommand('discord.enable', async () => {
|
||||||
|
await enable();
|
||||||
void window.showInformationMessage('Enabled Discord Presence for this workspace');
|
void window.showInformationMessage('Enabled Discord Presence for this workspace');
|
||||||
});
|
});
|
||||||
|
|
||||||
const disabler = commands.registerCommand('discord.disable', () => {
|
const disabler = commands.registerCommand('discord.disable', async () => {
|
||||||
void config.update('enabled', false);
|
await disable();
|
||||||
rpc.destroy();
|
|
||||||
rpc.statusBarIcon.hide();
|
|
||||||
void window.showInformationMessage('Disabled Discord Presence for this workspace');
|
void window.showInformationMessage('Disabled Discord Presence for this workspace');
|
||||||
});
|
});
|
||||||
|
|
||||||
const reconnecter = commands.registerCommand('discord.reconnect', () => {
|
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
|
||||||
deactivate();
|
await disable(false);
|
||||||
void activate(context);
|
await enable(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
const disconnect = commands.registerCommand('discord.disconnect', () => {
|
const disconnect = commands.registerCommand('discord.disconnect', async () => {
|
||||||
rpc.destroy();
|
await disable(false);
|
||||||
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
||||||
rpc.statusBarIcon.command = 'discord.reconnect';
|
statusBarIcon.command = 'discord.reconnect';
|
||||||
});
|
});
|
||||||
|
|
||||||
context.subscriptions.push(enabler, disabler, reconnecter, disconnect);
|
context.subscriptions.push(enabler, disabler, reconnecter, disconnect);
|
||||||
|
|
||||||
if (!isWorkspaceExcluded && config[CONFIG_KEYS.Enabled]) {
|
if (!isWorkspaceExcluded && config[CONFIG_KEYS.Enabled]) {
|
||||||
statusBarIcon.show();
|
statusBarIcon.show();
|
||||||
void login(context);
|
await login();
|
||||||
}
|
}
|
||||||
|
|
||||||
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
|
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
|
||||||
void gitExtension?.activate();
|
await gitExtension?.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deactivate() {
|
export async function deactivate() {
|
||||||
rpc.destroy();
|
cleanUp();
|
||||||
|
await rpc.destroy();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue