8
8
import { BaseException } from '@angular-devkit/core' ;
9
9
import { Observable } from 'rxjs/Observable' ;
10
10
import { Rule , SchematicContext , Source } from '../engine/interface' ;
11
- import { Tree } from '../tree/interface' ;
12
- import { VirtualTree } from '../tree/virtual' ;
11
+ import { Tree , TreeSymbol } from '../tree/interface' ;
13
12
14
13
15
14
declare const Symbol : Symbol & {
16
15
readonly observable : symbol ;
17
16
} ;
18
17
19
18
19
+ function _getTypeOfResult ( value ?: { } ) : string {
20
+ if ( value === undefined ) {
21
+ return 'undefined' ;
22
+ } else if ( value === null ) {
23
+ return 'null' ;
24
+ } else if ( typeof value == 'function' ) {
25
+ return `Function()` ;
26
+ } else if ( typeof value != 'object' ) {
27
+ return `${ typeof value } (${ JSON . stringify ( value ) } )` ;
28
+ } else {
29
+ if ( Object . getPrototypeOf ( value ) == Object ) {
30
+ return `Object(${ JSON . stringify ( value ) } )` ;
31
+ } else if ( value . constructor ) {
32
+ return `Instance of class ${ value . constructor . name } ` ;
33
+ } else {
34
+ return 'Unknown Object' ;
35
+ }
36
+ }
37
+ }
38
+
39
+
20
40
/**
21
41
* When a rule or source returns an invalid value.
22
42
*/
23
43
export class InvalidRuleResultException extends BaseException {
24
44
constructor ( value ?: { } ) {
25
- let v = 'Unknown Type' ;
26
- if ( value === undefined ) {
27
- v = 'undefined' ;
28
- } else if ( value === null ) {
29
- v = 'null' ;
30
- } else if ( typeof value == 'function' ) {
31
- v = `Function()` ;
32
- } else if ( typeof value != 'object' ) {
33
- v = `${ typeof value } (${ JSON . stringify ( value ) } )` ;
34
- } else {
35
- if ( Object . getPrototypeOf ( value ) == Object ) {
36
- v = `Object(${ JSON . stringify ( value ) } )` ;
37
- } else if ( value . constructor ) {
38
- v = `Instance of class ${ value . constructor . name } ` ;
39
- } else {
40
- v = 'Unknown Object' ;
41
- }
42
- }
43
- super ( `Invalid rule or source result: ${ v } .` ) ;
45
+ super ( `Invalid rule result: ${ _getTypeOfResult ( value ) } .` ) ;
46
+ }
47
+ }
48
+
49
+
50
+ export class InvalidSourceResultException extends BaseException {
51
+ constructor ( value ?: { } ) {
52
+ super ( `Invalid source result: ${ _getTypeOfResult ( value ) } .` ) ;
44
53
}
45
54
}
46
55
47
56
48
57
export function callSource ( source : Source , context : SchematicContext ) : Observable < Tree > {
49
- const result = source ( context ) ;
58
+ const result = source ( context ) as object ;
50
59
51
- if ( result instanceof VirtualTree ) {
52
- return Observable . of ( result ) ;
60
+ if ( result === undefined ) {
61
+ throw new InvalidSourceResultException ( result ) ;
62
+ } else if ( TreeSymbol in result ) {
63
+ return Observable . of ( result as Tree ) ;
53
64
} else if ( Symbol . observable in result ) {
54
65
return result as Observable < Tree > ;
55
66
} else {
56
- throw new InvalidRuleResultException ( result ) ;
67
+ throw new InvalidSourceResultException ( result ) ;
57
68
}
58
69
}
59
70
@@ -64,7 +75,9 @@ export function callRule(rule: Rule,
64
75
return input . mergeMap ( inputTree => {
65
76
const result = rule ( inputTree , context ) as object ;
66
77
67
- if ( result instanceof VirtualTree ) {
78
+ if ( result === undefined ) {
79
+ return Observable . of ( inputTree ) ;
80
+ } else if ( TreeSymbol in result ) {
68
81
return Observable . of ( result as Tree ) ;
69
82
} else if ( Symbol . observable in result ) {
70
83
return result as Observable < Tree > ;
0 commit comments