interface Callback { ( cb ) : void ; } export class EventFeed { register : Callback ; unregister : Callback ; constructor ( register , unregister ) { this . register = register ; this . unregister = unregister ; } subscribers : Array < Function > = [ ] ; handle ( ... args ) { this . subscribers . forEach ( ( listener ) => { listener ( ... args ) ; } ) ; } add ( listener ) { this . subscribers . push ( listener ) ; if ( this . subscribers . length === 1 ) { this . register ( this . handle ) ; } } remove ( listener ) { const index = this . subscribers . indexOf ( listener ) ; this . subscribers . splice ( index , 1 ) ; if ( this . subscribers . length === 0 ) { this . unregister ( this . handle ) ; } } }