From 12f43d1c8dfd0bdc7ecd2fecd2d28254ba231b55 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Fri, 31 Oct 2014 16:45:08 -0700 Subject: [PATCH] [Framework] First-pass implementation of bundle loader Begin implementing the bundle loader component of the framework layer, responsible for taking a list of included bundles and exposing their contents in a useful manner. WTD-518. --- platform/framework/src/BundleLoader.js | 80 ++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/platform/framework/src/BundleLoader.js b/platform/framework/src/BundleLoader.js index e8f72d0e6d..deb99846a9 100644 --- a/platform/framework/src/BundleLoader.js +++ b/platform/framework/src/BundleLoader.js @@ -4,17 +4,89 @@ * Module defining BundleLoader.js. Created by vwoeltje on 10/31/14. */ define( - [], - function () { + ['./Constants', './Bundle'], + function (Constants, Bundle) { "use strict"; + var INVALID_ARGUMENT_MESSAGE = "Malformed loadBundles argument; " + + "expected string or array", + BAD_CONTENTS_PREFIX = "Invalid bundle contents for ", + LOAD_ERROR_PREFIX = "Failed to load bundle "; + /** * * @constructor + * @param {object} $http Angular's HTTP requester + * @param {object} $log Angular's logging service */ - function BundleLoader() { - return { + function BundleLoader($http, $log) { + // Utility function; load contents of JSON file using $http + function getJSON(file) { + return $http.get(file).then(function (response) { + return response.data; + }); + } + + // Remove bundles which failed to load properly. + // These should have been logged when loaded by + // loadBundleDefinition, so at this point they are safe + // to discard. + function filterBundles(array) { + return array.map(function (x) { return x !== undefined; }); + } + + // Convert JSON bundle definitions to Bundle objects. + function objectifyBundles(bundleDefinitions) { + return bundleDefinitions.map(function (definition) { + return new Bundle() + }); + } + + // Load a definition for a bundle + function loadBundleDefinition(bundlePath) { + return getJSON(bundlePath + "/" + Constants.BUNDLE_FILE).then( + function (x) { + if (x === null || typeof x !== 'object') { + $log.warn(BAD_CONTENTS_PREFIX + bundlePath); + return undefined; + } + return x; + }, + function () { + $log.warn(LOAD_ERROR_PREFIX + bundlePath); + return undefined; + } + ); + } + + function loadBundle(bundlePath) { + return loadBundleDefinition(bundlePath).then(function (definition) { + return definition && (new Bundle(bundlePath, definition)); + }); + } + + function loadBundlesFromArray(bundleArray) { + var bundlePromises = bundleArray.map(loadBundleDefinition); + + return Promise.all(bundlePromises) + .then(filterBundles) + .then(objectifyBundles); + } + + function loadBundlesFromFile(listFile) { + return getJSON(listFile).then(loadBundlesFromArray); + } + + function loadBundles(bundles) { + return Array.isArray(bundles) ? loadBundlesFromArray(bundles) : + (typeof bundles === 'string') ? loadBundlesFromFile(bundles) : + Promise.reject(new Error(INVALID_ARGUMENT_MESSAGE)); + } + + + return { + loadBundles: loadBundles }; }