import { IInjector } from "../../../framework/api/IInjector" ; import { ILogger } from "../../../framework/api/ILogger" ; import { getQualifiedClassName } from "../../../framework/impl/getQualifiedClassName" ; import { IEvent } from "../../../events/api/IEvent" ; import { IEventDispatcher } from "../../../events/api/IEventDispatcher" ; import { IClass } from "../../matching/IClass" ; import { isInstanceOfType } from "../../matching/isInstanceOfType" ; import { ICommandExecutor } from "../../commandCenter/api/ICommandExecutor" ; import { ICommandMappingList } from "../../commandCenter/api/ICommandMappingList" ; import { ICommandTrigger } from "../../commandCenter/api/ICommandTrigger" ; import { CommandPayload } from "../../commandCenter/api/CommandPayload" ; import { CommandExecutor } from "../../commandCenter/impl/CommandExecutor" ; import { CommandMapper } from "../../commandCenter/impl/CommandMapper" ; import { CommandMappingList } from "../../commandCenter/impl/CommandMappingList" ; private export class EventCommandTrigger implements ICommandTrigger { private _dispatcher : IEventDispatcher ; private _type : string ; private _eventClass : IClass < IEvent > ; private _mappings : ICommandMappingList ; private _executor : ICommandExecutor ; private constructor ( injector , dispatcher , type , eventClass ? , processors ? , logger ? ) { this . _dispatcher = dispatcher ; this . _type = type ; this . _eventClass = eventClass ; this . _mappings = new CommandMappingList ( this , processors ? processors : [ ] , logger ) ; this . _executor = new CommandExecutor ( injector , this . _mappings . removeMapping . bind ( this . _mappings ) ) ; } private public createMapper ( ) { return new CommandMapper ( this . _mappings ) ; } inheritDoc public activate ( ) { this . _dispatcher . addEventListener ( this . _type , this . eventHandler , this ) ; } inheritDoc public deactivate ( ) { this . _dispatcher . removeEventListener ( this . _type , this . eventHandler , this ) ; } public toString ( ) { let eventDescription = "" ; eventDescription = ! this . _eventClass ? "Event" : getQualifiedClassName ( this . _eventClass ) ; return eventDescription + " with selector '" + this . _type + "'" ; } private eventHandler ( event ) { let eventConstructor = < IClass < IEvent > > event . constructor ; let payloadEventClass ; if ( ! this . _eventClass ) { payloadEventClass = eventConstructor ; } else if ( isInstanceOfType ( event , this . _eventClass ) ) { payloadEventClass = this . _eventClass ; } else { return ; } this . _executor . executeCommands ( this . _mappings . getList ( ) , new CommandPayload ( [ event ] , [ payloadEventClass ] ) ) ; } }