Skip to content

Commit 8485498

Browse files
committed
Remove usage of Promise.withResolvers.
See #409 This is now tested indirectly through transitive imports via `src/bin` type checking.
1 parent 9db33fd commit 8485498

2 files changed

Lines changed: 30 additions & 24 deletions

File tree

src/cubing/twisty/views/InitialValueTracker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export class InitialValueTracker<T> {
2+
// @ts-expect-error: We do initialize this synchronously (assuming no one has tampered with the `Promise` constructor).
23
#resolve: (t: T) => void;
4+
// @ts-expect-error: We do initialize this synchronously (assuming no one has tampered with the `Promise` constructor).
35
reject: (e?: Error) => void; // TODO: AbortController?
46
promise: Promise<T>;
57
constructor() {
6-
const { promise, resolve, reject } = Promise.withResolvers<T>();
7-
this.promise = promise;
8-
this.#resolve = resolve;
9-
this.reject = reject;
8+
this.promise = new Promise((resolve, reject) => {
9+
this.#resolve = resolve;
10+
this.reject = reject;
11+
});
1012
}
1113
handleNewValue(t: T): void {
1214
this.#resolve(t);
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
// Normally we'd capitalize `ID`, but `Id` matches DOM API function names.
22
export async function firstElementWithId(id: string): Promise<Element> {
3-
const { promise, resolve, reject: _ } = Promise.withResolvers<Element>();
4-
const currentElem = document.getElementById(id);
5-
if (currentElem) {
6-
resolve(currentElem);
7-
}
8-
const observer = new MutationObserver((mutations: MutationRecord[]) => {
9-
for (const mutation of mutations) {
10-
if (
11-
mutation.attributeName === "id" &&
12-
mutation.target instanceof Element &&
13-
mutation.target.getAttribute("id") === id
14-
) {
15-
resolve(mutation.target);
16-
observer.disconnect();
3+
return new Promise((resolve, reject) => {
4+
try {
5+
const currentElem = document.getElementById(id);
6+
if (currentElem) {
7+
resolve(currentElem);
178
}
18-
}
19-
});
9+
const observer = new MutationObserver((mutations: MutationRecord[]) => {
10+
for (const mutation of mutations) {
11+
if (
12+
mutation.attributeName === "id" &&
13+
mutation.target instanceof Element &&
14+
mutation.target.getAttribute("id") === id
15+
) {
16+
resolve(mutation.target);
17+
observer.disconnect();
18+
}
19+
}
20+
});
2021

21-
observer.observe(document.body, {
22-
attributeFilter: ["id"],
23-
subtree: true,
22+
observer.observe(document.body, {
23+
attributeFilter: ["id"],
24+
subtree: true,
25+
});
26+
} catch (e) {
27+
reject(e);
28+
}
2429
});
25-
return promise;
2630
}

0 commit comments

Comments
 (0)