| 38 | } |
| 39 | |
| 40 | class CastManager { |
| 41 | private castDestinations = $state<ICastDestination[]>([]); |
| 42 | private current = $derived<ICastDestination | null>(this.monitorConnectedDestination()); |
| 43 | |
| 44 | availableDestinations = $state<ICastDestination[]>([]); |
| 45 | initialized = $state(false); |
| 46 | |
| 47 | isCasting = $derived<boolean>(this.current?.isConnected ?? false); |
| 48 | receiverName = $derived<string | null>(this.current?.receiverName ?? null); |
| 49 | castState = $derived<CastState | null>(this.current?.castState ?? null); |
| 50 | currentTime = $derived<number | null>(this.current?.currentTime ?? null); |
| 51 | duration = $derived<number | null>(this.current?.duration ?? null); |
| 52 | |
| 53 | private sessionKey: SessionCreateResponseDto | null = null; |
| 54 | |
| 55 | constructor() { |
| 56 | // load each cast destination |
| 57 | this.castDestinations = [ |
| 58 | new GCastDestination(), |
| 59 | // Add other cast destinations here (ie FCast) |
| 60 | ]; |
| 61 | |
| 62 | eventManager.on({ |
| 63 | AppInit: () => void this.initialize(), |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | private async initialize() { |
| 68 | // this goes first to prevent multiple calls to initialize |
| 69 | if (this.initialized) { |
| 70 | return; |
| 71 | } |
| 72 | this.initialized = true; |
| 73 | |
| 74 | // try to initialize each cast destination |
| 75 | for (const castDestination of this.castDestinations) { |
| 76 | const destAvailable = await castDestination.initialize(); |
| 77 | if (destAvailable) { |
| 78 | this.availableDestinations.push(castDestination); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // monitor all cast destinations for changes |
| 84 | // we want to make sure only one session is active at a time |
| 85 | private monitorConnectedDestination(): ICastDestination | null { |
| 86 | // check if we have a connected destination |
| 87 | const connectedDest = this.castDestinations.find((dest) => dest.isConnected); |
| 88 | return connectedDest || null; |
| 89 | } |
| 90 | |
| 91 | private isTokenValid() { |
| 92 | // check if we already have a session token |
| 93 | // we should always have a expiration date |
| 94 | if (!this.sessionKey || !this.sessionKey.expiresAt) { |
| 95 | return false; |
| 96 | } |
| 97 |
nothing calls this directly
no test coverage detected