summaryrefslogtreecommitdiff
path: root/src/UI.js
blob: 59ab106e6477ae0a4f56ca1370d176e846085958 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export const _cheatLog = (a) => () => console.log(a);
export const _setInterval = (delay) => (f) => () => setInterval(f, delay);
export const _toJSON = (x) => JSON.stringify(x);

export const _reload = () => {
    window.location.reload();
};

export const _redirect = (url) => () => {
    window.location.href = url;
};

export const _submitRedeem = (url) => (el) => () => {
    const redeem = el.children[0].textContent;
    const inp = el.children[1]?.value;
    console.log(redeem, inp);
    const data = new FormData();
    data.append("name", redeem);
    data.append("input", inp);
    fetch(url, {
        method: "post",
        body: data,
    });
};

export const _setShader = (shader) => () => {
    window.wasmBindings.set_shader(shader);
};

export const _submitShader = (url) => (shader) => () => {
    const data = new FormData();
    data.append("name", "throw shade");
    data.append("input", shader);
    fetch(url, {
        method: "post",
        body: data,
    });
};

export const _addOption = (nm) => (el) => () => {
    const opt = document.createElement("option");
    opt.value = nm
    opt.innerHTML = nm;
    el.appendChild(opt);
};

export const _onInput = (el) => (f) => () => {
    el.addEventListener("input", (ev) => {
        f(ev.target.value)();
    });
};

let SOCKET = null;
function connectRefreshSocket(url, iframe) {
    if (!SOCKET) {
        SOCKET = new WebSocket(url);
        SOCKET.addEventListener("open", (ev) => {
            console.log("connected");
        });
        SOCKET.addEventListener("close", (ev) => {
            console.log("closed");
            SOCKET = null;
        });
        SOCKET.addEventListener("error", (ev) => {
            console.log("error");
            SOCKET = null;
        });
        const select = document.getElementById("lcolonq-gizmo-select");
        SOCKET.addEventListener("message", async (ev) => {
            console.log(`incoming: ${ev.data}`)
            if (select.value == ev.data) {
                iframe.src = iframe.src;
            }
        });
    }
};
export const _startBufferRefresh = (url) => (iframe) => () => {
    window.setInterval(() => connectRefreshSocket(url, iframe), 1000);
};