Skip to content

Commit 8c393dc

Browse files
committed
fixed double file not getting called back
closes #64
1 parent f4bf28a commit 8c393dc

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

dist/script.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,26 @@
5151
}
5252
}
5353
setTimeout(function () {
54-
each(paths, function (path) {
54+
each(paths, function loading(path, force) {
5555
if (path === null) return callback()
56+
path = !force && !/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path
5657
if (scripts[path]) {
57-
id && (ids[id] = 1)
58-
return scripts[path] == 2 && callback()
58+
if (id) ids[id] = 1
59+
if (scripts[path] == 2) callback()
60+
else setTimeout(loading.bind(null, path, true), 0)
61+
return;
5962
}
63+
6064
scripts[path] = 1
61-
id && (ids[id] = 1)
62-
create(!/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path, callback)
65+
if (id) ids[id] = 1
66+
create(path, callback)
6367
})
6468
}, 0)
6569
return $script
6670
}
6771

6872
function create(path, fn) {
69-
var el = doc.createElement('script')
70-
, loaded = f
73+
var el = doc.createElement('script'), loaded
7174
el.onload = el.onerror = el[onreadystatechange] = function () {
7275
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
7376
el.onload = el[onreadystatechange] = null

dist/script.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/script.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,26 @@
4545
}
4646
}
4747
setTimeout(function () {
48-
each(paths, function (path) {
48+
each(paths, function loading(path, force) {
4949
if (path === null) return callback()
50+
path = !force && !/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path
5051
if (scripts[path]) {
51-
id && (ids[id] = 1)
52-
return scripts[path] == 2 && callback()
52+
if (id) ids[id] = 1
53+
if (scripts[path] == 2) callback()
54+
else setTimeout(loading.bind(null, path, true), 0)
55+
return;
5356
}
57+
5458
scripts[path] = 1
55-
id && (ids[id] = 1)
56-
create(!/^https?:\/\//.test(path) && scriptpath ? scriptpath + path + '.js' : path, callback)
59+
if (id) ids[id] = 1
60+
create(path, callback)
5761
})
5862
}, 0)
5963
return $script
6064
}
6165

6266
function create(path, fn) {
63-
var el = doc.createElement('script')
64-
, loaded = f
67+
var el = doc.createElement('script'), loaded
6568
el.onload = el.onerror = el[onreadystatechange] = function () {
6669
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
6770
el.onload = el[onreadystatechange] = null

tests/tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ script('../node_modules/domready/ready.js', function () {
111111
script.done('b')
112112
})
113113

114+
test('double loaded files', function (done) {
115+
var count = 0
116+
function load () {
117+
if (++count == 2) {
118+
ok(true, 'loaded callbacks twice')
119+
done()
120+
}
121+
}
122+
script('double-load', load)
123+
script('double-load', load)
124+
})
125+
114126
})
115127
start()
116128
})

vendor/double-load.js

Whitespace-only changes.

0 commit comments

Comments
 (0)