@@ -5,7 +5,7 @@ const window = require('./windowOrGlobal');
5
5
export const ScriptCache = ( function ( global ) {
6
6
global . _scriptMap = global . _scriptMap || scriptMap ;
7
7
return function ScriptCache ( scripts ) {
8
- const Cache = { }
8
+ const Cache = { } ;
9
9
10
10
Cache . _onLoad = function ( key ) {
11
11
return ( cb ) => {
@@ -24,14 +24,14 @@ export const ScriptCache = (function(global) {
24
24
}
25
25
26
26
return stored ;
27
- } ) ;
27
+ } ) . catch ( error => cb ( error ) ) ;
28
28
} else {
29
29
// TODO:
30
30
}
31
31
32
32
return unregister ;
33
33
}
34
- }
34
+ } ;
35
35
36
36
Cache . _scriptTag = ( key , src ) => {
37
37
if ( ! scriptMap . has ( key ) ) {
@@ -41,9 +41,7 @@ export const ScriptCache = (function(global) {
41
41
42
42
let tag = document . createElement ( 'script' ) ;
43
43
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 ] ;
47
45
48
46
tag . type = 'text/javascript' ;
49
47
tag . async = false ; // Load in order
@@ -69,24 +67,24 @@ export const ScriptCache = (function(global) {
69
67
70
68
cleanup ( ) ;
71
69
}
72
- }
70
+ } ;
73
71
74
72
const cleanup = ( ) => {
75
73
if ( global [ cbName ] && typeof global [ cbName ] === 'function' ) {
76
74
global [ cbName ] = null ;
77
75
delete global [ cbName ]
78
76
}
79
- }
77
+ } ;
80
78
81
79
tag . onload = handleResult ( 'loaded' ) ;
82
- tag . onerror = handleResult ( 'error' )
80
+ tag . onerror = handleResult ( 'error' ) ;
83
81
tag . onreadystatechange = ( ) => {
84
82
handleResult ( tag . readyState )
85
- }
83
+ } ;
86
84
87
85
// Pick off callback, if there is one
88
86
if ( src . match ( / c a l l b a c k = C A L L B A C K _ N A M E / ) ) {
89
- src = src . replace ( / ( c a l l b a c k = ) [ ^ \& ] + / , `$1${ cbName } ` )
87
+ src = src . replace ( / ( c a l l b a c k = ) [ ^ \& ] + / , `$1${ cbName } ` ) ;
90
88
cb = window [ cbName ] = tag . onload ;
91
89
} else {
92
90
tag . addEventListener ( 'load' , tag . onload )
@@ -101,13 +99,13 @@ export const ScriptCache = (function(global) {
101
99
let initialState = {
102
100
loaded : false ,
103
101
error : false ,
104
- promise : promise ,
102
+ promise,
105
103
tag
106
- }
104
+ } ;
107
105
scriptMap . set ( key , initialState ) ;
108
106
}
109
- return scriptMap . get ( key ) ;
110
- }
107
+ return scriptMap . get ( key ) . tag ;
108
+ } ;
111
109
112
110
// let scriptTags = document.querySelectorAll('script')
113
111
//
@@ -132,7 +130,7 @@ export const ScriptCache = (function(global) {
132
130
tag : tag ,
133
131
onLoad : Cache . _onLoad ( key ) ,
134
132
}
135
- } )
133
+ } ) ;
136
134
137
135
return Cache ;
138
136
}
0 commit comments