still-alive/assets/js/modules/PlayerManager.mjs
Victor Westerlund 7a6ed67d92 Fix Monkeydo events and #3
Monkeydo events will now be executed through the Proxy created in 8b659066cc

This commit also implements a solution for #3 by having each window report to its PlayerManager when it has been closed.
2021-12-05 22:09:56 +01:00

39 lines
No EOL
1 KiB
JavaScript

import { default as Player } from "./PlayerWindow.mjs";
export default class PlayerManager {
constructor() {
this.players = {
"lyrics": new Player("lyrics","monkeydo_lyrics.json"),
"credits": new Player("credits","monkeydo_credits.json"),
"art": new Player("art")
};
this.channels = new WeakMap();
for(const player of Object.values(this.players)) {
// Create BroadcastChannels for each player
const channel = new BroadcastChannel(player.name);
this.channels.set(player,channel);
channel.addEventListener("message",event => this.message(event));
// Open each player
if(player.open() === null) return this.windowOpenFailed();
}
}
// Close all opened windows (some browsers allow one window to open)
closeAll() {
for(const player of Object.values(this.players)) {
player.window?.close();
}
}
// Window blocked from opening, show "allow popups" message
windowOpenFailed() {
this.closeAll();
throw new Error("WINDOW_OPEN_FAILED");
}
message(event) {
if(event.data === "PLAYER_CLOSED") return this.closeAll();
}
}