Skip to content

Instantly share code, notes, and snippets.

@sameerkat
Forked from patrickrbc/pubsub.js
Last active August 29, 2015 14:19
Show Gist options
  • Save sameerkat/060aafe8b6e4422758b1 to your computer and use it in GitHub Desktop.
Save sameerkat/060aafe8b6e4422758b1 to your computer and use it in GitHub Desktop.
;(function (root) {
'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);
});
};
root.PubSub = new PubSub();
} (window));
PubSub.subscribe('foo', function (a, b) {
console.log('foo callback', a, b)
});
PubSub.subscribe('foo', function (a, b) {
console.log('hey there, thats foo callback ', a, b)
});
PubSub.publish('foo', ['arg A', 'arg b']); // log: foo callback "myargument A" "arg b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment