The VS Code of video editing, built for agents.
TypeScript
Refactor HtmlHost and rendering update mechanisms for improved performance
konstantin-paulus59dc6c01f758
diff --git a/apps/web/src/components/engine/decoders/html.ts b/apps/web/src/components/engine/decoders/html.tsindex bc7fea57..ec4de380 100644--- a/apps/web/src/components/engine/decoders/html.ts+++ b/apps/web/src/components/engine/decoders/html.ts@@ -17,40 +17,18 @@ export function isHtmlInCanvasSupported(): boolean {}/**- * Resolves after the browser's next rendering update (style/layout/paint) has- * completed. drawElementImage samples paint records that are only refreshed- * while the host canvas paints, so an offline render loop that mutates the- * paint's DOM must yield one rendering update before rasterizing — otherwise- * every frame in the same task samples the records of the last real paint.- * The task queued from inside rAF runs after that frame's paint, so this- * costs one vsync, not two.- *- * Memoized while in flight: every host forwarded in the same tick shares one- * rendering update, so a scene with many HTML paints still waits a single rAF- * per frame rather than scheduling one per host.+ * Resolves after the browser's next rendering update (style/layout/paint) has completed.*/-let pendingRenderingUpdate: Promise<void> | null = null;-export function nextRenderingUpdate(): Promise<void> {- pendingRenderingUpdate ??= Promise.race<void>([- new Promise(resolve => {- requestAnimationFrame(() => setTimeout(() => {- pendingRenderingUpdate = null;- resolve();- }, 0))- }),- // A little slower than 60fps (20ms ≈ 50fps): comfortably past a 60Hz vsync so a- // live rAF wins the race and we keep waiting for the real paint, while still- // capping the per-frame wait when rAF is paused (hidden/headless).- new Promise(resolve => {- setTimeout(() => {- pendingRenderingUpdate = null;- resolve();- }, 1000 / 50);- }),- ]);-- return pendingRenderingUpdate;+ return new Promise(resolve => {+ // timeout in case requestAnimationFrame is not called+ const timeout = setTimeout(resolve, 250);++ requestAnimationFrame(() => {+ clearTimeout(timeout);+ setTimeout(resolve, 0);+ });+ });}let htmlCanvas: HTMLCanvasElement | null = null;@@ -105,8 +83,17 @@ export class HtmlHost {/*** Resolves asynchronously loaded html resources.*/- public whenReady(): Promise<void> {+ public whenReady(timeInSeconds: number): Promise<void> {+ // sync animations+ for (const animation of this.element.getAnimations({ subtree: true })) {+ if (animation.playState !== 'paused') animation.pause();+ animation.currentTime = timeInSeconds * 1000;+ }++ // wait for fonts to be readyconst pending: Promise<unknown>[] = [document.fonts.ready];++ // wait for images to be readyfor (const image of this.element.querySelectorAll('img')) {// `complete` covers loaded, failed, and srcless images alike; the// rest are still in flight, and a failed decode just paints nothing.diff --git a/apps/web/src/components/engine/systems/playback.ts b/apps/web/src/components/engine/systems/playback.tsindex 24a8fcd9..87337d35 100644--- a/apps/web/src/components/engine/systems/playback.ts+++ b/apps/web/src/components/engine/systems/playback.ts@@ -207,10 +207,10 @@ function forwardAudioDecoder(world: EngineWorld, sid: number, eid: number, aid?:}}-function forwardHtmlHost(world: EngineWorld, eid: number, fid: number): void {+function forwardHtmlHost(world: EngineWorld, sid: number, eid: number, fid: number): void {const c = world.components;if (world.mode === 'realtime' || c.Computed.visibility[eid] !== 1 || !c.HtmlHost[fid]) return;- world.promises?.push(c.HtmlHost[fid].whenReady());+ world.promises?.push(c.HtmlHost[fid].whenReady(c.Computed.localTimeInSeconds[sid] ?? 0));}function forwardImageDecoder(world: EngineWorld, _sid: number, _eid: number, fid: number): void {@@ -257,7 +257,7 @@ function forwardDecoders(world: EngineWorld, sid: number, eid: number): void {}if (c.Paint[fid] === PaintType.HTML && visualsEnabled) {- forwardHtmlHost(world, eid, fid);+ forwardHtmlHost(world, sid, eid, fid);}if (c.Paint[fid] === PaintType.SHADER && visualsEnabled) {