Skip to content

Instantly share code, notes, and snippets.

@sahilsk
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save sahilsk/9e03e3c13e257ed18882 to your computer and use it in GitHub Desktop.

Select an option

Save sahilsk/9e03e3c13e257ed18882 to your computer and use it in GitHub Desktop.
topic based simple async pub-sub implementation
//Example Usage
var seeker1 = function(msg) {
console.log("seeker1 called: " + msg);
}
var seeker2 = function(msg) {
console.log("seeker2 called: " + msg);
}
var Sub_Seeker1 = SK_PubSub.subscribe("JOB", seeker1);
//or
SK_PubSub.subscribe("JOB", seeker2);
//Publish New Job
SK_PubSub.publish("JOB", "New Job Posted: Post 1");
SK_PubSub.unsubscribe("JOB", seeker2);
SK_PubSub.publish("JOB", "New Job Posted: Post 2");
//Unsubscribe seeker1
Sub_Seeker1.remove();
//Unsubcribe all seekers from "JOB" topic
SK_PubSub.unsubscribeAll("JOB");
//keep pubblishing new job
SK_PubSub.publish("JOB", "New Job Posted: Post 3");
//whoever comes first, get all queued message
SK_PubSub.subscribe("JOB", seeker2);
//Create new channel and subscribe receiver
var subscriber = SK_PubSub.subscribe("GAME", seeker1);
SK_PubSub.publish("GAME", "cricket match ");
SK_PubSub.publish("JOB", "New Job Posted: Post 4");
// Code goes here
/*
About:
Simple Async Pub-Sub implementation
Usage:
Create Subscriber
seeker = function(msg){
//---task to do on new 'msg' arrival
}
//Subscribe to a topic
var sub_seeker = SK_PubSub.subscribe("JOB", seeker);
Publish message
SK_PubSub.publish("JOB", {a:1, b:2, c:3 } )
Unsubscribe
SK_PubSub.unsubscribe("JOB", seeker);
or
sub_seeker.remove()
Unsubscribe all subscribers
SK_PubSub.unsubcribeAll("JOB");
*/
SK_PubSub = (
function() {
var topics = [];
var subscribers = [];
var messages = [];
var self = this;
return {
subscribe: function(topic, subscriber) {
//subscriber should be function
if (typeof subscriber !== "function") {
throw "subscriber should be a function";
}
// create topic if doesn't exist
if (topics.indexOf(topic) < 0) {
topics.push(topic);
subscribers[topic] = [];
messages[topic] = [];
}
//warn if susbcriber already added
if (subscribers[topic].indexOf(subscriber) >= 0) {
console.log("WARNING: Subscriber already added");
return true;
}
//add subscriber
subscribers[topic].push(subscriber);
console.log("New subscriber added " + subscribers[topic].length);
//give all pending messages
messages[topic].forEach(function(msg) {
setTimeout(function() {
subscriber.call(this, msg);
}, 0);
});
return {
remove : function(){
subscribers[topic].splice(subscribers.indexOf(subscriber), 1);
console.log("Unsubscribed");
}
}
return true;
},
publish: function(topic, pubObj) {
//let pubObj be anything: string or object
if (typeof pubObj === "function") {
throw "Not a valid publish data. Use string or object ";
}
//add message
messages[topic].push(pubObj);
//return if no subscriber
if (subscribers[topic].length === 0) {
console.log("no subcribers yet");
return 0;
}
//call all subscribers
var subs = subscribers[topic];
subs.forEach(function(subscriber) {
messages[topic].forEach(function(msg) {
setTimeout(function() {
subscriber.call(this, msg);
}, 0);
});
});
//clear message
messages[topic] = [];
},
unsubscribe: function(topic, subscriber) {
if (typeof subscriber != "function") {
throw "not a valid subscriber";
}
subscribers[topic].splice(subscribers.indexOf(subscriber), 1);
console.log("Unsubscribed");
},
unsubscribeAll: function(topic) {
if (topics.indexOf(topic) < 0) {
console.log("Warn: Invalid topic. No such topic")
return 0;
}
//total subscribers
var t_sub = subscribers[topic].length;
//just empty the topic
subscribers[topic] = [];
return t_sub;
},
}
}()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment