Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(@angular-devkit/architect): support all observable types as build…
… results

Fixes #14579
  • Loading branch information
clydin authored and alexeagle committed May 30, 2019
1 parent 231afee commit cf3e3af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 3 additions & 1 deletion etc/api/angular_devkit/architect/src/index.d.ts
Expand Up @@ -38,7 +38,7 @@ export declare type BuilderInput = json.JsonObject & RealBuilderInput;

export declare type BuilderOutput = json.JsonObject & RealBuilderOutput;

export declare type BuilderOutputLike = Observable<BuilderOutput> | Promise<BuilderOutput> | BuilderOutput;
export declare type BuilderOutputLike = SubscribableOrPromise<BuilderOutput> | BuilderOutput;

export declare type BuilderProgress = json.JsonObject & RealBuilderProgress & TypedBuilderProgress;

Expand All @@ -60,6 +60,8 @@ export interface BuilderRun {

export declare function createBuilder<OptT extends json.JsonObject, OutT extends BuilderOutput = BuilderOutput>(fn: BuilderHandlerFn<OptT>): Builder<OptT>;

export declare function isBuilderOutput(obj: any): obj is BuilderOutput;

export interface ScheduleOptions {
analytics?: analytics.Analytics;
logger?: logging.Logger;
Expand Down
12 changes: 10 additions & 2 deletions packages/angular_devkit/architect/src/api.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import { analytics, experimental, json, logging } from '@angular-devkit/core';
import { Observable, from } from 'rxjs';
import { Observable, SubscribableOrPromise, from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Schema as RealBuilderInput, Target as RealTarget } from './input-schema';
import { Schema as RealBuilderOutput } from './output-schema';
Expand Down Expand Up @@ -250,8 +250,16 @@ export interface BuilderContext {
/**
* An accepted return value from a builder. Can be either an Observable, a Promise or a vector.
*/
export type BuilderOutputLike = Observable<BuilderOutput> | Promise<BuilderOutput> | BuilderOutput;
export type BuilderOutputLike = SubscribableOrPromise<BuilderOutput> | BuilderOutput;

// tslint:disable-next-line:no-any
export function isBuilderOutput(obj: any): obj is BuilderOutput {
if (!obj || typeof obj.then === 'function' || typeof obj.subscribe === 'function') {
return false;
}

return typeof obj.success === 'boolean';
}

/**
* A builder handler function. The function signature passed to `createBuilder()`.
Expand Down
19 changes: 9 additions & 10 deletions packages/angular_devkit/architect/src/create-builder.ts
Expand Up @@ -5,20 +5,20 @@
* 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 { analytics, experimental, isPromise, json, logging } from '@angular-devkit/core';
import { Observable, Subscription, from, isObservable, of, throwError } from 'rxjs';
import { analytics, experimental, json, logging } from '@angular-devkit/core';
import { Observable, Subscription, from, of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import {
BuilderContext,
BuilderHandlerFn,
BuilderInfo,
BuilderInput,
BuilderOutput,
BuilderOutputLike,
BuilderProgressState,
ScheduleOptions,
Target,
TypedBuilderProgress,
isBuilderOutput,
targetStringFromTarget,
} from './api';
import { Builder, BuilderSymbol, BuilderVersionSymbol } from './internal';
Expand Down Expand Up @@ -189,19 +189,18 @@ export function createBuilder<
};

context.reportRunning();
let result: BuilderOutputLike;
let result;
try {
result = fn(i.options as OptT, context);
if (isBuilderOutput(result)) {
result = of(result);
} else {
result = from(result);
}
} catch (e) {
result = throwError(e);
}

if (isPromise(result)) {
result = from(result);
} else if (!isObservable(result)) {
result = of(result);
}

// Manage some state automatically.
progress({ state: BuilderProgressState.Running, current: 0, total: 1 }, context);
subscriptions.push(result.pipe(
Expand Down

0 comments on commit cf3e3af

Please sign in to comment.