diff options
author | metamuffin <metamuffin@disroot.org> | 2025-05-18 15:02:18 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-05-18 15:02:18 +0200 |
commit | 477f85145c6bb6eec7e86796651afaa35da0e7d2 (patch) | |
tree | b779ec076538e81becd7536350cbbf1c0322952a /scripts/dummy_worker.ts | |
parent | 439f184ac655eb649f056cdf0b9326dcc0af4648 (diff) | |
download | isda-477f85145c6bb6eec7e86796651afaa35da0e7d2.tar isda-477f85145c6bb6eec7e86796651afaa35da0e7d2.tar.bz2 isda-477f85145c6bb6eec7e86796651afaa35da0e7d2.tar.zst |
webui live updating
Diffstat (limited to 'scripts/dummy_worker.ts')
-rw-r--r-- | scripts/dummy_worker.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/dummy_worker.ts b/scripts/dummy_worker.ts new file mode 100644 index 0000000..00e6ad2 --- /dev/null +++ b/scripts/dummy_worker.ts @@ -0,0 +1,30 @@ + +const ws = new WebSocket(Deno.args[0]) + +async function do_work(key: string) { + let progress = 0 + while (progress < 1) { + await new Promise(r => setTimeout(r, 200)) + progress += 0.1 + ws.send(JSON.stringify({ t: "metadata", key, data: { progress } })) + } + ws.send(JSON.stringify({ t: "complete", key })) +} + +ws.onerror = () => console.error("ws error") +ws.onclose = () => console.error("ws closed") +ws.onopen = () => { + console.log("ws open"); + ws.send(JSON.stringify({ t: "register", name: "dummy worker", task_kinds: ["youtube"] })) + ws.send(JSON.stringify({ t: "accept" })) +} +ws.onmessage = async ev => { + if (typeof ev.data != "string") return + const p = JSON.parse(ev.data) + if (p.t == "error") console.error(`error: ${p.message}`); + if (p.t == "work") { + if (!p.data.output) throw new Error("no output"); + await do_work(p.key) + ws.send(JSON.stringify({ t: "accept" })) + } +} |