import { Command } from '../../domain/Command' ; import { CommandHandler } from '../../domain/CommandHandler' ; import { CommandNotRegisteredError } from '../../domain/CommandNotRegisteredError' ; export class CommandHandlersInformation { private commandHandlersMap : Map < Command , CommandHandler < Command > > ; constructor ( commandHandlers ) { this . commandHandlersMap = this . formatHandlers ( commandHandlers ) ; } private formatHandlers ( commandHandlers ) { const handlersMap = new Map ( ) ; commandHandlers . forEach ( commandHandler => { handlersMap . set ( commandHandler . subscribedTo ( ) , commandHandler ) ; } ) ; return handlersMap ; } public search ( command ) { const commandHandler = this . commandHandlersMap . get ( command . constructor ) ; if ( ! commandHandler ) { throw new CommandNotRegisteredError ( command ) ; } return commandHandler ; } }