mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
fix(discord): Improve state verification before sending activity updates
- Enhanced the throttling mechanism to ensure that activity updates are only sent if the song state has not changed. - Added checks for video ID and pause state to prevent unnecessary updates. - Improved handling of elapsed time during updates.
This commit is contained in:
@ -201,7 +201,7 @@ export const backend = createBackend<
|
|||||||
const pauseChanged = songInfo.isPaused !== lastPausedState;
|
const pauseChanged = songInfo.isPaused !== lastPausedState;
|
||||||
const seeked = isSeek(lastElapsedSeconds, songInfo.elapsedSeconds ?? 0);
|
const seeked = isSeek(lastElapsedSeconds, songInfo.elapsedSeconds ?? 0);
|
||||||
if (songChanged || pauseChanged || seeked) {
|
if (songChanged || pauseChanged || seeked) {
|
||||||
// Immediate update
|
// Always cancel any pending throttle on important event
|
||||||
if (updateTimeout) clearTimeout(updateTimeout);
|
if (updateTimeout) clearTimeout(updateTimeout);
|
||||||
updateTimeout = null;
|
updateTimeout = null;
|
||||||
sendActivityToDiscord(songInfo, config);
|
sendActivityToDiscord(songInfo, config);
|
||||||
@ -218,10 +218,19 @@ export const backend = createBackend<
|
|||||||
lastElapsedSeconds = songInfo.elapsedSeconds ?? 0;
|
lastElapsedSeconds = songInfo.elapsedSeconds ?? 0;
|
||||||
} else {
|
} else {
|
||||||
if (updateTimeout) clearTimeout(updateTimeout);
|
if (updateTimeout) clearTimeout(updateTimeout);
|
||||||
|
// Capture current state for verification before sending
|
||||||
|
const expectedVideoId = songInfo.videoId;
|
||||||
|
const expectedPaused = songInfo.isPaused;
|
||||||
updateTimeout = setTimeout(() => {
|
updateTimeout = setTimeout(() => {
|
||||||
sendActivityToDiscord(songInfo, config);
|
// Only send if state hasn't changed
|
||||||
lastProgressUpdate = Date.now();
|
if (
|
||||||
lastElapsedSeconds = songInfo.elapsedSeconds ?? 0;
|
info.lastSongInfo?.videoId === expectedVideoId &&
|
||||||
|
info.lastSongInfo?.isPaused === expectedPaused
|
||||||
|
) {
|
||||||
|
sendActivityToDiscord(info.lastSongInfo, config);
|
||||||
|
lastProgressUpdate = Date.now();
|
||||||
|
lastElapsedSeconds = info.lastSongInfo.elapsedSeconds ?? 0;
|
||||||
|
}
|
||||||
}, PROGRESS_THROTTLE_MS - (now - lastProgressUpdate));
|
}, PROGRESS_THROTTLE_MS - (now - lastProgressUpdate));
|
||||||
}
|
}
|
||||||
if (songInfo.isPaused && config.activityTimeoutEnabled) {
|
if (songInfo.isPaused && config.activityTimeoutEnabled) {
|
||||||
|
|||||||
Reference in New Issue
Block a user