-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceHelper.js
More file actions
61 lines (53 loc) · 1.59 KB
/
InterfaceHelper.js
File metadata and controls
61 lines (53 loc) · 1.59 KB
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
56
57
58
59
60
61
'use strict'
class InterfaceHelper {
static #initiated = false
static preInit() {
if (InterfaceHelper.#initiated) {
console.error('InterfaceHelper is already initiated.')
return
}
InterfaceHelper.#initiated = true
const globalStyle = document.createElement('link')
globalStyle.rel = 'stylesheet'
globalStyle.href = '/global.css'
document.head.prepend(globalStyle)
const fallbackStyle = document.createElement('style')
fallbackStyle.textContent = 'html { background-color: var(--main-background-color); }'
document.head.prepend(fallbackStyle)
}
/** Coordinator shell: popup opener or `/join` parent. */
static #target() {
return globalThis.opener ?? globalThis.parent
}
/** First ping after init is applied (same as legacy `postMessage(null)`; coordinator accepts `Arena-Interface-Ready` too). */
static signalReady() {
const t = InterfaceHelper.#target()
if (t) {
t.postMessage(null, '*')
}
}
/**
* Send a normal interface reply to the arena coordinator.
* @param {object} payload
* @param {unknown} payload.value
* @param {{ toRespond: number, toTerminate: number }} [payload.executionSteps]
* @param {unknown} [payload.console]
*/
static respond(payload) {
const t = InterfaceHelper.#target()
if (!t) {
return
}
const executionSteps = payload.executionSteps ?? { toRespond: 0, toTerminate: 0 }
t.postMessage({
type: 'Response',
response: {
value: payload.value,
executionSteps,
...(payload.console ? { console: payload.console } : {}),
},
}, '*')
}
}
globalThis.InterfaceHelper = InterfaceHelper
InterfaceHelper.preInit()