Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Created March 23, 2010 02:47
Show Gist options
  • Save coolaj86/340803 to your computer and use it in GitHub Desktop.
Save coolaj86/340803 to your computer and use it in GitHub Desktop.
// fake jQuery and jGrowl
window.$ = window.$ || {};
$.jGrowl = $.jGrowl || function(data,params){
alert('for non-obtrusive alerts, get jGrowl...\n' + JSON.stringify(data));
};
// This creates a "closure" for all variables inside.
(function(window){
/**
* Message hub
*
* Should data centers should provide their own caching? Or should this?
* If so any data which transmits through this hub should give
* name, timestamp, soft_ttl, hard_ttl
* soft_ttl would define that the cache is usable, but should be updated in the background as soon as possible
* hard_ttl would mean the cache is too stale
* The timestamp would aide in ensuring that 'wanters' don't cause 'subscribers' to get false updates
*/
Hub = new function() {
/**
* provide{}["keyname"]{}["provider_id"]->requestDataFromProvider()
*/
var provide = {};
/**
* subscriptions{}["keyname"]->[]notifySubscriberOnEachUpdate()
*/
var subscriptions = {};
/**
* wanters{}["keyname"]->[]notifyWanterJustThisOnce()
*/
var wanters = {};
/**
* Given the name of some data which is needed, find all providers
* and ask each of them for the data.
*
* The fastest (and then most recent) data should win. (Currently not handled)
* This only makes sense if the caching is handled by the hub.
*
* If the caching isn't to handled by the hub then would there be only one provider
* which handles multiple sources? Perhaps this makes more sense?
*
* @key a name describing the desired data
*/
this.callfor = function(key, ping){
var i;
if (provide[key]) {
for (id in provide[key]) {
provide[key][id](ping); // provider.callme
}
} else {
$.jGrowl('No service is registered to provide "' + key + '"');
}
};
/**
* Update or ignore
*
* A subscriber wants to know if the chache is still good
* (Not implemented)
*/
this.ping = function(key){
this.callfor(key, 'ping');
};
/**
* Allow a provider to register itself
*
* @id The name of the Namespace which provides the dataset
* i.e. we may get data for Friends from FB and ClueList (BrowserSync)
* @key The name of the data set
* @callme The name of the function which will
* ultimately trigger update(key,value,ttl)
*/
this.register = function(id,key,askMe) {
provide[key] = provide[key] || {};
provide[key][id] = askMe;
$.jGrowl('providing: ' + key + ' ' + id + ' ' + askMe);
};
/**
* Receive notifications as soon as data updates
*
* Subscribers shouldn't 'wants' data. They should
* ping for it instead;
*/
this.subscribe = function(key,tellMe) {
subscriptions[key] = subscriptions[key] || [];
subscriptions[key].push(tellMe);
}
/**
* One-time request for particular data
*
* @keys an array of strings
*/
this.wants = function(keys, callback){
var i, key;
// I don't care about order
for (i in keys) {
key = keys[i];
wanters[key] = wanters[key] || [];
wanters[key].push(callback);
this.callfor(key);
}
};
/**
* Forwarded to all wanters and subscribers.
*
* How do we decide if it's actually newer?
* Again, this is a matter of caching.
*
*/
this.update = function(key, cur_cache){
var i;
if (wanters[key]) {
for (i in wanters[key]) {
wanters[key][i](cur_cache);
wanters[key][i] = undefined;
}
// These only get called once
wanters[key] = [];
}
};
};
window.Hub = Hub;
// Some dummy classes to test that the hub works
Dummy = new function() {
var service_name = 'DummyProvider';
var key = 'Dummy.all';
// Get all dummies from some service, database, etc
this.all = function(){
// Normally we might want to call a remote service here:
// $.get('/url',data,function(){});
Hub.update(key,{
"dummy" : "dummy",
"timestamp" : (new Date()).valueOf()
});
};
Hub.register(service_name, key, this.all);
};
DummyConsumer = new function() {
var wants = ['Dummy.all','Doesnt.exist'];
var has = {};
this.get = function(){Hub.wants(wants, function(dummies){
// put stuff into has and then check to see if we have everything before executing this
$.jGrowl('Got the dummy data: ' + JSON.stringify(dummies));
// delete the items from has after execution
//
})};
}
DummyConsumer.get();
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment