A generic vehicle for dispatching and listening to events.
npm install ts-event-dispatcher
import { EventDispatcher, DispatchableEvent } from 'ts-event-dispatcher';
class SomeClassOrService extends EventDispatcher {
public static MY_EVENT:string = 'my_event';
construct(){
super();
setTimeout( () => {
this.dispatchEvent( new DispatchableEvent( SomeClassOrService.MY_EVENT ));
}, 5000);
}
}
import DispatchableEvent from 'ts-event-dispatcher';
import SomeClassOrService from './some-class-or-service';
class SomeOtherClassOrService {
constructor(
private someClassOrService:SomeClassOrService;
){
this.someClassOrService.addEventListener( SomeClassOrService.MY_EVENT, this.onMyEvent.bind( this ));
}
private onMyEvent(){
console.log( 'MY_EVENT fired' );
this.someClassOrService.removeEventListener( SomeClassOrService.MY_EVENT, this.onMyEvent.bind( this ));
}
}