mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00

Fix autoCast race condition
Original-merge: 7f575d724e
Merged-by: thornbill <thornbill@users.noreply.github.com>
Backported-by: thornbill <thornbill@users.noreply.github.com>
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
import { playbackManager } from '../components/playback/playbackmanager';
|
|
import Events from '../utils/events.ts';
|
|
|
|
export function enable(enabled) {
|
|
if (enabled) {
|
|
const currentPlayerInfo = playbackManager.getPlayerInfo();
|
|
|
|
if (currentPlayerInfo?.id) {
|
|
localStorage.setItem('autocastPlayerId', currentPlayerInfo.id);
|
|
}
|
|
} else {
|
|
localStorage.removeItem('autocastPlayerId');
|
|
}
|
|
}
|
|
|
|
export function isEnabled() {
|
|
const playerId = localStorage.getItem('autocastPlayerId');
|
|
const currentPlayerInfo = playbackManager.getPlayerInfo();
|
|
|
|
return playerId && currentPlayerInfo?.id === playerId;
|
|
}
|
|
|
|
function onOpen() {
|
|
const playerId = localStorage.getItem('autocastPlayerId');
|
|
|
|
playbackManager.getTargets().then(function (targets) {
|
|
for (const target of targets) {
|
|
if (target.id == playerId) {
|
|
playbackManager.trySetActivePlayer(target.playerName, target);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
export function initialize(apiClient) {
|
|
if (apiClient) {
|
|
Events.on(apiClient, 'websocketopen', onOpen);
|
|
} else {
|
|
console.warn('[autoCast] cannot initialize missing apiClient');
|
|
}
|
|
}
|