UNPKG

@lillallol/dic

Version:

My own dependency injection container.

87 lines (86 loc) 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateDependencyGraph = void 0; const getRegistrationStrictOf_1 = require("../Dic/getRegistrationStrictOf"); const errorMessages_1 = require("../errorMessages"); const getIndexOfFirstDuplicateValueOfArray_1 = require("../es-utils/getIndexOfFirstDuplicateValueOfArray"); const getFirstOverlappingElementData_1 = require("../es-utils/getFirstOverlappingElementData"); const detectCycles_1 = require("./detectCycles"); const isLiveAbstraction_1 = require("./isLiveAbstraction"); const throwIfExtraAbstractionsFromTypes_1 = require("./throwIfExtraAbstractionsFromTypes"); const throwIfMissingAbstractionsFromTypes_1 = require("./throwIfMissingAbstractionsFromTypes"); const validateDependencyGraph = function validateDependencyGraph(parameters) { const { dic, entryPointAbstractions, TYPES } = parameters; // initialize parameters let { ignoreAbstractions } = parameters; if (ignoreAbstractions === undefined) ignoreAbstractions = []; // throw for duplicate value in entry point abstractions array const entryPointAbstractionsDuplicateIndex = getIndexOfFirstDuplicateValueOfArray_1.getIndexOfFirstDuplicateValueOfArray({ array: entryPointAbstractions, }); if (entryPointAbstractionsDuplicateIndex !== -1) { throw Error(errorMessages_1.errorMessages.entryPointAbstractionsArrayHasDuplicateValue({ duplicateValueIndex: entryPointAbstractionsDuplicateIndex, entryPointAbstractions, })); } // throw for duplicate value in ignore abstractions array const ignoreAbstractionsDuplicateIndex = getIndexOfFirstDuplicateValueOfArray_1.getIndexOfFirstDuplicateValueOfArray({ array: ignoreAbstractions }); if (ignoreAbstractionsDuplicateIndex !== -1) { throw Error(errorMessages_1.errorMessages.ignoreAbstractionsArrayHasDuplicateValue({ duplicateValueIndex: ignoreAbstractionsDuplicateIndex, ignoreAbstractions, })); } // throw for overlapping abstraction between ignore abstractions and entry point abstractions const indexes = getFirstOverlappingElementData_1.getFirstOverlappingElementData({ array1: entryPointAbstractions, array2: ignoreAbstractions, }); if (indexes !== null) { const overlappingElement = indexes.overlappingElement; throw Error(errorMessages_1.errorMessages.overlapBetweenEntryPointAndIngoredAbstractions(overlappingElement)); } // throw for ignored abstraction not registered to the dic //@TODO make it throw error with all the ignored abstractions that are not registered to the dic ignoreAbstractions.forEach((ignoredAbstraction) => { try { getRegistrationStrictOf_1.getRegistrationStrictOf(dic.registry, ignoredAbstraction); } catch { throw Error(errorMessages_1.errorMessages.ignoredAbstractionNotRegisteredToTheDic(ignoredAbstraction)); } }); // throw for cycles in the dependency graph detectCycles_1.detectCycles({ dic, entryPointAbstractions, }); // throw for dead registrations const hasBeenIterated = isLiveAbstraction_1.isLiveAbstraction({ dic, entryPointAbstractions, ignoreAbstractions, }); const nonIteratedAbstractions = [...hasBeenIterated.entries()].filter(([, v]) => v === false).map(([k]) => k); if (nonIteratedAbstractions.length !== 0) { throw Error(errorMessages_1.errorMessages.abstractionsAreNotUsed({ deadAbstractions: nonIteratedAbstractions, entryPointAbstractions, })); } // throwIfMissingAbstractionsFromTypes_1.throwIfMissingAbstractionsFromTypes({ TYPES, entryPointAbstractions, dic, }); // throwIfExtraAbstractionsFromTypes_1.throwIfExtraAbstractionsFromTypes({ TYPES, dic, entryPointAbstractions, }); }; exports.validateDependencyGraph = validateDependencyGraph;