feat: implement an idle timeout

Co-authored-by: MRmlik12 <mrmlik12sup@gmail.com>
This commit is contained in:
iCrawl 2021-04-10 15:17:11 +02:00
parent fbd799f371
commit a99faaa729
No known key found for this signature in database
GPG key ID: 1AB888B16355FBB2
5 changed files with 45 additions and 6 deletions

View file

@ -149,6 +149,11 @@
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Removes the View Repository button" "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"
} }
} }
} }

View file

@ -49,7 +49,7 @@ export async function activity(previous: ActivityPayload = {}) {
const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling]; const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling];
const removeDetails = config[CONFIG_KEYS.RemoveDetails]; const removeDetails = config[CONFIG_KEYS.RemoveDetails];
const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails]; const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails];
const removeRemotRepository = config[CONFIG_KEYS.removeRemotRepository]; const removeRemoteRepository = config[CONFIG_KEYS.RemoveRemoteRepository];
const git = await getGit(); 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; let repo = git.repositories.find((repo) => repo.ui.selected)?.state.remotes[0].fetchUrl;
if (repo) { if (repo) {
@ -84,6 +84,8 @@ export async function activity(previous: ActivityPayload = {}) {
...state, ...state,
buttons: [{ label: 'View Repository', url: repo }], buttons: [{ label: 'View Repository', url: repo }],
}; };
console.log(state);
} }
} }

View file

@ -56,5 +56,6 @@ export const enum CONFIG_KEYS {
RemoveDetails = 'removeDetails', RemoveDetails = 'removeDetails',
RemoveLowerDetails = 'removeLowerDetails', RemoveLowerDetails = 'removeLowerDetails',
RemoveTimestamp = 'removeTimestamp', RemoveTimestamp = 'removeTimestamp',
removeRemotRepository = 'removeRemoteRepository', RemoveRemoteRepository = 'removeRemoteRepository',
IdleTimeout = 'idleTimeout',
} }

View file

@ -24,16 +24,25 @@ let rpc = new Client({ transport: 'ipc' });
const config = getConfig(); const config = getConfig();
let state = {}; let state = {};
let interval: NodeJS.Timeout; let interval: 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 = [];
clearInterval(interval);
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)),
}; };
@ -51,7 +60,6 @@ async function login() {
statusBarIcon.tooltip = 'Connected to Discord'; statusBarIcon.tooltip = 'Connected to Discord';
void sendActivity(); 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());
@ -143,6 +151,28 @@ export async function activate(context: ExtensionContext) {
await login(); 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 { try {
const gitExtension = extensions.getExtension<GitExtension>('vscode.git'); const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
if (!gitExtension?.isActive) { if (!gitExtension?.isActive) {

View file

@ -24,6 +24,7 @@ type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
removeLowerDetails: boolean; removeLowerDetails: boolean;
removeTimestamp: boolean; removeTimestamp: boolean;
removeRemoteRepository: boolean; removeRemoteRepository: boolean;
idleTimeout: number;
}; };
export function getConfig() { export function getConfig() {