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‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ export function scheduleTargetAndForget(context: BuilderContext, target: Target,
451451

452452
// @public
453453
interfaceSimpleJobHandlerContext<AextendsJsonValue, IextendsJsonValue, OextendsJsonValue> extendsJobHandlerContext<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‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
4747
constscheduler=context.scheduler;
4848
constprogressChannel=context.createChannel('progress');
4949
constlogChannel=context.createChannel('log');
50+
constaddTeardown=context.addTeardown.bind(context);
5051
letcurrentState: BuilderProgressState=BuilderProgressState.Stopped;
51-
constteardownLogics: Array<()=>PromiseLike<void>|void>=[];
52-
lettearingDown=false;
5352
letcurrent=0;
5453
letstatus='';
5554
lettotal=1;
@@ -83,18 +82,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
8382

8483
constinputSubscription=context.inboundBus.subscribe((i)=>{
8584
switch(i.kind){
86-
caseJobInboundMessageKind.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
caseJobInboundMessageKind.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‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface SimpleJobHandlerContext<
4444
>extendsJobHandlerContext<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
constinboundBus=context.inboundBus;
7374
constinputChannel=newSubject<I>();
7475
letsubscription: Subscription;
76+
constteardownLogics: Array<()=>PromiseLike<void>|void>=[];
77+
lettearingDown=false;
7578

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

9396
caseJobInboundMessageKind.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
caseJobInboundMessageKind.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
constnewContext: 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
thrownewChannelAlreadyExistException(name);

0 commit comments

Comments
(0)