Files
romm/frontend/src/utils/releaseMedia.ts
Ellis Nieuwpoort 7941d352eb fix(v2): stop game videos from holding connections they no longer need (#5236)
* fix(v2): stop game videos from holding connections they no longer need

* fix(v2): keep hover previews from stopping a playing clip (pr-ready: code-review)

Hover previews get their own release slot so a cover hover no longer
stops a trailer the user started, the play() promise no longer marks a
released preview as playing, and MiniPlayer reuses releaseMedia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(v2): share the cover release path, dedupe a test stub (pr-ready: simplify)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(v2): skip the hover clip release when it never played

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 11:00:22 -04:00

40 lines
1.4 KiB
TypeScript

// A paused clip keeps its request open too, so only the clip played last may
// hold one. Hover previews have a slot of their own and never stop a clip.
type Holder = { el: HTMLMediaElement; onRelease: () => void };
const holders: { clip: Holder | null; preview: Holder | null } = {
clip: null,
preview: null,
};
// Dropping the src aborts the in-flight request; pause() alone keeps it open
// and ties up a browser connection.
export function releaseMedia(el: HTMLMediaElement) {
if (holders.clip?.el === el) holders.clip = null;
if (holders.preview?.el === el) holders.preview = null;
el.pause();
el.removeAttribute("src");
el.load();
}
function displace(holder: Holder | null, el: HTMLMediaElement) {
if (!holder || holder.el === el) return;
releaseMedia(holder.el);
holder.onRelease();
}
// Call when a clip starts playing. The clip and preview played before it are
// released, and their owners make them playable again (onRelease).
export function claimMedia(el: HTMLMediaElement, onRelease: () => void) {
const { clip, preview } = holders;
holders.clip = { el, onRelease };
displace(clip, el);
displace(preview, el);
}
// claimMedia for a hover preview: it only displaces the preview before it.
export function claimPreview(el: HTMLMediaElement, onRelease: () => void) {
const { preview } = holders;
holders.preview = { el, onRelease };
displace(preview, el);
}