import { MessageType } from "../enums" ; import { IClient } from "../models/client" ; import { IMessage } from "../models/message" ; import { Handler } from "./handler" ; export interface IHandlersRegistry { registerHandler ( messageType , handler ) : void ; handle ( client , message ) : boolean ; } export class HandlersRegistry implements IHandlersRegistry { private readonly handlers : Map < MessageType , Handler > = new Map ( ) ; public registerHandler ( messageType , handler ) { if ( this . handlers . has ( messageType ) ) return ; this . handlers . set ( messageType , handler ) ; } public handle ( client , message ) { const { type } = message ; const handler = this . handlers . get ( type ) ; if ( ! handler ) return false ; return handler ( client , message ) ; } }