Skip to content

Commit

Permalink
load all scripts in order
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jul 1, 2019
1 parent fcee45f commit 21120f6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 79 deletions.
75 changes: 36 additions & 39 deletions packages/moon-browser/dist/moon-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,6 @@
return prelude + output;
}

/**
* Async script sources
*/

var scriptsAsync = [];
/**
* Head element
*/
Expand All @@ -1197,52 +1192,54 @@
* Script elements
*/

var scripts;
var scripts = [];
/**
* Load async scripts in the order they appear.
* Load scripts in the order they appear.
*/

function load() {
if (scriptsAsync.length !== 0) {
var xhr = new XMLHttpRequest();
var src = scriptsAsync.shift();
xhr.addEventListener("load", function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 0 || xhr.status === 200) {
var scriptNew = document.createElement("script");
scriptNew.text = compile(this.responseText);
head.appendChild(scriptNew);
} else {
error("Failed to load script with source \"" + src + "\" and status " + xhr.status + ".");
}
if (scripts.length !== 0) {
var script = scripts.shift();
var src = script.src;

if (src.length === 0) {
var scriptNew = document.createElement("script");
scriptNew.text = compile(script.text);
head.appendChild(scriptNew);
script.parentNode.removeChild(script);
load();
} else {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 0 || xhr.status === 200) {
var _scriptNew = document.createElement("script");

_scriptNew.text = compile(this.responseText);
head.appendChild(_scriptNew);
} else {
error("Failed to load script with source \"" + src + "\" and status " + xhr.status + ".");
}

load();
}
});
xhr.open("GET", src, true);
xhr.send();
script.parentNode.removeChild(script);
load();
}
});
xhr.open("GET", src, true);
xhr.send();
}
}
}

document.addEventListener("DOMContentLoaded", function () {
var scriptElements = document.querySelectorAll("script");
head = document.querySelector("head");
scripts = document.querySelectorAll("script");

for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];

if (script.type === "text/moon") {
var src = script.src;
for (var i = 0; i < scriptElements.length; i++) {
var scriptElement = scriptElements[i];

if (src.length === 0) {
var scriptNew = document.createElement("script");
scriptNew.text = compile(script.text);
head.appendChild(scriptNew);
} else {
scriptsAsync.push(src);
}

script.parentNode.removeChild(script);
if (scriptElement.type === "text/moon") {
scripts.push(scriptElement);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/moon-browser/dist/moon-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 21120f6

Please sign in to comment.