Skip to content

Commit

Permalink
Merge pull request facebook#1583 from nhunzaker/jsxtransform-parallel
Browse files Browse the repository at this point in the history
Load external scripts in parallel in JSXTransformer
  • Loading branch information
zpao committed May 27, 2014
2 parents bca1f0e + 59a9251 commit f230e0a
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions vendor/browser-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

'use strict';
var runScripts;
var loadScripts;
var headEl;

var buffer = require('buffer');
Expand Down Expand Up @@ -157,18 +158,49 @@ var load = exports.load = function(url, callback) {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 0 || xhr.status === 200) {
run(xhr.responseText, url);
callback(xhr.responseText, url);
} else {
throw new Error("Could not load " + url);
}
if (callback) {
return callback();
}
}
};
return xhr.send(null);
};

loadScripts = function(scripts) {
var result = scripts.map(function() {
return false;
});
var count = result.length;

var check = function() {
var script, i;

for (i = 0; i < count; i++) {
script = result[i];

if (script && !script.executed) {
run(script.content, script.url);
script.executed = true;
} else if (!script) {
break;
}
};
};

scripts.forEach(function(script, i) {
if (script.src) {
load(script.src, function(content, url) {
result[i] = { executed: false, content: content, url: url };
check();
});
} else {
result[i] = { executed: false, content: script.innerHTML, url: null };
check();
}
});
};

runScripts = function() {
var scripts = document.getElementsByTagName('script');

Expand All @@ -182,20 +214,7 @@ runScripts = function() {

console.warn("You are using the in-browser JSX transformer. Be sure to precompile your JSX for production - http://facebook.github.io/react/docs/tooling-integration.html#jsx");

function tick() {
if (!jsxScripts.length) return;

var script = jsxScripts.shift();

if (script.src) {
load(script.src, tick);
} else {
run(script.innerHTML, null);
tick();
}
}

tick();
loadScripts(jsxScripts);
};

if (typeof window !== "undefined" && window !== null) {
Expand Down

0 comments on commit f230e0a

Please sign in to comment.