Skip to content

Commit f230e0a

Browse files
committed
Merge pull request facebook#1583 from nhunzaker/jsxtransform-parallel
Load external scripts in parallel in JSXTransformer
2 parents bca1f0e + 59a9251 commit f230e0a

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

vendor/browser-transforms.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
'use strict';
2020
var runScripts;
21+
var loadScripts;
2122
var headEl;
2223

2324
var buffer = require('buffer');
@@ -157,18 +158,49 @@ var load = exports.load = function(url, callback) {
157158
xhr.onreadystatechange = function() {
158159
if (xhr.readyState === 4) {
159160
if (xhr.status === 0 || xhr.status === 200) {
160-
run(xhr.responseText, url);
161+
callback(xhr.responseText, url);
161162
} else {
162163
throw new Error("Could not load " + url);
163164
}
164-
if (callback) {
165-
return callback();
166-
}
167165
}
168166
};
169167
return xhr.send(null);
170168
};
171169

170+
loadScripts = function(scripts) {
171+
var result = scripts.map(function() {
172+
return false;
173+
});
174+
var count = result.length;
175+
176+
var check = function() {
177+
var script, i;
178+
179+
for (i = 0; i < count; i++) {
180+
script = result[i];
181+
182+
if (script && !script.executed) {
183+
run(script.content, script.url);
184+
script.executed = true;
185+
} else if (!script) {
186+
break;
187+
}
188+
};
189+
};
190+
191+
scripts.forEach(function(script, i) {
192+
if (script.src) {
193+
load(script.src, function(content, url) {
194+
result[i] = { executed: false, content: content, url: url };
195+
check();
196+
});
197+
} else {
198+
result[i] = { executed: false, content: script.innerHTML, url: null };
199+
check();
200+
}
201+
});
202+
};
203+
172204
runScripts = function() {
173205
var scripts = document.getElementsByTagName('script');
174206

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

183215
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");
184216

185-
function tick() {
186-
if (!jsxScripts.length) return;
187-
188-
var script = jsxScripts.shift();
189-
190-
if (script.src) {
191-
load(script.src, tick);
192-
} else {
193-
run(script.innerHTML, null);
194-
tick();
195-
}
196-
}
197-
198-
tick();
217+
loadScripts(jsxScripts);
199218
};
200219

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

0 commit comments

Comments
 (0)