Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Last active August 29, 2015 14:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save weslleyaraujo/c906b58a89bf82032ae6 to your computer and use it in GitHub Desktop.
Save weslleyaraujo/c906b58a89bf82032ae6 to your computer and use it in GitHub Desktop.
/**
* @method: PubSub
* */
;(function(global) {
'use strict';
function PubSub () {
this.topics = {};
};
PubSub.prototype.subscribe = function(name, fn) {
this.topics[name] = this.topics[name] || [];
this.topics[name].push(fn);
}
PubSub.prototype.publish = function(name, args) {
this.topics[name] = this.topics[name] || [];
this.topics[name].forEach(function(fn) {
fn.apply(this, args);
});
}
global.PubSub = new PubSub();
}) (window);
PubSub.subscribe('foo', function (a, b) {
console.log('hey there from foo callback', arguments, a, b)
});
PubSub.subscribe('foo', function (a, b) {
console.log('hey there again from another foo callback', arguments, a, b)
});
PubSub.publish('foo', ['myArgument', 'anotherArg']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment