UNPKG

4.12 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.callRule = exports.callSource = exports.InvalidSourceResultException = exports.InvalidRuleResultException = void 0;
11const core_1 = require("@angular-devkit/core");
12const rxjs_1 = require("rxjs");
13const operators_1 = require("rxjs/operators");
14const interface_1 = require("../tree/interface");
15function _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 * When a rule or source returns an invalid value.
42 */
43class InvalidRuleResultException extends core_1.BaseException {
44 constructor(value) {
45 super(`Invalid rule result: ${_getTypeOfResult(value)}.`);
46 }
47}
48exports.InvalidRuleResultException = InvalidRuleResultException;
49class InvalidSourceResultException extends core_1.BaseException {
50 constructor(value) {
51 super(`Invalid source result: ${_getTypeOfResult(value)}.`);
52 }
53}
54exports.InvalidSourceResultException = InvalidSourceResultException;
55function callSource(source, context) {
56 const result = source(context);
57 if ((0, rxjs_1.isObservable)(result)) {
58 // Only return the last Tree, and make sure it's a Tree.
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}
72exports.callSource = callSource;
73function 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 // This is considered a Rule, chain the rule and return its output.
81 return callRule(result, inputTree, context);
82 }
83 else if ((0, rxjs_1.isObservable)(result)) {
84 // Only return the last Tree, and make sure it's a Tree.
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 // This is considered a Rule, chain the rule and return its output.
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}
110exports.callRule = callRule;