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.

37 lines
824 B

import { Message } from "./message.js";
/**
* @typedef {Object} MessageLike
* @property {string} action - every string valid for JS function name.
* @property {Object} data - The user's last name.
*/
/**
* @param {string|MessageLike} message - Message itself of JSON representation of Message
*/
function processMessage(messageLike = {}) {
const message = new Message(messageLike);
if(!message.isValid()) {
console.error("Got mallformed message:", message);
return;
};
if (this.actions[message.action]) {
this.actions[message.action](message.data);
} else {
this.fallbackAction(message);
}
};
const messageProcessor = {
// Use Map instead?
actions: {},
processMessage,
fallbackAction: function(x) {
console.log("No actions for message:", x)
}
}
export { messageProcessor };