-
-
Save fmal/aed4287af3c81d23e5ef906bd9e67277 to your computer and use it in GitHub Desktop.
Simple event system
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 _callbacks = {}; | |
const events = { | |
on(name, cb) { | |
_callbacks[name] = _callbacks[name] || []; | |
_callbacks[name].push(cb); | |
}, | |
dispatch(name, data) { | |
setTimeout(function() { | |
(_callbacks[name] || []).forEach(cb => cb(data)); | |
}); | |
} | |
} | |
events.on('hi', data => console.log(data)); | |
events.dispatch('hi', 'hello'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment