Skip to content

Instantly share code, notes, and snippets.

@unknownuser88
Last active August 29, 2015 14:18
Show Gist options
  • Save unknownuser88/d653b8be88bc6b2dec8f to your computer and use it in GitHub Desktop.
Save unknownuser88/d653b8be88bc6b2dec8f to your computer and use it in GitHub Desktop.
pubsub javascript/jquery
var pubsub = (function() {
// Storage for topics that can be broadcast
// or listened to
var topics = {};
// An topic identifier
var subUid = -1;
// Publish or broadcast events of interest
// with a specific topic name and arguments
// such as the data to pass along
function publish( topic, args ) {
if ( !topics[topic] ) {
return false;
}
var subscribers = topics[topic],
len = subscribers ? subscribers.length : 0;
while (len--) {
subscribers[len].func( topic, args );
}
return this;
}
// Subscribe to events of interest
// with a specific topic name and a
// callback function, to be executed
// when the topic/event is observed
function subscribe( topic, func ) {
if (!topics[topic]) {
topics[topic] = [];
}
var token = ( ++subUid ).toString();
topics[topic].push({
token: token,
func: func
});
console.log('topics ' , topics);
return token;
}
// Unsubscribe from a specific
// topic, based on a tokenized reference
// to the subscription
function unsubscribe( token ) {
for ( var m in topics ) {
if ( topics[m] ) {
for ( var i = 0, j = topics[m].length; i < j; i++ ) {
if ( topics[m][i].token === token ) {
topics[m].splice( i, 1 );
return token;
}
}
}
}
return this;
}
return {
publish: publish,
subscribe: subscribe,
unsubscribe: unsubscribe
};
})();
// Subscribers listen for topics they have subscribed to and
// invoke a callback function (e.g messageLogger) once a new
// notification is broadcast on that topic
// var subscription = pubsub.subscribe( "inbox/newMessage", function(eventName, data) {
// console.log('eventName ' , eventName);
// console.log('data ' , data);
// } );
// Publishers are in charge of publishing topics or notifications of
// interest to the application. e.g:
// pubsub.publish( "inbox/newMessage", "hello world!" );
// // or
// pubsub.publish( "inbox/newMessage", ["test", "a", "b", "c"] );
// or
// pubsub.publish( "inbox/newMessage", {
// sender: "[email protected]",
// body: "Hey again!"
// });
// We cab also unsubscribe if we no longer wish for our subscribers
// to be notified
// pubsub.unsubscribe( subscription );
// // Once unsubscribed, this for example won't result in our
// // messageLogger being executed as the subscriber is
// // no longer listening
// pubsub.publish( "inbox/newMessage", "Hello! are you still there?" );
var pubsub = (function($) {
function publish(eventName, data, obj) {
var object = obj || document;
$(object).trigger(eventName, data);
}
function subscribe(eventName, func, obj) {
var object = obj || document;
$(object).on(eventName, function() {
func(eventName, [].slice.call(arguments, 1));
});
}
function unsubscribe(eventName, obj) {
var object = obj || document;
$(object).off(eventName);
}
return {
publish: publish,
subscribe: subscribe,
unsubscribe: unsubscribe
};
})($);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment