@carlosv2/glue
Version:
Dependency injection library that stays out of the way
63 lines (62 loc) • 2.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssemblingContainer = void 0;
const utils_1 = require("../utils");
const index_1 = require("../context/index");
const service_1 = require("../error/service");
const parameter_1 = require("../error/parameter");
class AssemblingContainer {
constructor(parameters, aliases, services) {
this.parameters = parameters;
this.aliases = aliases;
this.services = services;
this.instances = {};
}
get(id, context) {
return __awaiter(this, void 0, void 0, function* () {
const runningContext = (context !== null && context !== void 0 ? context : new index_1.RunningContext(this)).pushServiceFrame(id);
if ((0, utils_1.has)(id, this.instances)) {
return (yield this.instances[id]);
}
if ((0, utils_1.has)(id, this.aliases)) {
return (yield this.aliases[id].build(runningContext));
}
if ((0, utils_1.has)(id, this.services)) {
this.instances[id] = this.services[id].build(runningContext);
return (yield this.instances[id]);
}
throw new service_1.ServiceNotFoundError(id, runningContext);
});
}
getParameter(id, context) {
return __awaiter(this, void 0, void 0, function* () {
const runningContext = (context !== null && context !== void 0 ? context : new index_1.RunningContext(this)).pushParameterFrame(id);
if ((0, utils_1.has)(id, this.parameters)) {
return (yield this.parameters[id].build(runningContext));
}
throw new parameter_1.ParameterNotFoundError(id, runningContext);
});
}
findServiceIdsByTag(tag) {
const ids = [];
for (const [id, service] of Object.entries(this.services)) {
if (service.getTagsByName(tag).length) {
ids.push(id);
}
}
return ids;
}
static from(services) {
return new AssemblingContainer({}, {}, services);
}
}
exports.AssemblingContainer = AssemblingContainer;