MessageHandler.ts
783 Bytes
import { Connection } from "../connection/Connection";
import {
ServerInboundMessage,
ServerInboundMessageKey,
ServerResponse,
} from "../../common/index";
import { User } from "../user/User";
type UserHandlerMap = {
[Key in ServerInboundMessageKey]?: (
user: User,
message: ServerInboundMessage<Key>
) => ServerResponse<Key>;
};
export class MessageHandler {
private handlers: UserHandlerMap;
constructor(handlers: UserHandlerMap) {
this.handlers = handlers;
}
public handle(
type: ServerInboundMessageKey,
user: User,
message: any,
callback: Function
): boolean {
const handler = this.handlers[type];
if (!handler) return false;
const response = handler(user, message);
callback(response);
return true;
}
}