Skip to content

Instantly share code, notes, and snippets.

@VoloshchenkoAl
Created August 16, 2017 20:59
Show Gist options
  • Save VoloshchenkoAl/45e3a96f07fe503ae5365d805e79dbbc to your computer and use it in GitHub Desktop.
Save VoloshchenkoAl/45e3a96f07fe503ae5365d805e79dbbc 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