fix: commands & jupyter support

This commit is contained in:
iCrawl 2021-06-23 13:05:42 +02:00
parent db4f370863
commit 3f991c262a
No known key found for this signature in database
GPG key ID: 1AB888B16355FBB2
4 changed files with 24 additions and 28 deletions

1
.npmrc
View file

@ -1,4 +1,3 @@
audit=false audit=false
fund=false fund=false
node-version=false
legacy-peer-deps=true legacy-peer-deps=true

View file

@ -130,8 +130,6 @@ export async function activity(previous: ActivityPayload = {}) {
log(LogLevel.Trace, `VSCode language id: ${window.activeTextEditor.document.languageId}`); log(LogLevel.Trace, `VSCode language id: ${window.activeTextEditor.document.languageId}`);
} }
log(LogLevel.Debug, `Discord Presence being sent to discord:\n${JSON.stringify(state, null, 2)}`);
return state; return state;
} }

View file

@ -26,6 +26,7 @@
{ "language": "jsx", "image": "jsx" }, { "language": "jsx", "image": "jsx" },
{ "language": "json", "image": "json" }, { "language": "json", "image": "json" },
{ "language": "jsonc", "image": "json" }, { "language": "jsonc", "image": "json" },
{ "language": "jupyter", "image": "jupyter" },
{ "language": "latex", "image": "text" }, { "language": "latex", "image": "text" },
{ "language": "less", "image": "less" }, { "language": "less", "image": "less" },
{ "language": "lua", "image": "lua" }, { "language": "lua", "image": "lua" },

View file

@ -14,25 +14,15 @@ let rpc = new Client({ transport: 'ipc' });
const config = getConfig(); const config = getConfig();
let state = {}; let state = {};
let interval: NodeJS.Timeout | undefined;
let idle: NodeJS.Timeout | undefined; let idle: NodeJS.Timeout | undefined;
let listeners: { dispose(): any }[] = []; let listeners: { dispose(): any }[] = [];
export function cleanUp() { export function cleanUp() {
listeners.forEach((listener) => listener.dispose()); listeners.forEach((listener) => listener.dispose());
listeners = []; listeners = [];
if (interval) {
clearTimeout(interval);
}
} }
async function sendActivity() { async function sendActivity() {
if (interval) {
clearTimeout(interval);
}
interval = setTimeout(() => void sendActivity(), 5000);
state = { state = {
...(await activity(state)), ...(await activity(state)),
}; };
@ -40,9 +30,10 @@ async function sendActivity() {
} }
async function login() { async function login() {
log(LogLevel.Info, 'Creating discord-rpc client');
rpc = new Client({ transport: 'ipc' }); rpc = new Client({ transport: 'ipc' });
rpc.once('ready', () => { rpc.on('ready', () => {
log(LogLevel.Info, 'Successfully connected to Discord'); log(LogLevel.Info, 'Successfully connected to Discord');
cleanUp(); cleanUp();
@ -51,14 +42,14 @@ async function login() {
void sendActivity(); void sendActivity();
const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity()); const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity());
const onChangeTextDocument = workspace.onDidChangeTextDocument(throttle(() => sendActivity(), 1000)); const onChangeTextDocument = workspace.onDidChangeTextDocument(throttle(() => sendActivity(), 2000));
const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity()); const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity());
const onTerminateDebugSession = debug.onDidTerminateDebugSession(() => sendActivity()); const onTerminateDebugSession = debug.onDidTerminateDebugSession(() => sendActivity());
listeners.push(onChangeActiveTextEditor, onChangeTextDocument, onStartDebugSession, onTerminateDebugSession); listeners.push(onChangeActiveTextEditor, onChangeTextDocument, onStartDebugSession, onTerminateDebugSession);
}); });
rpc.once('disconnected', async () => { rpc.on('disconnected', async () => {
cleanUp(); cleanUp();
await rpc.destroy(); await rpc.destroy();
statusBarIcon.text = '$(pulse) Reconnect to Discord'; statusBarIcon.text = '$(pulse) Reconnect to Discord';
@ -72,6 +63,7 @@ async function login() {
cleanUp(); cleanUp();
await rpc.destroy(); await rpc.destroy();
if (!config[CONFIG_KEYS.SuppressNotifications]) { if (!config[CONFIG_KEYS.SuppressNotifications]) {
// @ts-ignore
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}`);
} }
@ -96,31 +88,40 @@ export async function activate(context: ExtensionContext) {
const enable = async (update = true) => { const enable = async (update = true) => {
if (update) { if (update) {
void config.update('enabled', true); try {
await config.update('enabled', true);
} catch {}
} }
log(LogLevel.Info, 'Enable: Cleaning up old listeners');
cleanUp(); cleanUp();
statusBarIcon.text = '$(pulse) Connecting to Discord...'; statusBarIcon.text = '$(pulse) Connecting to Discord...';
statusBarIcon.show(); statusBarIcon.show();
await login(); log(LogLevel.Info, 'Enable: Attempting to recreate login');
void login();
}; };
const disable = async (update = true) => { const disable = async (update = true) => {
if (update) { if (update) {
void config.update('enabled', false); try {
await config.update('enabled', false);
} catch {}
} }
log(LogLevel.Info, 'Disable: Cleaning up old listeners');
cleanUp(); cleanUp();
await rpc.destroy(); void rpc?.destroy();
log(LogLevel.Info, 'Disable: Destroyed the rpc instance');
statusBarIcon.hide(); statusBarIcon.hide();
}; };
const enabler = commands.registerCommand('discord.enable', async () => { const enabler = commands.registerCommand('discord.enable', async () => {
await disable();
await enable(); await enable();
void window.showInformationMessage('Enabled Discord Presence for this workspace'); await window.showInformationMessage('Enabled Discord Presence for this workspace');
}); });
const disabler = commands.registerCommand('discord.disable', async () => { const disabler = commands.registerCommand('discord.disable', async () => {
await disable(); await disable();
void window.showInformationMessage('Disabled Discord Presence for this workspace'); await window.showInformationMessage('Disabled Discord Presence for this workspace');
}); });
const reconnecter = commands.registerCommand('discord.reconnect', async () => { const reconnecter = commands.registerCommand('discord.reconnect', async () => {
@ -132,6 +133,7 @@ export async function activate(context: ExtensionContext) {
await disable(false); await disable(false);
statusBarIcon.text = '$(pulse) Reconnect to Discord'; statusBarIcon.text = '$(pulse) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect'; statusBarIcon.command = 'discord.reconnect';
statusBarIcon.show();
}); });
context.subscriptions.push(enabler, disabler, reconnecter, disconnect); context.subscriptions.push(enabler, disabler, reconnecter, disconnect);
@ -152,10 +154,6 @@ export async function activate(context: ExtensionContext) {
} else { } else {
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
idle = setTimeout(async () => { idle = setTimeout(async () => {
if (interval) {
clearTimeout(interval);
}
state = {}; state = {};
await rpc.clearActivity(); await rpc.clearActivity();
}, config[CONFIG_KEYS.IdleTimeout] * 1000); }, config[CONFIG_KEYS.IdleTimeout] * 1000);
@ -166,7 +164,7 @@ export async function activate(context: ExtensionContext) {
await getGit(); await getGit();
} }
export async function deactivate() { export function deactivate() {
cleanUp(); cleanUp();
await rpc.destroy(); void rpc.destroy();
} }