@carlosv2/glue
Version:
Dependency injection library that stays out of the way
61 lines (60 loc) • 2.59 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());
});
};
import { RunningContext } from '../context/index.js';
import { AggregatedError } from '../error/aggregated.js';
import { ParameterNotFoundError } from '../error/parameter.js';
import { ServiceNotFoundError } from '../error/service.js';
import { uniq } from '../utils.js';
export 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 RunningContext(this);
const errors = [];
for (const container of this.containers) {
try {
return yield container.get(id, runningContext);
}
catch (e) {
if (!(e instanceof ServiceNotFoundError)) {
throw e;
}
errors.push(e);
}
}
throw new AggregatedError(...errors);
});
}
getParameter(id, context) {
return __awaiter(this, void 0, void 0, function* () {
const runningContext = context !== null && context !== void 0 ? context : new RunningContext(this);
const errors = [];
for (const container of this.containers) {
try {
return yield container.getParameter(id, runningContext);
}
catch (e) {
if (!(e instanceof ParameterNotFoundError)) {
throw e;
}
errors.push(e);
}
}
throw new AggregatedError(...errors);
});
}
findServiceIdsByTag(tag) {
return uniq(this.containers
.map(container => container.findServiceIdsByTag(tag))
.flat());
}
}