Skip to content

Commit b9668c8

Browse files
committed
fix(runtime): a scene swap resets a kept video's volume as a load does, not through the volume probe
1 parent 86d0fae commit b9668c8

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎packages/core/src/runtime/init.swapScenes.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,20 @@ describe("__hfSwapScenes", () => {
609609
},
610610
);
611611

612+
it("gives a kept video its authored data-volume back when its unchanged script wrote volume only over the old text", async () => {
613+
quietMedia();
614+
const { html } = bootWithHeadingScript(
615+
"video.volume = 0.2",
616+
'<video src="clip.mp4" data-volume="0.5"></video>',
617+
);
618+
await tick();
619+
const video = sceneHost("a").querySelector("video")!;
620+
expect(video.volume).toBe(0.2);
621+
await window.__hfSwapScenes!(html);
622+
expect(sceneHost("a").querySelector("video")).toBe(video);
623+
expect(video.volume).toBe(0.5);
624+
});
625+
612626
it.each([
613627
"video.className = 'dim'",
614628
"video.hidden = true",

‎packages/core/src/runtime/init.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,16 +2420,19 @@ export function initSandboxRuntimeModular(): void {
24202420
}
24212421
};
24222422

2423+
const authoredMediaVolume = (el: HTMLMediaElement): number | null => {
2424+
const parsed = Number.parseFloat(el.dataset.volume ?? "");
2425+
return Number.isFinite(parsed) ? Math.max(0, Math.min(1, parsed)) : null;
2426+
};
2427+
24232428
const bindMediaMetadataListeners = () => {
24242429
if (state.tornDown) return;
24252430
const mediaEls = Array.from(document.querySelectorAll("video, audio")) as HTMLMediaElement[];
24262431
for (const mediaEl of mediaEls) {
24272432
if (metadataBoundMedia.has(mediaEl)) continue;
24282433
metadataBoundMedia.add(mediaEl);
2429-
const parsedVolume = Number.parseFloat(mediaEl.dataset.volume ?? "");
2430-
if (Number.isFinite(parsedVolume)) {
2431-
mediaEl.volume = Math.max(0, Math.min(1, parsedVolume));
2432-
}
2434+
const volume = authoredMediaVolume(mediaEl);
2435+
if (volume !== null) mediaEl.volume = volume;
24332436
mediaEl.addEventListener("loadedmetadata", scheduleMetadataDurationHydration);
24342437
mediaEl.addEventListener("durationchange", scheduleMetadataDurationHydration);
24352438
// Web Audio eligibility, reported at DISCOVERY rather than only at
@@ -3333,8 +3336,9 @@ export function initSandboxRuntimeModular(): void {
33333336
else kept.setAttribute("style", style);
33343337
// The swap's closing pass puts its move back, as a load's first pass does.
33353338
forgetPositionEdit(kept as HTMLElement);
3336-
// Properties no attribute shows, as the runtime sets them; the volume probe below redoes volume.
3339+
// Properties no attribute shows, as a load sets them.
33373340
const media = kept as HTMLMediaElement;
3341+
media.volume = authoredMediaVolume(media) ?? 1;
33383342
media.muted = state.bridgeMuted || state.mediaOutputMuted || media.defaultMuted;
33393343
media.defaultPlaybackRate = 1;
33403344
media.playbackRate = state.playbackRate;

0 commit comments

Comments
 (0)