MessageHandler.ts
681 Bytes
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
): ServerResponse<any> | undefined {
const handler = this.handlers[type];
if (!handler) return undefined;
return handler(user, message);
}
}