Commit 4dc97e6d authored by Romain Courteaud's avatar Romain Courteaud

Load gadget's JS and CSS in parallel.

Use document fragment to add all dependencies with one DOM call only.
parent e78ac402
...@@ -719,12 +719,6 @@ ...@@ -719,12 +719,6 @@
options.element = document.createElement("div"); options.element = document.createElement("div");
} }
function loadDependency(method, url) {
return function () {
return method(url);
};
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return renderJS.declareGadgetKlass(url); return renderJS.declareGadgetKlass(url);
...@@ -751,17 +745,19 @@ ...@@ -751,17 +745,19 @@
}) })
// Load all JS/CSS // Load all JS/CSS
.push(function (all_list) { .push(function (all_list) {
var q = new RSVP.Queue(), var fragment = document.createDocumentFragment(),
promise_list = [],
i; i;
// Load JS // Load JS
for (i = 0; i < all_list[0].length; i += 1) { for (i = 0; i < all_list[0].length; i += 1) {
q.push(loadDependency(renderJS.declareJS, all_list[0][i])); promise_list.push(renderJS.declareJS(all_list[0][i], fragment));
} }
// Load CSS // Load CSS
for (i = 0; i < all_list[1].length; i += 1) { for (i = 0; i < all_list[1].length; i += 1) {
q.push(loadDependency(renderJS.declareCSS, all_list[1][i])); promise_list.push(renderJS.declareCSS(all_list[1][i], fragment));
} }
return q; document.head.appendChild(fragment);
return RSVP.all(promise_list);
}) })
.push(function () { .push(function () {
return gadget_instance; return gadget_instance;
...@@ -1096,7 +1092,8 @@ ...@@ -1096,7 +1092,8 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// renderJS.declareJS // renderJS.declareJS
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
renderJS.declareJS = function (url) { renderJS.declareJS = function (url, container) {
// https://www.html5rocks.com/en/tutorials/speed/script-loading/
// Prevent infinite recursion if loading render.js // Prevent infinite recursion if loading render.js
// more than once // more than once
var result; var result;
...@@ -1106,8 +1103,8 @@ ...@@ -1106,8 +1103,8 @@
result = new RSVP.Promise(function (resolve, reject) { result = new RSVP.Promise(function (resolve, reject) {
var newScript; var newScript;
newScript = document.createElement('script'); newScript = document.createElement('script');
newScript.async = false;
newScript.type = 'text/javascript'; newScript.type = 'text/javascript';
newScript.src = url;
newScript.onload = function () { newScript.onload = function () {
javascript_registration_dict[url] = null; javascript_registration_dict[url] = null;
resolve(); resolve();
...@@ -1115,7 +1112,8 @@ ...@@ -1115,7 +1112,8 @@
newScript.onerror = function (e) { newScript.onerror = function (e) {
reject(e); reject(e);
}; };
document.head.appendChild(newScript); newScript.src = url;
container.appendChild(newScript);
}); });
} }
return result; return result;
...@@ -1124,7 +1122,7 @@ ...@@ -1124,7 +1122,7 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// renderJS.declareCSS // renderJS.declareCSS
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
renderJS.declareCSS = function (url) { renderJS.declareCSS = function (url, container) {
// https://github.com/furf/jquery-getCSS/blob/master/jquery.getCSS.js // https://github.com/furf/jquery-getCSS/blob/master/jquery.getCSS.js
// No way to cleanly check if a css has been loaded // No way to cleanly check if a css has been loaded
// So, always resolve the promise... // So, always resolve the promise...
...@@ -1146,7 +1144,7 @@ ...@@ -1146,7 +1144,7 @@
link.onerror = function (e) { link.onerror = function (e) {
reject(e); reject(e);
}; };
document.head.appendChild(link); container.appendChild(link);
}); });
} }
return result; return result;
......
...@@ -621,7 +621,7 @@ ...@@ -621,7 +621,7 @@
var url = 'http://0.0.0.0/bar'; var url = 'http://0.0.0.0/bar';
stop(); stop();
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function () { .then(function () {
ok(false, "404 should fail"); ok(false, "404 should fail");
}) })
...@@ -639,7 +639,7 @@ ...@@ -639,7 +639,7 @@
var url = 'http://0.0.0.0/bar2'; var url = 'http://0.0.0.0/bar2';
stop(); stop();
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function () { .then(function () {
return renderJS.declareJS(url); return renderJS.declareJS(url);
}) })
...@@ -663,7 +663,7 @@ ...@@ -663,7 +663,7 @@
stop(); stop();
window.onerror = undefined; window.onerror = undefined;
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function (value, textStatus, jqXHR) { .then(function (value, textStatus, jqXHR) {
ok(ok, "Non JS mime type should load"); ok(ok, "Non JS mime type should load");
}) })
...@@ -683,7 +683,7 @@ ...@@ -683,7 +683,7 @@
"= 'JS fetched and loaded';"); "= 'JS fetched and loaded';");
stop(); stop();
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function () { .then(function () {
equal( equal(
document.getElementById("qunit-fixture").textContent, document.getElementById("qunit-fixture").textContent,
...@@ -706,7 +706,7 @@ ...@@ -706,7 +706,7 @@
stop(); stop();
window.onerror = undefined; window.onerror = undefined;
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function (aaa) { .then(function (aaa) {
ok(true, "JS with error cleanly loaded"); ok(true, "JS with error cleanly loaded");
}) })
...@@ -726,14 +726,14 @@ ...@@ -726,14 +726,14 @@
"= 'JS not fetched twice';"); "= 'JS not fetched twice';");
stop(); stop();
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function () { .then(function () {
equal( equal(
document.getElementById("qunit-fixture").textContent, document.getElementById("qunit-fixture").textContent,
"JS not fetched twice" "JS not fetched twice"
); );
document.getElementById("qunit-fixture").textContent = ""; document.getElementById("qunit-fixture").textContent = "";
return renderJS.declareJS(url); return renderJS.declareJS(url, document.head);
}) })
.then(function () { .then(function () {
equal(document.getElementById("qunit-fixture").textContent, ""); equal(document.getElementById("qunit-fixture").textContent, "");
...@@ -760,7 +760,7 @@ ...@@ -760,7 +760,7 @@
var url = 'foo//://bar'; var url = 'foo//://bar';
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function () { .then(function () {
// IE accept the css // IE accept the css
ok(true, "404 should fail"); ok(true, "404 should fail");
...@@ -780,7 +780,7 @@ ...@@ -780,7 +780,7 @@
window.btoa("= = ="); window.btoa("= = =");
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function (value, textStatus, jqXHR) { .then(function (value, textStatus, jqXHR) {
// Chrome accept the css // Chrome accept the css
ok(true, "Non CSS mime type should load"); ok(true, "Non CSS mime type should load");
...@@ -800,7 +800,7 @@ ...@@ -800,7 +800,7 @@
window.btoa("#qunit-fixture {background-color: red;}"); window.btoa("#qunit-fixture {background-color: red;}");
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function () { .then(function () {
var result = document.querySelectorAll("link[href='" + url + "']"); var result = document.querySelectorAll("link[href='" + url + "']");
ok(result.length > 0, "CSS in the head"); ok(result.length > 0, "CSS in the head");
...@@ -827,7 +827,7 @@ ...@@ -827,7 +827,7 @@
window.btoa("throw new Error('foo');"); window.btoa("throw new Error('foo');");
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function () { .then(function () {
// Chrome does not consider this as error // Chrome does not consider this as error
ok(true, "CSS with error cleanly loaded"); ok(true, "CSS with error cleanly loaded");
...@@ -846,7 +846,7 @@ ...@@ -846,7 +846,7 @@
window.btoa("#qunit-fixture {background-color: blue;}"); window.btoa("#qunit-fixture {background-color: blue;}");
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function () { .then(function () {
equal( equal(
window.getComputedStyle( window.getComputedStyle(
...@@ -864,7 +864,7 @@ ...@@ -864,7 +864,7 @@
).backgroundColor !== "rgb(0, 0, 255)" ).backgroundColor !== "rgb(0, 0, 255)"
); );
return renderJS.declareCSS(url); return renderJS.declareCSS(url, document.head);
}) })
.then(function () { .then(function () {
var element_list = var element_list =
...@@ -948,7 +948,7 @@ ...@@ -948,7 +948,7 @@
"= 'JS not fetched twice';"); "= 'JS not fetched twice';");
stop(); stop();
renderJS.declareJS(url) renderJS.declareJS(url, document.head)
.then(function () { .then(function () {
renderJS.clearGadgetKlassList(); renderJS.clearGadgetKlassList();
equal( equal(
...@@ -956,7 +956,7 @@ ...@@ -956,7 +956,7 @@
"JS not fetched twice" "JS not fetched twice"
); );
document.getElementById("qunit-fixture").textContent = ""; document.getElementById("qunit-fixture").textContent = "";
return renderJS.declareJS(url); return renderJS.declareJS(url, document.head);
}) })
.then(function () { .then(function () {
equal( equal(
...@@ -980,14 +980,14 @@ ...@@ -980,14 +980,14 @@
count = document.querySelectorAll("link[rel=stylesheet]").length; count = document.querySelectorAll("link[rel=stylesheet]").length;
stop(); stop();
renderJS.declareCSS(url) renderJS.declareCSS(url, document.head)
.then(function () { .then(function () {
renderJS.clearGadgetKlassList(); renderJS.clearGadgetKlassList();
equal( equal(
document.querySelectorAll("link[rel=stylesheet]").length, document.querySelectorAll("link[rel=stylesheet]").length,
count + 1 count + 1
); );
return renderJS.declareCSS(url); return renderJS.declareCSS(url, document.head);
}) })
.then(function () { .then(function () {
equal( equal(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment