Browse Source

HUE-8330 [frontend] Add caching of namespaces

Johan Ahlen 7 years ago
parent
commit
9e178b2c56

+ 59 - 13
desktop/core/src/desktop/static/desktop/js/contextCatalog.js

@@ -22,7 +22,10 @@
 
 var ContextCatalog = (function () {
 
+  var STORAGE_POSTFIX = LOGGED_USERNAME;
   var CONTEXT_CATALOG_VERSION = 1;
+  var NAMESPACES_CONTEXT_TYPE = 'namespaces';
+  var COMPUTES_CONTEXT_TYPE = 'computes';
 
   var ContextCatalog = (function () {
 
@@ -33,10 +36,41 @@ var ContextCatalog = (function () {
 
       self.computes = {};
       self.computePromises = {};
-
-      // TODO: Add caching
     }
 
+    ContextCatalog.prototype.getStore = function () {
+      if (!self.store) {
+        self.store = localforage.createInstance({
+          name: 'HueContextCatalog_' + STORAGE_POSTFIX
+        });
+      }
+      return self.store;
+    };
+
+    ContextCatalog.prototype.saveLater = function (contextType, sourceType, entry) {
+      var self = this;
+      window.setTimeout(function () {
+        self.getStore().setItem(sourceType + '_' + contextType, { version: CONTEXT_CATALOG_VERSION, entry: entry });
+      }, 1000);
+    };
+
+    ContextCatalog.prototype.getSaved = function (contextType, sourceType) {
+      var self = this;
+      var deferred = $.Deferred();
+      self.getStore().getItem(sourceType + '_' + contextType).then(function (saved) {
+        if (saved && saved.version === CONTEXT_CATALOG_VERSION) {
+          deferred.resolve(saved.entry);
+        } else {
+          deferred.reject();
+        }
+      }).catch(function (error) {
+        console.warn(error);
+        deferred.reject();
+      });
+
+      return deferred.promise();
+    };
+
     /**
      * @param {Object} options
      * @param {string} options.sourceType
@@ -62,22 +96,34 @@ var ContextCatalog = (function () {
       }
 
       var deferred = $.Deferred();
+
       self.namespacePromises[options.sourceType] = deferred.promise();
 
-      ApiHelper.getInstance().fetchContextNamespaces(options).done(function (namespaces) {
-        if (namespaces[options.sourceType]) {
-          var namespaces = namespaces[options.sourceType];
-          if (namespaces) {
-            self.namespaces[self.sourceType] = namespaces;
-            deferred.resolve(self.namespaces[self.sourceType])
-            // TODO: save
+      var fetchNamespaces = function () {
+        ApiHelper.getInstance().fetchContextNamespaces(options).done(function (namespaces) {
+          if (namespaces[options.sourceType]) {
+            var namespaces = namespaces[options.sourceType];
+            if (namespaces) {
+              self.namespaces[self.sourceType] = namespaces;
+              deferred.resolve(self.namespaces[self.sourceType]);
+              self.saveLater(NAMESPACES_CONTEXT_TYPE, options.sourceType, namespaces);
+            } else {
+              deferred.reject();
+            }
           } else {
             deferred.reject();
           }
-        } else {
-          deferred.reject();
-        }
-      });
+        });
+      };
+
+      if (!options.clearCache) {
+        self.getSaved(NAMESPACES_CONTEXT_TYPE, options.sourceType).done(function (namespaces) {
+          self.namespaces[options.sourceType] = namespaces;
+          deferred.resolve(self.namespaces[options.sourceType]);
+        }).fail(fetchNamespaces);
+      } else {
+        fetchNamespaces();
+      }
 
       return self.namespacePromises[options.sourceType];
     };

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/dataCatalog.js

@@ -16,7 +16,7 @@
 
 var DataCatalog = (function () {
 
-  var STORAGE_POSTFIX = LOGGED_USERNAME; // TODO: Add flag for embedded mode
+  var STORAGE_POSTFIX = LOGGED_USERNAME;
 
   var DATA_CATALOG_VERSION = 4;