Created
December 4, 2024 12:33
-
-
Save andreasvirkus/12195dbb089a527b8b7ce679fc0722e1 to your computer and use it in GitHub Desktop.
This file contains 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 const EventBus = { | |
$on (eventType, callback) { | |
document.addEventListener(eventType, (ev) => callback(ev.detail)) | |
}, | |
$dispatch (eventType, data) { | |
const event = new CustomEvent(eventType, { detail: data }) | |
document.dispatchEvent(event) | |
}, | |
$off (eventType, callback) { | |
document.removeEventListener(eventType, callback) | |
} | |
} | |
// usage | |
EventBus.$dispatch('user.connect', { some: 'data' }) | |
// consumption | |
EventBus.$on('user.connect', (data) => console.log(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment