export interface IEventListener < T > { addEventListener ( event , callback ) : void ; addEventListenerOnce ( event , callback ) : void ; } export class EventListener < T > implements IEventListener < T > { protected event : { [ key ] : Array < { callback : Function ; param : { once : boolean ; } ; } > ; } ; constructor ( ) { this . event = { } ; } public addEventListener ( event , callback ) { if ( ! this . event [ < any > event ] ) { this . event [ < any > event ] = new Array < any > ( ) ; } this . event [ < any > event ] . push ( { callback : callback , param : { once : false } , } ) ; } public addEventListenerOnce ( event , callback ) { if ( ! this . event [ < any > event ] ) { this . event [ < any > event ] = new Array < any > ( ) ; } this . event [ < any > event ] . push ( { callback : callback , param : { once : true } , } ) ; } protected callEvent ( event , ... args ) { if ( ! this . event [ < any > event ] ) { return ; } let del = new Array < number > ( ) ; this . event [ < any > event ] . forEach ( ( v , index ) => { v . callback . apply ( this , args ) ; if ( v . param . once ) { del . push ( index ) ; } } ) ; del . forEach ( ( v , index ) => { this . event [ < any > event ] . splice ( v - index , 1 ) ; } ) ; } }