dependency-injection-cat
Version:
DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!
69 lines (68 loc) • 2.92 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyGraph = void 0;
var graphlib_1 = require("graphlib");
var BeanRepository_1 = require("../bean/BeanRepository");
var DependencyGraph = /** @class */ (function () {
function DependencyGraph() {
}
DependencyGraph.addNodeWithEdges = function (node, edges) {
var _this = this;
this.graph.setNodes(__spreadArray([
node.id
], __read(edges.map(function (it) { return it.id; })), false));
edges.forEach(function (edge) { return _this.graph.setEdge(node.id, edge.id); });
};
DependencyGraph.getCycle = function () {
var cycleIds = graphlib_1.alg.findCycles(this.graph);
var resultMap = new Map();
cycleIds.forEach(function (list) {
var _a;
var descriptorList = list
.map(function (beanId) { var _a; return (_a = BeanRepository_1.BeanRepository.beanIdToBeanDescriptorMap.get(beanId)) !== null && _a !== void 0 ? _a : null; })
.filter(function (it) { return it !== null; });
var addedDescriptorList = (_a = resultMap.get(descriptorList[0].contextDescriptor.name)) !== null && _a !== void 0 ? _a : null;
if (addedDescriptorList === null) {
addedDescriptorList = [];
resultMap.set(descriptorList[0].contextDescriptor.name, addedDescriptorList);
}
addedDescriptorList.push(descriptorList);
});
return resultMap;
};
DependencyGraph.clearByContextDescriptor = function (contextDescriptor) {
var _this = this;
var _a;
((_a = BeanRepository_1.BeanRepository.contextIdToBeanDescriptorsMap.get(contextDescriptor.id)) !== null && _a !== void 0 ? _a : []).forEach(function (descriptor) {
_this.graph.removeNode(descriptor.id);
});
};
DependencyGraph.graph = new graphlib_1.Graph({ directed: true });
return DependencyGraph;
}());
exports.DependencyGraph = DependencyGraph;