Skip to content

Commit 4887138

Browse files
clydinangular-robot[bot]
authored andcommitted
fix(@angular-devkit/architect): allow registered builder teardowns to execute
Previously, the base job handler was completing the entire job before any teardowns could attempt to execute.
1 parent 67670b6 commit 4887138

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

goldens/public-api/angular_devkit/architect/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ export function scheduleTargetAndForget(context: BuilderContext, target: Target,
451451

452452
// @public
453453
interface SimpleJobHandlerContext<A extends JsonValue, I extends JsonValue, O extends JsonValue> extends JobHandlerContext<A, I, O> {
454+
// (undocumented)
455+
addTeardown(teardown: () => Promise<void> | void): void;
454456
// (undocumented)
455457
createChannel: (name: string) => Observer<JsonValue>;
456458
// (undocumented)

packages/angular_devkit/architect/src/create-builder.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
4747
const scheduler = context.scheduler;
4848
const progressChannel = context.createChannel('progress');
4949
const logChannel = context.createChannel('log');
50+
const addTeardown = context.addTeardown.bind(context);
5051
let currentState: BuilderProgressState = BuilderProgressState.Stopped;
51-
const teardownLogics: Array<() => PromiseLike<void> | void> = [];
52-
let tearingDown = false;
5352
let current = 0;
5453
let status = '';
5554
let total = 1;
@@ -83,18 +82,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
8382

8483
const inputSubscription = context.inboundBus.subscribe((i) => {
8584
switch (i.kind) {
86-
case JobInboundMessageKind.Stop:
87-
// Run teardown logic then complete.
88-
tearingDown = true;
89-
Promise.all(teardownLogics.map((fn) => fn() || Promise.resolve())).then(
90-
() => observer.complete(),
91-
(err) => observer.error(err),
92-
);
93-
break;
9485
case JobInboundMessageKind.Input:
95-
if (!tearingDown) {
96-
onInput(i.value);
97-
}
86+
onInput(i.value);
9887
break;
9988
}
10089
});
@@ -209,9 +198,7 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
209198
progress({ state: currentState, current, total, status }, context);
210199
}
211200
},
212-
addTeardown(teardown: () => Promise<void> | void): void {
213-
teardownLogics.push(teardown);
214-
},
201+
addTeardown,
215202
};
216203

217204
context.reportRunning();

packages/angular_devkit/architect/src/jobs/create-job-handler.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface SimpleJobHandlerContext<
4444
> extends JobHandlerContext<A, I, O> {
4545
createChannel: (name: string) => Observer<JsonValue>;
4646
input: Observable<I>;
47+
addTeardown(teardown: () => Promise<void> | void): void;
4748
}
4849

4950
/**
@@ -72,6 +73,8 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
7273
const inboundBus = context.inboundBus;
7374
const inputChannel = new Subject<I>();
7475
let subscription: Subscription;
76+
const teardownLogics: Array<() => PromiseLike<void> | void> = [];
77+
let tearingDown = false;
7578

7679
return new Observable<JobOutboundMessage<O>>((subject) => {
7780
function complete() {
@@ -91,13 +94,22 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
9194
break;
9295

9396
case JobInboundMessageKind.Stop:
94-
// There's no way to cancel a promise or a synchronous function, but we do cancel
95-
// observables where possible.
96-
complete();
97+
// Run teardown logic then complete.
98+
tearingDown = true;
99+
if (teardownLogics.length) {
100+
Promise.all(teardownLogics.map((fn) => fn())).then(
101+
() => complete(),
102+
() => complete(),
103+
);
104+
} else {
105+
complete();
106+
}
97107
break;
98108

99109
case JobInboundMessageKind.Input:
100-
inputChannel.next(message.value);
110+
if (!tearingDown) {
111+
inputChannel.next(message.value);
112+
}
101113
break;
102114
}
103115
});
@@ -108,6 +120,9 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
108120
const newContext: SimpleJobHandlerContext<A, I, O> = {
109121
...context,
110122
input: inputChannel.asObservable(),
123+
addTeardown(teardown: () => Promise<void> | void): void {
124+
teardownLogics.push(teardown);
125+
},
111126
createChannel(name: string) {
112127
if (channels.has(name)) {
113128
throw new ChannelAlreadyExistException(name);

0 commit comments

Comments
 (0)