ima
Version:
IMA.js framework for isomorphic javascript application
196 lines (145 loc) • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _imaHelpers = require("ima-helpers");
var _imaHelpers2 = _interopRequireDefault(_imaHelpers);
var _namespace = require("./namespace");
var _namespace2 = _interopRequireDefault(_namespace);
var _ObjectContainer = require("./ObjectContainer");
var _ObjectContainer2 = _interopRequireDefault(_ObjectContainer);
var _Router = require("./router/Router");
var _Router2 = _interopRequireDefault(_Router);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_namespace2.default.namespace('ima');
/**
* Environment name value in the production environment.
*
* @const
* @type {string}
*/
const PRODUCTION_ENVIRONMENT = 'prod';
/**
* Application bootstrap used to initialize the environment and the application
* itself.
*/
class Bootstrap {
/**
* Initializes the bootstrap.
*
* @param {ObjectContainer} oc The application's object container to use
* for managing dependencies.
*/
constructor(oc) {
/**
* The object container used to manage dependencies.
*
* @type {ObjectContainer}
*/
this._oc = oc;
/**
* Application configuration.
*
* @type {Object<string, *>}
*/
this._config = {};
}
/**
* Initializes the application by running the bootstrap sequence. The
* sequence initializes the components of the application in the following
* order:
* - application settings
* - constants, service providers and class dependencies configuration
* - services
* - UI components
* - routing
*
* @param {Object<string, *>} config The application environment
* configuration for the current environment.
*/
run(config) {
this._config = config;
this._initSettings();
this._bindDependencies();
this._initServices();
this._initRoutes();
}
/**
* Initializes the application settings. The method loads the settings for
* all environments and then pics the settings for the current environment.
*
* The method also handles using the values in the production environment
* as default values for configuration items in other environments.
*/
_initSettings() {
let currentApplicationSettings = {};
let plugins = this._config.plugins.concat([{
name: _ObjectContainer2.default.APP_BINDING_STATE,
module: this._config
}]);
plugins.filter(plugin => typeof plugin.module.initSettings === 'function').forEach(plugin => {
let allPluginSettings = plugin.module.initSettings(_namespace2.default, this._oc, this._config.settings);
let environmentPluginSetting = _imaHelpers2.default.resolveEnvironmentSetting(allPluginSettings, this._config.settings.$Env);
_imaHelpers2.default.assignRecursivelyWithTracking(plugin.name)(currentApplicationSettings, environmentPluginSetting);
});
this._config.bind = Object.assign(this._config.bind || {}, currentApplicationSettings, this._config.settings);
}
/**
* Returns setting for current environment where base values are from production
* environment and other environments override base values.
*
* @return {Object<string, *>}
*/
_getEnvironmentSetting(allSettings) {
let environment = this._config.settings.$Env;
let environmentSetting = allSettings[environment] || {};
if (environment !== PRODUCTION_ENVIRONMENT) {
let productionSettings = allSettings[PRODUCTION_ENVIRONMENT];
_imaHelpers2.default.assignRecursively(productionSettings, environmentSetting);
environmentSetting = productionSettings;
}
return environmentSetting;
}
/**
* Binds the constants, service providers and class dependencies to the
* object container.
*/
_bindDependencies() {
this._oc.setBindingState(_ObjectContainer2.default.IMA_BINDING_STATE);
this._config.initBindIma(_namespace2.default, this._oc, this._config.bind, _ObjectContainer2.default.IMA_BINDING_STATE);
this._config.plugins.filter(plugin => typeof plugin.module.initBind === 'function').forEach(plugin => {
this._oc.setBindingState(_ObjectContainer2.default.PLUGIN_BINDING_STATE, plugin.name);
plugin.module.initBind(_namespace2.default, this._oc, this._config.bind, plugin.name);
});
this._oc.setBindingState(_ObjectContainer2.default.APP_BINDING_STATE);
this._config.initBindApp(_namespace2.default, this._oc, this._config.bind, _ObjectContainer2.default.APP_BINDING_STATE);
}
/**
* Initializes the routes.
*/
_initRoutes() {
let router = this._oc.get(_Router2.default);
this._config.initRoutes(_namespace2.default, this._oc, this._config.routes, router);
}
/**
* Initializes the basic application services.
*/
_initServices() {
this._config.initServicesIma(_namespace2.default, this._oc, this._config.services);
this._config.plugins.filter(plugin => typeof plugin.module.initServices === 'function').forEach(plugin => {
plugin.module.initServices(_namespace2.default, this._oc, this._config.services);
});
this._config.initServicesApp(_namespace2.default, this._oc, this._config.services);
}
}
exports.default = Bootstrap;
_namespace2.default.ima.Bootstrap = Bootstrap;
typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/Bootstrap', [], function (_export, _context) {
'use strict';
return {
setters: [],
execute: function () {
_export('default', exports.default);
}
};
});