Skip to content

Commit

Permalink
DiscordRPC: Debounce updates
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ckoates committed Sep 1, 2024
1 parent 2559d8b commit 89c5c44
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion plugins/DiscordRPC/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,19 @@ export const onTimeUpdate = async (currentTime?: number) => {
return setRPC(activity);
};

// Debounce RPC updates, to prevent reaching the rate limit
let lastSetAt = 0;
let timeoutId: number | undefined;
function setRPC(activity?: SetActivity) {
return window.electron.ipcRenderer.invoke("DISCORD_SET_ACTIVITY", activity);
if (timeoutId) window.clearTimeout(timeoutId);
const timeout = Date.now() - lastSetAt < 3000 ? 3000 : 0;
timeoutId = window.setTimeout(() => {
window.electron.ipcRenderer
.invoke("DISCORD_SET_ACTIVITY", activity)
.catch(trace.msg.err.withContext("Failed to update"));
timeoutId = undefined;
}, timeout);
lastSetAt = Date.now();
}

const onUnloadTimeUpdate = intercept("playbackControls/TIME_UPDATE", ([newTime]) => {
Expand Down

0 comments on commit 89c5c44

Please sign in to comment.