import { InvalidOperationException } from "./Exception" ; /**
 * Represents an event system
 */ export class Events { private handlers : { [ propertyName ] : Function [ ] ; } ; constructor ( ) { this . handlers = { } ; } addHandler ( eventName , handler ) { this . registEvent ( eventName ) ; if ( this . handlers [ eventName ] ) { this . handlers [ eventName ] . push ( handler ) ; } else { throw new InvalidOperationException ( "The event is not registed." ) ; } return this ; } removeHandler ( eventName , handler ) { const handlers = this . handlers [ eventName ] as Function [ ] ; if ( handlers && handlers . length > 0 ) { const index = handlers . findIndex ( ( value ) => { return value === handler ; } ) ; if ( index >= 0 ) { handlers . splice ( index , 1 ) ; } } return this ; } registEvent ( eventName ) { if ( ! this . handlers [ eventName ] ) { this . handlers [ eventName ] = [ ] ; } return this ; } raiseEvent ( eventName , ... args ) { if ( this . handlers [ eventName ] ) { this . handlers [ eventName ] . forEach ( ( action ) => { action && action ( ... args ) ; } ) ; } } dispose ( ) { this . handlers = { } ; } }