-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.common.ts
40 lines (35 loc) · 930 Bytes
/
index.common.ts
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
interface WebAPIModule {
tick?: (now: number) => void;
initialize?: () => void;
uninitialize?: () => void;
exports?: Record<string, any>
}
let registered_modules: WebAPIModule[] = [];
export function initialize(modules: WebAPIModule[]) {
Object.defineProperty(globalThis, 'window', { value: globalThis });
for (const m of modules) {
if (m.initialize) m.initialize();
if (!m.exports) continue;
for (const key in m.exports) {
Object.defineProperty(window, key, { value: m.exports[key] });
}
}
registered_modules = modules;
}
export function finalize() {
for (const m of registered_modules) {
if (m.uninitialize) m.uninitialize();
}
}
export function tick() {
for (const m of registered_modules) {
if (m.tick && WebAPI.getHighResTimeStamp) {
m.tick(WebAPI.getHighResTimeStamp());
}
}
}
Object.defineProperty(globalThis, "WebAPI", { value: {
tick,
finalize,
getHighResTimeStamp: Date.now,
}});