@carlosv2/glue
Version:
Dependency injection library that stays out of the way
65 lines (64 loc) • 2.79 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.FallbackContainer = void 0;
const index_1 = require("../context/index");
const aggregated_1 = require("../error/aggregated");
const parameter_1 = require("../error/parameter");
const service_1 = require("../error/service");
const utils_1 = require("../utils");
class FallbackContainer {
constructor(...containers) {
this.containers = containers;
}
get(id, context) {
return __awaiter(this, void 0, void 0, function* () {
const runningContext = context !== null && context !== void 0 ? context : new index_1.RunningContext(this);
const errors = [];
for (const container of this.containers) {
try {
return yield container.get(id, runningContext);
}
catch (e) {
if (!(e instanceof service_1.ServiceNotFoundError)) {
throw e;
}
errors.push(e);
}
}
throw new aggregated_1.AggregatedError(...errors);
});
}
getParameter(id, context) {
return __awaiter(this, void 0, void 0, function* () {
const runningContext = context !== null && context !== void 0 ? context : new index_1.RunningContext(this);
const errors = [];
for (const container of this.containers) {
try {
return yield container.getParameter(id, runningContext);
}
catch (e) {
if (!(e instanceof parameter_1.ParameterNotFoundError)) {
throw e;
}
errors.push(e);
}
}
throw new aggregated_1.AggregatedError(...errors);
});
}
findServiceIdsByTag(tag) {
return (0, utils_1.uniq)(this.containers
.map(container => container.findServiceIdsByTag(tag))
.flat());
}
}
exports.FallbackContainer = FallbackContainer;