Преглед изворни кода

[assist] Introduce a fetch queue for requests to the same URL with same parameters

Multiple calls that are considered to be equal are queued up and just one ajax request is made for the entire queue. This is to prevent unnecessary calls to the API.
Johan Ahlen пре 9 година
родитељ
комит
171fdb1069
1 измењених фајлова са 37 додато и 6 уклоњено
  1. 37 6
      desktop/core/src/desktop/static/desktop/js/assist/assistHelper.js

+ 37 - 6
desktop/core/src/desktop/static/desktop/js/assist/assistHelper.js

@@ -43,6 +43,7 @@
     self.i18n = i18n;
     self.user = user;
     self.lastKnownDatabases = {};
+    self.fetchQueue = {};
   }
 
   AssistHelper.prototype.hasExpired = function (timestamp) {
@@ -196,30 +197,60 @@
     }));
   };
 
+  AssistHelper.prototype.getFetchQueue = function (fetchFunction, identifier) {
+    var self = this;
+    if (! self.fetchQueue[fetchFunction]) {
+      self.fetchQueue[fetchFunction] = {};
+    }
+    var queueForFunction = self.fetchQueue[fetchFunction];
+
+    var id = typeof identifier === 'undefined' || identifier === null ? 'DEFAULT_QUEUE' : identifier;
+
+    if (! queueForFunction[id]) {
+      queueForFunction[id] = [];
+    }
+    return queueForFunction[id];
+  };
+
   /**
    * @param {Object} options
    * @param {Function} options.successCallback
    * @param {Function} [options.errorCallback]
    * @param {boolean} [options.silenceErrors]
    *
-   * @param {string} [options.path]
+   * @param {string} [options.uuid]
    */
   AssistHelper.prototype.fetchDocuments = function (options) {
     var self = this;
+
+    var queue = self.getFetchQueue('fetchDocuments', options.uuid);
+    queue.push(options);
+    if (queue.length > 1) {
+      return;
+    }
+
     $.ajax({
       url: DOCUMENTS_API,
       data: {
         uuid: options.uuid
       },
       success: function (data) {
-        if (! self.successResponseIsError(data)) {
-          options.successCallback(data);
-        } else {
-          self.assistErrorCallback(options)(data);
+        while (queue.length > 0) {
+          var next = queue.shift();
+          if (! self.successResponseIsError(data)) {
+            next.successCallback(data);
+          } else {
+            self.assistErrorCallback(next)(data);
+          }
         }
       }
     })
-    .fail(self.assistErrorCallback(options));
+    .fail(function () {
+      while (queue.length > 0) {
+        var next = queue.shift();
+        self.assistErrorCallback(next);
+      }
+    });
   };
 
   /**