mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
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
This commit is contained in:
@@ -86,10 +86,14 @@ export class CompositionProbe {
|
||||
private _interval: ReturnType<typeof setInterval> | 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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user