Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d8902

Browse files
committed
parallelized script loading in browser, yet order remain
1 parent 0c9f0fd commit 46d8902

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

src/browser.coffee

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if btoa? and JSON? and unescape? and encodeURIComponent?
3232
"#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa unescape encodeURIComponent v3SourceMap}\n//@ sourceURL=coffeescript"
3333

3434
# Load a remote script from the current domain via XHR.
35-
CoffeeScript.load = (url, callback, options = {}) ->
35+
CoffeeScript.load = (url, callback = (->), options = {}, hold = false) ->
3636
options.sourceFiles = [url]
3737
xhr = if window.ActiveXObject
3838
new window.ActiveXObject('Microsoft.XMLHTTP')
@@ -43,10 +43,13 @@ CoffeeScript.load = (url, callback, options = {}) ->
4343
xhr.onreadystatechange = ->
4444
if xhr.readyState is 4
4545
if xhr.status in [0, 200]
46-
CoffeeScript.run xhr.responseText, options
46+
run = ->
47+
CoffeeScript.run xhr.responseText, options
48+
null
4749
else
4850
throw new Error "Could not load #{url}"
49-
callback() if callback
51+
run() unless hold
52+
callback run
5053
xhr.send null
5154

5255
# Activate CoffeeScript in the browser by having it compile and evaluate
@@ -56,19 +59,32 @@ runScripts = ->
5659
scripts = window.document.getElementsByTagName 'script'
5760
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
5861
coffees = (s for s in scripts when s.type in coffeetypes)
59-
index = 0
60-
length = coffees.length
61-
do execute = ->
62-
script = coffees[index++]
63-
mediatype = script?.type
64-
if mediatype in coffeetypes
65-
options = {literate: mediatype is 'text/literate-coffeescript'}
62+
63+
check = ->
64+
for run, index in coffees
65+
continue unless run
66+
return unless run instanceof Function
67+
coffees[index] = run()
68+
69+
for script, index in coffees
70+
do (script, index) ->
71+
mediatype = script?.type
72+
return unless mediatype in coffeetypes
73+
options = {literate: mediatype is coffeetypes[1]}
6674
if script.src
67-
CoffeeScript.load script.src, execute, options
75+
CoffeeScript.load script.src,
76+
(run) ->
77+
coffees[index] = run
78+
check()
79+
options
80+
true
6881
else
6982
options.sourceFiles = ['embedded']
70-
CoffeeScript.run script.innerHTML, options
71-
execute()
83+
coffees[index] = ->
84+
CoffeeScript.run script.innerHTML, options
85+
null
86+
87+
check()
7288
null
7389

7490
# Listen for window load, both in decent browsers and in IE.

0 commit comments

Comments
 (0)