Skip to content

Commit c016f81

Browse files
authored
Merge pull request #348 from jsperezg/fix_loading_issues
Fix uncatched exception when loading google maps api.
2 parents 2b27a1c + ffb4a18 commit c016f81

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/lib/ScriptCache.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const window = require('./windowOrGlobal');
55
export const ScriptCache = (function(global) {
66
global._scriptMap = global._scriptMap || scriptMap;
77
return function ScriptCache(scripts) {
8-
const Cache = {}
8+
const Cache = {};
99

1010
Cache._onLoad = function(key) {
1111
return (cb) => {
@@ -24,14 +24,14 @@ export const ScriptCache = (function(global) {
2424
}
2525

2626
return stored;
27-
});
27+
}).catch(error => cb(error));
2828
} else {
2929
// TODO:
3030
}
3131

3232
return unregister;
3333
}
34-
}
34+
};
3535

3636
Cache._scriptTag = (key, src) => {
3737
if (!scriptMap.has(key)) {
@@ -41,9 +41,7 @@ export const ScriptCache = (function(global) {
4141

4242
let tag = document.createElement('script');
4343
let promise = new Promise((resolve, reject) => {
44-
let resolved = false,
45-
errored = false,
46-
body = document.getElementsByTagName('body')[0];
44+
let body = document.getElementsByTagName('body')[0];
4745

4846
tag.type = 'text/javascript';
4947
tag.async = false; // Load in order
@@ -69,24 +67,24 @@ export const ScriptCache = (function(global) {
6967

7068
cleanup();
7169
}
72-
}
70+
};
7371

7472
const cleanup = () => {
7573
if (global[cbName] && typeof global[cbName] === 'function') {
7674
global[cbName] = null;
7775
delete global[cbName]
7876
}
79-
}
77+
};
8078

8179
tag.onload = handleResult('loaded');
82-
tag.onerror = handleResult('error')
80+
tag.onerror = handleResult('error');
8381
tag.onreadystatechange = () => {
8482
handleResult(tag.readyState)
85-
}
83+
};
8684

8785
// Pick off callback, if there is one
8886
if (src.match(/callback=CALLBACK_NAME/)) {
89-
src = src.replace(/(callback=)[^\&]+/, `$1${cbName}`)
87+
src = src.replace(/(callback=)[^\&]+/, `$1${cbName}`);
9088
cb = window[cbName] = tag.onload;
9189
} else {
9290
tag.addEventListener('load', tag.onload)
@@ -101,13 +99,13 @@ export const ScriptCache = (function(global) {
10199
let initialState = {
102100
loaded: false,
103101
error: false,
104-
promise: promise,
102+
promise,
105103
tag
106-
}
104+
};
107105
scriptMap.set(key, initialState);
108106
}
109-
return scriptMap.get(key);
110-
}
107+
return scriptMap.get(key).tag;
108+
};
111109

112110
// let scriptTags = document.querySelectorAll('script')
113111
//
@@ -132,7 +130,7 @@ export const ScriptCache = (function(global) {
132130
tag: tag,
133131
onLoad: Cache._onLoad(key),
134132
}
135-
})
133+
});
136134

137135
return Cache;
138136
}

0 commit comments

Comments
 (0)