fix: scope htmx:beforeSwap and clear searchDebounce on teardown

This commit is contained in:
2026-06-16 11:01:33 +02:00
committed by Milas Holsting
parent 06b50509e8
commit 1d531ab181

View File

@@ -101,7 +101,7 @@ const initPlayer = (): void => {
currentContainer = container;
const abortController = new AbortController();
const signal = abortController.signal;
cleanup = () => abortController.abort();
cleanup = null;
const loading = container.querySelector("[data-loading]") as HTMLElement | null;
const progressWrap = container.querySelector("[data-progress-wrap]") as HTMLElement | null;
@@ -396,6 +396,22 @@ const initPlayer = (): void => {
setupThumbnails();
void hydrateAlternateMode(signal);
document.body.addEventListener(
"htmx:beforeSwap",
(e: Event) => {
const target = (e as CustomEvent).detail?.target as HTMLElement | null;
if (target && currentContainer && target.contains(currentContainer)) {
teardownPlayer();
}
},
{ signal },
);
cleanup = () => {
clearTimeout(searchDebounce);
abortController.abort();
};
};
onReady(initPlayer);
@@ -404,10 +420,3 @@ onHtmxLoad((root) => {
initPlayer();
}
});
document.body.addEventListener("htmx:beforeSwap", (e: Event) => {
const target = (e as CustomEvent).detail?.target as HTMLElement | null;
if (target && currentContainer && target.contains(currentContainer)) {
teardownPlayer();
}
});