Modified:
/trunk/src/jopensocial.js
=======================================
--- /trunk/src/jopensocial.js Mon Jul 27 23:16:54 2009
+++ /trunk/src/jopensocial.js Thu Aug 20 01:54:07 2009
@@ -30,20 +30,48 @@
/**
* @private
*/
+var _resuestTimeout,
+ _cuedRequest,
+ _cuedCallbacks = [],
+ _responsKeys = [];
+
function _request( map, callback, ns ) {
- var req = (ns||opensocial).newDataRequest();
- $.each(map,function( i ){
- if(typeof(this)=="function")
+ if(_resuestTimeout) clearTimeout(_resuestTimeout);
+ var req = _cuedRequest||(ns||opensocial).newDataRequest();
+ if(typeof(callback)=="function") _cuedCallbacks.push(callback);
+ $.each(map,function(i){
+ if(typeof(this)=="function") {
+ _responsKeys.push(i);
req.add( this.call(req), i );
+ }
});
- req.send(function( res ) {
- var obj = {};
- $.each(map,function(i){
- var item = res.get(i);
- obj[i] = item.hadError() ? { error:1 } : item.getData();
+ _resuestTimeout = setTimeout(function(){
+ req.send(function( res ) {
+ var obj = {};
+ $.each(_responsKeys,function(){
+ var i = String(this);
+ var item = res.get(i);
+ obj[i] = item.hadError() ? { error:1 } : item.getData();
+ });
+ var cc = _cuedCallbacks;
+ _responsKeys = [];
+ _cuedCallbacks = [];
+ $.each(cc,function(){ this.call(this,obj); });
});
- if(typeof(callback)=="function") callback(obj);
- });
+ _cuedRequest = null;
+ _resuestTimeout = 0;
+ },99);
+ _cuedRequest = req;
+}
+
+var _rankeys = [];
+
+function rankey() {
+ do {
+ var rt = "r"+Math.ceil(Math.random()*1000000000).toString()
+ } while($.inArray(rt,_rankeys)!=-1);
+ _rankeys.push(rt);
+ return rt;
}
var
@@ -321,15 +349,15 @@
var ov = userId.match(/^viewer$|^owner$/i);
if(ids) {
var idSpec = opensocial.newIdSpec({userId:ids[1].toUpperCase(),
groupId:"FRIENDS"})
- var obj = {
- peopleData : function() { return
this.newFetchPersonAppDataRequest(idSpec, (key instanceof
Array)?key:[key]); }
- }
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function() { return this.newFetchPersonAppDataRequest(idSpec,
(key instanceof Array)?key:[key]); }
_request(obj, function(res) {
- if(!res.peopleData||!res.peopleData[id]) {
+ if(!res[rky]||!res[rky][id]) {
callback(null);
return;
}
- var data = res.peopleData||{};
+ var data = res[rky]||{};
var vals = {};
$.each(data, function(k, v) { _setval(k, v, vals) } );
callback(vals);
@@ -345,15 +373,15 @@
callback( _cachedData[id][key] );
return;
}
- var obj = {
- userData : function() { return
this.newFetchPersonAppDataRequest(opensocial.newIdSpec({userId:userId}),
(key instanceof Array)?key:[key]); }
- }
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function() { return
this.newFetchPersonAppDataRequest(opensocial.newIdSpec({userId:userId}),
(key instanceof Array)?key:[key]); }
_request(obj, function(res) {
- if(!res.userData||!res.userData[id]) {
+ if(!res[rky]||!res[rky][id]) {
callback(null);
return;
}
- var data = res.userData[id]||{};
+ var data = res[rky][id]||{};
var val = _setval(id, data);
callback(val);
});
@@ -380,15 +408,13 @@
person("viewer",function(psn){
var id = psn?psn.getId():null;
if(id&&_cachedData[id]&&_cachedData[id][key]) _cachedData[id][key] =
value;
- var obj = {
- userData : function() {
- return
this.newUpdatePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, key,
encodeURIComponent(gadgets.json.stringify(value)));
- }
- };
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function() { return
this.newUpdatePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, key,
encodeURIComponent(gadgets.json.stringify(value))); }
_request(obj, function(res) {
callback(true);
- });
- })
+ });
+ });
}
/**
@@ -401,10 +427,10 @@
function removeAppData( key, callback ) {
callback = callback || function() { return false; }
if(!key) callback( { } );
- var obj = {
- userData : function() {
- return
this.newRemovePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, [key]
);
- }
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function() {
+ return
this.newRemovePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, [key]
);
}
_request(obj, function(res) {
callback(true);
@@ -438,18 +464,16 @@
callback(_cachedPeople[id]);
return _cachedPeople[id];
}
- var obj = {
- userId : function() {
- return this.newFetchPersonRequest(id);
- }
- }
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function(){ return this.newFetchPersonRequest(id); }
_request(obj,function(res) {
if(!res) return callback(null);
- var user = res["userId"];
+ var user = res[rky];
if(!user) return callback(null);
var id = user.getId();
if(!id) return callback(null);
- if(res&&res["userId"]) {
+ if(res&&res[rky]) {
if(user.isViewer()) _viewerId = id;
if(user.isOwner()) _ownerId = id;
_cachedPeople[id] = user;
@@ -496,21 +520,20 @@
opt_params[f] = opt_params[f] || opensocial.DataRequest.FilterType.ALL;
f = opensocial.DataRequest.PeopleRequestFields.MAX;
opt_params[f] = opt_params[f] || 1000;
- _request({
- people : function() {
- return this.newFetchPeopleRequest(idspec,opt_params)
- }
- },function(res){
- if(!res||!res.people||res.people.error) {
+ var rky = rankey();
+ var obj = {};
+ obj[rky] = function() { return
this.newFetchPeopleRequest(idspec,opt_params) }
+ _request(obj,function(res){
+ if(!res||!res[rky]||res[rky].error) {
callback(null);
return false;
}
var ar = [];
- res.people.each(function(p){
+ res[rky].each(function(p){
if(!return_object) ar.push(p);
_cachedPeople[p.getId()] = p;
});
- callback(return_object?res.people:ar);
+ callback(return_object?res[rky]:ar);
});
}