2020-10-18 18:57:20 +01:00
|
|
|
import { playbackManager } from '../components/playback/playbackmanager';
|
|
|
|
import ServerConnections from '../components/ServerConnections';
|
2022-10-14 10:53:16 -04:00
|
|
|
import Events from '../utils/events.ts';
|
2020-09-07 12:54:12 -04:00
|
|
|
|
|
|
|
export function supported() {
|
|
|
|
return typeof(Storage) !== 'undefined';
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:10:38 -04:00
|
|
|
export function enable(enabled) {
|
2020-09-07 12:54:12 -04:00
|
|
|
if (!supported()) return;
|
|
|
|
|
2020-09-07 13:10:38 -04:00
|
|
|
if (enabled) {
|
2020-09-07 12:54:12 -04:00
|
|
|
const currentPlayerInfo = playbackManager.getPlayerInfo();
|
|
|
|
|
2023-07-06 13:39:48 -04:00
|
|
|
if (currentPlayerInfo?.id) {
|
2020-09-07 12:54:12 -04:00
|
|
|
localStorage.setItem('autocastPlayerId', currentPlayerInfo.id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem('autocastPlayerId');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isEnabled() {
|
|
|
|
if (!supported()) return false;
|
|
|
|
|
|
|
|
const playerId = localStorage.getItem('autocastPlayerId');
|
|
|
|
const currentPlayerInfo = playbackManager.getPlayerInfo();
|
|
|
|
|
2023-07-26 17:41:08 +03:30
|
|
|
return currentPlayerInfo?.id === playerId;
|
2020-09-07 12:54:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function onOpen() {
|
|
|
|
const playerId = localStorage.getItem('autocastPlayerId');
|
|
|
|
|
|
|
|
playbackManager.getTargets().then(function (targets) {
|
2023-05-04 11:27:15 -04:00
|
|
|
for (const target of targets) {
|
|
|
|
if (target.id == playerId) {
|
|
|
|
playbackManager.trySetActivePlayer(target.playerName, target);
|
2020-09-07 12:54:12 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-22 01:09:18 -05:00
|
|
|
try {
|
|
|
|
const apiClient = ServerConnections.currentApiClient();
|
2020-10-18 18:57:20 +01:00
|
|
|
|
2020-11-22 01:09:18 -05:00
|
|
|
if (apiClient && supported()) {
|
|
|
|
Events.on(apiClient, 'websocketopen', onOpen);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
console.warn('Could not get current apiClient', ex);
|
2020-09-07 12:54:12 -04:00
|
|
|
}
|