export type EventSlotCallback = ( eventData:T ) => void; export class EventSlot { private _listeners:EventSlotCallback[] = []; addListener( callback:EventSlotCallback ) { this._listeners.push( callback ); } once( callback:EventSlotCallback ) { let onceCallback = ( t:T ) => { try { callback( t ); } catch( e ) { console.error( "Error on callback", e ); } this.removeListener( onceCallback ); }; this.addListener( onceCallback ); } removeListener( callback:EventSlotCallback ) { let index = this._listeners.indexOf( callback ); if ( index === -1 ) { return; } this._listeners.splice( index, 1 ); } removeAll() { this._listeners = []; } dispatch( eventData:T ) { for ( let listener of this._listeners ) { listener( eventData ); } } }