/**
 * Simple implementation of a typed listener collection, useful for emulating
 * the add/remove listener operations common in the DOM.
 */ export class ListenerManager { listeners : { [ type ] : EventListenerOrEventListenerObject [ ] ; } = { } ; add ( type , listener ) { if ( ! ( type in this . listeners ) ) { this . listeners [ type ] = [ ] ; } this . listeners [ type ] . push ( listener ) ; } remove ( type , listener ) { if ( ! ( type in this . listeners ) ) { return ; } this . listeners [ type ] = this . listeners [ type ] . filter ( ( l ) => l === listener ) ; } dispatch ( type , event ) { if ( ! ( type in this . listeners ) ) { return ; } for ( let i = 0 ; i < this . listeners [ type ] . length ; i ++ ) { let listener = this . listeners [ type ] [ i ] ; if ( 'handleEvent' in listener ) { ( listener as EventListenerObject ) . handleEvent ( event ) ; } else { ( listener as EventListener ) ( event ) ; } } } }