mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +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 seeked = isSeek(lastElapsedSeconds, songInfo.elapsedSeconds ?? 0);
|
||||
if (songChanged || pauseChanged || seeked) {
|
||||
// Immediate update
|
||||
// Always cancel any pending throttle on important event
|
||||
if (updateTimeout) clearTimeout(updateTimeout);
|
||||
updateTimeout = null;
|
||||
sendActivityToDiscord(songInfo, config);
|
||||
@ -218,10 +218,19 @@ export const backend = createBackend<
|
||||
lastElapsedSeconds = songInfo.elapsedSeconds ?? 0;
|
||||
} else {
|
||||
if (updateTimeout) clearTimeout(updateTimeout);
|
||||
// Capture current state for verification before sending
|
||||
const expectedVideoId = songInfo.videoId;
|
||||
const expectedPaused = songInfo.isPaused;
|
||||
updateTimeout = setTimeout(() => {
|
||||
sendActivityToDiscord(songInfo, config);
|
||||
lastProgressUpdate = Date.now();
|
||||
lastElapsedSeconds = songInfo.elapsedSeconds ?? 0;
|
||||
// Only send if state hasn't changed
|
||||
if (
|
||||
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));
|
||||
}
|
||||
if (songInfo.isPaused && config.activityTimeoutEnabled) {
|
||||
|
||||
Reference in New Issue
Block a user