diff --git a/src/client/RPCClient.ts b/src/client/RPCClient.ts index 1c157e2..d32fc82 100644 --- a/src/client/RPCClient.ts +++ b/src/client/RPCClient.ts @@ -7,7 +7,7 @@ import { window } from 'vscode'; import * as vsls from 'vsls'; -import Activity from '../structures/Activity'; +import Activity, { State } from '../structures/Activity'; import Logger from '../structures/Logger'; const clipboardy = require('clipboardy'); // eslint-disable-line @@ -66,27 +66,27 @@ export default class RPCClient implements Disposable { await this._activity.disableJoinRequests(); } - public async login(): Promise { + public async login(reconnect: boolean = false): Promise { if (this._rpc) return; this._rpc = new Client({ transport: 'ipc' }); Logger.log('Logging into RPC.'); - this._rpc.once('ready', async (): Promise => { + this._rpc.once('ready', async () => { Logger.log('Successfully connected to Discord.'); this.statusBarIcon.text = '$(globe) Connected to Discord'; this.statusBarIcon.tooltip = 'Connected to Discord'; // @ts-ignore - setTimeout((): void => this.statusBarIcon.text = '$(globe)', 5000); + setTimeout(() => this.statusBarIcon.text = '$(globe)', 5000); if (activityTimer) clearInterval(activityTimer); this.setActivity(); - activityTimer = setInterval((): void => { + activityTimer = setInterval(() => { this.config = workspace.getConfiguration('discord'); this.setActivity(this.config.get('workspaceElapsedTime')); }, 10000); - this._rpc.subscribe('ACTIVITY_SPECTATE', async ({ secret }: { secret: string }): Promise => { + this._rpc.subscribe('ACTIVITY_SPECTATE', async ({ secret }: { secret: string }) => { const liveshare = await vsls.getApi(); if (!liveshare) return; try { @@ -106,17 +106,17 @@ export default class RPCClient implements Disposable { // You might be asking yourself again: "but why?" // Same here, this is a real nasty race condition that happens inside the discord-rpc module currently // To circumvent this we need to timeout sending the subscribe events to the discord client - setTimeout((): void => { - this._rpc.subscribe('ACTIVITY_JOIN_REQUEST', async ({ user }: { user: { username: string; discriminator: string } }): Promise => + setTimeout(() => { + this._rpc.subscribe('ACTIVITY_JOIN_REQUEST', async ({ user }: { user: { username: string; discriminator: string } }) => window.showInformationMessage(`${user.username}#${user.discriminator} wants to join your session`, { title: 'Accept' }, { title: 'Decline' }) // eslint-disable-next-line .then(async (val: { title: string } | undefined) => { if (val && val.title === 'Accept') await this._rpc.sendJoinInvite(user); // eslint-disable-line else await this._rpc.closeJoinRequest(user); })); - }, 500); - setTimeout((): void => { - this._rpc.subscribe('ACTIVITY_JOIN', async ({ secret }: { secret: string }): Promise => { + }, 1000); + setTimeout(() => { + this._rpc.subscribe('ACTIVITY_JOIN', async ({ secret }: { secret: string }) => { const liveshare = await vsls.getApi(); if (!liveshare) return; try { @@ -130,26 +130,24 @@ export default class RPCClient implements Disposable { Logger.log(error); } }); - }, 1000); + }, 2000); const liveshare = await vsls.getApi(); if (!liveshare) return; - liveshare.onDidChangeSession(({ session }: { session: vsls.Session }): void => { - // @ts-ignore + liveshare.onDidChangeSession(({ session }: { session: vsls.Session }): State | void => { if (session.id) return this._activity.changePartyId(session.id); - // @ts-ignore return this._activity.changePartyId(); }); - liveshare.onDidChangePeers(({ added, removed }: { added: vsls.Peer[]; removed: vsls.Peer[] }): void => { - // @ts-ignore + liveshare.onDidChangePeers(({ added, removed }: { added: vsls.Peer[]; removed: vsls.Peer[] }): State | void => { if (added.length) return this._activity.increasePartySize(added.length); - // @ts-ignore else if (removed.length) return this._activity.decreasePartySize(removed.length); }); }); - this._rpc.transport.once('close', async (): Promise => { + this._rpc.transport.once('close', async () => { if (!this.config.get('enabled')) return; + if (reconnect) return; + console.log('called close', reconnect); await this.dispose(); this.statusBarIcon.text = '$(plug) Reconnect to Discord'; this.statusBarIcon.command = 'discord.reconnect'; @@ -162,7 +160,7 @@ export default class RPCClient implements Disposable { public async dispose(): Promise { this._activity.dispose(); try { - await this._rpc.destroy(); + if (this._rpc) await this._rpc.destroy(); } catch {} this._rpc = null; this.statusBarIcon.tooltip = ''; diff --git a/src/extension.ts b/src/extension.ts index 285e4b1..8164b4e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,6 +10,8 @@ import RPCClient from './client/RPCClient'; import Logger from './structures/Logger'; const { register } = require('discord-rpc'); // eslint-disable-line +let loginTimeout: NodeJS.Timer; + const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); statusBarIcon.text = '$(pulse) Connecting to Discord...'; @@ -50,7 +52,7 @@ export async function activate(context: ExtensionContext): Promise { } } - const enabler = commands.registerCommand('discord.enable', async (): Promise => { + const enabler = commands.registerCommand('discord.enable', async () => { await rpc.dispose(); config.update('enabled', true); rpc.config = workspace.getConfiguration('discord'); @@ -60,7 +62,7 @@ export async function activate(context: ExtensionContext): Promise { window.showInformationMessage('Enabled Discord Rich Presence for this workspace.'); }); - const disabler = commands.registerCommand('discord.disable', async (): Promise => { + const disabler = commands.registerCommand('discord.disable', async () => { config.update('enabled', false); rpc.config = workspace.getConfiguration('discord'); await rpc.dispose(); @@ -68,31 +70,40 @@ export async function activate(context: ExtensionContext): Promise { window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); }); - const reconnecter = commands.registerCommand('discord.reconnect', async (): Promise => { + const reconnecter = commands.registerCommand('discord.reconnect', async () => { + if (loginTimeout) clearTimeout(loginTimeout); await rpc.dispose(); - await rpc.login(); - if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...'); - rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...'; - rpc.statusBarIcon.command = undefined; + loginTimeout = setTimeout(async () => { + await rpc.login(); + if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...'); + rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...'; + rpc.statusBarIcon.command = 'discord.reconnect'; + }, 1000); }); - const allowSpectate = commands.registerCommand('discord.allowSpectate', async (): Promise => { + const disconnect = commands.registerCommand('discord.disconnect', async () => { + await rpc.dispose(); + rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord'; + rpc.statusBarIcon.command = 'discord.reconnect'; + }); + + const allowSpectate = commands.registerCommand('discord.allowSpectate', async () => { await rpc.allowSpectate(); }); - const disableSpectate = commands.registerCommand('discord.disableSpectate', async (): Promise => { + const disableSpectate = commands.registerCommand('discord.disableSpectate', async () => { await rpc.disableSpectate(); }); - const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', async (): Promise => { + const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', async () => { await rpc.allowJoinRequests(); }); - const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', async (): Promise => { + const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', async () => { await rpc.disableJoinRequests(); }); - context.subscriptions.push(enabler, disabler, reconnecter, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests); + context.subscriptions.push(enabler, disabler, reconnecter, disconnect, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests); } export async function deactivate(): Promise { diff --git a/src/structures/Activity.ts b/src/structures/Activity.ts index c52553c..fc8456c 100644 --- a/src/structures/Activity.ts +++ b/src/structures/Activity.ts @@ -15,7 +15,7 @@ const knownLanguages: string[] = lang.knownLanguages; const empty = '\u200b\u200b'; const sizes = [' bytes', 'kb', 'mb', 'gb', 'tb']; -interface State { +export interface State { details?: string; state?: string; startTimestamp?: number | null; diff --git a/webpack.config.js b/webpack.config.js index c67480f..c30f57b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -26,9 +26,12 @@ module.exports = { cache: false, parallel: true, sourceMap: true, + extractComments: true, terserOptions: { ecma: 8, - keep_classnames: true + mangle: false, + keep_classnames: true, + keep_fnames: true } }) ]