1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
export function recordToMap<T>(r: Record<string, T>): Map<string, T> {
const ret = new Map();
for (const k of Object.keys(r)) ret.set(k, r[k]);
return ret;
}
export function mapValues<K, V1, V2>(f: (v: V1) => V2, map: Map<K, V1>): Map<K, V2> {
const ret: Map<K, V2> = new Map();
for (const [k, v] of map) ret.set(k, f(v));
return ret;
}
export async function mapValuesAsync<K, V1, V2>(f: (v: V1) => Promise<V2>, map: Map<K, V1>): Promise<Map<K, V2>> {
const ret: Map<K, V2> = new Map();
for (const [k, v] of mapValues(f, map)) ret.set(k, await v);
return ret;
}
export function nextPowerOf2(n: number): number {
let i = 1;
while (i < n) i *= 2;
return i;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Listenable<T extends any[]> {
private readonly listeners: Array<(...args: T) => void> = [];
public addListener(listener: (...args: T) => void): void {
this.listeners.push(listener);
}
protected runListeners(...args: T): void {
this.listeners.forEach((l) => l(...args));
}
}
export async function getJSON(url: string): Promise<unknown> {
const res = await window.fetch(url);
if (res.status < 200 || res.status >= 300) throw new Error(res.statusText);
return await res.json();
}
export function nextAnimationFrame(): Promise<DOMHighResTimeStamp> {
return new Promise((resolve) => window.requestAnimationFrame(resolve));
}
|