From a99faaa729a9693a2906cdb71842a4e876041961 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Sat, 10 Apr 2021 15:17:11 +0200 Subject: [PATCH] feat: implement an idle timeout Co-authored-by: MRmlik12 --- package.json | 5 +++++ src/activity.ts | 6 ++++-- src/constants.ts | 3 ++- src/extension.ts | 36 +++++++++++++++++++++++++++++++++--- src/util.ts | 1 + 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4229da3..6e663c9 100644 --- a/package.json +++ b/package.json @@ -149,6 +149,11 @@ "type": "boolean", "default": false, "description": "Removes the View Repository button" + }, + "discord.idleTimeout": { + "type": "number", + "default": 0, + "description": "Time (in seconds) to clear the presence when idling. 0 (the default) means no clearing" } } } diff --git a/src/activity.ts b/src/activity.ts index 7bb8f6d..e37e285 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -49,7 +49,7 @@ export async function activity(previous: ActivityPayload = {}) { const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling]; const removeDetails = config[CONFIG_KEYS.RemoveDetails]; const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails]; - const removeRemotRepository = config[CONFIG_KEYS.removeRemotRepository]; + const removeRemoteRepository = config[CONFIG_KEYS.RemoveRemoteRepository]; const git = await getGit(); @@ -74,7 +74,7 @@ export async function activity(previous: ActivityPayload = {}) { }; } - if (!removeRemotRepository && git?.repositories.length) { + if (!removeRemoteRepository && git?.repositories.length) { let repo = git.repositories.find((repo) => repo.ui.selected)?.state.remotes[0].fetchUrl; if (repo) { @@ -84,6 +84,8 @@ export async function activity(previous: ActivityPayload = {}) { ...state, buttons: [{ label: 'View Repository', url: repo }], }; + + console.log(state); } } diff --git a/src/constants.ts b/src/constants.ts index 32effdd..a98a8f8 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -56,5 +56,6 @@ export const enum CONFIG_KEYS { RemoveDetails = 'removeDetails', RemoveLowerDetails = 'removeLowerDetails', RemoveTimestamp = 'removeTimestamp', - removeRemotRepository = 'removeRemoteRepository', + RemoveRemoteRepository = 'removeRemoteRepository', + IdleTimeout = 'idleTimeout', } diff --git a/src/extension.ts b/src/extension.ts index b0d5804..5629aaf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,16 +24,25 @@ let rpc = new Client({ transport: 'ipc' }); const config = getConfig(); let state = {}; -let interval: NodeJS.Timeout; +let interval: NodeJS.Timeout | undefined; +let idle: NodeJS.Timeout | undefined; let listeners: { dispose(): any }[] = []; export function cleanUp() { listeners.forEach((listener) => listener.dispose()); listeners = []; - clearInterval(interval); + + if (interval) { + clearTimeout(interval); + } } async function sendActivity() { + if (interval) { + clearTimeout(interval); + } + + interval = setTimeout(() => void sendActivity(), 5000); state = { ...(await activity(state)), }; @@ -51,7 +60,6 @@ async function login() { statusBarIcon.tooltip = 'Connected to Discord'; void sendActivity(); - interval = setInterval(() => void sendActivity(), 5000); const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity()); const onChangeTextDocument = workspace.onDidChangeTextDocument(throttle(() => sendActivity(), 1000)); const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity()); @@ -143,6 +151,28 @@ export async function activate(context: ExtensionContext) { await login(); } + window.onDidChangeWindowState(async (windowState) => { + if (config[CONFIG_KEYS.IdleTimeout] !== 0) { + if (windowState.focused) { + if (idle) { + clearTimeout(idle); + } + + await sendActivity(); + } else { + // eslint-disable-next-line @typescript-eslint/no-misused-promises + idle = setTimeout(async () => { + if (interval) { + clearTimeout(interval); + } + + state = {}; + await rpc.clearActivity(); + }, config[CONFIG_KEYS.IdleTimeout] * 1000); + } + } + }); + try { const gitExtension = extensions.getExtension('vscode.git'); if (!gitExtension?.isActive) { diff --git a/src/util.ts b/src/util.ts index f96ae00..32b9596 100644 --- a/src/util.ts +++ b/src/util.ts @@ -24,6 +24,7 @@ type WorkspaceExtensionConfiguration = WorkspaceConfiguration & { removeLowerDetails: boolean; removeTimestamp: boolean; removeRemoteRepository: boolean; + idleTimeout: number; }; export function getConfig() {