Bug 838072 - Part 1: Refactor data collection internals into function; r=rnewman

This commit is contained in:
Gregory Szorc 2013-02-05 09:59:10 -08:00
Родитель 0a81b06e9f
Коммит 597564df89
1 изменённых файлов: 33 добавлений и 10 удалений

Просмотреть файл

@ -147,7 +147,7 @@ Collector.prototype = Object.freeze({
* The resolved value to the promise is this `Collector` instance.
*/
collectConstantData: function () {
let promises = [];
let entries = [];
for (let [name, entry] of this._providers) {
if (entry.constantsCollected) {
@ -156,29 +156,52 @@ Collector.prototype = Object.freeze({
continue;
}
entries.push(entry);
}
let onCollect = function (entry, result) {
entry.constantsCollected = true;
};
return this._callCollectOnProviders(entries, "collectConstantData",
onCollect);
},
_callCollectOnProviders: function (entries, fnProperty, onCollect=null) {
let promises = [];
for (let entry of entries) {
let provider = entry.provider;
let collectPromise;
try {
collectPromise = entry.provider.collectConstantData();
collectPromise = provider[fnProperty].call(provider);
} catch (ex) {
this._log.warn("Exception when calling " + name +
".collectConstantData: " +
CommonUtils.exceptionStr(ex));
this.providerErrors.get(name).push(ex);
this._log.warn("Exception when calling " + provider.name + "." +
fnProperty + ": " + CommonUtils.exceptionStr(ex));
this.providerErrors.get(provider.name).push(ex);
continue;
}
if (!collectPromise) {
throw new Error("Provider does not return a promise from " +
"collectConstantData():" + name);
this._log.warn("Provider does not return a promise from " +
fnProperty + "(): " + provider.name);
continue;
}
let promise = collectPromise.then(function onCollected(result) {
entry.constantsCollected = true;
if (onCollect) {
try {
onCollect(entry, result);
} catch (ex) {
this._log.warn("onCollect callback threw: " +
CommonUtils.exceptionStr(ex));
}
}
return Promise.resolve(result);
});
promises.push([name, promise]);
promises.push([provider.name, promise]);
}
return this._handleCollectionPromises(promises);