From 306d8ae9dbd351d52c82c9aff1e15bb3be688abe Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 4 Nov 2014 08:35:06 -0800 Subject: [PATCH] [Framework] Add bundle resolver; normalize initialization Simplify the extension initializer by putting each phase's responsibilities into a specific class. WTD-518. --- platform/framework/src/Bundle.js | 10 +++ platform/framework/src/BundleResolver.js | 73 +++++++++++++++++++ platform/framework/src/ExtensionRegistrar.js | 17 ++++- .../framework/src/FrameworkInitializer.js | 24 ++---- 4 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 platform/framework/src/BundleResolver.js diff --git a/platform/framework/src/Bundle.js b/platform/framework/src/Bundle.js index 92c6027a92..8af337521a 100644 --- a/platform/framework/src/Bundle.js +++ b/platform/framework/src/Bundle.js @@ -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); }, diff --git a/platform/framework/src/BundleResolver.js b/platform/framework/src/BundleResolver.js new file mode 100644 index 0000000000..7f12952cd4 --- /dev/null +++ b/platform/framework/src/BundleResolver.js @@ -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.[]} resolvedBundles + * @returns {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; + } +); \ No newline at end of file diff --git a/platform/framework/src/ExtensionRegistrar.js b/platform/framework/src/ExtensionRegistrar.js index 1ff42bf605..7ee886314c 100644 --- a/platform/framework/src/ExtensionRegistrar.js +++ b/platform/framework/src/ExtensionRegistrar.js @@ -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 }; } diff --git a/platform/framework/src/FrameworkInitializer.js b/platform/framework/src/FrameworkInitializer.js index c54522dad5..e3fae0ef87 100644 --- a/platform/framework/src/FrameworkInitializer.js +++ b/platform/framework/src/FrameworkInitializer.js @@ -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.} 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); + } };