Skip to content

Commit 148575b

Browse files
committed
multiplex errors
1 parent 1961c26 commit 148575b

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/dictionary/watch.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@
22

33
import { unreachable } from "@std/assert/unreachable";
44
import { debounce } from "@std/async/debounce";
5+
import { MuxAsyncIterator } from "@std/async/mux-async-iterator";
56
import { build } from "./build.ts";
67
import { Parser } from "./parallel_parser.ts";
78

9+
// deno-lint-ignore require-yield
10+
async function* errorOnly(promise: Promise<never>): AsyncGenerator<never> {
11+
await promise;
12+
unreachable();
13+
}
814
if (import.meta.main) {
9-
await using stack = new AsyncDisposableStack();
1015
using watcher = Deno.watchFs("./dictionary.txt");
1116
using parser = new Parser();
17+
18+
const { promise, reject } = Promise.withResolvers<never>();
19+
const errorGenerator = errorOnly(promise);
20+
1221
let task = Promise.resolve();
13-
stack.defer(async () => await task);
1422
const buildDebounced = debounce(() => {
1523
task = task.then(async () => {
16-
await build(parser);
24+
try {
25+
await build(parser);
26+
} catch (error) {
27+
reject(error);
28+
}
1729
});
1830
}, 200);
31+
32+
const watcherWithError = new MuxAsyncIterator<Deno.FsEvent>();
33+
watcherWithError.add(watcher);
34+
watcherWithError.add(errorGenerator);
35+
1936
buildDebounced();
2037
buildDebounced.flush();
21-
for await (const _ of watcher) {
38+
for await (const _ of watcherWithError) {
2239
buildDebounced();
2340
}
2441
unreachable();

0 commit comments

Comments
 (0)