@@ -109,47 +109,90 @@ define([
109
109
* @private
110
110
*/
111
111
ScriptComponentHandler . prototype . _createOrLoadScript = function ( component , instanceConfig ) {
112
- var that = this ;
113
112
var ref = instanceConfig . scriptRef ;
114
- var prefix = ScriptComponentHandler . ENGINE_SCRIPT_PREFIX ;
115
- var isEngineScript = ref . indexOf ( prefix ) === 0 ;
116
-
117
- var existingScript = that . _findScript ( component , instanceConfig . id ) ;
113
+ var isEngineScript = ref . indexOf ( ScriptComponentHandler . ENGINE_SCRIPT_PREFIX ) === 0 ;
118
114
119
115
var promise = null ;
120
116
121
117
if ( isEngineScript ) {
122
- if ( existingScript ) {
123
- return PromiseUtils . resolve ( existingScript ) ;
124
- }
125
- promise = that . _createEngineScript ( ref . slice ( prefix . length ) ) ;
118
+ promise = this . _createOrLoadEngineScript ( component , instanceConfig ) ;
126
119
} else {
127
- promise = that . _load ( ref )
128
- . then ( function ( script ) {
129
- if ( existingScript && existingScript . body === script . body ) {
130
- return existingScript ;
131
- }
132
-
133
- // New body so reload the script.
134
- return that . _load ( ref , { reload : true } ) ;
135
- } ) ;
120
+ promise = this . _createOrLoadCustomScript ( component , instanceConfig ) ;
136
121
}
137
122
138
123
return promise . then ( function ( script ) {
124
+ // Save the instance identifier so we can find the instance later
125
+ // when updating again.
139
126
script . instanceId = instanceConfig . id ;
140
127
return script ;
141
128
} ) ;
142
129
} ;
143
130
144
- ScriptComponentHandler . prototype . _findScript = function ( component , instanceId ) {
145
- for ( var i = 0 ; i < component . scripts . length ; ++ i ) {
146
- var script = component . scripts [ i ] ;
147
- if ( script . instanceId === instanceId ) {
148
- return script ;
149
- }
131
+ /**
132
+ * Creates or loads an engine script. If the component already has an instance
133
+ * of that script, it will be returned.
134
+ *
135
+ * @param {ScriptComponent } component
136
+ * @param {object } instanceConfig
137
+ *
138
+ * @return {Promise }
139
+ * @private
140
+ */
141
+ ScriptComponentHandler . prototype . _createOrLoadEngineScript = function ( component , instanceConfig ) {
142
+ var existingScript = this . _findScript ( component , instanceConfig . id ) ;
143
+ var prefix = ScriptComponentHandler . ENGINE_SCRIPT_PREFIX ;
144
+
145
+ if ( existingScript ) {
146
+ return PromiseUtils . resolve ( existingScript ) ;
150
147
}
151
148
152
- return null ;
149
+ return this . _createEngineScript ( instanceConfig . scriptRef . slice ( prefix . length ) ) ;
150
+ }
151
+
152
+ /**
153
+ * Creates or loads a custom script. If the component already has an instance
154
+ * of that script with the same body, it will be returned.
155
+ *
156
+ * @param {ScriptComponent } component
157
+ * @param {object } instanceConfig
158
+ *
159
+ * @return {Promise }
160
+ * @private
161
+ */
162
+ ScriptComponentHandler . prototype . _createOrLoadCustomScript = function ( component , instanceConfig ) {
163
+ var that = this ;
164
+ var ref = instanceConfig . scriptRef ;
165
+
166
+ // Need to load the script (note that we are not reloading yet) so we
167
+ // can compare the new body with the old one.
168
+ return this . _load ( ref ) . then ( function ( script ) {
169
+ var existingScript = that . _findScript ( component , instanceConfig . id ) ;
170
+
171
+ if ( existingScript && existingScript . body === script . body ) {
172
+ return existingScript ;
173
+ }
174
+
175
+ // New script or the body was changed so reload the script.
176
+ return that . _load ( ref , { reload : true } ) ;
177
+ } ) ;
178
+ }
179
+
180
+ /**
181
+ * Searches the specified component to try to find the specified script
182
+ * instance.
183
+ *
184
+ * @param {ScriptComponent } component
185
+ * The component which is to be searched.
186
+ * @param {string } instanceId
187
+ * The identifier of the script instance which is to be found.
188
+ *
189
+ * @return {object } The script which was found, or undefined if none was found.
190
+ * @private
191
+ */
192
+ ScriptComponentHandler . prototype . _findScript = function ( component , instanceId ) {
193
+ return _ . find ( component . scripts , function ( script ) {
194
+ return script . instanceId === instanceId ;
195
+ } ) ;
153
196
}
154
197
155
198
/**
@@ -272,7 +315,7 @@ define([
272
315
// wait for the load to be completed. It will eventually resolve
273
316
// and the parameter will be set.
274
317
setRefParam ( ) ;
275
- return Promise . resolve ( ) ;
318
+ return PromiseUtils . resolve ( ) ;
276
319
} else if ( ScriptUtils . isRefType ( type ) ) {
277
320
return setRefParam ( ) ;
278
321
} else {
0 commit comments