-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcall.ts
99 lines (84 loc) · 2.69 KB
/
call.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException } from '@angular-devkit/core';
import { Observable, defer, isObservable } from 'rxjs';
import { defaultIfEmpty, mergeMap } from 'rxjs/operators';
import { Rule, SchematicContext, Source } from '../engine/interface';
import { Tree, TreeSymbol } from '../tree/interface';
function _getTypeOfResult(value?: {}): string {
if (value === undefined) {
return 'undefined';
} else if (value === null) {
return 'null';
} else if (typeof value == 'function') {
return `Function()`;
} else if (typeof value != 'object') {
return `${typeof value}(${JSON.stringify(value)})`;
} else {
if (Object.getPrototypeOf(value) == Object) {
return `Object(${JSON.stringify(value)})`;
} else if (value.constructor) {
return `Instance of class ${value.constructor.name}`;
} else {
return 'Unknown Object';
}
}
}
/**
* When a rule or source returns an invalid value.
*/
export class InvalidRuleResultException extends BaseException {
constructor(value?: {}) {
super(`Invalid rule result: ${_getTypeOfResult(value)}.`);
}
}
export class InvalidSourceResultException extends BaseException {
constructor(value?: {}) {
super(`Invalid source result: ${_getTypeOfResult(value)}.`);
}
}
export function callSource(source: Source, context: SchematicContext): Observable<Tree> {
return defer(async () => {
let result = source(context);
if (isObservable(result)) {
result = await result.pipe(defaultIfEmpty()).toPromise();
}
if (result && TreeSymbol in result) {
return result as Tree;
}
throw new InvalidSourceResultException(result);
});
}
export function callRule(
rule: Rule,
input: Tree | Observable<Tree>,
context: SchematicContext,
): Observable<Tree> {
if (isObservable(input)) {
return input.pipe(mergeMap((inputTree) => callRuleAsync(rule, inputTree, context)));
} else {
return defer(() => callRuleAsync(rule, input, context));
}
}
async function callRuleAsync(rule: Rule, tree: Tree, context: SchematicContext): Promise<Tree> {
let result = await rule(tree, context);
while (typeof result === 'function') {
// This is considered a Rule, chain the rule and return its output.
result = await result(tree, context);
}
if (typeof result === 'undefined') {
return tree;
}
if (isObservable(result)) {
result = await result.pipe(defaultIfEmpty(tree)).toPromise();
}
if (TreeSymbol in result) {
return result as Tree;
}
throw new InvalidRuleResultException(result);
}