Created
September 28, 2015 06:06
-
-
Save alexreardon/ef634e78841a2992e7c1 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