UNPKG

3.13 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 interface_1 = require("../tree/interface");
14function _getTypeOfResult(value) {
15 if (value === undefined) {
16 return 'undefined';
17 }
18 else if (value === null) {
19 return 'null';
20 }
21 else if (typeof value == 'function') {
22 return `Function()`;
23 }
24 else if (typeof value != 'object') {
25 return `${typeof value}(${JSON.stringify(value)})`;
26 }
27 else {
28 if (Object.getPrototypeOf(value) == Object) {
29 return `Object(${JSON.stringify(value)})`;
30 }
31 else if (value.constructor) {
32 return `Instance of class ${value.constructor.name}`;
33 }
34 else {
35 return 'Unknown Object';
36 }
37 }
38}
39/**
40 * When a rule or source returns an invalid value.
41 */
42class InvalidRuleResultException extends core_1.BaseException {
43 constructor(value) {
44 super(`Invalid rule result: ${_getTypeOfResult(value)}.`);
45 }
46}
47exports.InvalidRuleResultException = InvalidRuleResultException;
48class InvalidSourceResultException extends core_1.BaseException {
49 constructor(value) {
50 super(`Invalid source result: ${_getTypeOfResult(value)}.`);
51 }
52}
53exports.InvalidSourceResultException = InvalidSourceResultException;
54function callSource(source, context) {
55 return (0, rxjs_1.defer)(async () => {
56 let result = source(context);
57 if ((0, rxjs_1.isObservable)(result)) {
58 result = await (0, rxjs_1.lastValueFrom)(result.pipe((0, rxjs_1.defaultIfEmpty)(undefined)));
59 }
60 if (result && interface_1.TreeSymbol in result) {
61 return result;
62 }
63 throw new InvalidSourceResultException(result);
64 });
65}
66exports.callSource = callSource;
67function callRule(rule, input, context) {
68 if ((0, rxjs_1.isObservable)(input)) {
69 return input.pipe((0, rxjs_1.mergeMap)((inputTree) => callRuleAsync(rule, inputTree, context)));
70 }
71 else {
72 return (0, rxjs_1.defer)(() => callRuleAsync(rule, input, context));
73 }
74}
75exports.callRule = callRule;
76async function callRuleAsync(rule, tree, context) {
77 let result = await rule(tree, context);
78 while (typeof result === 'function') {
79 // This is considered a Rule, chain the rule and return its output.
80 result = await result(tree, context);
81 }
82 if (typeof result === 'undefined') {
83 return tree;
84 }
85 if ((0, rxjs_1.isObservable)(result)) {
86 result = await (0, rxjs_1.lastValueFrom)(result.pipe((0, rxjs_1.defaultIfEmpty)(tree)));
87 }
88 if (result && interface_1.TreeSymbol in result) {
89 return result;
90 }
91 throw new InvalidRuleResultException(result);
92}