Last active
December 14, 2015 15:29
-
-
Save strandel/5108369 to your computer and use it in GitHub Desktop.
Publish/subscribe
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
var pubsub = (function () { | |
var pubsubs = {} | |
function publish(name, data) {var listeners = pubsubs[name] || []; listeners.forEach(function (func) {func(data)})} | |
function subscribe(name, func) {if (!pubsubs[name]) pubsubs[name] = []; pubsubs[name].push(func)} | |
return {publish: publish, subscribe: subscribe} | |
})(); | |
// Example | |
pubsub.subscribe('HIT_ME', function (x) {console.log('Hit me ' + x)}) | |
pubsub.publish('HIT_ME', 'with your rhythm stick') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment