Created
February 1, 2019 08:53
-
-
Save johnny-b-good/049738faaf1fbdf9622072ac4d38fb00 to your computer and use it in GitHub Desktop.
Simple JS event bus
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
function EventBus(props) { | |
this.bus = document.createElement('fakeelement'); | |
this.addEventListener = function(event, callback) { | |
this.bus.addEventListener(event, callback); | |
}; | |
this.removeEventListener = function(event, callback) { | |
this.bus.removeEventListener(event, callback); | |
}; | |
this.dispatchEvent = function(event, detail) { | |
this.bus.dispatchEvent( | |
new CustomEvent( | |
event, | |
{ detail: detail } | |
) | |
); | |
}; | |
} | |
var bus = new EventBus(); | |
export default bus; | |
export { | |
EventBus as EventBus, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment