Skip to content

Commit 5a86fe4

Browse files
shivamhwpPR Batch Testerclaude
authored
fix(mobile): Android no longer freezes when you go back from a running thread (#19292)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e287582 commit 5a86fe4

2 files changed

Lines changed: 57 additions & 37 deletions

File tree

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ import {
3333
} from "react";
3434
import {
3535
AccessibilityInfo,
36+
Animated as NativeAnimated,
3637
AppState,
3738
type ColorValue,
39+
Easing,
3840
Pressable,
3941
ScrollView,
4042
StyleSheet,
43+
useAnimatedValue,
4144
View,
4245
} from "react-native";
4346
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
@@ -91,17 +94,12 @@ import type { FilePreviewSource } from "../../components/FilePreviewModal";
9194
import { FilePreviewGallery } from "../../components/FilePreviewGallery";
9295
import { ThreadMarkdownImage } from "./ThreadMarkdownImage";
9396
import Animated, {
94-
cancelAnimation,
95-
Easing,
9697
FadeIn,
9798
FadeOut,
9899
LinearTransition,
99100
ReduceMotion,
100101
useAnimatedStyle,
101102
useSharedValue,
102-
withDelay,
103-
withRepeat,
104-
withSequence,
105103
withTiming,
106104
} from "react-native-reanimated";
107105
import { useAssetUrl } from "../../state/assets";
@@ -267,7 +265,7 @@ export function ShimmeringWorkContent(props: {
267265
const [appIsActive, setAppIsActive] = useState(AppState.currentState === "active");
268266
const [reducedMotion, setReducedMotion] = useState(true);
269267
const screenIsFocused = useIsFocused();
270-
const progress = useSharedValue(0);
268+
const progress = useAnimatedValue(0);
271269
const gradientId = `work-shimmer-${useId().replaceAll(":", "")}`;
272270
const contentWidth = Math.min(
273271
availableWidth,
@@ -290,37 +288,37 @@ export function ShimmeringWorkContent(props: {
290288
return () => subscription.remove();
291289
}, []);
292290

291+
// Runs on React Native's native driver instead of Reanimated, whose
292+
// per-frame commits starve React's while commit pausing is disabled
293+
// (package.json). See docs/internals/performance-regressions.md.
293294
useEffect(() => {
294-
cancelAnimation(progress);
295-
progress.value = 0;
295+
progress.setValue(0);
296296
if (contentWidth <= 0 || reducedMotion || !appIsActive || !screenIsFocused) return;
297297

298-
progress.value = withRepeat(
299-
withSequence(
300-
withTiming(1, {
301-
duration: SHIMMER_SWEEP_MS,
302-
easing: Easing.linear,
303-
reduceMotion: ReduceMotion.Never,
304-
}),
305-
withDelay(
306-
SHIMMER_PAUSE_MS,
307-
withTiming(0, { duration: 0, reduceMotion: ReduceMotion.Never }),
308-
),
309-
),
310-
-1,
311-
false,
312-
undefined,
313-
ReduceMotion.Never,
298+
const loop = NativeAnimated.loop(
299+
NativeAnimated.timing(progress, {
300+
toValue: 1,
301+
duration: SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS,
302+
easing: Easing.linear,
303+
useNativeDriver: true,
304+
}),
314305
);
315-
return () => cancelAnimation(progress);
306+
loop.start();
307+
return () => loop.stop();
316308
}, [appIsActive, contentWidth, progress, reducedMotion, screenIsFocused]);
317309

318-
const sweepStyle = useAnimatedStyle(() => ({
319-
transform: [{ translateX: -SHIMMER_WIDTH + progress.value * (contentWidth + SHIMMER_WIDTH) }],
320-
}));
321-
const counterSweepStyle = useAnimatedStyle(() => ({
322-
transform: [{ translateX: SHIMMER_WIDTH - progress.value * (contentWidth + SHIMMER_WIDTH) }],
323-
}));
310+
// One timing covers the sweep and the pause, so the native driver can loop it.
311+
// The React Compiler memoizes these, so re-renders don't rebuild the native
312+
// animation nodes.
313+
const inputRange = [0, SHIMMER_SWEEP_MS / (SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS), 1];
314+
const sweepTranslateX = progress.interpolate({
315+
inputRange,
316+
outputRange: [-SHIMMER_WIDTH, contentWidth, contentWidth],
317+
});
318+
const counterSweepTranslateX = progress.interpolate({
319+
inputRange,
320+
outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],
321+
});
324322

325323
return (
326324
<View
@@ -341,13 +339,20 @@ export function ShimmeringWorkContent(props: {
341339
onTextLayout={(event) => setTextWidth(event.nativeEvent.lines[0]?.width ?? 0)}
342340
/>
343341
{!reducedMotion && appIsActive && screenIsFocused && contentWidth > 0 ? (
344-
<Animated.View
345-
className="absolute inset-y-0 left-0 overflow-hidden"
342+
<NativeAnimated.View
346343
pointerEvents="none"
347344
accessible={false}
348345
accessibilityElementsHidden
349346
importantForAccessibility="no-hide-descendants"
350-
style={[{ width: SHIMMER_WIDTH }, sweepStyle]}
347+
style={{
348+
position: "absolute",
349+
top: 0,
350+
bottom: 0,
351+
left: 0,
352+
overflow: "hidden",
353+
width: SHIMMER_WIDTH,
354+
transform: [{ translateX: sweepTranslateX }],
355+
}}
351356
>
352357
<MaskedView
353358
style={StyleSheet.absoluteFill}
@@ -368,7 +373,9 @@ export function ShimmeringWorkContent(props: {
368373
</Svg>
369374
}
370375
>
371-
<Animated.View style={[{ width: availableWidth }, counterSweepStyle]}>
376+
<NativeAnimated.View
377+
style={{ width: availableWidth, transform: [{ translateX: counterSweepTranslateX }] }}
378+
>
372379
<ShimmerWorkContent
373380
textClassName={props.textClassName}
374381
compact={props.compact}
@@ -381,9 +388,9 @@ export function ShimmeringWorkContent(props: {
381388
themeAppearance={props.themeAppearance}
382389
toolIcon={props.toolIcon}
383390
/>
384-
</Animated.View>
391+
</NativeAnimated.View>
385392
</MaskedView>
386-
</Animated.View>
393+
</NativeAnimated.View>
387394
) : null}
388395
</View>
389396
);

‎docs/internals/performance-regressions.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,16 @@ continuous traffic during an outage.
121121
Focused coverage: `ProjectionSettlement.test.ts`, `ThreadSettlementService.test.ts`,
122122
`runtimeLayer.test.ts`, `AgentAwarenessRelay.test.ts`, `GitVcsDriverCore.test.ts`,
123123
`ThreadStatusIndicators.subscriptions.browser.test.tsx`, and `desktopLocal.test.ts`.
124+
125+
## Mobile looping animations
126+
127+
`apps/mobile/package.json` sets Reanimated's `DISABLE_COMMIT_PAUSING_MECHANISM` so keyboard avoiding
128+
animates on iOS. Reanimated documents that flag as safe only alongside React Native's
129+
`preventShadowTreeCommitExhaustion`, which defaults to off and needs React Native built from source,
130+
so our builds run without it. Reanimated commits each animation frame to the shadow tree, and
131+
without commit pausing those commits can starve React's: the JS thread keeps retrying its commit,
132+
the screen freezes, and back does nothing. Release builds retry forever; dev builds abort with
133+
`ShadowTree.cpp: assertion failed (attempts < 1024)`. Looping animations that stay on screen while
134+
heavy content renders should use React Native's `Animated` with `useNativeDriver: true`, which
135+
updates views without commits, not Reanimated's `withRepeat`. The running-thread shimmer in
136+
`thread-work-log.tsx` is the example.

0 commit comments

Comments
 (0)