From 2b1f3470f463419ea8972c9f49dac558371e8516 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Tue, 3 Sep 2024 12:30:48 -0400 Subject: [PATCH] Fix autocast when already connected --- src/scripts/autocast.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/scripts/autocast.js b/src/scripts/autocast.js index ead25e06ebb..16f58657239 100644 --- a/src/scripts/autocast.js +++ b/src/scripts/autocast.js @@ -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'); }