@robotlegsjs/core
Version:
An architecture-based IoC framework for JavaScript/TypeScript
140 lines • 6.13 kB
JavaScript
"use strict";
// ------------------------------------------------------------------------------
// Copyright (c) 2017-present, RobotlegsJS. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
var LifecycleEvent_1 = require("../api/LifecycleEvent");
var ClassMatcher_1 = require("./ClassMatcher");
var instantiateUnmapped_1 = require("./instantiateUnmapped");
var ObjectMatcher_1 = require("./ObjectMatcher");
var ObjectProcessor_1 = require("./ObjectProcessor");
/**
* The config manager handles configuration files and
* allows the installation of custom configuration handlers.
*
* <p>It is pre-configured to handle plain objects and classes</p>
*
* @private
*/
var ConfigManager = /** @class */ (function () {
/*============================================================================*/
/* Constructor */
/*============================================================================*/
/**
* @private
*/
function ConfigManager(context) {
/*============================================================================*/
/* Private Properties */
/*============================================================================*/
this._objectProcessor = new ObjectProcessor_1.ObjectProcessor();
this._configs = new Map();
this._queue = [];
this._initialized = false;
this._context = context;
this._injector = context.injector;
this._logger = context.getLogger(this);
this.addConfigHandler(new ClassMatcher_1.ClassMatcher(), this._handleClass.bind(this));
this.addConfigHandler(new ObjectMatcher_1.ObjectMatcher(), this._handleObject.bind(this));
// The ConfigManager should process the config queue
// at the end of the INITIALIZE phase,
// but *before* POST_INITIALIZE, so use low event priority
context.addEventListener(LifecycleEvent_1.LifecycleEvent.INITIALIZE, this._initialize, this, false, -100);
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
/**
* Process a given configuration object by running it through registered handlers.
* <p>If the manager is not initialized the configuration will be queued.</p>
*
* @param config The configuration object or class
*/
ConfigManager.prototype.addConfig = function (config) {
if (!this._configs.get(config)) {
this._configs.set(config, true);
this._objectProcessor.processObject(config);
}
};
/**
* Adds a custom configuration handlers
*
* @param matcher Pattern to match configuration objects
* @param handler Handler to process matching configurations
*/
ConfigManager.prototype.addConfigHandler = function (matcher, handler) {
this._objectProcessor.addObjectHandler(matcher, handler);
};
/**
* Destroy
*/
ConfigManager.prototype.destroy = function () {
this._context.removeEventListener(LifecycleEvent_1.LifecycleEvent.INITIALIZE, this._initialize);
this._objectProcessor.removeAllHandlers();
this._queue.length = 0;
this._configs.clear();
};
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
ConfigManager.prototype._initialize = function (event) {
if (!this._initialized) {
this._initialized = true;
this._processQueue();
}
};
ConfigManager.prototype._handleClass = function (type) {
if (this._initialized) {
this._logger.debug("Already initialized. Instantiating config class {0}", [type]);
this._processClass(type);
}
else {
this._logger.debug("Not yet initialized. Queuing config class {0}", [type]);
this._queue.push(type);
}
};
ConfigManager.prototype._handleObject = function (object) {
if (this._initialized) {
this._logger.debug("Already initialized. Injecting into config object {0}", [object]);
this._processObject(object);
}
else {
this._logger.debug("Not yet initialized. Queuing config object {0}", [object]);
this._queue.push(object);
}
};
ConfigManager.prototype._processQueue = function () {
var _this = this;
this._queue.forEach(function (config) {
if (typeof config === "function") {
// instanceof Class
_this._logger.debug("Now initializing. Instantiating config class {0}", [config]);
_this._processClass(config);
}
else {
_this._logger.debug("Now initializing. Injecting into config object {0}", [config]);
_this._processObject(config);
}
});
this._queue.length = 0;
};
ConfigManager.prototype._processClass = function (type) {
var config = instantiateUnmapped_1.instantiateUnmapped(this._injector, type);
if (config) {
config.configure();
}
};
ConfigManager.prototype._processObject = function (object) {
var config = object;
if (config && config.configure) {
config.configure();
}
};
return ConfigManager;
}());
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=ConfigManager.js.map