[6a3a178] | 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 | */
|
---|
| 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 | * When a rule or source returns an invalid value.
|
---|
| 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 (rxjs_1.isObservable(result)) {
|
---|
| 58 | // Only return the last Tree, and make sure it's a Tree.
|
---|
| 59 | return result.pipe(operators_1.defaultIfEmpty(), operators_1.last(), 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 rxjs_1.of(result);
|
---|
| 67 | }
|
---|
| 68 | else {
|
---|
| 69 | return rxjs_1.throwError(new InvalidSourceResultException(result));
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | exports.callSource = callSource;
|
---|
| 73 | function callRule(rule, input, context) {
|
---|
| 74 | return (rxjs_1.isObservable(input) ? input : rxjs_1.of(input)).pipe(operators_1.mergeMap((inputTree) => {
|
---|
| 75 | const result = rule(inputTree, context);
|
---|
| 76 | if (!result) {
|
---|
| 77 | return 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 (rxjs_1.isObservable(result)) {
|
---|
| 84 | // Only return the last Tree, and make sure it's a Tree.
|
---|
| 85 | return result.pipe(operators_1.defaultIfEmpty(), operators_1.last(), operators_1.tap((inner) => {
|
---|
| 86 | if (!inner || !(interface_1.TreeSymbol in inner)) {
|
---|
| 87 | throw new InvalidRuleResultException(inner);
|
---|
| 88 | }
|
---|
| 89 | }));
|
---|
| 90 | }
|
---|
| 91 | else if (core_1.isPromise(result)) {
|
---|
| 92 | return rxjs_1.from(result).pipe(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 rxjs_1.of(inputTree);
|
---|
| 99 | }
|
---|
| 100 | }));
|
---|
| 101 | }
|
---|
| 102 | else if (interface_1.TreeSymbol in result) {
|
---|
| 103 | return rxjs_1.of(result);
|
---|
| 104 | }
|
---|
| 105 | else {
|
---|
| 106 | return rxjs_1.throwError(new InvalidRuleResultException(result));
|
---|
| 107 | }
|
---|
| 108 | }));
|
---|
| 109 | }
|
---|
| 110 | exports.callRule = callRule;
|
---|