You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by zh...@apache.org on 2011/01/07 19:14:36 UTC

svn commit: r1056437 - in /shindig/trunk/features/src: main/javascript/features/container/container.js main/javascript/features/container/service.js test/javascript/features/container/container_test.js test/javascript/features/container/service_test.js

Author: zhoresh
Date: Fri Jan  7 18:14:35 2011
New Revision: 1056437

URL: http://svn.apache.org/viewvc?rev=1056437&view=rev
Log:
http://codereview.appspot.com/3842042/
Support preloading gadgets metadata as part of container config

Modified:
    shindig/trunk/features/src/main/javascript/features/container/container.js
    shindig/trunk/features/src/main/javascript/features/container/service.js
    shindig/trunk/features/src/test/javascript/features/container/container_test.js
    shindig/trunk/features/src/test/javascript/features/container/service_test.js

Modified: shindig/trunk/features/src/main/javascript/features/container/container.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/container.js?rev=1056437&r1=1056436&r2=1056437&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/container.js (original)
+++ shindig/trunk/features/src/main/javascript/features/container/container.js Fri Jan  7 18:14:35 2011
@@ -114,6 +114,8 @@ shindig.container.Container = function(o
    */
   this.tokenRefreshTimer_ = null;
 
+  this.preloadFromConfig_(config);
+
   this.registerRpcServices_();
 
   this.onConstructed(config);
@@ -221,17 +223,7 @@ shindig.container.Container.prototype.pr
   
   this.refreshService_();
   this.service_.getGadgetMetadata(request, function(response) {
-    for (var id in response) {
-      if (response[id].error) {
-        gadgets.warn(['Failed to preload gadget ', id, '.'].join(''));
-      } else {
-        self.addPreloadedGadgetUrl_(id);
-        if (response[id][shindig.container.MetadataResponse.NEEDS_TOKEN_REFRESH]) {
-          // Safe to re-schedule many times.
-          self.scheduleRefreshTokens_();
-        }
-      }
-    }
+    self.addPreloadGadgets_(response);
     callback(response);
   });
 };
@@ -351,6 +343,26 @@ shindig.container.ContainerConfig.TOKEN_
  */
 shindig.container.ContainerConfig.NAVIGATE_CALLBACK = 'navigateCallback';
 
+/**
+ * Provide server reference time for preloaded data. 
+ * This time is used instead of each response time in order to support server caching of results.
+ * @type {number}
+ * @const
+ */
+shindig.container.ContainerConfig.PRELOAD_REF_TIME = 'preloadRefTime';
+/**
+ * Preloaded hash of gadgets metadata
+ * @type {Object}
+ * @const
+ */
+shindig.container.ContainerConfig.PRELOAD_METADATAS = 'preloadMetadatas';
+/**
+ * Preloaded hash of gadgets tokens
+ * @type {Object}
+ * @const
+ */
+shindig.container.ContainerConfig.PRELOAD_TOKENS = 'preloadTokens';
+
 
 /**
  * Enum keys for gadget rendering params. Gadget rendering params affect which
@@ -408,6 +420,45 @@ shindig.container.ContainerRender.WIDTH 
 // Private variables and methods.
 // -----------------------------------------------------------------------------
 
+/**
+ * Add list of gadgets to preload list
+ * @param {Object} hash of gadgets data
+ * @private
+ */
+shindig.container.Container.prototype.addPreloadGadgets_ = function(response) {
+  for (var id in response) {
+    if (response[id].error) {
+      gadgets.warn(['Failed to preload gadget ', id, '.'].join(''));
+    } else {
+      this.addPreloadedGadgetUrl_(id);
+      if (response[id][shindig.container.MetadataResponse.NEEDS_TOKEN_REFRESH]) {
+        // Safe to re-schedule many times.
+        this.scheduleRefreshTokens_();
+      }
+    }
+  }
+}
+
+
+/**
+ * Preload gadgets and tokens from container config.
+ * Support caching by providing server time to override respnse time usage
+ * @param {Object} config container configuration
+ * @private
+ */
+shindig.container.Container.prototype.preloadFromConfig_ = function(config) {
+  var gadgets = shindig.container.util.getSafeJsonValue(
+      config, shindig.container.ContainerConfig.PRELOAD_METADATAS, {});
+  var tokens = shindig.container.util.getSafeJsonValue(
+      config, shindig.container.ContainerConfig.PRELOAD_TOKENS, {});
+  var refTime = shindig.container.util.getSafeJsonValue(
+      config, shindig.container.ContainerConfig.PRELOAD_REF_TIME, null);
+
+  this.service_.addGadgetMetadatas(gadgets, refTime);
+  this.service_.addGadgetTokens(tokens, refTime);
+  this.addPreloadGadgets_(gadgets);
+}
+
 
 /**
  * Deletes stale cached data in service. The container knows what data are safe

Modified: shindig/trunk/features/src/main/javascript/features/container/service.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/service.js?rev=1056437&r1=1056436&r2=1056437&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/service.js (original)
+++ shindig/trunk/features/src/main/javascript/features/container/service.js Fri Jan  7 18:14:35 2011
@@ -112,14 +112,8 @@ shindig.container.Service.prototype.getG
       } else {
         var currentTimeMs = shindig.container.util.getCurrentTimeMs();
         for (var id in response) {
-          var resp = response[id]; 
-          resp[shindig.container.MetadataParam.URL] = id;
-          
-          // This ignores time to fetch metadata. Okay, expect to be < 2s.
-          resp[shindig.container.MetadataParam.LOCAL_EXPIRE_TIME]
-              = resp[shindig.container.MetadataResponse.EXPIRE_TIME_MS]
-              - resp[shindig.container.MetadataResponse.RESPONSE_TIME_MS]
-              + currentTimeMs;
+          var resp = response[id];
+          self.updateResponse_(resp, id, currentTimeMs);
           self.cachedMetadatas_[id] = resp;
           finalResponse[id] = resp;
         }
@@ -132,6 +126,63 @@ shindig.container.Service.prototype.getG
 
 
 /**
+ * Add preloaded gadgets to cache
+ * @param {Object} response hash of gadgets metadata
+ * @param {Object} data time to override responseTime (in order to support external caching)
+ */
+shindig.container.Service.prototype.addGadgetMetadatas = function(response, refTime) {
+  this.addToCache_(response, refTime, this.cachedMetadatas_);
+};
+
+
+/**
+ * Add preloaded tokens to cache
+ * @param {Object} response hash of gadgets metadata
+ * @param {Object} refTime data time to override responseTime (in order to support external caching)
+ */
+shindig.container.Service.prototype.addGadgetTokens = function(response, refTime) {
+  this.addToCache_(response, refTime, this.cachedTokens_);
+};
+
+
+/**
+ * Utility function to add data to cache
+ * @param {Object} response hash of gadgets metadata
+ * @param {Object} refTime data time to override responseTime (in order to support external caching)
+ * @param {Object} cache the cache to update
+ * @private
+ */
+shindig.container.Service.prototype.addToCache_ = function(response, refTime, cache) {
+  var currentTimeMs = shindig.container.util.getCurrentTimeMs();
+  for (var id in response) {
+    var resp = response[id];
+    this.updateResponse_(resp, id, currentTimeMs, refTime);
+    cache[id] = resp;
+  }
+};
+
+
+/**
+ * Update gadget data, set gadget id and calculate expiration time
+ * @param {Object} resp gadget metadata item
+ * @param {string} id gadget id
+ * @param {Object} opt_refTime data time to override responseTime (support external caching)
+ * @param {Object} currentTimeMs current time
+ * @private
+ */
+shindig.container.Service.prototype.updateResponse_ = function(
+    resp, id, currentTimeMs, opt_refTime) { 
+  resp[shindig.container.MetadataParam.URL] = id;
+  // This ignores time to fetch metadata. Okay, expect to be < 2s.
+  resp[shindig.container.MetadataParam.LOCAL_EXPIRE_TIME]
+      = resp[shindig.container.MetadataResponse.EXPIRE_TIME_MS]
+      - (opt_refTime == null ? 
+          resp[shindig.container.MetadataResponse.RESPONSE_TIME_MS] : opt_refTime)
+      + currentTimeMs;
+};
+
+
+/**
  * @param {Object} request JSON object representing the request.
  * @param {function(Object)=} opt_callback function to call upon data receive.
  */

Modified: shindig/trunk/features/src/test/javascript/features/container/container_test.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/container/container_test.js?rev=1056437&r1=1056436&r2=1056437&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/container/container_test.js (original)
+++ shindig/trunk/features/src/test/javascript/features/container/container_test.js Fri Jan  7 18:14:35 2011
@@ -71,6 +71,16 @@ ContainerTest.prototype.testUnloadGadget
   this.assertTrue('3', container.preloadedGadgetUrls_['preloaded3.xml'] != null);
 };
 
+ContainerTest.prototype.testPreloadConfigGadgets = function() {
+  this.setupGadgetsRpcRegister();
+  var container = new shindig.container.Container( 
+    { 'preloadMetadatas' : { 'preloaded1.xml' : {}}});
+  var test = null;
+  this.assertTrue('1', 'preloaded1.xml' in container.preloadedGadgetUrls_);
+  this.assertFalse('2', 'preloaded2.xml' in container.preloadedGadgetUrls_);
+};
+
+
 ContainerTest.prototype.testNavigateGadget = function() {
   this.setupGadgetsRpcRegister();
   var container = new shindig.container.Container({

Modified: shindig/trunk/features/src/test/javascript/features/container/service_test.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/container/service_test.js?rev=1056437&r1=1056436&r2=1056437&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/container/service_test.js (original)
+++ shindig/trunk/features/src/test/javascript/features/container/service_test.js Fri Jan  7 18:14:35 2011
@@ -149,3 +149,33 @@ ServiceTest.prototype.testUncacheStaleGa
   this.assertTrue(service.cachedMetadatas_['except2.xml'] != null);
 };
 
+ServiceTest.prototype.testUpdateResponse = function() {
+  var service = new shindig.container.Service();
+  this.setupUtilCurrentTimeMs(120);
+
+  var data = {responseTimeMs : 100, expireTimeMs : 105};
+  service.addGadgetMetadatas({'id' : data});
+  this.assertEquals("id", data.url);
+  this.assertEquals(125, data.localExpireTimeMs);
+  
+  data = {responseTimeMs : 100, expireTimeMs : 105};
+  service.addGadgetMetadatas({'id' : data}, 104);
+  this.assertEquals("id", data.url);
+  this.assertEquals(121, data.localExpireTimeMs);
+}
+
+ServiceTest.prototype.testAddToCache = function() {
+  var service = new shindig.container.Service();
+  this.setupUtilCurrentTimeMs(120);
+
+  var cache = {};
+  service.addToCache_(
+    { "id1": { responseTimeMs : 100, expireTimeMs : 105 },
+      "id2": { responseTimeMs : 100, expireTimeMs : 135 }},
+    103, cache);
+  
+  this.assertTrue(122, cache["id1"].localExpireTimeMs);
+  this.assertTrue(152, cache["id2"].localExpireTimeMs);
+}
+  
+