Skip to content

Commit 0330430

Browse files
committed
better resource management
1 parent 309b71f commit 0330430

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

build/dev.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
import { assert } from "@std/assert/assert";
44
import { exists } from "@std/fs/exists";
5-
import { BuildContext, BuildOptions, context } from "esbuild";
5+
import { BuildOptions, context } from "esbuild";
66
import { OPTIONS } from "./config.ts";
77

88
const BUILD_OPTIONS: BuildOptions = {
99
...OPTIONS,
1010
minify: false,
1111
define: { LIVE_RELOAD: "true" },
1212
};
13-
async function watchMain(): Promise<BuildContext<BuildOptions>> {
13+
async function watchMain(): Promise<AsyncDisposable> {
14+
await using stack = new AsyncDisposableStack();
1415
const buildContext = await context(BUILD_OPTIONS);
15-
try {
16-
await buildContext.watch();
17-
await buildContext.serve({ servedir: "./dist/" });
18-
} catch (error) {
19-
await buildContext.dispose();
20-
throw error;
21-
}
22-
return buildContext;
16+
stack.defer(async () => await buildContext.dispose());
17+
buildContext.watch();
18+
buildContext.serve({ servedir: "./dist/" });
19+
return stack.move();
2320
}
2421
async function watchDictionary(): Promise<number> {
2522
const command = new Deno.Command(Deno.execPath(), {
@@ -47,16 +44,14 @@ async function watchDictionary(): Promise<number> {
4744
if (import.meta.main) {
4845
let statusCode: number;
4946
{
50-
await using stack = new AsyncDisposableStack();
5147
if (
5248
!await exists(new URL("../dictionary/dictionary.ts", import.meta.url))
5349
) {
5450
const Dictionary = await import("../dictionary/build.ts");
5551
await Dictionary.build();
5652
}
5753
const statusCodePromise = watchDictionary();
58-
const context = await watchMain();
59-
stack.defer(async () => await context.dispose());
54+
await using _ = await watchMain();
6055
statusCode = await statusCodePromise;
6156
}
6257
Deno.exit(statusCode);

0 commit comments

Comments
 (0)