MessageHandlerChain.ts 778 Bytes
import { Connection } from "../connection/Connection";
import { ServerInboundMessageMap, ServerResponse } from "../../common/index";
import { keys } from "ts-transformer-keys";

export class MessageHandlerChain {
  connection: Connection;

  constructor(connection: Connection) {
    this.connection = connection;

    for (const key in keys<ServerInboundMessageMap>()) {
      const type = key as keyof ServerInboundMessageMap;
      this.connection.socket.on(key, (message: any, callback: Function) => {
        if (
          connection?.user &&
          connection.user.handler.handle(
            type,
            connection,
            message.data,
            callback
          )
        )
          return;

        // TODO: Add more handlers
      });
    }
  }
}