[Framework] Add bundle resolver; normalize initialization

Simplify the extension initializer by putting each
phase's responsibilities into a specific class.
WTD-518.
This commit is contained in:
Victor Woeltjen 2014-11-04 08:35:06 -08:00
parent d8eb72621b
commit 306d8ae9db
4 changed files with 106 additions and 18 deletions

View File

@ -108,6 +108,12 @@ define(
getLogName: function () {
return logName;
},
/**
*
* @param category
* @memberof Bundle#
* @returns {Array}
*/
getExtensions: function (category) {
var extensions = definition.extensions[category] || [];
@ -115,6 +121,10 @@ define(
return new Extension(self, category, extDefinition);
});
},
/**
* @memberof Bundle#
* @returns {Array}
*/
getExtensionCategories: function () {
return Object.keys(definition.extensions);
},

View File

@ -0,0 +1,73 @@
/*global define,Promise*/
/**
* Module defining BundleResolver. Created by vwoeltje on 11/4/14.
*/
define(
[],
function () {
"use strict";
/**
*
* @constructor
*/
function BundleResolver(extensionResolver) {
/**
*
* @param {Object.<string, object[]>[]} resolvedBundles
* @returns {Object.<string, object[]>}
*/
function mergeResolvedBundles(resolvedBundles) {
var result = {};
resolvedBundles.forEach(function (resolved) {
Object.keys(resolved).forEach(function (k) {
result[k] = (result[k] || []).concat(resolved[k]);
});
});
return result;
}
function resolveBundle(bundle) {
var categories = bundle.getExtensionCategories(),
result = {};
function resolveExtension(extension) {
var category = extension.getCategory();
function push(resolved) {
result[category].push(resolved);
}
return extensionResolver.resolve(extension).then(push);
}
function resolveCategory(category) {
result[category] = [];
return Promise.all(
bundle.getExtensions(category).map(resolveExtension)
);
}
function giveResult() {
return result;
}
return Promise.all(categories.map(resolveCategory))
.then(giveResult);
}
return {
resolveBundles: function (bundles) {
return Promise.all(bundles.map(resolveBundle))
.then(mergeResolvedBundles);
}
};
}
return BundleResolver;
}
);

View File

@ -25,6 +25,10 @@ define(
return category + "[" + name + "]";
}
function identifyCategory(category) {
return category + Constants.EXTENSION_SUFFIX;
}
function echo() {
return arguments.slice;
}
@ -50,7 +54,7 @@ define(
app.factory(name, names.concat([echo]));
}
function registerExtensions(category, extensions) {
function registerExtensionsForCategory(category, extensions) {
var names = [];
function registerExtension(extension, index) {
@ -83,10 +87,19 @@ define(
}
}
function registerExtensionGroup(extensionGroup) {
Object.keys(extensionGroup).forEach(function (category) {
registerExtensionsForCategory(
category,
extensionGroup[category]
);
});
}
customRegistrars = customRegistrars || {};
return {
registerExtensions: registerExtensions
registerExtensions: registerExtensionGroup
};
}

View File

@ -12,7 +12,7 @@ define(
*
* @constructor
* @param {BundleLoader} loader
* @param {ExtensionResolver} resolver
* @param {BundleResolver} resolver
* @param {ExtensionRegistrar} registrar
* @param {ApplicationBootstrapper} bootstrapper
*/
@ -29,20 +29,11 @@ define(
/**
*
* @param bundles
* @param {Bundle[]} bundles
* @returns {Object.<string, object[]>} an object mapping
*/
function resolveExtensions(bundles) {
var resolvedExtensions = {};
function resolveExtensionsForBundle(bundle) {
}
return Promises.all(bundles.map(resolveExtensionsForBundle)).then(function () {
return resolvedExtensions;
});
return resolver.resolveBundles(bundles);
}
function loadBundles(bundleList) {
@ -51,10 +42,11 @@ define(
return {
runApplication: function (bundleList) {
return loadBundles()
.then(resolveExtensions)
.then(registerExtensions)
.then(bootstrapApplication);
return loader.loadBundles(bundleList)
.then(resolver.resolveBundles)
.then(registrar.registerExtensions)
.then(bootstrapper.bootstrapApplication);
}
};