Skip to content

Commit bcc7a22

Browse files
committed
refactor(core): tidy the Lottie animation end reader and pin its inferred length
1 parent 152ccf1 commit bcc7a22

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎packages/core/src/runtime/adapters/lottie.test.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("lottie adapter", () => {
4747
afterEach(() => {
4848
delete lottieWindow.lottie;
4949
delete lottieWindow.__hfLottie;
50+
document.body.innerHTML = "";
5051
});
5152

5253
it("has correct name", () => {
@@ -104,7 +105,6 @@ describe("lottie adapter", () => {
104105
expect(dot.setCurrentRawFrameValue).toHaveBeenCalledWith(30);
105106
adapter.seek({ time: 1 });
106107
expect(anim.goToAndStop).toHaveBeenLastCalledWith(0, false);
107-
document.body.innerHTML = "";
108108
});
109109

110110
it("keeps page time for a player in a composition that starts at 0", () => {
@@ -113,7 +113,6 @@ describe("lottie adapter", () => {
113113
lottieWindow.__hfLottie = [anim];
114114
adapter.seek({ time: 2 });
115115
expect(anim.goToAndStop).toHaveBeenCalledWith(2000, false);
116-
document.body.innerHTML = "";
117116
});
118117

119118
it("seeks lottie-web with goToAndStop in ms", () => {
@@ -304,13 +303,19 @@ describe("lottie adapter", () => {
304303
const adapter = createLottieAdapter();
305304
expect(adapter.getInferredDurationSeconds?.()).toBeNull();
306305
});
307-
});
308306

309-
describe("getAnimationCycleEndSeconds", () => {
310-
afterEach(() => {
311-
document.body.innerHTML = "";
307+
it("reports a player's own length, ignoring its composition's start and counting removed ones", () => {
308+
const { adapter, player } = mountedAt("5");
309+
lottieWindow.__hfLottie = [{ ...createLottieWebAnim({ totalFrames: 60 }), wrapper: player }];
310+
expect(adapter.getInferredDurationSeconds?.()).toBe(2);
311+
const gone = document.body.appendChild(document.createElement("div"));
312+
gone.remove();
313+
lottieWindow.__hfLottie.push({ ...createLottieWebAnim({ totalFrames: 90 }), wrapper: gone });
314+
expect(adapter.getInferredDurationSeconds?.()).toBe(3);
312315
});
316+
});
313317

318+
describe("getAnimationCycleEndSeconds", () => {
314319
it("ends one loop after the start of the player's composition", () => {
315320
const { adapter, player } = mountedAt("5");
316321
lottieWindow.__hfLottie = [{ ...createLottieWebAnim({ totalFrames: 60 }), wrapper: player }];

‎packages/core/src/runtime/adapters/lottie.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function createLottieAdapter(params?: {
6363
const secondsIntoComposition = (anim: unknown, pageTime: number): number =>
6464
Math.max(0, pageTime - compositionStartSeconds(anim));
6565

66-
const adapter: RuntimeDeterministicAdapter = {
66+
return {
6767
name: "lottie",
6868

6969
discover: () => {
@@ -167,39 +167,38 @@ export function createLottieAdapter(params?: {
167167
// Just let them be garbage collected naturally.
168168
},
169169

170-
getInferredDurationSeconds: () => latestInstanceEnd((_, seconds) => seconds),
170+
getInferredDurationSeconds: () => latestInstanceEnd((_, length) => length),
171171

172172
// Plays from its composition's start, as seek anchors it; a removed scene's instance stays registered.
173173
getAnimationCycleEndSeconds: () =>
174-
latestInstanceEnd((anim, seconds) =>
174+
latestInstanceEnd((anim, length) =>
175175
(lottieElement(anim) as Node | undefined)?.isConnected === false
176176
? null
177-
: compositionStartSeconds(anim) + seconds,
177+
: compositionStartSeconds(anim) + length,
178178
),
179179
};
180-
return adapter;
181180
}
182181

183182
/** Max of `endOf(instance, its length)` over registered instances; null skips an instance. */
184183
function latestInstanceEnd(
185-
endOf: (anim: LottieWebAnimation | DotLottiePlayer, seconds: number) => number | null,
184+
endOf: (anim: LottieWebAnimation | DotLottiePlayer, length: number) => number | null,
186185
): number | null {
187186
const instances = (window as LottieWindow).__hfLottie;
188187
if (!instances || instances.length === 0) return null;
189188
let maxSeconds = 0;
190189
let sawAny = false;
191190
for (const anim of instances) {
192-
let seconds: number | null = null;
191+
let end: number | null = null;
193192
try {
194193
const length = inferAnimationDurationSeconds(anim);
195-
seconds = length == null ? null : endOf(anim, length);
194+
end = length == null ? null : endOf(anim, length);
196195
} catch (err) {
197196
// ignore per-animation failures — keep going for other instances
198197
swallow("runtime.adapters.lottie.site4", err);
199198
}
200-
if (seconds == null) continue;
199+
if (end == null) continue;
201200
sawAny = true;
202-
maxSeconds = Math.max(maxSeconds, seconds);
201+
maxSeconds = Math.max(maxSeconds, end);
203202
}
204203
// Not-yet-loaded animations report totalFrames=0 — return null (not 0)
205204
// so the caller doesn't treat "still loading" as "genuinely zero

0 commit comments

Comments
 (0)