Bläddra i källkod

HUE-8458 [frontend] Evaluate the js resources while others are being fetched

Instead of waiting for all the get requests to finish it will now wait for them in order and evaluate while others are being loaded.
Johan Ahlen 7 år sedan
förälder
incheckning
e2b3ada
1 ändrade filer med 22 tillägg och 11 borttagningar
  1. 22 11
      desktop/core/src/desktop/templates/hue.mako

+ 22 - 11
desktop/core/src/desktop/templates/hue.mako

@@ -786,14 +786,14 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
             if (loadedJs.indexOf(scriptUrl) !== -1) {
               continue;
             }
-            var deferred = $.Deferred();
-            promises.push(deferred.promise());
-            deferred.always(function () {
+            var loadDeferred = $.Deferred();
+            promises.push(loadDeferred.promise());
+            loadDeferred.always(function () {
               loadedJs.push(scriptUrl);
             });
-            $.get(scriptUrl).done(deferred.resolve).fail(function () {
-              // Ignore failed ones, they appear in the dev tools
-              deferred.resolve('');
+            $.get(scriptUrl).done(loadDeferred.resolve).fail(function () {
+              // Ignore failed ones, they'll still appear as failed in the network tab as before
+              loadDeferred.resolve('');
             });
           }
           return promises;
@@ -850,12 +850,23 @@ ${ smart_unicode(login_modal(request).content) | n,unicode }
 
           $rawHtml.unwrap('<span>');
 
-          $.when.apply($, scriptPromises).done(function () {
-            for (var i = 0; i < arguments.length; i++) {
-              eval(arguments[i]);
+          var evalScriptSync = function () {
+            if (scriptPromises.length) {
+              // Evaluate the scripts in the order they were defined in the page
+              var nextScriptPromise = scriptPromises.shift();
+              nextScriptPromise.done(function (contents) {
+                if (contents) {
+                  eval(contents);
+                }
+                evalScriptSync();
+              });
+            } else {
+              // All evaluated
+              promise.resolve($rawHtml);
             }
-            promise.resolve($rawHtml);
-          });
+          };
+
+          evalScriptSync();
           return promise;
         };