UNPKG

dependency-injection-cat

Version:

DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!

96 lines (95 loc) 4.23 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalCatContext = void 0; var NotInitializedConfig_1 = require("../exceptions/runtime/NotInitializedConfig"); var BeanNotFoundInContext_1 = require("../exceptions/runtime/BeanNotFoundInContext"); var InternalCatContext = /** @class */ (function () { function InternalCatContext(contextName, beanConfigurationRecord, lifecycleConfiguration) { this.contextName = contextName; this.beanConfigurationRecord = beanConfigurationRecord; this.lifecycleConfiguration = lifecycleConfiguration; this.singletonMap = new Map(); this.notInitializedConfigMarker = {}; this._config = this.notInitializedConfigMarker; } InternalCatContext.prototype.___postConstruct = function () { var _this = this; this.lifecycleConfiguration['post-construct'].forEach(function (methodName) { _this[methodName](); }); }; InternalCatContext.prototype.___beforeDestruct = function () { var _this = this; this.lifecycleConfiguration['before-destruct'].forEach(function (methodName) { _this[methodName](); }); }; Object.defineProperty(InternalCatContext.prototype, "config", { get: function () { if (this._config === this.notInitializedConfigMarker) { throw new NotInitializedConfig_1.NotInitializedConfig(); } return this._config; }, set: function (config) { this._config = config; }, enumerable: false, configurable: true }); InternalCatContext.prototype.getBean = function (beanName) { var beanConfiguration = this.getBeanConfiguration(beanName); if (!beanConfiguration.isPublic) { console.warn("Bean " + beanName + " is not defined in TBeans interface.\nThis Bean will not be checked for correctness at compile-time.\nContext name: " + this.contextName); } return this.getPrivateBean(beanName); }; InternalCatContext.prototype.getPrivateBean = function (beanName) { var _a; var beanConfiguration = this.getBeanConfiguration(beanName); if (beanConfiguration.scope !== 'singleton') { return this[beanName](); } var savedInstance = (_a = this.singletonMap.get(beanName)) !== null && _a !== void 0 ? _a : this[beanName](); if (!this.singletonMap.has(beanName)) { this.singletonMap.set(beanName, savedInstance); } return savedInstance; }; InternalCatContext.prototype.getBeanConfiguration = function (beanName) { var _a; var beanConfiguration = (_a = this.beanConfigurationRecord[beanName]) !== null && _a !== void 0 ? _a : null; if (beanConfiguration === null) { throw new BeanNotFoundInContext_1.BeanNotFoundInContext(this.contextName, beanName); } return beanConfiguration; }; InternalCatContext.prototype.getBeans = function () { var _this = this; var publicBeansConfigurations = {}; Object.keys(this.beanConfigurationRecord).forEach(function (key) { var beanConfigRecord = _this.beanConfigurationRecord[key]; if (beanConfigRecord === null || beanConfigRecord === void 0 ? void 0 : beanConfigRecord.isPublic) { publicBeansConfigurations[key] = beanConfigRecord; } }); return Object.keys(publicBeansConfigurations) .reduce(function (previousValue, currentValue) { var _a; return (__assign(__assign({}, previousValue), (_a = {}, _a[currentValue] = _this.getBean(currentValue), _a))); }, {}); }; return InternalCatContext; }()); exports.InternalCatContext = InternalCatContext;