Last active
April 12, 2017 08:56
-
-
Save gemmadlou/eb6761f63d659286c4fa53bfcffa98a8 to your computer and use it in GitHub Desktop.
Simple Event Bus (ES6) < 20 lines
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
| let bus = { | |
| messages: {}, | |
| on(message, callback) { | |
| if (!this.messages[message]) { | |
| this.messages[message] = []; | |
| } | |
| if (typeof callback === 'function') { | |
| this.messages[message].push(callback); | |
| } | |
| }, | |
| emit(message) { | |
| if (this.messages[message]) { | |
| this.messages[message].forEach((func) => { | |
| func(); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment