Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active December 10, 2015 03:38
Show Gist options
  • Save jasdeepkhalsa/4375978 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/4375978 to your computer and use it in GitHub Desktop.
A basic implementation of the Publish/Subscribe pattern by ExampleJS.com
var PubSub = (function (window) {
var uid, topics, ps;
// Unique ID that gets incremented with every subscription.
uid = 0;
// Objects subscribe to a specific topic so this data structure
// needs to be organized as a set of key-value pairs keyed to
// those topics.
topics = {};
ps = {};
ps.subscribe = function (topic, fn) {
// If this topic does not yet exist, create it.
if (!topics.hasOwnProperty(topic)) {
topics[topic] = {};
}
// Register the function with the specified topic using a
// unique ID as the key.
topics[topic][++uid] = fn;
// Return the unique ID for unsubscribing.
return uid;
};
ps.unsubscribe = function (topic, id) {
// Delete the function registered with the specified key in
// the specified topic.
if (topics[topic][id]) {
delete topics[topic][id];
}
};
ps.publish = function (topic, obj) {
var key;
// If this is a valid topic, then invoke every subscribed
// function and pass the topic and specified object as the
// arguments.
if (topics.hasOwnProperty(topic)) {
for (key in topics[topic]) {
topics[topic][key](topic, obj);
}
}
};
ps.topics = function () {
return topics;
};
window.PubSub = ps;
})(window);
var PubSub, Joe, Mom, Bank, Newspaper, IRS;
(function (ps) {
var uid, topics;
//
// Unique ID that gets incremented for every subscription.
//
uid = 0;
//
// Objects can subscribe to particular topics so the `topics` data structure needs to be
// organized with key-value pairs keyed to those topics.
//
topics = {};
ps.subscribe = function (topic) {
var i, l;
//
// Add the specified topic to `topics` if `topics` does not already have a matching key.
//
if (!topics.hasOwnProperty(topic)) {
topics[topic] = {};
}
//
// Register any specified functions with this topic using a unique ID as the key.
//
for (i = 1, l = arguments.length; i < l; i++) {
topics[topic][++uid] = arguments[i];
}
//
// Return the unique ID used as the key for this subscription so we can later unsubscribe
// from this topic if necessary.
//
return uid;
};
ps.unsubscribe = function (topic, id) {
//
// Delete the function stored in `topics` at the two keys defined by `topic` and `id`.
//
if (topics[topic][id]) {
delete topics[topic][id];
}
};
ps.publish = function (topic, obj) {
var key;
//
// If `topic` is a valid property of the `topics` hash then iterate and invoke each function
// paired with one of its keys.
//
if (topics[topic]) {
for (key in topics[topic]) {
topics[topic][key](topic, obj);
}
}
};
ps.topics = function () {
return topics;
};
})(PubSub = {});
Joe = {
occupation: function () {
return 'plumber';
},
hometown: function () {
return 'Stamford, Connecticut';
},
win: function () {
var amount = 85000000;
console.log('***** Joe wins the $' + amount + ' lottery jackpot!!! *****');
PubSub.publish('jackpot', {
amount: amount,
hometown: this.hometown(),
occupation: this.occupation()
});
}
};
Mom = {
congrats: function (topic, data) {
console.log('Mom says, "You know you were always my favorite child!"');
}
};
Bank = {
getBalance: function (topic, data) {
console.log('Savings Account Balance: $' + (data.amount + 9.00));
}
};
Newspaper = {
headline: function (topic, data) {
console.log('Tomorrow' + "'" + 's headline: "' + data.hometown + ' ' + data.occupation + ' wins $' + data.amount + ' jackpot"');
}
};
IRS = {
tax: function (topic, data) {
console.log('Dear Joe,\n\nYou owe $' + (data.amount * 0.5) + ' in taxes. Have a nice day.\n\nSincerely,\n\nThe I.R.S.');
}
};
PubSub.subscribe('jackpot', Mom.congrats, Bank.getBalance, Newspaper.headline, IRS.tax);
Joe.win();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment