From b96d3c8bd6aefe412dda92d3b788fea0ee4fa3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 5 Nov 2026 09:39:37 +0000 Subject: [PATCH] fix(core): a multi-worker render replays overlapping tweens behind the timeline stand-in (#7231) * fix(core): a multi-worker render replays overlapping tweens behind the timeline stand-in The overlap and landing-mark walks compared tween parents with the stand-in the producer's GSAP stub hands out, never met it, and lost the overlaps and step times. Both now compare with the real timeline behind the stand-in. * fix(core): drop the moved helper's comment so the file's comment share holds --- .../src/runtime/adapters/gsap.order.test.ts | 25 ++++++++++++--- .../src/runtime/adapters/gsap.steps.test.ts | 31 ++++++++++++------- packages/core/src/runtime/adapters/gsap.ts | 10 ++++-- .../adapters/gsapStandIn.test-helpers.ts | 15 +++++++++ packages/core/src/runtime/init.ts | 4 +-- 5 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 packages/core/src/runtime/adapters/gsapStandIn.test-helpers.ts diff --git a/packages/core/src/runtime/adapters/gsap.order.test.ts b/packages/core/src/runtime/adapters/gsap.order.test.ts index 8a61b593cc..28a993ab45 100644 --- a/packages/core/src/runtime/adapters/gsap.order.test.ts +++ b/packages/core/src/runtime/adapters/gsap.order.test.ts @@ -1,24 +1,33 @@ import gsap from "gsap"; import { describe, expect, it } from "vitest"; import { seekGsapTimeline } from "./gsap"; +import { producerStandIn } from "./gsapStandIn.test-helpers"; import type { RuntimeTimelineLike } from "../types"; const FPS = 60; const FRAMES = 150; // A zoom punch still running when a cut sets the zoom and a slower move starts from wherever the zoom is. -function overlappingZoom() { +function overlappingZoom(standIn = false) { const params = { z: 1 }; const timeline = gsap.timeline({ paused: true }); timeline.to(params, { z: 1.45, duration: 0.3, ease: "power4.out" }, 0.573); timeline.set(params, { z: 1.04 }, 0.8); timeline.to(params, { z: 1.12, duration: 1.39, ease: "power1.in" }, 0.869); - return { params, timeline: timeline as unknown as RuntimeTimelineLike }; + return { + params, + timeline: standIn ? producerStandIn(timeline) : (timeline as unknown as RuntimeTimelineLike), + }; } /** The zoom at each frame a renderer captures, in order; a scene's clock starts at `hostStart`. */ -function capture(frames: number[], hostStart = 0, seekFirst?: number): Map { - const { params, timeline } = overlappingZoom(); +function capture( + frames: number[], + hostStart = 0, + seekFirst?: number, + standIn = false, +): Map { + const { params, timeline } = overlappingZoom(standIn); const zoom = new Map(); if (seekFirst !== undefined) seekGsapTimeline(timeline, seekFirst, true, undefined, FPS); for (const frame of frames) { @@ -42,6 +51,14 @@ describe("a render that captures frames out of order", () => { } }); + it("gives every frame the same value when the producer hands out a stand-in for the timeline", () => { + for (let worker = 0; worker < 8; worker++) { + for (const [frame, z] of capture(range(worker, FRAMES, 8), 0, undefined, true)) { + expect([frame, z]).toEqual([frame, inOrder.get(frame)]); + } + } + }); + it("gives every frame the same value when a chunk starts inside the overlap", () => { for (const [frame, z] of capture(range(53, FRAMES))) { expect([frame, z]).toEqual([frame, inOrder.get(frame)]); diff --git a/packages/core/src/runtime/adapters/gsap.steps.test.ts b/packages/core/src/runtime/adapters/gsap.steps.test.ts index fc40d97c5d..499e80433d 100644 --- a/packages/core/src/runtime/adapters/gsap.steps.test.ts +++ b/packages/core/src/runtime/adapters/gsap.steps.test.ts @@ -9,6 +9,7 @@ import { seekGsapTimeline, } from "./gsap"; import type { GsapStructureKey } from "./gsap"; +import { producerStandIn } from "./gsapStandIn.test-helpers"; import type { RuntimeTimelineLike } from "../types"; // Every 0.1 s: hide all three frames, then show one, as frame-by-frame films do. @@ -646,18 +647,24 @@ describe("seekGsapTimeline", () => { expect(scene.field.style.visibility).toBe("visible"); }); - it("shows two steps on one later time in authored order after the playhead comes back onto it", () => { - const word = document.body.appendChild(document.createElement("span")); - const timeline = gsap.timeline({ paused: true }); - timeline.set(word, { visibility: "hidden" }, 1).set(word, { visibility: "visible" }, 1); - timeline.to({}, { duration: 3 }, 0); - const scene = timeline as unknown as RuntimeTimelineLike; - sceneSeek(scene, 2); - sceneSeek(scene, 1); - expect(word.style.visibility).toBe("visible"); - sceneSeek(scene, 1.2); - expect(word.style.visibility).toBe("visible"); - }); + it.each([ + ["a timeline", (timeline: gsap.core.Timeline) => timeline as unknown as RuntimeTimelineLike], + ["the producer's stand-in for a timeline", producerStandIn], + ])( + "shows two steps on one later time in authored order after the playhead comes back onto %s", + (_, seekable) => { + const word = document.body.appendChild(document.createElement("span")); + const timeline = gsap.timeline({ paused: true }); + timeline.set(word, { visibility: "hidden" }, 1).set(word, { visibility: "visible" }, 1); + timeline.to({}, { duration: 3 }, 0); + const scene = seekable(timeline); + sceneSeek(scene, 2); + sceneSeek(scene, 1); + expect(word.style.visibility).toBe("visible"); + sceneSeek(scene, 1.2); + expect(word.style.visibility).toBe("visible"); + }, + ); it("keeps a scene's steps at 0 through the root's redraw at 0 on every seek", () => { const scene = revealedAtZero(); diff --git a/packages/core/src/runtime/adapters/gsap.ts b/packages/core/src/runtime/adapters/gsap.ts index 396650d5c0..41d1c39753 100644 --- a/packages/core/src/runtime/adapters/gsap.ts +++ b/packages/core/src/runtime/adapters/gsap.ts @@ -159,9 +159,14 @@ function spanOnParent( return [Number(parent._start) + from / Math.abs(speed), span / Math.abs(speed)]; } +export function gsapTimelineOf(timeline: object): object { + return (timeline as { __hfReal?: object }).__hfReal ?? timeline; +} + function writeOf(tween: PlacedTween, timeline: RuntimeTimelineLike): PropertyWrite | null { + const owner = gsapTimelineOf(timeline); let span: [number, number] | null = [Number(tween._start), Number(tween.totalDuration?.() ?? 0)]; - for (let parent = tween.parent; span && parent && parent !== timeline; parent = parent.parent) { + for (let parent = tween.parent; span && parent && parent !== owner; parent = parent.parent) { span = spanOnParent(span, parent); } return ( @@ -355,8 +360,9 @@ type GsapPlaced = { }; function startOnOwner(child: GsapPlaced, owner: object): number | null { + const timeline = gsapTimelineOf(owner); let at = Number(child._start); - for (let parent = child.parent; parent && parent !== owner; parent = parent.parent) { + for (let parent = child.parent; parent && parent !== timeline; parent = parent.parent) { if (parent._repeat || !parent._ts) return null; at = Number(parent._start) + at / parent._ts; } diff --git a/packages/core/src/runtime/adapters/gsapStandIn.test-helpers.ts b/packages/core/src/runtime/adapters/gsapStandIn.test-helpers.ts new file mode 100644 index 0000000000..cf56216a9b --- /dev/null +++ b/packages/core/src/runtime/adapters/gsapStandIn.test-helpers.ts @@ -0,0 +1,15 @@ +import type gsap from "gsap"; +import type { RuntimeTimelineLike } from "../types"; + +/** The shape the producer's GSAP stub hands a composition instead of the timeline it created. */ +export function producerStandIn(real: gsap.core.Timeline): RuntimeTimelineLike { + const standIn = { + __hfReal: real, + __hfIsProxy: true, + totalTime: (...args: [number, boolean?]) => + args.length > 0 ? (real.totalTime(...args), standIn) : real.totalTime(), + totalDuration: () => real.totalDuration(), + getChildren: (...args: Parameters) => real.getChildren(...args), + }; + return standIn as unknown as RuntimeTimelineLike; +} diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 85beab1cfb..0b10ea2561 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -57,6 +57,7 @@ import { createGsapAdapter, holdFrameMarks, rerenderGsapTimelineAt, + gsapTimelineOf, seekGsapTimeline, type GsapStructureKey, } from "./adapters/gsap"; @@ -1641,9 +1642,6 @@ export function initSandboxRuntimeModular(): void { // Sub-composition timelines the runtime nested into the root: the host composition id, and where. const autoNested = new WeakMap(); - // The producer's GSAP stub registers stand-ins; GSAP's tree holds the timelines behind them. - const gsapTimelineOf = (timeline: object): object => - (timeline as { __hfReal?: object }).__hfReal ?? timeline; // The composite that holds the children of a root with no timeline, and how many it holds. let missingRootComposite: { timeline: RuntimeTimelineLike; size: number } | null = null; // Roots the runtime built. One it no longer holds would stay on GSAP's global timeline with all it last held.