Skip to content

Commit

Permalink
feat(@angular-devkit/schematics): support basic promise/async based r…
Browse files Browse the repository at this point in the history
…ules (#13660)

Currently, all third-party schematic developers are forced to use and directly depend on `rxjs` if any logic is asynchronous.   Doing so can can also add overhead and unneeded complexity for organizations that have chosen to standardize on async/await usage. This change allows such parties to rely on native promise support if desired.
  • Loading branch information
clydin authored and vikerman committed Mar 7, 2019
1 parent fd5cb7a commit 324d9f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion etc/api/angular_devkit/schematics/src/_golden-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export interface RequiredWorkflowExecutionContext {
schematic: string;
}

export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;
export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | Promise<void> | void;

export declare type RuleFactory<T extends object> = (options: T) => Rule;

Expand Down
3 changes: 2 additions & 1 deletion packages/angular_devkit/schematics/src/engine/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@ export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null
* know which types is the schematic or collection metadata, as they are both tooling specific.
*/
export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;
export type Rule = (tree: Tree, context: SchematicContext) =>
Tree | Observable<Tree> | Rule | Promise<void> | void;
8 changes: 5 additions & 3 deletions packages/angular_devkit/schematics/src/rules/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 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, isObservable } from '@angular-devkit/core';
import { Observable, of as observableOf, throwError } from 'rxjs';
import { defaultIfEmpty, last, mergeMap, tap } from 'rxjs/operators';
import { BaseException, isObservable, isPromise } from '@angular-devkit/core';
import { Observable, from, of as observableOf, throwError } from 'rxjs';
import { defaultIfEmpty, last, map, mergeMap, tap } from 'rxjs/operators';
import { Rule, SchematicContext, Source } from '../engine/interface';
import { Tree, TreeSymbol } from '../tree/interface';

Expand Down Expand Up @@ -96,6 +96,8 @@ export function callRule(
}
}),
);
} else if (isPromise(result)) {
return from(result).pipe(map(() => inputTree));
} else if (TreeSymbol in result) {
return observableOf(result);
} else {
Expand Down

0 comments on commit 324d9f2

Please sign in to comment.