web/remux: warn user & terminate libav before switching tabs

warning about aborting processing will be shown before navigating away from remuxing tab
This commit is contained in:
wukko
2024-09-09 03:08:18 +06:00
parent dd1c630c71
commit 8a18645e0b
4 changed files with 48 additions and 22 deletions

View File

@@ -24,6 +24,13 @@ export default class LibAVWrapper {
}
}
async terminate() {
if (this.libav) {
const libav = await this.libav;
libav.terminate();
}
}
async probe(blob: Blob) {
if (!this.libav) throw new Error("LibAV wasn't initialized");
const libav = await this.libav;
@@ -173,7 +180,7 @@ export default class LibAVWrapper {
await libav.unlink(outputName);
await libav.unlink('progress.txt');
await libav.unlinkreadaheadfile("input");
} catch {}
} catch { /* catch & ignore */ }
}
}

View File

@@ -1,10 +1,8 @@
<script lang="ts">
import mime from "mime";
import LibAVWrapper from "$lib/libav";
import { browser } from "$app/environment";
import { beforeNavigate } from "$app/navigation";
import { beforeNavigate, goto } from "$app/navigation";
import { device } from "$lib/device";
import { t } from "$lib/i18n/translations";
import { createDialog } from "$lib/dialogs";
import { downloadFile } from "$lib/download";
@@ -22,6 +20,8 @@
let speed: number | undefined;
let progress: string | undefined;
let wentAway = false;
$: {
if (totalDuration && processedDuration) {
const percentage = Math.max(
@@ -136,10 +136,7 @@
if (!render) {
return console.log("not a valid file");
}
return await downloadFile({
file,
})
return await downloadFile({ file });
} finally {
processing = false;
file = undefined;
@@ -148,23 +145,40 @@
}
};
const beforeUnloadHandler = (event: BeforeUnloadEvent) => {
event.preventDefault();
};
beforeNavigate((event) => {
if (processing) {
if (processing && !wentAway) {
event.cancel();
//TODO: show a popup with an option to kill ongoing processing
const path = event.to?.route?.id;
if (path) {
return createDialog({
id: "remux-ongoing",
type: "small",
icon: "warn-red",
title: $t("dialog.processing.title.ongoing"),
bodyText: $t("dialog.processing.ongoing"),
buttons: [
{
text: $t("button.no"),
main: false,
action: () => {},
},
{
text: $t("button.yes"),
main: true,
color: "red",
action: async () => {
await ff.terminate();
wentAway = true;
goto(path);
},
},
],
});
}
}
});
$: if (browser && processing) {
window.addEventListener("beforeunload", beforeUnloadHandler);
} else if (browser) {
window.removeEventListener("beforeunload", beforeUnloadHandler);
}
$: if (file) {
render();
}