Skip to content

Commit 0a33fc0

Browse files
committed
add polyfill
1 parent 0330430 commit 0a33fc0

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

build/dev.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { assert } from "@std/assert/assert";
44
import { exists } from "@std/fs/exists";
55
import { BuildOptions, context } from "esbuild";
6+
import { AsyncDisposableStack } from "../misc/async_disposable_stack.ts";
67
import { OPTIONS } from "./config.ts";
78

89
const BUILD_OPTIONS: BuildOptions = {

dictionary/watch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { unreachable } from "@std/assert/unreachable";
44
import { debounce } from "@std/async/debounce";
5+
import { AsyncDisposableStack } from "../misc/async_disposable_stack.ts";
56
import { build } from "./build.ts";
67

78
async function tryBuild(): Promise<void> {

misc/async_disposable_stack.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This code is Deno only
2+
3+
// TODO: remove this after Deno supports it
4+
export class AsyncDisposableStack implements AsyncDisposable {
5+
#callbacks: Array<() => Promise<void>> = [];
6+
defer(onDisposeAsync: () => Promise<void>): void {
7+
this.#callbacks.push(onDisposeAsync);
8+
}
9+
move(): AsyncDisposableStack {
10+
const newStack = new AsyncDisposableStack();
11+
newStack.#callbacks = this.#callbacks;
12+
this.#callbacks = [];
13+
return newStack;
14+
}
15+
async [Symbol.asyncDispose](): Promise<void> {
16+
const errors = [];
17+
for (const callback of this.#callbacks.reverse()) {
18+
try {
19+
await callback();
20+
} catch (error) {
21+
errors.push(error);
22+
}
23+
}
24+
switch (errors.length) {
25+
case 0:
26+
break;
27+
case 1:
28+
throw errors[0];
29+
default:
30+
throw AggregateError(errors);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)