Merge pull request #6013 from thornbill/fix-autocast-maybe

Fix autocast when already connected
This commit is contained in:
Bill Thornton 2024-09-03 14:53:14 -04:00 committed by GitHub
commit a1721ddd17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@ import { playbackManager } from '../components/playback/playbackmanager';
import Events from '../utils/events.ts';
export function enable(enabled) {
console.debug('[autocast] %s cast player', enabled ? 'enabling' : 'disabling');
if (enabled) {
const currentPlayerInfo = playbackManager.getPlayerInfo();
@ -22,20 +23,35 @@ export function isEnabled() {
function onOpen() {
const playerId = localStorage.getItem('autocastPlayerId');
if (!playerId) {
console.debug('[autocast] no active cast player');
return;
}
console.debug('[autocast] initializing cast player', playerId);
playbackManager.getTargets().then(function (targets) {
for (const target of targets) {
if (target.id == playerId) {
playbackManager.trySetActivePlayer(target.playerName, target);
break;
}
console.debug('[autocast] playback targets', targets);
const player = targets.find(target => target.id === playerId);
if (player) {
console.debug('[autocast] found target player', player);
playbackManager.trySetActivePlayer(player.playerName, player);
} else {
console.debug('[autocast] selected cast player not found');
}
});
}
export function initialize(apiClient) {
if (apiClient) {
Events.on(apiClient, 'websocketopen', onOpen);
if (apiClient.isWebSocketOpen()) {
console.debug('[autoCast] connection ready');
onOpen();
} else {
console.debug('[autoCast] initializing connection listener');
Events.on(apiClient, 'websocketopen', onOpen);
}
} else {
console.warn('[autoCast] cannot initialize missing apiClient');
}