Skip to content

Instantly share code, notes, and snippets.

@strandel
Last active December 14, 2015 15:29
Show Gist options
  • Save strandel/5108369 to your computer and use it in GitHub Desktop.
Save strandel/5108369 to your computer and use it in GitHub Desktop.
Publish/subscribe
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