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!

61 lines (60 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextPool = void 0; var NoContextByKey_1 = require("../exceptions/runtime/NoContextByKey"); var ContextPool = /** @class */ (function () { function ContextPool(contextName, beanConfigurationRecord, lifecycleConfiguration, context) { this.contextName = contextName; this.beanConfigurationRecord = beanConfigurationRecord; this.lifecycleConfiguration = lifecycleConfiguration; this.context = context; this.DEFAULT_CONTEXT_KEY = {}; this.pool = new Map(); } ContextPool.prototype.initContext = function (_a) { var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b, config = _a.config; var newContext = new this.context(this.contextName, this.beanConfigurationRecord, this.lifecycleConfiguration); newContext.config = config; this.pool.set(key, newContext); newContext.___postConstruct(); return newContext; }; ContextPool.prototype.getContext = function (_a) { var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b; var context = this.pool.get(key); if (context === undefined) { throw new NoContextByKey_1.NoContextByKey(this.contextName, key); } return context; }; ContextPool.prototype.getOrInitContext = function (_a) { var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b, config = _a.config; var oldContext = this.pool.get(key); if (oldContext) { return oldContext; } return this.initContext({ key: key, config: config, }); }; ContextPool.prototype.clearContext = function (_a) { var _b = _a.key, key = _b === void 0 ? this.DEFAULT_CONTEXT_KEY : _b; var contextInstance = this.pool.get(key); if (!contextInstance) { if (this.isDefaultKey(key)) { console.warn("Trying to clear not initialized context, contextName: " + this.contextName); } else { console.warn("Trying to clear not initialized context, contextName: " + this.contextName + ", key: " + key); } } contextInstance === null || contextInstance === void 0 ? void 0 : contextInstance.___beforeDestruct(); this.pool.delete(key); }; ContextPool.prototype.isDefaultKey = function (key) { return key === this.DEFAULT_CONTEXT_KEY; }; return ContextPool; }()); exports.ContextPool = ContextPool;