@lillallol/dic
Version:
My own dependency injection container.
63 lines (61 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.printDependencyGraph = void 0;
const getRegistrationStrictOf_1 = require("../Dic/getRegistrationStrictOf");
const symbolToTypesKeyFactory_1 = require("./symbolToTypesKeyFactory");
const tagUnindent_1 = require("../es-utils/tagUnindent");
const constants_1 = require("../constants");
const printDependencyGraph = function printDependencyGraph(parameters) {
const { dic, rootAbstraction, TYPES: types } = parameters;
const { stem, subGraphHasBeenAlreadyPrintedSymbol, twig } = constants_1.constants;
const symbolToTypesPropertyPath = symbolToTypesKeyFactory_1.symbolToTypesKeyFactory(types);
let depth = 1;
let maxDepth = 0;
let totalNumberOfComponents = 0;
const isCommon = new Map();
let stringToReturn = symbolToTypesPropertyPath(rootAbstraction);
const intend = [];
if (twig.length - stem.length < 0) {
throw Error(tagUnindent_1.tagUnindent `
Internal library error:
Twig length has to be greater than stem length.
`);
}
const twigTypeSeparator = " ";
(function recurse(currentAbstraction, identString) {
if (depth > maxDepth)
maxDepth = depth;
const registration = getRegistrationStrictOf_1.getRegistrationStrictOf(dic.registry, currentAbstraction);
if (!isCommon.has(currentAbstraction))
totalNumberOfComponents++;
const { dependencies } = registration;
const lastI = dependencies.length - 1;
if (isCommon.has(currentAbstraction) && dependencies.length !== 0) {
stringToReturn += " " + subGraphHasBeenAlreadyPrintedSymbol;
return;
}
else {
isCommon.set(currentAbstraction, true);
}
for (let i = 0; i < dependencies.length; i++) {
const nextDependency = dependencies[i];
stringToReturn +=
"\n" + intend.join("") + twig + twigTypeSeparator + symbolToTypesPropertyPath(nextDependency);
depth++;
if (i === lastI) {
intend.push(" ".repeat(twig.length) + twigTypeSeparator);
recurse(nextDependency, identString);
intend.pop();
}
else {
intend.push(stem + " ".repeat(twig.length - stem.length) + twigTypeSeparator);
recurse(nextDependency, identString);
intend.pop();
}
depth--;
}
})(rootAbstraction, "");
stringToReturn = `total number of unique components: ${totalNumberOfComponents}\n` + "\n" + stringToReturn;
return stringToReturn.trim();
};
exports.printDependencyGraph = printDependencyGraph;