import { DomainEventClass , DomainEvent } from '../../domain/DomainEvent' ; import { DomainEventSubscriber } from '../../domain/DomainEventSubscriber' ; type Mapping = Map < string , DomainEventClass > ; export class DomainEventMapping { private mapping : Mapping ; constructor ( mapping ) { this . mapping = mapping . reduce ( this . eventsExtractor ( ) , new Map < string , DomainEventClass > ( ) ) ; } private eventsExtractor ( ) { return ( map , subscriber ) => { subscriber . subscribedTo ( ) . forEach ( this . eventNameExtractor ( map ) ) ; return map ; } ; } private eventNameExtractor ( map ) { return domainEvent => { const eventName = domainEvent . EVENT_NAME ; map . set ( eventName , domainEvent ) ; } ; } for ( name ) { const domainEvent = this . mapping . get ( name ) ; if ( ! domainEvent ) { return ; } return domainEvent ; } }