UNPKG

ajsfw

Version:
282 lines (281 loc) 13.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var exceptions = require("./exceptions"); var index_1 = require("ajsfw/utils/index"); var Container = (function () { function Container() { this.__transientServices = []; this.__scopedServices = []; this.__singletonServices = []; this.__scopedInstances = []; this.__singletonInstances = []; } Container.prototype.addTransient = function (serviceInterfaceIdentifier, serviceConstructor) { var serviceConfiguration = []; for (var _i = 2; _i < arguments.length; _i++) { serviceConfiguration[_i - 2] = arguments[_i]; } this.__addService(this.__transientServices, { serviceInterfaceIdentifier: serviceInterfaceIdentifier, serviceConstructor: serviceConstructor, serviceConfiguration: serviceConfiguration }); return this; }; Container.prototype.addScoped = function (serviceInterfaceIdentifier, serviceConstructor) { var serviceConfiguration = []; for (var _i = 2; _i < arguments.length; _i++) { serviceConfiguration[_i - 2] = arguments[_i]; } this.__addService(this.__scopedServices, { serviceInterfaceIdentifier: serviceInterfaceIdentifier, serviceConstructor: serviceConstructor, serviceConfiguration: serviceConfiguration }); return this; }; Container.prototype.addSingleton = function (serviceInterfaceIdentifier, classToConstruct) { var serviceConfiguration = []; for (var _i = 2; _i < arguments.length; _i++) { serviceConfiguration[_i - 2] = arguments[_i]; } this.__addService(this.__singletonServices, { serviceInterfaceIdentifier: serviceInterfaceIdentifier, serviceConstructor: classToConstruct, serviceConfiguration: serviceConfiguration }); return this; }; Container.prototype.resolve = function (serviceInterfaceIdentifier, throwUnresolvedException) { if (throwUnresolvedException === void 0) { throwUnresolvedException = true; } return __awaiter(this, void 0, void 0, function () { var resolved; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.__resolve(serviceInterfaceIdentifier)]; case 1: resolved = _a.sent(); if (resolved !== null) { return [2, resolved]; } else { if (throwUnresolvedException) { throw new exceptions.UnableToResolveDependencyException("Requested service identifier is not registered"); } else { return [2, null]; } } return [2]; } }); }); }; Container.prototype.releaseSingletonInstanceReference = function (serviceInstance) { this.__releaseInstanceReference(this.__scopedInstances, serviceInstance); }; Container.prototype.releaseScopedInstanceReference = function (serviceInstance) { return this.__releaseInstanceReference(this.__singletonInstances, serviceInstance); }; Container.prototype.__addService = function (serviceList, service) { var s = this.__getService(serviceList, service.serviceInterfaceIdentifier); if (s !== null) { return; } serviceList.push(service); }; Container.prototype.__getService = function (serviceList, serviceInterfaceIdentifier) { for (var _i = 0, serviceList_1 = serviceList; _i < serviceList_1.length; _i++) { var serviceDescriptor = serviceList_1[_i]; if (serviceDescriptor.serviceInterfaceIdentifier === serviceInterfaceIdentifier) { return serviceDescriptor; } } return null; }; Container.prototype.__getServiceInstance = function (instanceList, serviceInstance) { for (var _i = 0, instanceList_1 = instanceList; _i < instanceList_1.length; _i++) { var instance = instanceList_1[_i]; if (instance.serviceInstance === serviceInstance) { return instance; } } return null; }; Container.prototype.__findServiceInstance = function (instanceList, serviceInterfaceIdentifier) { for (var _i = 0, instanceList_2 = instanceList; _i < instanceList_2.length; _i++) { var instance = instanceList_2[_i]; if (instance.serviceInterfaceIdentifier === serviceInterfaceIdentifier) { return instance; } } return null; }; Container.prototype.__releaseInstanceReference = function (instanceList, serviceInstance) { var instance = this.__getServiceInstance(instanceList, serviceInstance); if (instance !== null) { if (instanceList === this.__singletonInstances) { instance.referenceCount--; } if (instance.referenceCount === 0) { var index = instanceList.indexOf(instance); if (index !== -1) { instanceList.splice(index, 1); } } return instance.referenceCount === 0; } return false; }; Container.prototype.__resolve = function (serviceInterfaceIdentifier) { return __awaiter(this, void 0, void 0, function () { var instance; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.__resolveScoped(serviceInterfaceIdentifier)]; case 1: instance = _a.sent(); if (instance !== null) { return [2, instance]; } return [4, this.__resolveSingleton(serviceInterfaceIdentifier)]; case 2: instance = _a.sent(); if (instance !== null) { return [2, instance]; } return [4, this.__resolveTransient(serviceInterfaceIdentifier)]; case 3: instance = _a.sent(); return [2, instance]; } }); }); }; Container.prototype.__resolveTransient = function (serviceInterfaceIdentifier) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.__constructService(this.__transientServices, null, serviceInterfaceIdentifier)]; case 1: return [2, _a.sent()]; } }); }); }; Container.prototype.__resolveScoped = function (serviceInterfaceIdentifier) { return __awaiter(this, void 0, void 0, function () { var instance; return __generator(this, function (_a) { switch (_a.label) { case 0: instance = this.__findServiceInstance(this.__scopedInstances, serviceInterfaceIdentifier); if (instance !== null) { instance.referenceCount++; return [2, instance.serviceInstance]; } return [4, this.__constructService(this.__scopedServices, this.__scopedInstances, serviceInterfaceIdentifier)]; case 1: return [2, _a.sent()]; } }); }); }; Container.prototype.__resolveSingleton = function (serviceInterfaceIdentifier) { return __awaiter(this, void 0, void 0, function () { var i, serviceInstance; return __generator(this, function (_a) { switch (_a.label) { case 0: i = this.__findServiceInstance(this.__singletonInstances, serviceInterfaceIdentifier); if (i !== null) { return [2, i.serviceInstance]; } return [4, this.__constructService(this.__singletonServices, this.__singletonInstances, serviceInterfaceIdentifier)]; case 1: serviceInstance = _a.sent(); this.__singletonInstances.push({ serviceInterfaceIdentifier: serviceInterfaceIdentifier, serviceInstance: serviceInstance, referenceCount: 0 }); return [2, serviceInstance]; } }); }); }; Container.prototype.__constructService = function (serviceList, instanceList, serviceInterfaceIdentifier) { return __awaiter(this, void 0, void 0, function () { function containerConstructService(ctor, args) { return new (ctor.bind.apply(ctor, [null].concat(args))); } var serviceDescriptor, params, instance, initResult; return __generator(this, function (_a) { switch (_a.label) { case 0: serviceDescriptor = this.__getService(serviceList, serviceInterfaceIdentifier); if (serviceDescriptor === null) { return [2, null]; } return [4, this.__resolveParameters(serviceDescriptor)]; case 1: params = _a.sent(); instance = containerConstructService(serviceDescriptor.serviceConstructor, params); if (instanceList !== null) { instanceList.push({ serviceInterfaceIdentifier: serviceInterfaceIdentifier, serviceInstance: instance, referenceCount: 1 }); } if (!(typeof instance.initialize === "function")) return [3, 3]; initResult = instance.initialize(); if (!(initResult instanceof Promise)) return [3, 3]; return [4, initResult]; case 2: _a.sent(); _a.label = 3; case 3: return [2, instance]; } }); }); }; Container.prototype.__resolveParameters = function (service) { return __awaiter(this, void 0, void 0, function () { var ctorParams, i, _i, _a, parameter, instance; return __generator(this, function (_b) { switch (_b.label) { case 0: ctorParams = []; if (service.serviceConfiguration.length === 0) { return [2, ctorParams]; } i = 0; _i = 0, _a = service.serviceConfiguration; _b.label = 1; case 1: if (!(_i < _a.length)) return [3, 4]; parameter = _a[_i]; return [4, this.resolve(parameter, false)]; case 2: instance = _b.sent(); if (instance !== null) { ctorParams.push(instance); } else { if (parameter && typeof parameter === "object" && parameter !== null && "__diService__" in parameter) { throw new exceptions.UnableToResolveDependencyException("Service: '" + index_1.getClassName(service.serviceConstructor) + "', parameter index: '" + i + "'"); } ctorParams.push(parameter); } i++; _b.label = 3; case 3: _i++; return [3, 1]; case 4: return [2, ctorParams]; } }); }); }; return Container; }()); exports.Container = Container;