Skip to content

Instantly share code, notes, and snippets.

@kumaraksi
Created February 24, 2019 06:42
Show Gist options
  • Save kumaraksi/e0c4506497efee85fb467bc72a2f5db7 to your computer and use it in GitHub Desktop.
Save kumaraksi/e0c4506497efee85fb467bc72a2f5db7 to your computer and use it in GitHub Desktop.
EventBus
export class EventBus{
static instance;
constructor(){
if(EventBus.instance){
return EventBus.instance
}
this.subscribers = [];
EventBus.instance = this;
}
static getInstance(){
return new EventBus();
}
subscribe(eventName,handler){
const eventHanlder = {
event:eventName,
handler:handler
}
this.subscribers.push(eventHanlder)
}
emit(eventName,payload){
for(const subscriber of this.subscribers){
if(subscriber.event === eventName){
subscriber.handler(payload);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment