$(function () {
var PubSub = {};
(function(p){
"use strict";
var topics = {},
lastUid = -1;
var publish = function( topic , data){
if ( !topics.hasOwnProperty( topic ) ){
return false;
}
var notify = function(){
var subscribers = topics[topic],
throwException = function(e){
return function(){
throw e;
};
};
for ( var i = 0, j = subscribers.length; i < j; i++ ){
try {
subscribers[i].func( topic, data );
} catch( e ){
setTimeout( throwException(e), 0);
}
}
};
setTimeout( notify , 0 );
return true;
};
/**
* Publishes the topic, passing the data to it's subscribers
* @topic (String): The topic to publish
* @data: The data to pass to subscribers
**/
p.publish = function( topic, data ){
return publish( topic, data, false );
};
/**
* Subscribes the passed function to the passed topic.
* Every returned token is unique and should be stored if you need to unsubscribe
* @topic (String): The topic to subscribe to
* @func (Function): The function to call when a new topic is published
**/
p.subscribe = function( topic, func ){
// topic is not registered yet
if ( !topics.hasOwnProperty( topic ) ){
topics[topic] = [];
}
var token = (++lastUid).toString();
topics[topic].push( { token : token, func : func } );
// return token for unsubscribing
return token;
};
/**
* Unsubscribes a specific subscriber from a specific topic using the unique token
* @token (String): The...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.