Skip to content

Commit cb9b629

Browse files
committed
fix: maybe improve now playing info with poor network
1 parent b83532b commit cb9b629

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/hooks/useSpotifyPlayerState.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ let pendingSpotifyMediaUpdate = null;
1919
let spotifyFallbackTimeout = null;
2020
let cachedActiveDeviceType = null;
2121
let progressResetSignal = null;
22+
let nowPlayingTrackLatch = null;
23+
const NOWPLAYING_PRECEDENCE_WINDOW_MS = 300000;
2224

2325
export const getActiveDeviceType = () => cachedActiveDeviceType;
2426

@@ -632,6 +634,31 @@ export function useSpotifyPlayerState(immediateLoad = false) {
632634
playback_speed: playerState.options?.playback_speed || 1,
633635
};
634636

637+
if (nowPlayingTrackLatch) {
638+
const latchAge =
639+
Date.now() - nowPlayingTrackLatch.timestamp;
640+
if (latchAge < NOWPLAYING_PRECEDENCE_WINDOW_MS) {
641+
const incomingDeviceTitle = playerState.track?.metadata?.title
642+
?.toLowerCase()
643+
?.trim();
644+
if (
645+
incomingDeviceTitle &&
646+
incomingDeviceTitle !== nowPlayingTrackLatch.title
647+
) {
648+
nowPlayingTrackLatch.timestamp = Date.now();
649+
return;
650+
}
651+
if (
652+
incomingDeviceTitle &&
653+
incomingDeviceTitle === nowPlayingTrackLatch.title
654+
) {
655+
nowPlayingTrackLatch = null;
656+
}
657+
} else {
658+
nowPlayingTrackLatch = null;
659+
}
660+
}
661+
635662
processPlaybackState(transformedState);
636663
}
637664
}
@@ -778,6 +805,10 @@ export function useSpotifyPlayerState(immediateLoad = false) {
778805

779806
if (isTitleChange) {
780807
progressResetSignal = { position: 0, timestamp: Date.now() };
808+
nowPlayingTrackLatch = {
809+
title: incomingTitle.toLowerCase().trim(),
810+
timestamp: Date.now(),
811+
};
781812
}
782813

783814
setCurrentPlayback((prevPlayback) => {
@@ -1118,6 +1149,31 @@ export function useSpotifyPlayerState(immediateLoad = false) {
11181149
getPlayerState()
11191150
.then((playerData) => {
11201151
if (playerData && Object.keys(playerData).length > 0) {
1152+
if (nowPlayingTrackLatch) {
1153+
const latchAge =
1154+
Date.now() - nowPlayingTrackLatch.timestamp;
1155+
if (latchAge < NOWPLAYING_PRECEDENCE_WINDOW_MS) {
1156+
const fetchedTitle = playerData.item?.name
1157+
?.toLowerCase()
1158+
?.trim();
1159+
if (
1160+
fetchedTitle &&
1161+
fetchedTitle !== nowPlayingTrackLatch.title
1162+
) {
1163+
nowPlayingTrackLatch.timestamp = Date.now();
1164+
return;
1165+
}
1166+
if (
1167+
fetchedTitle &&
1168+
fetchedTitle === nowPlayingTrackLatch.title
1169+
) {
1170+
nowPlayingTrackLatch = null;
1171+
}
1172+
} else {
1173+
nowPlayingTrackLatch = null;
1174+
}
1175+
}
1176+
11211177
if (nowPlayingUpdateTimeout) {
11221178
clearTimeout(nowPlayingUpdateTimeout);
11231179
nowPlayingUpdateTimeout = null;
@@ -1168,6 +1224,30 @@ export function useSpotifyPlayerState(immediateLoad = false) {
11681224
if (!data || Object.keys(data).length === 0) {
11691225
resetPlaybackState();
11701226
} else {
1227+
if (nowPlayingTrackLatch) {
1228+
const latchAge =
1229+
Date.now() - nowPlayingTrackLatch.timestamp;
1230+
if (latchAge < NOWPLAYING_PRECEDENCE_WINDOW_MS) {
1231+
const fetchedTitle = data.item?.name
1232+
?.toLowerCase()
1233+
?.trim();
1234+
if (
1235+
fetchedTitle &&
1236+
fetchedTitle !== nowPlayingTrackLatch.title
1237+
) {
1238+
nowPlayingTrackLatch.timestamp = Date.now();
1239+
return;
1240+
}
1241+
if (
1242+
fetchedTitle &&
1243+
fetchedTitle === nowPlayingTrackLatch.title
1244+
) {
1245+
nowPlayingTrackLatch = null;
1246+
}
1247+
} else {
1248+
nowPlayingTrackLatch = null;
1249+
}
1250+
}
11711251
processPlaybackState(data);
11721252
}
11731253
} catch (err) {

0 commit comments

Comments
 (0)