mirror of
https://codeberg.org/vlw/still-alive.git
synced 2025-09-14 00:13:41 +02:00
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.
39 lines
No EOL
1 KiB
JavaScript
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();
|
|
}
|
|
} |