1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.callRule = exports.callSource = exports.InvalidSourceResultException = exports.InvalidRuleResultException = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const rxjs_1 = require("rxjs");
|
13 | const operators_1 = require("rxjs/operators");
|
14 | const interface_1 = require("../tree/interface");
|
15 | function _getTypeOfResult(value) {
|
16 | if (value === undefined) {
|
17 | return 'undefined';
|
18 | }
|
19 | else if (value === null) {
|
20 | return 'null';
|
21 | }
|
22 | else if (typeof value == 'function') {
|
23 | return `Function()`;
|
24 | }
|
25 | else if (typeof value != 'object') {
|
26 | return `${typeof value}(${JSON.stringify(value)})`;
|
27 | }
|
28 | else {
|
29 | if (Object.getPrototypeOf(value) == Object) {
|
30 | return `Object(${JSON.stringify(value)})`;
|
31 | }
|
32 | else if (value.constructor) {
|
33 | return `Instance of class ${value.constructor.name}`;
|
34 | }
|
35 | else {
|
36 | return 'Unknown Object';
|
37 | }
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | class InvalidRuleResultException extends core_1.BaseException {
|
44 | constructor(value) {
|
45 | super(`Invalid rule result: ${_getTypeOfResult(value)}.`);
|
46 | }
|
47 | }
|
48 | exports.InvalidRuleResultException = InvalidRuleResultException;
|
49 | class InvalidSourceResultException extends core_1.BaseException {
|
50 | constructor(value) {
|
51 | super(`Invalid source result: ${_getTypeOfResult(value)}.`);
|
52 | }
|
53 | }
|
54 | exports.InvalidSourceResultException = InvalidSourceResultException;
|
55 | function callSource(source, context) {
|
56 | const result = source(context);
|
57 | if ((0, rxjs_1.isObservable)(result)) {
|
58 |
|
59 | return result.pipe((0, operators_1.defaultIfEmpty)(), (0, operators_1.last)(), (0, operators_1.tap)((inner) => {
|
60 | if (!inner || !(interface_1.TreeSymbol in inner)) {
|
61 | throw new InvalidSourceResultException(inner);
|
62 | }
|
63 | }));
|
64 | }
|
65 | else if (result && interface_1.TreeSymbol in result) {
|
66 | return (0, rxjs_1.of)(result);
|
67 | }
|
68 | else {
|
69 | return (0, rxjs_1.throwError)(new InvalidSourceResultException(result));
|
70 | }
|
71 | }
|
72 | exports.callSource = callSource;
|
73 | function callRule(rule, input, context) {
|
74 | return ((0, rxjs_1.isObservable)(input) ? input : (0, rxjs_1.of)(input)).pipe((0, operators_1.mergeMap)((inputTree) => {
|
75 | const result = rule(inputTree, context);
|
76 | if (!result) {
|
77 | return (0, rxjs_1.of)(inputTree);
|
78 | }
|
79 | else if (typeof result == 'function') {
|
80 |
|
81 | return callRule(result, inputTree, context);
|
82 | }
|
83 | else if ((0, rxjs_1.isObservable)(result)) {
|
84 |
|
85 | return result.pipe((0, operators_1.defaultIfEmpty)(), (0, operators_1.last)(), (0, operators_1.tap)((inner) => {
|
86 | if (!inner || !(interface_1.TreeSymbol in inner)) {
|
87 | throw new InvalidRuleResultException(inner);
|
88 | }
|
89 | }));
|
90 | }
|
91 | else if ((0, core_1.isPromise)(result)) {
|
92 | return (0, rxjs_1.from)(result).pipe((0, operators_1.mergeMap)((inner) => {
|
93 | if (typeof inner === 'function') {
|
94 |
|
95 | return callRule(inner, inputTree, context);
|
96 | }
|
97 | else {
|
98 | return (0, rxjs_1.of)(inputTree);
|
99 | }
|
100 | }));
|
101 | }
|
102 | else if (interface_1.TreeSymbol in result) {
|
103 | return (0, rxjs_1.of)(result);
|
104 | }
|
105 | else {
|
106 | return (0, rxjs_1.throwError)(new InvalidRuleResultException(result));
|
107 | }
|
108 | }));
|
109 | }
|
110 | exports.callRule = callRule;
|