import { inject , injectable } from 'inversify' ; import { ICommandManager } from '../application/types/commandManager' ; import { IDisposableRegistry } from '../application/types/disposableRegistry' ; import { IServiceContainer } from '../ioc/types' ; import { CommandHandlerRegister } from './registration' ; import { ICommandHandler , ICommandHandlerManager } from './types' ; import { sendTelemetryEvent , sendTelemetryWhenDone } from '../common/telemetry' ; import { StopWatch } from '../common/stopWatch' ; @ injectable ( ) export class CommandHandlerManager implements ICommandHandlerManager { constructor ( @ inject ( IDisposableRegistry ) private disposableRegistry , @ inject ( ICommandManager ) private commandManager , @ inject ( IServiceContainer ) private serviceContainer ) { } public registerHandlers ( ) { for ( const item of CommandHandlerRegister . getHandlers ( ) ) { const serviceIdentifier = item [ 0 ] ; const handlers = item [ 1 ] ; const target = this . serviceContainer . get < ICommandHandler > ( serviceIdentifier ) ; handlers . forEach ( handlerInfo => this . registerCommand ( handlerInfo . commandName , handlerInfo . handlerMethodName , target ) ) ; } } private registerCommand ( commandName , handlerMethodName , target ) { const handler = target [ handlerMethodName ] as ( ... args : any [ ] ) => void ; const disposable = this . commandManager . registerCommand ( commandName , ( ... args ) => { const stopWatch = new StopWatch ( ) ; let result ; try { result = handler . apply ( target , args ) ; } finally { if ( result && result . then && typeof result . then === 'function' ) { sendTelemetryWhenDone ( commandName as any , result ) ; } else { sendTelemetryEvent ( commandName as any , stopWatch . elapsedTime ) ; } } } ) ; this . disposableRegistry . register ( disposable ) ; } }