UNPKG

@carlosv2/glue

Version:

Dependency injection library that stays out of the way

59 lines (58 loc) 2.64 kB
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()); }); }; import { has } from '../utils.js'; import { RunningContext } from '../context/index.js'; import { ServiceNotFoundError } from '../error/service.js'; import { ParameterNotFoundError } from '../error/parameter.js'; export 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 RunningContext(this)).pushServiceFrame(id); if (has(id, this.instances)) { return (yield this.instances[id]); } if (has(id, this.aliases)) { return (yield this.aliases[id].build(runningContext)); } if (has(id, this.services)) { this.instances[id] = this.services[id].build(runningContext); return (yield this.instances[id]); } throw new ServiceNotFoundError(id, runningContext); }); } getParameter(id, context) { return __awaiter(this, void 0, void 0, function* () { const runningContext = (context !== null && context !== void 0 ? context : new RunningContext(this)).pushParameterFrame(id); if (has(id, this.parameters)) { return (yield this.parameters[id].build(runningContext)); } throw new 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); } }