From 58de4c783d488e564a6c1a2d3e0e493629023066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 20 Nov 2026 15:35:06 +0000 Subject: [PATCH] fix(player): a sealed film shows when its timeline arrives after the 8 s check (#8293) * fix(player): a sealed film shows when its timeline lands after the 8 s check * fix(player): a navigation away from a sealed film keeps its missed check latched until the next load * fix(player): the latch is set before the probe stops, so stop keeps it * test(player): the sealed test reads the error event's detail as the player sends it --- packages/player/src/composition-probe.ts | 17 +++-- packages/player/src/hyperframes-player.ts | 1 + .../player/src/sealedLateTimeline.test.ts | 62 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 packages/player/src/sealedLateTimeline.test.ts diff --git a/packages/player/src/composition-probe.ts b/packages/player/src/composition-probe.ts index f5630146c2..4d7148788c 100644 --- a/packages/player/src/composition-probe.ts +++ b/packages/player/src/composition-probe.ts @@ -86,10 +86,14 @@ export class CompositionProbe { private _interval: ReturnType | null = null; private _runtimeInjected = false; private _runtimeScript: HTMLScriptElement | null = null; - private _failure: { document: Document | null } | null = null; + private _failure: { document: Document | null; deadline: boolean } | null = null; get failed(): boolean { - return this._failure !== null && this._failure.document === this._iframe.contentDocument; + if (this._failure === null) return false; + const unreadableFrameMayStillPost = + this._failure.deadline && this._iframe.contentDocument === null; + if (unreadableFrameMayStillPost) return false; + return this._failure.document === this._iframe.contentDocument; } constructor( @@ -122,11 +126,12 @@ export class CompositionProbe { }; } } + const deadline = outcome === null; outcome ??= this._deadlineOutcome(attempts); if (outcome) { this.stop(); if (outcome.kind === "error") { - this._failure = { document: this._iframe.contentDocument }; + this._failure = { document: this._iframe.contentDocument, deadline }; this._callbacks.onError(outcome.message); } else this._callbacks.onReady(outcome.result); } @@ -181,6 +186,10 @@ export class CompositionProbe { }; } + latchUntilNextLoad(): void { + if (this._failure) this._failure.deadline = false; + } + stop(): void { if (this._runtimeScript) { this._runtimeScript.onerror = null; @@ -227,7 +236,7 @@ export class CompositionProbe { script.onerror = () => { if (this._runtimeScript !== script || this._iframe.contentDocument !== doc) return; this.stop(); - this._failure = { document: doc }; + this._failure = { document: doc, deadline: false }; this._callbacks.onError("HyperFrames runtime failed to load from " + script.src); }; (doc.head || doc.documentElement).appendChild(script); diff --git a/packages/player/src/hyperframes-player.ts b/packages/player/src/hyperframes-player.ts index be86c17c71..b0c661f164 100644 --- a/packages/player/src/hyperframes-player.ts +++ b/packages/player/src/hyperframes-player.ts @@ -1461,6 +1461,7 @@ class HyperframesPlayer extends HTMLElement { /** Every host-driven navigation or teardown: the old document's handshake, asset wait and * data deliveries end here. A queued play is the caller's, so only its owners clear it. */ private _abandonComposition(reason: string): void { + this.probe.latchUntilNextLoad(); this.probe.stop(); this._ready = false; this._readyDocument = null; diff --git a/packages/player/src/sealedLateTimeline.test.ts b/packages/player/src/sealedLateTimeline.test.ts new file mode 100644 index 0000000000..8ea6b7562f --- /dev/null +++ b/packages/player/src/sealedLateTimeline.test.ts @@ -0,0 +1,62 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +type Player = HTMLElement & { + ready: boolean; + iframeElement: HTMLIFrameElement; + _onIframeLoad(): void; +}; + +/** A player whose frame is sealed as the browser seals it: its window posts, its document can't be read. */ +function sealedPlayer(sealed = true) { + const player = document.createElement("hyperframes-player") as Player; + player.setAttribute("sandbox-origin", ""); + document.body.appendChild(player); + const frame = player.iframeElement; + if (sealed) Object.defineProperty(frame, "contentDocument", { value: null, configurable: true }); + const events: string[] = []; + player.addEventListener("error", (event: Event) => + events.push(`error ${(event as CustomEvent<{ message?: string }>).detail?.message}`), + ); + player.addEventListener("ready", () => events.push("ready")); + player._onIframeLoad(); + const postTimeline = () => + window.dispatchEvent( + new MessageEvent("message", { + source: frame.contentWindow, + data: { source: "hf-preview", type: "timeline", durationInFrames: 120, durationSeconds: 4 }, + }), + ); + return { player, events, postTimeline }; +} + +describe("a sealed film whose timeline comes after the 8 s check", () => { + beforeEach(async () => { + vi.useFakeTimers(); + await import("./hyperframes-player.js"); + }); + afterEach(() => { + vi.useRealTimers(); + document.body.innerHTML = ""; + }); + + // fails without the fix: after the 8 s check gave up, the player dropped every later post from a sealed frame, so a + // film whose runtime was ready at 10 s stayed blank until something reloaded it. + it("shows the film when the timeline lands at 10 s", () => { + const { player, events, postTimeline } = sealedPlayer(); + vi.advanceTimersByTime(8000); + expect(events).toEqual(["error Composition timeline not found after 8s"]); + vi.advanceTimersByTime(2000); + postTimeline(); + expect(events).toEqual(["error Composition timeline not found after 8s", "ready"]); + expect(player.ready).toBe(true); + }); + + it("a frame whose document the player reads keeps ignoring a post that comes after the check gave up", () => { + const { player, events, postTimeline } = sealedPlayer(false); + vi.advanceTimersByTime(8000); + expect(events).toEqual(["error Composition timeline not found after 8s"]); + postTimeline(); + expect(events).toEqual(["error Composition timeline not found after 8s"]); + expect(player.ready).toBe(false); + }); +});