From 477f85145c6bb6eec7e86796651afaa35da0e7d2 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 18 May 2025 15:02:18 +0200 Subject: webui live updating --- scripts/dummy_worker.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 scripts/dummy_worker.ts (limited to 'scripts/dummy_worker.ts') 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" })) + } +} -- cgit v1.2.3-70-g09d2