Skip to content

Commit 735128a

Browse files
fix(core): install runtime bridge after transport setup (heygen-com#2558)
## Summary The Studio preview now installs the runtime bridge only after its transport is ready, preventing initialization-order gaps without changing the bridge contract. ## Stack Part 1 of 20. Parent: `main`. Next: heygen-com#2559. The golden reference, heygen-com#2387, remains open and unchanged. ## Test plan - [x] Integrated Studio suite: 2,800 tests passed - [x] Integrated parser suite: 853 tests passed - [x] Studio and parser typechecks passed - [x] Studio and parser production builds passed - [ ] Per-PR CI completes on the submitted stack ## Post-Deploy Monitoring & Validation Validation window: first 24 hours after the stack merges. Owner: Studio maintainers. Watch browser console and support reports for `[Timeline]`, `gsap-parser`, failed keyframe mutations, or preview/render easing mismatches. Healthy means edits persist and preview/render agree; revert the first failing layer if authored animation data changes unexpectedly. --- [![Compound Engineering](https://img.shields.io/badge/Built_with-Compound_Engineering-6366f1)](https://github.com/EveryInc/compound-engineering-plugin) ![Codex](https://img.shields.io/badge/GPT--5-000000)
1 parent 458df4c commit 735128a

2 files changed

Lines changed: 175 additions & 117 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,59 @@ describe("initSandboxRuntimeModular", () => {
18451845
expect(seekTimes[seekTimes.length - 1]).toBe(0);
18461846
});
18471847

1848+
it("accepts replayed transport controls when the bridge announces ready without duplicate listeners", () => {
1849+
const root = document.createElement("div");
1850+
root.setAttribute("data-composition-id", "root");
1851+
root.setAttribute("data-root", "true");
1852+
root.setAttribute("data-duration", "5");
1853+
root.setAttribute("data-width", "1920");
1854+
root.setAttribute("data-height", "1080");
1855+
document.body.appendChild(root);
1856+
1857+
const timeline = createMockTimeline(5);
1858+
timeline.timeScale = vi.fn();
1859+
window.__timelines = { root: timeline };
1860+
const outbound: Array<Record<string, unknown>> = [];
1861+
vi.spyOn(window.parent, "postMessage").mockImplementation((message: unknown) => {
1862+
if (typeof message !== "object" || message === null) return;
1863+
const payload = message as Record<string, unknown>;
1864+
outbound.push(payload);
1865+
if (payload.source !== "hf-preview" || payload.type !== "ready") return;
1866+
window.dispatchEvent(
1867+
new MessageEvent("message", {
1868+
data: {
1869+
source: "hf-parent",
1870+
type: "control",
1871+
action: "seek",
1872+
timeSeconds: 2,
1873+
},
1874+
}),
1875+
);
1876+
window.dispatchEvent(
1877+
new MessageEvent("message", {
1878+
data: {
1879+
source: "hf-parent",
1880+
type: "control",
1881+
action: "set-playback-rate",
1882+
playbackRate: 2,
1883+
},
1884+
}),
1885+
);
1886+
});
1887+
1888+
expect(() => initSandboxRuntimeModular()).not.toThrow();
1889+
expect(() => initSandboxRuntimeModular()).not.toThrow();
1890+
1891+
expect(timeline.time()).toBe(2);
1892+
expect(timeline.timeScale).toHaveBeenLastCalledWith(2);
1893+
expect(outbound.filter((message) => message.type === "ready")).toHaveLength(2);
1894+
expect(
1895+
outbound.filter(
1896+
(message) => message.type === "analytics" && message.event === "composition_seeked",
1897+
),
1898+
).toHaveLength(2);
1899+
});
1900+
18481901
it("restores timed element visibility after a forced timeline rebind", () => {
18491902
document.body.innerHTML = `
18501903
<div data-composition-id="root" data-root="true" data-duration="30" data-width="1920" data-height="1080">

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

Lines changed: 122 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,118 +2373,6 @@ export function initSandboxRuntimeModular(): void {
23732373
document.querySelector("[data-composition-id]")?.getAttribute("data-composition-id") ?? null,
23742374
});
23752375

2376-
state.controlBridgeHandler = installRuntimeControlBridge({
2377-
onPlay: () => {
2378-
player.play();
2379-
emitAnalyticsEvent("composition_played", { time: player.getTime() });
2380-
},
2381-
onPause: () => {
2382-
player.pause();
2383-
emitAnalyticsEvent("composition_paused", { time: player.getTime() });
2384-
},
2385-
onStopMedia: () => {
2386-
webAudio.stopAll();
2387-
const mediaEls = document.querySelectorAll("video, audio");
2388-
for (const el of mediaEls) {
2389-
if (el instanceof HTMLMediaElement && !el.paused) el.pause();
2390-
}
2391-
},
2392-
onSeek: (timeSeconds, _seekMode) => {
2393-
player.seek(timeSeconds);
2394-
emitAnalyticsEvent("composition_seeked", { time: timeSeconds });
2395-
},
2396-
onSetMuted: (muted) => {
2397-
state.bridgeMuted = muted;
2398-
const effective = muted || state.mediaOutputMuted;
2399-
webAudio.setMuted(effective);
2400-
const mediaEls = document.querySelectorAll("video, audio");
2401-
for (const el of mediaEls) {
2402-
if (!(el instanceof HTMLMediaElement)) continue;
2403-
el.muted = effective || el.defaultMuted;
2404-
}
2405-
},
2406-
onSetVolume: (volume) => {
2407-
state.bridgeVolume = volume;
2408-
webAudio.setVolume(volume);
2409-
const mediaEls = document.querySelectorAll("video, audio");
2410-
for (const el of mediaEls) {
2411-
if (!(el instanceof HTMLMediaElement)) continue;
2412-
const parsed = parseFloat(el.dataset.volume ?? "");
2413-
const clipVolume = Number.isFinite(parsed) ? parsed : 1;
2414-
el.volume = clipVolume * volume;
2415-
}
2416-
},
2417-
onSetMediaOutputMuted: (muted) => {
2418-
state.mediaOutputMuted = muted;
2419-
const effective = muted || state.bridgeMuted;
2420-
webAudio.setMuted(effective);
2421-
const mediaEls = document.querySelectorAll("video, audio");
2422-
for (const el of mediaEls) {
2423-
if (!(el instanceof HTMLMediaElement)) continue;
2424-
el.muted = effective || el.defaultMuted;
2425-
}
2426-
},
2427-
onSetNativeMediaSyncDisabled: (disabled) => {
2428-
if (state.nativeMediaSyncDisabled === disabled) return;
2429-
state.nativeMediaSyncDisabled = disabled;
2430-
state.mediaForceSyncNextTick = true;
2431-
if (disabled) {
2432-
webAudio.stopAll();
2433-
clock.detachAudioSource();
2434-
} else {
2435-
syncMediaForCurrentState();
2436-
}
2437-
},
2438-
onSetWebAudioMediaDisabled: (disabled) => {
2439-
if (state.webAudioMediaDisabled === disabled) return;
2440-
state.webAudioMediaDisabled = disabled;
2441-
state.mediaForceSyncNextTick = true;
2442-
if (disabled) {
2443-
webAudio.stopAll();
2444-
clock.detachAudioSource();
2445-
syncMediaForCurrentState();
2446-
} else {
2447-
syncMediaForCurrentState();
2448-
}
2449-
},
2450-
onSetPlaybackRate: (rate) => {
2451-
applyPlaybackRate(rate);
2452-
if (state.transportClock) state.transportClock.setRate(state.playbackRate);
2453-
applyWebAudioRate();
2454-
},
2455-
onSetRootDuration: growRootDurationLive,
2456-
onSetColorGrading: (target, grading) => {
2457-
colorGrading.setGrading(target, grading);
2458-
},
2459-
onSetColorGradingCompare: (target, compare) => {
2460-
colorGrading.setCompare(target, compare);
2461-
},
2462-
onTick: () => {
2463-
if (state.tornDown || !clock.isPlaying()) return;
2464-
const t = clock.now();
2465-
state.currentTime = t;
2466-
seekTimelineAndAdapters(t);
2467-
if (clock.reachedEnd()) {
2468-
webAudio.stopAll();
2469-
clock.detachAudioSource();
2470-
clock.pause();
2471-
state.isPlaying = false;
2472-
const dur = clock.getDuration();
2473-
if (Number.isFinite(dur)) {
2474-
clock.seek(dur);
2475-
state.currentTime = dur;
2476-
seekTimelineAndAdapters(dur);
2477-
}
2478-
runAdapters("pause");
2479-
syncMediaForCurrentState();
2480-
postState(true);
2481-
}
2482-
},
2483-
onEnablePickMode: () => picker.enablePickMode(),
2484-
onDisablePickMode: () => picker.disablePickMode(),
2485-
getCanonicalFps: () => state.canonicalFps,
2486-
});
2487-
24882376
state.deterministicAdapters = [
24892377
createWaapiAdapter(),
24902378
createCssAdapter({
@@ -2756,10 +2644,10 @@ export function initSandboxRuntimeModular(): void {
27562644
return false;
27572645
};
27582646

2759-
const seekTimelineAndAdapters = (
2647+
function seekTimelineAndAdapters(
27602648
t: number,
27612649
opts?: { activateChildren?: boolean; suppressEvents?: boolean },
2762-
) => {
2650+
) {
27632651
const tl = state.capturedTimeline;
27642652
const suppressEvents = opts?.suppressEvents === true;
27652653
if (tl) {
@@ -2825,7 +2713,7 @@ export function initSandboxRuntimeModular(): void {
28252713
swallow("runtime.init.transport.adapter", err);
28262714
}
28272715
}
2828-
};
2716+
}
28292717

28302718
// True while the Studio is mid-drag on an element (the gesture marker is
28312719
// stamped on the gestured element for the duration of the drag). During a
@@ -3061,7 +2949,7 @@ export function initSandboxRuntimeModular(): void {
30612949
// rescaled in place; but a bounded source's window was baked into start()'s
30622950
// duration at its prior rate and can't be rescaled, so when one is active we
30632951
// stopAll()+reschedule at the new rate to keep trimmed clips ending on time.
3064-
const applyWebAudioRate = () => {
2952+
function applyWebAudioRate() {
30652953
const changed = webAudio.setRate(state.playbackRate);
30662954
if (
30672955
changed &&
@@ -3074,7 +2962,7 @@ export function initSandboxRuntimeModular(): void {
30742962
webAudio.stopAll();
30752963
scheduleWebAudioForActiveClips();
30762964
}
3077-
};
2965+
}
30782966

30792967
// Sync clock duration from any captured timeline
30802968
if (state.capturedTimeline) {
@@ -3090,6 +2978,123 @@ export function initSandboxRuntimeModular(): void {
30902978
postTimeline();
30912979
postState(true);
30922980

2981+
// Wire the control bridge LAST — after every transport helper its handlers
2982+
// dispatch to (seekTimelineAndAdapters, applyWebAudioRate, ...) is declared.
2983+
// The runtime's external control surface only goes live once all of its
2984+
// dependencies exist, so a load-time seek / set-playback-rate can never reach
2985+
// a not-yet-initialized helper (the 'before initialization' TDZ this fixes).
2986+
state.controlBridgeHandler = installRuntimeControlBridge({
2987+
onPlay: () => {
2988+
player.play();
2989+
emitAnalyticsEvent("composition_played", { time: player.getTime() });
2990+
},
2991+
onPause: () => {
2992+
player.pause();
2993+
emitAnalyticsEvent("composition_paused", { time: player.getTime() });
2994+
},
2995+
onStopMedia: () => {
2996+
webAudio.stopAll();
2997+
const mediaEls = document.querySelectorAll("video, audio");
2998+
for (const el of mediaEls) {
2999+
if (el instanceof HTMLMediaElement && !el.paused) el.pause();
3000+
}
3001+
},
3002+
onSeek: (timeSeconds, _seekMode) => {
3003+
player.seek(timeSeconds);
3004+
emitAnalyticsEvent("composition_seeked", { time: timeSeconds });
3005+
},
3006+
onSetMuted: (muted) => {
3007+
state.bridgeMuted = muted;
3008+
const effective = muted || state.mediaOutputMuted;
3009+
webAudio.setMuted(effective);
3010+
const mediaEls = document.querySelectorAll("video, audio");
3011+
for (const el of mediaEls) {
3012+
if (!(el instanceof HTMLMediaElement)) continue;
3013+
el.muted = effective || el.defaultMuted;
3014+
}
3015+
},
3016+
onSetVolume: (volume) => {
3017+
state.bridgeVolume = volume;
3018+
webAudio.setVolume(volume);
3019+
const mediaEls = document.querySelectorAll("video, audio");
3020+
for (const el of mediaEls) {
3021+
if (!(el instanceof HTMLMediaElement)) continue;
3022+
const parsed = parseFloat(el.dataset.volume ?? "");
3023+
const clipVolume = Number.isFinite(parsed) ? parsed : 1;
3024+
el.volume = clipVolume * volume;
3025+
}
3026+
},
3027+
onSetMediaOutputMuted: (muted) => {
3028+
state.mediaOutputMuted = muted;
3029+
const effective = muted || state.bridgeMuted;
3030+
webAudio.setMuted(effective);
3031+
const mediaEls = document.querySelectorAll("video, audio");
3032+
for (const el of mediaEls) {
3033+
if (!(el instanceof HTMLMediaElement)) continue;
3034+
el.muted = effective || el.defaultMuted;
3035+
}
3036+
},
3037+
onSetNativeMediaSyncDisabled: (disabled) => {
3038+
if (state.nativeMediaSyncDisabled === disabled) return;
3039+
state.nativeMediaSyncDisabled = disabled;
3040+
state.mediaForceSyncNextTick = true;
3041+
if (disabled) {
3042+
webAudio.stopAll();
3043+
clock.detachAudioSource();
3044+
} else {
3045+
syncMediaForCurrentState();
3046+
}
3047+
},
3048+
onSetWebAudioMediaDisabled: (disabled) => {
3049+
if (state.webAudioMediaDisabled === disabled) return;
3050+
state.webAudioMediaDisabled = disabled;
3051+
state.mediaForceSyncNextTick = true;
3052+
if (disabled) {
3053+
webAudio.stopAll();
3054+
clock.detachAudioSource();
3055+
syncMediaForCurrentState();
3056+
} else {
3057+
syncMediaForCurrentState();
3058+
}
3059+
},
3060+
onSetPlaybackRate: (rate) => {
3061+
applyPlaybackRate(rate);
3062+
if (state.transportClock) state.transportClock.setRate(state.playbackRate);
3063+
applyWebAudioRate();
3064+
},
3065+
onSetRootDuration: growRootDurationLive,
3066+
onSetColorGrading: (target, grading) => {
3067+
colorGrading.setGrading(target, grading);
3068+
},
3069+
onSetColorGradingCompare: (target, compare) => {
3070+
colorGrading.setCompare(target, compare);
3071+
},
3072+
onTick: () => {
3073+
if (state.tornDown || !clock.isPlaying()) return;
3074+
const t = clock.now();
3075+
state.currentTime = t;
3076+
seekTimelineAndAdapters(t);
3077+
if (clock.reachedEnd()) {
3078+
webAudio.stopAll();
3079+
clock.detachAudioSource();
3080+
clock.pause();
3081+
state.isPlaying = false;
3082+
const dur = clock.getDuration();
3083+
if (Number.isFinite(dur)) {
3084+
clock.seek(dur);
3085+
state.currentTime = dur;
3086+
seekTimelineAndAdapters(dur);
3087+
}
3088+
runAdapters("pause");
3089+
syncMediaForCurrentState();
3090+
postState(true);
3091+
}
3092+
},
3093+
onEnablePickMode: () => picker.enablePickMode(),
3094+
onDisablePickMode: () => picker.disablePickMode(),
3095+
getCanonicalFps: () => state.canonicalFps,
3096+
});
3097+
30933098
const teardown = () => {
30943099
if (state.tornDown) return;
30953100
state.tornDown = true;

0 commit comments

Comments
 (0)