From 28eb9ebe5d949db9c0189bcc9952c3d0d41b4fc2 Mon Sep 17 00:00:00 2001 From: wukko Date: Fri, 17 Jan 2025 01:16:51 +0600 Subject: [PATCH] web/remux: improve page <-> worker messaging --- web/src/lib/workers/remux.ts | 35 +++++++++++++++++++------------ web/src/routes/remux/+page.svelte | 19 ++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/web/src/lib/workers/remux.ts b/web/src/lib/workers/remux.ts index cd4f73fd..502d03c0 100644 --- a/web/src/lib/workers/remux.ts +++ b/web/src/lib/workers/remux.ts @@ -2,16 +2,20 @@ import mime from "mime"; import LibAVWrapper from "$lib/libav"; const error = (code: string) => { - return { - error: `error.${code}`, - } + self.postMessage({ + cobaltRemuxWorker: { + error: `error.${code}`, + } + }) } const ff = new LibAVWrapper((progress) => { self.postMessage({ - progress: { - durationProcessed: progress.out_time_sec, - speed: progress.speed, + cobaltRemuxWorker: { + progress: { + durationProcessed: progress.out_time_sec, + speed: progress.speed, + } } }) }); @@ -29,18 +33,21 @@ const remux = async (file: File) => { console.error("uh oh! out of memory"); console.error(e); - self.postMessage(error("remux.out_of_resources")); + error("remux.out_of_resources"); + self.close(); } }); if (!file_info?.format) { - self.postMessage(error("remux.corrupted")); + error("remux.corrupted"); return; } self.postMessage({ - progressInfo: { - duration: Number(file_info.format.duration), + cobaltRemuxWorker: { + progressInfo: { + duration: Number(file_info.format.duration), + } } }); @@ -59,7 +66,7 @@ const remux = async (file: File) => { console.error("uh-oh! render error"); console.error(e); - self.postMessage(error("remux.out_of_resources")); + error("remux.out_of_resources"); }); if (!render) { @@ -72,8 +79,10 @@ const remux = async (file: File) => { const filename = `${filenameParts.join(".")} (remux).${filenameExt}`; self.postMessage({ - render, - filename + cobaltRemuxWorker: { + render, + filename + } }); } catch (e) { console.log(e); diff --git a/web/src/routes/remux/+page.svelte b/web/src/routes/remux/+page.svelte index 0c9e00f7..ff644693 100644 --- a/web/src/routes/remux/+page.svelte +++ b/web/src/routes/remux/+page.svelte @@ -45,10 +45,13 @@ }; worker.onmessage = (event) => { - console.log(event.data); + const eventData = event.data.cobaltRemuxWorker; + if (!eventData) return; - if (event.data.progress) { - let eprogress = event.data.progress; + console.log(eventData); + + if (eventData.progress) { + let eprogress = eventData.progress; if (eprogress?.speed) { speed = eprogress.speed; @@ -72,17 +75,17 @@ console.log(eprogress, progress, speed, currentProgress); } - if (event.data.render) { + if (eventData.render) { processing = false; worker.terminate(); return downloadFile({ - file: new File([event.data.render], event.data.filename, { - type: event.data.render.type, + file: new File([eventData.render], eventData.filename, { + type: eventData.render.type, }), }); } - if (event.data.error) { + if (eventData.error) { processing = false; worker.terminate(); @@ -90,7 +93,7 @@ id: "remux-error", type: "small", meowbalt: "error", - bodyText: $t(event.data.error), + bodyText: $t(eventData.error), buttons: [ { text: $t("button.gotit"),