get the encoder work off the main thread and harden the update path

This commit is contained in:
2026-09-21 06:29:39 +03:00
parent af30963223
commit 205f68222c
7 changed files with 125 additions and 67 deletions
+28 -37
View File
@@ -149,38 +149,34 @@
})
onMount(() => {
let unlistenHotkey: (() => void) | null = null
let unlistenProgress: (() => void) | null = null
let unlistenAudio: (() => void) | null = null
let unlistenAudioRecovered: (() => void) | null = null
let unlistenOverlayClosed: (() => void) | null = null
const unlisten: (() => void)[] = []
// Registered one at a time rather than through a single Promise.all: one
// rejected registration used to lose the whole batch, which left every
// listener live and none of them removable - hotkeys included.
const keep = (p: Promise<() => void>) => {
void p.then(fn => unlisten.push(fn))
.catch(e => console.warn("could not register a listener:", e))
}
keep(listen("overlay-closed", () => { useFrame = false }))
keep(api.onHotkey((kind) => {
if (kind === "start" && phase === "idle") void startRec()
else if (kind === "pause") {
if (phase === "recording") void pauseRec()
else if (phase === "paused") void resumeRec()
} else if (kind === "stop") {
if (phase === "recording" || phase === "paused") void stopRec()
}
}))
keep(api.onEncodeProgress((p) => {
encodeProgress = { pass: p.pass, total: p.total_passes, pct: p.percent }
}))
keep(api.onAudioFailed((msg) => {
audioWarning = msg || "audio capture failed - audio will not be recorded"
}))
keep(listen("audio-recovered", () => { audioWarning = null }))
void (async () => {
const [overlayClosed, hotkey, progress, audio, audioRecovered] = await Promise.all([
listen("overlay-closed", () => { useFrame = false }),
api.onHotkey((kind) => {
if (kind === "start" && phase === "idle") void startRec()
else if (kind === "pause") {
if (phase === "recording") void pauseRec()
else if (phase === "paused") void resumeRec()
} else if (kind === "stop") {
if (phase === "recording" || phase === "paused") void stopRec()
}
}),
api.onEncodeProgress((p) => {
encodeProgress = { pass: p.pass, total: p.total_passes, pct: p.percent }
}),
api.onAudioFailed((msg) => {
audioWarning = msg || "audio capture failed - audio will not be recorded"
}),
listen("audio-recovered", () => { audioWarning = null }),
])
unlistenOverlayClosed = overlayClosed
unlistenHotkey = hotkey
unlistenProgress = progress
unlistenAudio = audio
unlistenAudioRecovered = audioRecovered
try {
config = await api.getConfig()
format = config.export.default_format
@@ -191,13 +187,8 @@
console.warn("failed to load config:", e)
}
})()
return () => {
unlistenHotkey?.()
unlistenProgress?.()
unlistenAudio?.()
unlistenAudioRecovered?.()
unlistenOverlayClosed?.()
}
return () => { unlisten.forEach(fn => fn()) }
})
// ---- Actions ----
+18 -7
View File
@@ -11,7 +11,15 @@
onMount(() => {
const win = getCurrentWebviewWindow()
let unlisten: (() => void)[] = []
const unlisten: (() => void)[] = []
let hoverDebounce = 0
// A registration that lands after the window is gone still has to
// unregister, so a late unlisten runs immediately rather than being dropped.
const keep = (p: Promise<() => void>) => {
void p.then(fn => { if (mounted) unlisten.push(fn); else fn() })
.catch(e => console.warn("could not register a listener:", e))
}
const refresh = async () => {
scale = await win.scaleFactor()
@@ -20,18 +28,21 @@
}
void (async () => {
await refresh()
win.onMoved(refresh).then(fn => { if (mounted) unlisten.push(fn) })
.catch(e => console.warn("could not watch the picker window position:", e))
let hoverDebounce = 0
listen<Rect>("picker-hover", (e) => {
try {
await refresh()
} catch (e) {
console.warn("could not read the picker window geometry:", e)
}
keep(win.onMoved(refresh))
keep(listen<Rect>("picker-hover", (e) => {
clearTimeout(hoverDebounce)
hoverDebounce = window.setTimeout(() => { hover = e.payload }, 30)
}).then(fn => { if (mounted) unlisten.push(fn) })
}))
})()
return () => {
mounted = false
clearTimeout(hoverDebounce)
unlisten.forEach(fn => fn())
emit("picker-cancel", {}).catch(() => {})
}
+13 -8
View File
@@ -9,14 +9,19 @@
let errMsg: string | null = $state(null)
onMount(() => {
let unlistenInfo: (() => void) | null = null
let unlistenProg: (() => void) | null = null
void (async () => {
unlistenInfo = await api.onUpdateAvailable((i) => { info = i; phase = "info" })
unlistenProg = await api.onUpdateDownloadProgress((p) => { progress = p })
if (await api.pendingUpdate()) phase = "ready"
})()
return () => { unlistenInfo?.(); unlistenProg?.() }
const unlisten: (() => void)[] = []
const keep = (p: Promise<() => void>) => {
void p.then(fn => unlisten.push(fn))
.catch(e => console.warn("could not register an update listener:", e))
}
keep(api.onUpdateAvailable((i) => { info = i; phase = "info" }))
keep(api.onUpdateDownloadProgress((p) => { progress = p }))
void api.pendingUpdate()
.then(pending => { if (pending) phase = "ready" })
.catch(e => console.warn("could not read the pending update state:", e))
return () => { unlisten.forEach(fn => fn()) }
})
function dismiss() { info = null; errMsg = null; phase = "info" }