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, Math.random() * 400)) 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.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" })) } }