[Framework] Refactor source folder

Refactor framework source folder; move each initialization
stage into its own directory. WTD-518.
This commit is contained in:
Victor Woeltjen
2014-11-05 12:18:19 -08:00
parent dbd5b148e2
commit 73e767228f
11 changed files with 11 additions and 11 deletions

View File

@ -0,0 +1,98 @@
/*global define,Promise*/
/**
* Module defining BundleResolver. Created by vwoeltje on 11/4/14.
*/
define(
[],
function () {
"use strict";
/**
* Responsible for the extension resolution phase of framework
* initialization. During this phase, any scripts implementing
* extensions provided by bundles are loaded.
*
* @constructor
*/
function BundleResolver(extensionResolver, $log) {
/**
* Merge resolved bundles (where each is expressed as an
* object containing key-value pairs, where keys are extension
* categories and values are arrays of resolved extensions)
* into one large object containing resolved extensions from
* all bundles (in the same form.)
*
* @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;
}
// Resolve a bundle; resolve all extensions, and return
// the resolved extensions in an object in the format described
// for mergeResolvedBundles above
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;
}
// Log the large-scale task
$log.info(
"Resolving extensions for bundle " + bundle.getLogName()
);
return Promise.all(categories.map(resolveCategory))
.then(giveResult);
}
return {
/**
* Resolve all extensions exposed by these bundles.
*
* @param {Bundle[]} bundles the bundles to resolve
* @returns {Object.<string, object[]>} an object containing
* key-value pairs, where keys are extension
* categories and values are arrays of resolved
* extensions belonging to those categories
*/
resolveBundles: function (bundles) {
return Promise.all(bundles.map(resolveBundle))
.then(mergeResolvedBundles);
}
};
}
return BundleResolver;
}
);

View File

@ -0,0 +1,101 @@
/*global define,Promise*/
/**
* Module defining ExtensionResolver. Created by vwoeltje on 11/3/14.
*/
define(
[],
function () {
"use strict";
/**
* An ExtensionResolver is responsible for loading any implementation
* modules associated with specific extensions.
*
* @param {ImplementationLoader} loader used to load implementations
* @param {*} $log Angular's logging service
* @constructor
*/
function ExtensionResolver(loader, $log) {
function loadImplementation(extension) {
var implPath = extension.getImplementationPath(),
implPromise = loader.load(implPath),
definition = extension.getDefinition();
// Attach values from the object definition to the
// loaded implementation.
function attachDefinition(impl) {
var result = (typeof impl === 'function') ?
function () {
return impl.apply({}, arguments);
} :
Object.create(impl);
Object.keys(definition).forEach(function (k) {
result[k] = definition[k];
});
// Log that this load was successful
$log.info("Loaded " + extension.getLogName());
return result;
}
// Log any errors in loading the implementation, and
// return the plain extension definition instead.
function handleError(err) {
// Build up a log message from parts
var message = [
"Could not load implementation for extension ",
extension.getLogName(),
" due to ",
err.message
].join("");
// Log that the extension was not loaded
$log.warn(message);
return extension.getDefinition();
}
// Log that loading has begun
$log.info([
"Loading implementation ",
implPath,
" for extension ",
extension.getLogName()
].join(""));
return implPromise.then(attachDefinition, handleError);
}
return {
/**
* Resolve the provided extension; this will give a promise
* for the extension's implementation, if one has been
* specified, or for the plain definition of the extension
* otherwise. The plain definition will also be given
* if the implementation fails to load for some reason.
*
* All key-value pairs from the extension definition
* will additionally be attached to any loaded implementation.
*
* @param {Extension} extension
*/
resolve: function (extension) {
// Log that loading has begun
$log.info([
"Resolving extension ",
extension.getLogName()
].join(""));
return extension.hasImplementation() ?
loadImplementation(extension) :
Promise.resolve(extension.getDefinition());
}
};
}
return ExtensionResolver;
}
);

View File

@ -0,0 +1,46 @@
/*global define,Promise*/
/**
* Module defining ImplementationLoader. Created by vwoeltje on 11/3/14.
*/
define(
[],
function () {
"use strict";
/**
* Responsible for loading extension implementations
* (AMD modules.) Acts as a wrapper around RequireJS to
* provide a promise-like API.
* @constructor
* @param {*} require RequireJS, or an object with similar API
* @param {*} $log Angular's logging service
*/
function ImplementationLoader(require) {
function loadModule(path) {
return new Promise(function (fulfill, reject) {
require([path], fulfill, reject);
});
}
return {
/**
* Load an extension's implementation; or, equivalently,
* load an AMD module. This is fundamentally similar
* to a call to RequireJS, except that the result is
* wrapped in a promise. The promise will be fulfilled
* with the loaded module, or rejected with the error
* reported by Require.
*
* @method
* @memberof ImplementationLoader#
* @param {string} path the path to the module to load
* @returns {Promise} a promise for the specified module.
*/
load: loadModule
};
}
return ImplementationLoader;
}
);