Skip to content

Instantly share code, notes, and snippets.

@kstulgys
Created April 29, 2019 23:49
Show Gist options
  • Save kstulgys/8a190aff7336127bc3fb3219d1b182e0 to your computer and use it in GitHub Desktop.
Save kstulgys/8a190aff7336127bc3fb3219d1b182e0 to your computer and use it in GitHub Desktop.
export default class PubSub {
constructor() {
this.registry = {};
}
on(evtName, cb) {
this.registry[evtName] = this.registry[evtName] || [];
this.registry[evtName].push(cb);
}
off(evtName, cb) {
if (this.registry[evtName]) {
for (let i = 0; i < this.registry[evtName].length; i++) {
if (this.registry[evtName][i] === cb) {
this.registry[evtName].splice(i, 1);
break;
}
}
}
}
emit(evtName, data) {
if (this.registry[evtName]) {
this.registry[evtName].forEach(cb => cb(data));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment