Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Forked from ryanflorence/pubsub.js
Created October 14, 2011 22:57
Show Gist options
  • Save jacobdubail/1288602 to your computer and use it in GitHub Desktop.
Save jacobdubail/1288602 to your computer and use it in GitHub Desktop.
JavaScript: Simple Pub/Sub
!function () {
var channels = {};
this.subscribe = function (channel, subscription) {
if (!channels[channel]) channels[channel] = [];
channels[channel].push(subscription);
};
this.publish = function (channel) {
if (!channels[channel]) return;
var args = [].slice.call(arguments, 1);
for (var i = 0, l = channels[channel].length; i < l; i++)
channels[channel][i].apply(this, args);
};
}.call(this);
subscribe('foo', function (a, b) {
console.log(a, b);
});
publish('foo', 1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment