import { state } from "./state.js"; function updateMeta(message) { const { data } = message; this.state.lastPlayed = data; this.notifyAll(message); } function startOnline(message) { this.state.isOnline = true; this.notifyAll(message); } function stopOnline(message) { this.state.isOnline = false; this.notifyAll(message); } function addClient({ data }) { const { socket, id } = data; this.clients.set(id, socket); this.notifyOne(socket, { action: "setState", data: this.state }); } function deleteClient({ data }) { const { id } = data; this.clients.delete(id); } const actions = { clientConnected: addClient, cliendDisconnected: deleteClient, metadataChange: updateMeta, liveStarted: startOnline, liveEnded: stopOnline, }; const notifier = { __proto__: state, notifyAll: function (message) { const messageStrinified = JSON.stringify(message); this.clients.forEach((c) => c.send(messageStrinified)); }, notifyOne: function (socket, message) { const messageStrinified = JSON.stringify(message); console.log("NOTIFY ONE:", message); socket.send(messageStrinified); }, processMessage: function (message) { const action = actions[message.action]; if (action) { action.call(this, message); console.log("PERFORM ACTION", message.action); } else { console.log("no avaible command for state mutation", message.action); } }, }; export { notifier };