Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(web): unsubscribe avatar motion before Lottie destroy()

Dongdong Chen1e0856106612
diff --git a/apps/web/src/features/rooms/members/agent-avatar-motion.tsx b/apps/web/src/features/rooms/members/agent-avatar-motion.tsx
index aca86a79..1c0c1bd2 100644
--- a/apps/web/src/features/rooms/members/agent-avatar-motion.tsx
+++ b/apps/web/src/features/rooms/members/agent-avatar-motion.tsx
@@ -61,9 +61,18 @@ export default function AgentAvatarMotion({
}) {
const boxRef = useRef<HTMLSpanElement>(null);
const [animation, setAnimation] = useState<AnimationItem>();
+ // The complete listener reads the latest wishes without re-subscribing.
+ const idleRef = useRef({ hold, onIdle, playing });
+ useEffect(() => {
+ idleRef.current = { hold, onIdle, playing };
+ });
+ // One effect owns the Lottie item, so its cleanup can unsubscribe before
+ // destroy(); a separate listener effect would unsubscribe after it and throw.
+ // oxlint-disable-next-line react-doctor/effect-needs-cleanup -- The cleanup calls stopListening, which the rule cannot follow out of the async load.
useEffect(() => {
let cancelled = false;
let item: AnimationItem | undefined;
+ let stopListening: (() => void) | undefined;
void Promise.all([
import("lottie-web/build/player/lottie_light"),
fetch(motionUrl(figure)).then((response) => {
@@ -87,6 +96,10 @@ export default function AgentAvatarMotion({
renderer: "svg",
rendererSettings: { preserveAspectRatio: "xMidYMid slice" },
});
+ stopListening = item.addEventListener("complete", () => {
+ const { hold: held, onIdle: idle, playing: wanted } = idleRef.current;
+ if (!held && !wanted) idle?.();
+ });
// Inline animation data renders its SVG synchronously.
setAnimation(item);
})
@@ -94,22 +107,13 @@ export default function AgentAvatarMotion({
.catch(() => undefined);
return () => {
cancelled = true;
+ // Unsubscribe before destroy(): Lottie clears its listener table there,
+ // and an unsubscribe after it throws.
+ stopListening?.();
item?.destroy();
setAnimation(undefined);
};
}, [figure]);
- // The complete listener reads the latest wishes without re-subscribing.
- const idleRef = useRef({ hold, onIdle, playing });
- useEffect(() => {
- idleRef.current = { hold, onIdle, playing };
- });
- useEffect(() => {
- if (!animation) return;
- return animation.addEventListener("complete", () => {
- const { hold: held, onIdle: idle, playing: wanted } = idleRef.current;
- if (!held && !wanted) idle?.();
- });
- }, [animation]);
const heldRef = useRef(false);
useEffect(() => {
if (!animation) return;
diff --git a/apps/web/src/features/rooms/members/agent-avatar-portrait.test.tsx b/apps/web/src/features/rooms/members/agent-avatar-portrait.test.tsx
index e331c15d..1143e0b9 100644
--- a/apps/web/src/features/rooms/members/agent-avatar-portrait.test.tsx
+++ b/apps/web/src/features/rooms/members/agent-avatar-portrait.test.tsx
@@ -5,14 +5,18 @@ import { figureKey } from "./agent-avatar";
import { AgentAvatarPortrait } from "./agent-avatar-portrait";
const animation = vi.hoisted(() => {
- const listeners = new Set<() => void>();
+ // Like Lottie, destroy() drops the listener table, so an unsubscribe called
+ // after destroy() throws.
+ let listeners: Set<() => void> | null = new Set();
const item = {
addEventListener: vi.fn((_name: string, listener: () => void) => {
- listeners.add(listener);
- return () => listeners.delete(listener);
+ listeners!.add(listener);
+ return () => listeners!.delete(listener);
}),
currentFrame: 0,
- destroy: vi.fn(),
+ destroy: vi.fn(() => {
+ listeners = null;
+ }),
firstFrame: 0,
isPaused: true,
play: vi.fn(() => {
@@ -27,12 +31,21 @@ const animation = vi.hoisted(() => {
complete() {
item.isPaused = true;
item.currentFrame = 83;
- listeners.forEach((listener) => listener());
+ listeners?.forEach((listener) => listener());
+ },
+ /** A fresh item for each loadAnimation, as Lottie returns. */
+ reset() {
+ listeners = new Set();
},
};
return item;
});
-const loadAnimation = vi.hoisted(() => vi.fn(() => animation));
+const loadAnimation = vi.hoisted(() =>
+ vi.fn(() => {
+ animation.reset();
+ return animation;
+ }),
+);
vi.mock("lottie-web/build/player/lottie_light", () => ({
default: { loadAnimation },
}));