Merge pull request #3012 from imcotton/parallel-loading

Script loading parallelized in browser
This commit is contained in:
Jeremy Ashkenas 2013-10-20 14:10:40 -07:00
commit db87d817e8
2 changed files with 63 additions and 36 deletions

View File

@ -45,11 +45,14 @@
};
}
CoffeeScript.load = function(url, callback, options) {
CoffeeScript.load = function(url, callback, options, hold) {
var xhr;
if (options == null) {
options = {};
}
if (hold == null) {
hold = false;
}
options.sourceFiles = [url];
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new window.XMLHttpRequest();
xhr.open('GET', url, true);
@ -57,15 +60,18 @@
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
var _ref;
var param, _ref;
if (xhr.readyState === 4) {
if ((_ref = xhr.status) === 0 || _ref === 200) {
CoffeeScript.run(xhr.responseText, options);
param = [xhr.responseText, options];
if (!hold) {
CoffeeScript.run.apply(CoffeeScript, param);
}
} else {
throw new Error("Could not load " + url);
}
if (callback) {
return callback();
return callback(param);
}
}
};
@ -73,7 +79,7 @@
};
runScripts = function() {
var coffees, coffeetypes, execute, index, length, s, scripts;
var coffees, coffeetypes, execute, i, index, s, script, scripts, _fn, _i, _len;
scripts = window.document.getElementsByTagName('script');
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'];
coffees = (function() {
@ -88,25 +94,35 @@
return _results;
})();
index = 0;
length = coffees.length;
(execute = function() {
var mediatype, options, script;
script = coffees[index++];
mediatype = script != null ? script.type : void 0;
if (__indexOf.call(coffeetypes, mediatype) >= 0) {
options = {
literate: mediatype === 'text/literate-coffeescript'
};
if (script.src) {
return CoffeeScript.load(script.src, execute, options);
} else {
options.sourceFiles = ['embedded'];
CoffeeScript.run(script.innerHTML, options);
return execute();
}
execute = function() {
var param;
param = coffees[index];
if (param instanceof Array) {
CoffeeScript.run.apply(CoffeeScript, param);
index++;
return execute();
}
})();
return null;
};
_fn = function(script, i) {
var options;
options = {
literate: script.type === coffeetypes[1]
};
if (script.src) {
return CoffeeScript.load(script.src, function(param) {
coffees[i] = param;
return execute();
}, options, true);
} else {
options.sourceFiles = ['embedded'];
return coffees[i] = [script.innerHTML, options];
}
};
for (i = _i = 0, _len = coffees.length; _i < _len; i = ++_i) {
script = coffees[i];
_fn(script, i);
}
return execute();
};
if (window.addEventListener) {

View File

@ -32,7 +32,7 @@ if btoa? and JSON? and unescape? and encodeURIComponent?
"#{js}\n//# sourceMappingURL=data:application/json;base64,#{btoa unescape encodeURIComponent v3SourceMap}\n//# sourceURL=coffeescript"
# Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, callback, options = {}) ->
CoffeeScript.load = (url, callback, options = {}, hold = false) ->
options.sourceFiles = [url]
xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP')
@ -43,10 +43,11 @@ CoffeeScript.load = (url, callback, options = {}) ->
xhr.onreadystatechange = ->
if xhr.readyState is 4
if xhr.status in [0, 200]
CoffeeScript.run xhr.responseText, options
param = [xhr.responseText, options]
CoffeeScript.run param... unless hold
else
throw new Error "Could not load #{url}"
callback() if callback
callback param if callback
xhr.send null
# Activate CoffeeScript in the browser by having it compile and evaluate
@ -57,19 +58,29 @@ runScripts = ->
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
coffees = (s for s in scripts when s.type in coffeetypes)
index = 0
length = coffees.length
do execute = ->
script = coffees[index++]
mediatype = script?.type
if mediatype in coffeetypes
options = {literate: mediatype is 'text/literate-coffeescript'}
execute = ->
param = coffees[index]
if param instanceof Array
CoffeeScript.run param...
index++
execute()
for script, i in coffees
do (script, i) ->
options = literate: script.type is coffeetypes[1]
if script.src
CoffeeScript.load script.src, execute, options
CoffeeScript.load script.src,
(param) ->
coffees[i] = param
execute()
options
true
else
options.sourceFiles = ['embedded']
CoffeeScript.run script.innerHTML, options
execute()
null
coffees[i] = [script.innerHTML, options]
execute()
# Listen for window load, both in decent browsers and in IE.
if window.addEventListener