Skip to content

Commit d970dc4

Browse files
committed
Merge pull request #1826 from syranide/jsxasync
JSXTransformer respects async attribute
2 parents f479cff + 079400c commit d970dc4

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

vendor/browser-transforms.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function run(code, url, options) {
198198
* @param {function} callback Function to call with the content of url
199199
* @internal
200200
*/
201-
function load(url, callback) {
201+
function load(url, successCallback, errorCallback) {
202202
var xhr;
203203
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP')
204204
: new XMLHttpRequest();
@@ -212,8 +212,9 @@ function load(url, callback) {
212212
xhr.onreadystatechange = function() {
213213
if (xhr.readyState === 4) {
214214
if (xhr.status === 0 || xhr.status === 200) {
215-
callback(xhr.responseText, url);
215+
successCallback(xhr.responseText);
216216
} else {
217+
errorCallback();
217218
throw new Error("Could not load " + url);
218219
}
219220
}
@@ -230,21 +231,19 @@ function load(url, callback) {
230231
* @internal
231232
*/
232233
function loadScripts(scripts) {
233-
var result = scripts.map(function() {
234-
return false;
235-
});
236-
var count = result.length;
234+
var result = [];
235+
var count = scripts.length;
237236

238237
function check() {
239238
var script, i;
240239

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

244-
if (script && !script.executed) {
245-
run(script.content, script.url, script.options);
243+
if (script.loaded && !script.executed) {
246244
script.executed = true;
247-
} else if (!script) {
245+
run(script.content, script.url, script.options);
246+
} else if (!script.loaded && !script.error && !script.async) {
248247
break;
249248
}
250249
}
@@ -258,26 +257,42 @@ function loadScripts(scripts) {
258257
};
259258
}
260259

260+
// script.async is always true for non-javascript script tags
261+
var async = script.hasAttribute('async');
262+
261263
if (script.src) {
262-
load(script.src, function(content, url) {
263-
result[i] = {
264-
executed: false,
265-
content: content,
266-
url: url,
267-
options: options
268-
};
264+
result[i] = {
265+
async: async,
266+
error: false,
267+
executed: false,
268+
content: null,
269+
loaded: false,
270+
url: script.src,
271+
options: options
272+
};
273+
274+
load(script.src, function(content) {
275+
result[i].loaded = true;
276+
result[i].content = content;
277+
check();
278+
}, function() {
279+
result[i].error = true;
269280
check();
270281
});
271282
} else {
272283
result[i] = {
284+
async: async,
285+
error: false,
273286
executed: false,
274287
content: script.innerHTML,
288+
loaded: true,
275289
url: null,
276290
options: options
277291
};
278-
check();
279292
}
280293
});
294+
295+
check();
281296
}
282297

283298
/**

0 commit comments

Comments
 (0)