Skip to content

Instantly share code, notes, and snippets.

@yagopv
Created July 16, 2018 13:45
Show Gist options
  • Save yagopv/e86eab689d28186921bcfef9cdba970d to your computer and use it in GitHub Desktop.
Save yagopv/e86eab689d28186921bcfef9cdba970d to your computer and use it in GitHub Desktop.
class EventEmitter {
constructor() {
this.events = {};
}
emit(eventName, data) {
const event = this.events[eventName];
if( event ) {
event.forEach(fn => {
fn.call(null, data);
});
}
}
subscribe(eventName, fn) {
if(!this.events[eventName]) {
this.events[eventName] = [];
}
this.events[eventName].push(fn);
return () => {
this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment