You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
metadata/src/notifier.js

61 lines
1.4 KiB

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 };