Skip to content
TypeScript

fix(mobile): Android no longer freezes when you go back from a running thread (#19292)

shivam5a86fe433f89
diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsx
index 89657ce6b6d..b347227caec 100644
--- a/apps/mobile/src/features/threads/thread-work-log.tsx
+++ b/apps/mobile/src/features/threads/thread-work-log.tsx
@@ -33,11 +33,14 @@ import {
} from "react";
import {
AccessibilityInfo,
+ Animated as NativeAnimated,
AppState,
type ColorValue,
+ Easing,
Pressable,
ScrollView,
StyleSheet,
+ useAnimatedValue,
View,
} from "react-native";
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
@@ -91,17 +94,12 @@ import type { FilePreviewSource } from "../../components/FilePreviewModal";
import { FilePreviewGallery } from "../../components/FilePreviewGallery";
import { ThreadMarkdownImage } from "./ThreadMarkdownImage";
import Animated, {
- cancelAnimation,
- Easing,
FadeIn,
FadeOut,
LinearTransition,
ReduceMotion,
useAnimatedStyle,
useSharedValue,
- withDelay,
- withRepeat,
- withSequence,
withTiming,
} from "react-native-reanimated";
import { useAssetUrl } from "../../state/assets";
@@ -267,7 +265,7 @@ export function ShimmeringWorkContent(props: {
const [appIsActive, setAppIsActive] = useState(AppState.currentState === "active");
const [reducedMotion, setReducedMotion] = useState(true);
const screenIsFocused = useIsFocused();
- const progress = useSharedValue(0);
+ const progress = useAnimatedValue(0);
const gradientId = `work-shimmer-${useId().replaceAll(":", "")}`;
const contentWidth = Math.min(
availableWidth,
@@ -290,37 +288,37 @@ export function ShimmeringWorkContent(props: {
return () => subscription.remove();
}, []);
+ // Runs on React Native's native driver instead of Reanimated, whose
+ // per-frame commits starve React's while commit pausing is disabled
+ // (package.json). See docs/internals/performance-regressions.md.
useEffect(() => {
- cancelAnimation(progress);
- progress.value = 0;
+ progress.setValue(0);
if (contentWidth <= 0 || reducedMotion || !appIsActive || !screenIsFocused) return;
- progress.value = withRepeat(
- withSequence(
- withTiming(1, {
- duration: SHIMMER_SWEEP_MS,
- easing: Easing.linear,
- reduceMotion: ReduceMotion.Never,
- }),
- withDelay(
- SHIMMER_PAUSE_MS,
- withTiming(0, { duration: 0, reduceMotion: ReduceMotion.Never }),
- ),
- ),
- -1,
- false,
- undefined,
- ReduceMotion.Never,
+ const loop = NativeAnimated.loop(
+ NativeAnimated.timing(progress, {
+ toValue: 1,
+ duration: SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS,
+ easing: Easing.linear,
+ useNativeDriver: true,
+ }),
);
- return () => cancelAnimation(progress);
+ loop.start();
+ return () => loop.stop();
}, [appIsActive, contentWidth, progress, reducedMotion, screenIsFocused]);
- const sweepStyle = useAnimatedStyle(() => ({
- transform: [{ translateX: -SHIMMER_WIDTH + progress.value * (contentWidth + SHIMMER_WIDTH) }],
- }));
- const counterSweepStyle = useAnimatedStyle(() => ({
- transform: [{ translateX: SHIMMER_WIDTH - progress.value * (contentWidth + SHIMMER_WIDTH) }],
- }));
+ // One timing covers the sweep and the pause, so the native driver can loop it.
+ // The React Compiler memoizes these, so re-renders don't rebuild the native
+ // animation nodes.
+ const inputRange = [0, SHIMMER_SWEEP_MS / (SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS), 1];
+ const sweepTranslateX = progress.interpolate({
+ inputRange,
+ outputRange: [-SHIMMER_WIDTH, contentWidth, contentWidth],
+ });
+ const counterSweepTranslateX = progress.interpolate({
+ inputRange,
+ outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],
+ });
return (
<View
@@ -341,13 +339,20 @@ export function ShimmeringWorkContent(props: {
onTextLayout={(event) => setTextWidth(event.nativeEvent.lines[0]?.width ?? 0)}
/>
{!reducedMotion && appIsActive && screenIsFocused && contentWidth > 0 ? (
- <Animated.View
- className="absolute inset-y-0 left-0 overflow-hidden"
+ <NativeAnimated.View
pointerEvents="none"
accessible={false}
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
- style={[{ width: SHIMMER_WIDTH }, sweepStyle]}
+ style={{
+ position: "absolute",
+ top: 0,
+ bottom: 0,
+ left: 0,
+ overflow: "hidden",
+ width: SHIMMER_WIDTH,
+ transform: [{ translateX: sweepTranslateX }],
+ }}
>
<MaskedView
style={StyleSheet.absoluteFill}
@@ -368,7 +373,9 @@ export function ShimmeringWorkContent(props: {
</Svg>
}
>
- <Animated.View style={[{ width: availableWidth }, counterSweepStyle]}>
+ <NativeAnimated.View
+ style={{ width: availableWidth, transform: [{ translateX: counterSweepTranslateX }] }}
+ >
<ShimmerWorkContent
textClassName={props.textClassName}
compact={props.compact}
@@ -381,9 +388,9 @@ export function ShimmeringWorkContent(props: {
themeAppearance={props.themeAppearance}
toolIcon={props.toolIcon}
/>
- </Animated.View>
+ </NativeAnimated.View>
</MaskedView>
- </Animated.View>
+ </NativeAnimated.View>
) : null}
</View>
);
diff --git a/docs/internals/performance-regressions.md b/docs/internals/performance-regressions.md
index 6b6fa927ab8..0e395564f13 100644
--- a/docs/internals/performance-regressions.md
+++ b/docs/internals/performance-regressions.md
@@ -121,3 +121,16 @@ continuous traffic during an outage.
Focused coverage: `ProjectionSettlement.test.ts`, `ThreadSettlementService.test.ts`,
`runtimeLayer.test.ts`, `AgentAwarenessRelay.test.ts`, `GitVcsDriverCore.test.ts`,
`ThreadStatusIndicators.subscriptions.browser.test.tsx`, and `desktopLocal.test.ts`.
+
+## Mobile looping animations
+
+`apps/mobile/package.json` sets Reanimated's `DISABLE_COMMIT_PAUSING_MECHANISM` so keyboard avoiding
+animates on iOS. Reanimated documents that flag as safe only alongside React Native's
+`preventShadowTreeCommitExhaustion`, which defaults to off and needs React Native built from source,
+so our builds run without it. Reanimated commits each animation frame to the shadow tree, and
+without commit pausing those commits can starve React's: the JS thread keeps retrying its commit,
+the screen freezes, and back does nothing. Release builds retry forever; dev builds abort with
+`ShadowTree.cpp: assertion failed (attempts < 1024)`. Looping animations that stay on screen while
+heavy content renders should use React Native's `Animated` with `useNativeDriver: true`, which
+updates views without commits, not Reanimated's `withRepeat`. The running-thread shimmer in
+`thread-work-log.tsx` is the example.