Created
April 29, 2019 23:49
-
-
Save kstulgys/8a190aff7336127bc3fb3219d1b182e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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