Skip to content

Commit b168d0b

Browse files
krishs0404claude
andauthored
fix(core): the preview transport keeps ticking in a hidden tab (#5872)
* fix(core): the preview transport keeps ticking in a hidden tab A hidden document runs no animation frames, but its media keeps playing. The transport ticked on requestAnimationFrame only, so syncRuntimeMedia stopped running: a clip was never paused at the end of its window and the next clip never started until the tab was shown again. While document.visibilityState is "hidden", the transport now schedules its tick on a 16 ms timer instead. A visibilitychange listener moves a pending tick to whichever scheduler the new state calls for, and teardown removes it. A paused, hidden transport still parks as before. Refs #5710 (cause 1) Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * docs(core): note the hidden-tab timer clamp for silent media Co-Authored-By: Claude Sonnet 5.5 <[email protected]> * docs(core): shorten the hidden-tab transport comments to hold the comment share Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --------- Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent cea0bad commit b168d0b

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ const MEDIA_URL_ATTRS = new Map([
399399
]);
400400

401401
const SLOW_IDLE_HEARTBEAT_MS = 1000;
402+
const HIDDEN_TRANSPORT_TICK_MS = 16;
402403

403404
// One document.getAnimations() per seek and the pause after it, read on first use, shared by all adapters.
404405
function pageAnimationsForOnePass(): () => Animation[] {
@@ -4783,13 +4784,45 @@ export function initSandboxRuntimeModular(): void {
47834784
armParkTimer();
47844785
};
47854786

4787+
// Hidden documents run no animation frames but their media plays on, so tick on a timer.
4788+
// Chromium clamps it to ~1 s unless audio is audible: a clip may end up to 1 s late.
4789+
let transportTickTimerId: number | null = null;
4790+
const transportTickPending = () => state.transportRafId != null || transportTickTimerId != null;
4791+
const requestTransportTick = () => {
4792+
if (document.visibilityState === "hidden") {
4793+
transportTickTimerId = window.setTimeout(() => {
4794+
transportTickTimerId = null;
4795+
transportTick();
4796+
}, HIDDEN_TRANSPORT_TICK_MS);
4797+
return;
4798+
}
4799+
state.transportRafId = window.requestAnimationFrame(transportTick);
4800+
};
4801+
const cancelTransportTick = () => {
4802+
if (state.transportRafId != null) {
4803+
window.cancelAnimationFrame(state.transportRafId);
4804+
state.transportRafId = null;
4805+
}
4806+
if (transportTickTimerId != null) {
4807+
window.clearTimeout(transportTickTimerId);
4808+
transportTickTimerId = null;
4809+
}
4810+
};
4811+
// A frame requested just before the tab hid never fires: move a pending tick across.
4812+
const onTransportVisibilityChange = () => {
4813+
if (state.tornDown || !transportTickPending()) return;
4814+
cancelTransportTick();
4815+
requestTransportTick();
4816+
};
4817+
document.addEventListener("visibilitychange", onTransportVisibilityChange);
4818+
47864819
const scheduleNextTransportFrame = () => {
47874820
state.transportRafId = null;
47884821
if (state.tornDown) return;
47894822
const woken = transportWakeRequested;
47904823
transportWakeRequested = false;
47914824
if (woken || !canParkTransport()) {
4792-
state.transportRafId = window.requestAnimationFrame(transportTick);
4825+
requestTransportTick();
47934826
return;
47944827
}
47954828
// Read fresh rather than reusing the tick's own `timingRevision`: the tick
@@ -4805,13 +4838,13 @@ export function initSandboxRuntimeModular(): void {
48054838
// Inside a tick the tail is the only scheduler; setting the flag there is
48064839
// what stops it parking on work it has just been told about.
48074840
transportWakeRequested = true;
4808-
if (inTransportTick || state.transportRafId != null) return;
4841+
if (inTransportTick || transportTickPending()) return;
48094842
if (transportParkTimerId != null) {
48104843
window.clearTimeout(transportParkTimerId);
48114844
transportParkTimerId = null;
48124845
}
48134846
transportWakeRequested = false;
4814-
state.transportRafId = window.requestAnimationFrame(transportTick);
4847+
requestTransportTick();
48154848
};
48164849

48174850
const followedOrLongestRunningAudio = (
@@ -5183,8 +5216,8 @@ export function initSandboxRuntimeModular(): void {
51835216

51845217
installPositionEditsSeekReapply(window as Window & typeof globalThis);
51855218

5186-
// Start the rAF tick loop
5187-
state.transportRafId = window.requestAnimationFrame(transportTick);
5219+
// Start the tick loop
5220+
requestTransportTick();
51885221
postTimeline();
51895222
postState(true);
51905223

@@ -5339,10 +5372,8 @@ export function initSandboxRuntimeModular(): void {
53395372
const teardown = () => {
53405373
if (state.tornDown) return;
53415374
state.tornDown = true;
5342-
if (state.transportRafId != null) {
5343-
window.cancelAnimationFrame(state.transportRafId);
5344-
state.transportRafId = null;
5345-
}
5375+
cancelTransportTick();
5376+
document.removeEventListener("visibilitychange", onTransportVisibilityChange);
53465377
if (transportParkTimerId != null) {
53475378
window.clearTimeout(transportParkTimerId);
53485379
transportParkTimerId = null;

‎packages/core/src/runtime/transportPark.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ describe("parked transport loop", () => {
168168

169169
afterEach(() => {
170170
window.__hfRuntimeTeardown?.();
171+
delete (document as { visibilityState?: DocumentVisibilityState }).visibilityState;
171172
document.body.innerHTML = "";
172173
window.__timelines = {} as Record<string, RuntimeTimelineLike>;
173174
delete window.__player;
@@ -983,4 +984,50 @@ describe("parked transport loop", () => {
983984
expect(window.__player!.isPlaying()).toBe(true);
984985
expect(media.isPaused()).toBe(true);
985986
});
987+
988+
const setVisibility = (state: DocumentVisibilityState) => {
989+
Object.defineProperty(document, "visibilityState", { configurable: true, get: () => state });
990+
document.dispatchEvent(new Event("visibilitychange"));
991+
};
992+
993+
it("keeps the transport ticking on a timer while the document is hidden", () => {
994+
// A hidden document runs no animation frames but its media keeps playing, so a
995+
// transport driven by frames alone never ends a clip at the end of its window.
996+
let nowMs = 1000;
997+
vi.spyOn(performance, "now").mockImplementation(() => nowMs);
998+
mount(`<audio id="a1" data-start="0" data-duration="1"></audio>`);
999+
const media = stubMediaPlayback(document.getElementById("a1") as HTMLAudioElement, 10);
1000+
initSandboxRuntimeModular();
1001+
quiesce();
1002+
1003+
window.__player!.play();
1004+
settle();
1005+
expect(media.isPaused()).toBe(false);
1006+
1007+
setVisibility("hidden");
1008+
for (let i = 0; i < 100; i += 1) {
1009+
nowMs += 16;
1010+
vi.advanceTimersByTime(16);
1011+
}
1012+
1013+
expect(window.__player!.getTime()).toBeGreaterThan(1);
1014+
expect(media.isPaused()).toBe(true);
1015+
});
1016+
1017+
it("moves a pending tick between animation frames and a timer as visibility changes", () => {
1018+
mount();
1019+
initSandboxRuntimeModular();
1020+
quiesce();
1021+
window.__player!.play();
1022+
expect(raf.pending()).toBe(1);
1023+
1024+
setVisibility("hidden");
1025+
expect(raf.pending()).toBe(0);
1026+
vi.advanceTimersByTime(16);
1027+
expect(raf.pending()).toBe(0);
1028+
expect(window.__player!.isPlaying()).toBe(true);
1029+
1030+
setVisibility("visible");
1031+
expect(raf.pending()).toBe(1);
1032+
});
9861033
});

0 commit comments

Comments
 (0)