import Event from "./Event" ; export class EventDispatcher < T > { private readonly _listeners : { ( event ) : void ; } [ ] ; constructor ( ) { this . _listeners = [ ] ; } public addEventListener ( listenerFunc ) { if ( this . hasEventListener ( listenerFunc ) ) { return ; } this . _listeners . push ( listenerFunc ) ; } public removeEventListener ( listenerFunc ) { for ( let i = 0 ; i < this . _listeners . length ; i ++ ) { if ( this . _listeners [ i ] === listenerFunc ) { this . _listeners . splice ( i , 1 ) ; } } } public dispatchEvent ( evt ) { for ( const listenerElement of this . _listeners ) { listenerElement . call ( this , evt ) ; } } private hasEventListener ( listener ) { return this . _listeners . indexOf ( listener ) >= 0 ; } }