Wrap entire error loading script in anonymous function to avoid leaks

This commit is contained in:
Jérémie Astori 2017-12-16 18:14:58 -05:00
parent 0ffd4d60d9
commit 84db8d8866
No known key found for this signature in database
GPG key ID: B9A4F245CD67BDE8

View file

@ -8,54 +8,56 @@
* so that the timeout can be triggered while slow JS is loading * so that the timeout can be triggered while slow JS is loading
*/ */
function displayReload() { (function() {
var loadingReload = document.getElementById("loading-reload"); var displayReload = function displayReload() {
if (loadingReload) { var loadingReload = document.getElementById("loading-reload");
loadingReload.style.display = "block"; if (loadingReload) {
} loadingReload.style.display = "block";
} }
};
var loadingSlowTimeout = setTimeout(function() { var loadingSlowTimeout = setTimeout(function() {
var loadingSlow = document.getElementById("loading-slow"); var loadingSlow = document.getElementById("loading-slow");
// The parent element, #loading, is being removed when the app is loaded. // The parent element, #loading, is being removed when the app is loaded.
// Since the timer is not cancelled, `loadingSlow` can be not found after // Since the timer is not cancelled, `loadingSlow` can be not found after
// 5s. Wrap everything in this block to make sure nothing happens if the // 5s. Wrap everything in this block to make sure nothing happens if the
// element does not exist (i.e. page has loaded). // element does not exist (i.e. page has loaded).
if (loadingSlow) { if (loadingSlow) {
loadingSlow.style.display = "block"; loadingSlow.style.display = "block";
displayReload();
}
}, 5000);
document.getElementById("loading-reload").addEventListener("click", function() {
location.reload();
});
window.g_LoungeErrorHandler = function LoungeErrorHandler(e) {
var title = document.getElementById("loading-title");
title.textContent = "An error has occured";
var message = document.getElementById("loading-page-message");
message.textContent = "An error has occured that prevented the client from loading correctly.";
var summary = document.createElement("summary");
summary.textContent = "More details";
var data = document.createElement("pre");
data.textContent = e.message; // e is an ErrorEvent
var info = document.createElement("p");
info.textContent = "Open the developer tools of your browser for more information.";
var details = document.createElement("details");
details.appendChild(summary);
details.appendChild(data);
details.appendChild(info);
message.parentNode.insertBefore(details, message.nextSibling);
window.clearTimeout(loadingSlowTimeout);
displayReload(); displayReload();
} };
}, 5000);
document.getElementById("loading-reload").addEventListener("click", function() { window.addEventListener("error", window.g_LoungeErrorHandler);
location.reload(); })();
});
window.g_LoungeErrorHandler = function LoungeErrorHandler(e) {
var title = document.getElementById("loading-title");
title.textContent = "An error has occured";
var message = document.getElementById("loading-page-message");
message.textContent = "An error has occured that prevented the client from loading correctly.";
var summary = document.createElement("summary");
summary.textContent = "More details";
var data = document.createElement("pre");
data.textContent = e.message; // e is an ErrorEvent
var info = document.createElement("p");
info.textContent = "Open the developer tools of your browser for more information.";
var details = document.createElement("details");
details.appendChild(summary);
details.appendChild(data);
details.appendChild(info);
message.parentNode.insertBefore(details, message.nextSibling);
window.clearTimeout(loadingSlowTimeout);
displayReload();
};
window.addEventListener("error", window.g_LoungeErrorHandler);