-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This array is to keep track of the loaded libraries
window.__loadedLibraries = window.__loadedLibraries || []
/**
* Record the libs (id) only if the array doesn't contain the same already
*
* @param {String} id of the script DOM element
*/
function registerLibraryLoaded(id) {
if (window.__loadedLibraries.indexOf(id) < 0) {
window.__loadedLibraries.push(id)
}
}
/**
* @param {Object} with 'id' and 'src' as the html id and source
* @return {Promise} is resolved in multiple scenarios
*
* @scenario 1: load one external script
* @scenario 2: attempt to load one external script multiple times
* without the first attempt completed so the second call will
* get a listener and when is loaded first and second will be completed.
* @scenario 3: attempt load the same external script after is completed.
*/
function loadScript({ src, id, data }) {
const script = document.createElement('script')
script.id = id
script.src = src
script.setAttribute(`data-${data ? data : 'vendor'}`, id)
return new Promise((resolve, reject) => {
// once the lib is registered you can resolve immediatelly
// because it means that is fully loaded
if (window.__loadedLibraries.indexOf(src) > -1) {
resolve(`${id} was loaded before`)
}
script.addEventListener('load', function onLoadScript() {
script.removeEventListener('load', onLoadScript)
registerLibraryLoaded(src)
resolve(id)
})
script.onerror = function onErrorLoadingScript() {
// Remove the element from the body in case of error
// to give the possibility to try again later
// calling the same function
// document.body.removeChild(script)
reject(`error loading ${src}`)
}
document.body.appendChild(script)
})
}
export default loadScript