Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions goldens/public-api/angular_devkit/architect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ export function scheduleTargetAndForget(context: BuilderContext, target: Target,

// @public
interface SimpleJobHandlerContext<A extends JsonValue, I extends JsonValue, O extends JsonValue> extends JobHandlerContext<A, I, O> {
// (undocumented)
addTeardown(teardown: () => Promise<void> | void): void;
// (undocumented)
createChannel: (name: string) => Observer<JsonValue>;
// (undocumented)
Expand Down
19 changes: 3 additions & 16 deletions packages/angular_devkit/architect/src/create-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
const scheduler = context.scheduler;
const progressChannel = context.createChannel('progress');
const logChannel = context.createChannel('log');
const addTeardown = context.addTeardown.bind(context);
let currentState: BuilderProgressState = BuilderProgressState.Stopped;
const teardownLogics: Array<() => PromiseLike<void> | void> = [];
let tearingDown = false;
let current = 0;
let status = '';
let total = 1;
Expand Down Expand Up @@ -83,18 +82,8 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput

const inputSubscription = context.inboundBus.subscribe((i) => {
switch (i.kind) {
case JobInboundMessageKind.Stop:
// Run teardown logic then complete.
tearingDown = true;
Promise.all(teardownLogics.map((fn) => fn() || Promise.resolve())).then(
() => observer.complete(),
(err) => observer.error(err),
);
break;
case JobInboundMessageKind.Input:
if (!tearingDown) {
onInput(i.value);
}
onInput(i.value);
break;
}
});
Expand Down Expand Up @@ -209,9 +198,7 @@ export function createBuilder<OptT = json.JsonObject, OutT extends BuilderOutput
progress({ state: currentState, current, total, status }, context);
}
},
addTeardown(teardown: () => Promise<void> | void): void {
teardownLogics.push(teardown);
},
addTeardown,
};

context.reportRunning();
Expand Down
23 changes: 19 additions & 4 deletions packages/angular_devkit/architect/src/jobs/create-job-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface SimpleJobHandlerContext<
> extends JobHandlerContext<A, I, O> {
createChannel: (name: string) => Observer<JsonValue>;
input: Observable<I>;
addTeardown(teardown: () => Promise<void> | void): void;
}

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

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

case JobInboundMessageKind.Stop:
// There's no way to cancel a promise or a synchronous function, but we do cancel
// observables where possible.
complete();
// Run teardown logic then complete.
tearingDown = true;
if (teardownLogics.length) {
Promise.all(teardownLogics.map((fn) => fn())).then(
() => complete(),
() => complete(),
);
} else {
complete();
}
break;

case JobInboundMessageKind.Input:
inputChannel.next(message.value);
if (!tearingDown) {
inputChannel.next(message.value);
}
break;
}
});
Expand All @@ -108,6 +120,9 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
const newContext: SimpleJobHandlerContext<A, I, O> = {
...context,
input: inputChannel.asObservable(),
addTeardown(teardown: () => Promise<void> | void): void {
teardownLogics.push(teardown);
},
createChannel(name: string) {
if (channels.has(name)) {
throw new ChannelAlreadyExistException(name);
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ LARGE_SPECS = {
],
},
"browser-esbuild": {
"shards": 10,
"extra_deps": [
"@npm//buffer",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { BuilderContext } from '@angular-devkit/architect';
import type { json } from '@angular-devkit/core';
import assert from 'node:assert';
import { BinaryLike, createHash } from 'node:crypto';
import type { AddressInfo } from 'node:net';
import path from 'node:path';
import { InlineConfig, ViteDevServer, createServer, normalizePath } from 'vite';
import { buildEsbuildBrowser } from '../browser-esbuild';
Expand Down Expand Up @@ -51,6 +52,7 @@ export async function* serveWithVite(
)) as json.JsonObject & BrowserBuilderOptions;

let server: ViteDevServer | undefined;
let listeningAddress: AddressInfo | undefined;
const outputFiles = new Map<string, OutputFileRecord>();
const assets = new Map<string, string>();
// TODO: Switch this to an architect schedule call when infrastructure settings are supported
Expand Down Expand Up @@ -136,16 +138,24 @@ export async function* serveWithVite(
server = await setupServer(serverOptions, outputFiles, assets);

await server.listen();
listeningAddress = server.httpServer?.address() as AddressInfo;

// log connection information
server.printUrls();
}

// TODO: adjust output typings to reflect both development servers
yield { success: true } as unknown as DevServerBuilderOutput;
yield { success: true, port: listeningAddress?.port } as unknown as DevServerBuilderOutput;
}

await server?.close();
if (server) {
let deferred: () => void;
context.addTeardown(async () => {
await server?.close();
deferred?.();
});
await new Promise<void>((resolve) => (deferred = resolve));
}
}

async function setupServer(
Expand Down