import { DomainEvent } from '../../../domain/DomainEvent' ; import { DomainEventSubscriber } from '../../../domain/DomainEventSubscriber' ; import { EventBus } from '../../../domain/EventBus' ; import { DomainEventMapping } from '../DomainEventMapping' ; type Subscription = { boundedCallback : Function ; originalCallback : Function ; } ; export class InMemorySyncEventBus implements EventBus { private subscriptions : Map < string , Array < Subscription > > ; constructor ( ) { this . subscriptions = new Map ( ) ; } async start ( ) { } async publish ( events ) { const executions = [ ] ; events . map ( event => { const subscribers = this . subscriptions . get ( event . eventName ) ; if ( subscribers ) { return subscribers . map ( subscriber => executions . push ( subscriber . boundedCallback ( event ) ) ) ; } } ) ; await Promise . all ( executions ) ; } addSubscribers ( subscribers ) { subscribers . map ( subscriber => subscriber . subscribedTo ( ) . map ( event => this . subscribe ( event . EVENT_NAME ! , subscriber ) ) ) ; } setDomainEventMapping ( domainEventMapping ) { } private subscribe ( topic , subscriber ) { const currentSubscriptions = this . subscriptions . get ( topic ) ; const subscription = { boundedCallback : subscriber . on . bind ( subscriber ) , originalCallback : subscriber . on } ; if ( currentSubscriptions ) { currentSubscriptions . push ( subscription ) ; } else { this . subscriptions . set ( topic , [ subscription ] ) ; } } }