Skip to content

Commit c4e07ea

Browse files
committed
Some cleanup and comments in the ScriptComponentHandler.
1 parent 80d1eca commit c4e07ea

File tree

2 files changed

+94
-27
lines changed

2 files changed

+94
-27
lines changed

src/goo/scriptpack/ScriptComponentHandler.js

+69-26
Original file line numberDiff line numberDiff line change
@@ -109,47 +109,90 @@ define([
109109
* @private
110110
*/
111111
ScriptComponentHandler.prototype._createOrLoadScript = function (component, instanceConfig) {
112-
var that = this;
113112
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;
118114

119115
var promise = null;
120116

121117
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);
126119
} 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);
136121
}
137122

138123
return promise.then(function (script) {
124+
// Save the instance identifier so we can find the instance later
125+
// when updating again.
139126
script.instanceId = instanceConfig.id;
140127
return script;
141128
});
142129
};
143130

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);
150147
}
151148

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+
});
153196
}
154197

155198
/**
@@ -272,7 +315,7 @@ define([
272315
// wait for the load to be completed. It will eventually resolve
273316
// and the parameter will be set.
274317
setRefParam();
275-
return Promise.resolve();
318+
return PromiseUtils.resolve();
276319
} else if (ScriptUtils.isRefType(type)) {
277320
return setRefParam();
278321
} else {

src/goo/util/ObjectUtils.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ define(function () {
1414
return array.indexOf(value) !== -1;
1515
};
1616

17+
/**
18+
* Gets the first item in an array that matches the specified predicate.
19+
*
20+
* @param {Array} array
21+
* The array which is to be searched.
22+
* @param {Function} predicate
23+
* The preficate which will receive each item of the array and the
24+
* current index.
25+
*
26+
* @return {*}
27+
* The first item that matches the predicate or undefined if no
28+
* items matched the predicate.
29+
*/
30+
ObjectUtils.find = function (array, predicate) {
31+
for (var i = 0; i < array.length; ++i) {
32+
var item = array[i];
33+
if (predicate(item, i)) {
34+
return item;
35+
}
36+
}
37+
38+
return undefined;
39+
};
40+
1741
/**
1842
* Copies properties from an object onto another object if they're not already present
1943
* @param {Object} destination Destination object to copy to
@@ -308,4 +332,4 @@ define(function () {
308332
};
309333

310334
return ObjectUtils;
311-
});
335+
});

0 commit comments

Comments
 (0)