UNPKG

@lillallol/dic

Version:

My own dependency injection container.

43 lines (42 loc) 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectCycles = void 0; const getRegistrationStrictOf_1 = require("../Dic/getRegistrationStrictOf"); const errorMessages_1 = require("../errorMessages"); function detectCycles(_) { const { dic, entryPointAbstractions } = _; const hasNoCycles = new Set(); const path = new Set(); const hasNoChildren = (abstraction) => { return getDependencies(abstraction).length === 0; }; const getDependencies = (abstraction) => getRegistrationStrictOf_1.getRegistrationStrictOf(dic.registry, abstraction).dependencies; entryPointAbstractions.forEach((abstraction) => { if (hasNoCycles.has(abstraction)) { return; } if (hasNoChildren(abstraction)) { hasNoCycles.add(abstraction); return; } path.add(abstraction); (function recurse(abstraction) { getDependencies(abstraction).forEach((abstraction) => { if (path.has(abstraction)) { throw Error(errorMessages_1.errorMessages.dependencyGraphHasCycle(path, abstraction)); } if (hasNoCycles.has(abstraction)) return; if (hasNoChildren(abstraction)) { hasNoCycles.add(abstraction); return; } path.add(abstraction); recurse(abstraction); path.delete(abstraction); }); })(abstraction); path.delete(abstraction); }); } exports.detectCycles = detectCycles;