1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | class CircularOperationValidatorError extends Error {
|
4 | constructor(message) {
|
5 | super();
|
6 | this.message = message;
|
7 | }
|
8 | }
|
9 | class CircularOperationValidator {
|
10 | |
11 |
|
12 |
|
13 | constructor(operations) {
|
14 | this.operations = operations;
|
15 | this._checkCircular();
|
16 | }
|
17 | _checkCircular() {
|
18 | this.operations.forEach((operation) => {
|
19 | operation.dependencies.forEach(op => {
|
20 | let map = {};
|
21 | map[operation.id] = Object.keys(map).length;
|
22 | this._verifyOpMap(op, map);
|
23 | this._checkDependencies(op, map);
|
24 | });
|
25 | });
|
26 | }
|
27 | _checkDependencies(operation, mapHash) {
|
28 | operation.dependencies.forEach((op) => {
|
29 | let map = JSON.parse(JSON.stringify(mapHash));
|
30 | this._verifyOpMap(op, map);
|
31 | this._checkDependencies(op, map);
|
32 | });
|
33 | }
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 | _verifyOpMap(op, map) {
|
40 | if (map[op.id] !== undefined) {
|
41 | this._throwError(op, map);
|
42 | }
|
43 | map[op.id] = Object.keys(map).length;
|
44 | }
|
45 | _throwError(op, map) {
|
46 | let mapValues = {};
|
47 | for (let key in map) {
|
48 | const value = map[key];
|
49 | mapValues[value] = key;
|
50 | }
|
51 | const values = Object.values(mapValues);
|
52 | values.push(op.id);
|
53 | throw new CircularOperationValidatorError(`Circular: ${values}`);
|
54 | }
|
55 | }
|
56 | exports.CircularOperationValidator = CircularOperationValidator;
|
57 |
|
\ | No newline at end of file |