Skip to content

Commit 006d7e3

Browse files
shivamhwpPR Batch Testerclaude
authored
fix(mobile): stop the work-row shimmer from updating every frame (#28530)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9f2bb98 commit 006d7e3

3 files changed

Lines changed: 63 additions & 25 deletions

File tree

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

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
import {
3535
Animated as NativeAnimated,
3636
type ColorValue,
37-
Easing,
3837
Pressable,
3938
ScrollView,
4039
StyleSheet,
@@ -105,10 +104,9 @@ import Animated, {
105104
import { useAssetUrl } from "../../state/assets";
106105
import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
107106
import { useOpenLinkedThread } from "./useOpenLinkedThread";
107+
import { startShimmerSteps } from "./work-shimmer-steps";
108108

109109
const SHIMMER_WIDTH = 72;
110-
const SHIMMER_SWEEP_MS = 1_350;
111-
const SHIMMER_PAUSE_MS = 1_450;
112110
const SHIMMER_ICON_AND_GAP_WIDTH = 30;
113111
export const THREAD_DISCLOSURE_TRANSITION_MS = 180;
114112
const WORK_LOG_LAYOUT_TRANSITION = LinearTransition.duration(THREAD_DISCLOSURE_TRANSITION_MS);
@@ -269,44 +267,33 @@ export function ShimmeringWorkContent(props: {
269267
const appIsActive = useAppActive();
270268
const reducedMotion = useReduceMotionEnabled();
271269
const screenIsFocused = useIsFocused();
272-
const progress = useAnimatedValue(0);
270+
// Native from creation, so each setValue updates the view without a React render.
271+
const progress = useAnimatedValue(0, { useNativeDriver: true });
273272
const gradientId = `work-shimmer-${useId().replaceAll(":", "")}`;
274273
const contentWidth = Math.min(
275274
availableWidth,
276275
(props.showIcon ? SHIMMER_ICON_AND_GAP_WIDTH : 0) + Math.ceil(textWidth),
277276
);
278277

279-
// Runs on React Native's native driver instead of Reanimated, whose
280-
// per-frame commits starve React's while commit pausing is disabled
281-
// (package.json). See docs/internals/performance-regressions.md.
282-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the native-driver shimmer loop while visible and active, and stops it on cleanup
278+
// Uses a native Animated value instead of Reanimated, whose per-frame commits
279+
// starve React's while commit pausing is disabled (package.json). See
280+
// docs/internals/performance-regressions.md.
281+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- steps the native shimmer value on timers while visible and active, and stops them on cleanup
283282
useEffect(() => {
284283
progress.setValue(0);
285284
if (contentWidth <= 0 || reducedMotion || !appIsActive || !screenIsFocused) return;
286-
287-
const loop = NativeAnimated.loop(
288-
NativeAnimated.timing(progress, {
289-
toValue: 1,
290-
duration: SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS,
291-
easing: Easing.linear,
292-
useNativeDriver: true,
293-
}),
294-
);
295-
loop.start();
296-
return () => loop.stop();
285+
return startShimmerSteps(progress);
297286
}, [appIsActive, contentWidth, progress, reducedMotion, screenIsFocused]);
298287

299-
// One timing covers the sweep and the pause, so the native driver can loop it.
300288
// The React Compiler memoizes these, so re-renders don't rebuild the native
301289
// animation nodes.
302-
const inputRange = [0, SHIMMER_SWEEP_MS / (SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS), 1];
303290
const sweepTranslateX = progress.interpolate({
304-
inputRange,
305-
outputRange: [-SHIMMER_WIDTH, contentWidth, contentWidth],
291+
inputRange: [0, 1],
292+
outputRange: [-SHIMMER_WIDTH, contentWidth],
306293
});
307294
const counterSweepTranslateX = progress.interpolate({
308-
inputRange,
309-
outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],
295+
inputRange: [0, 1],
296+
outputRange: [SHIMMER_WIDTH, -contentWidth],
310297
});
311298

312299
return (
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
3+
import {
4+
SHIMMER_PAUSE_MS,
5+
SHIMMER_STEPS,
6+
SHIMMER_SWEEP_MS,
7+
startShimmerSteps,
8+
} from "./work-shimmer-steps";
9+
10+
it("steps the sweep, stays quiet through the pause, and stops on cleanup", () => {
11+
using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };
12+
vi.useFakeTimers();
13+
const values: number[] = [];
14+
const stop = startShimmerSteps({ setValue: (value) => values.push(value) });
15+
const valuesDuring = (ms: number) => {
16+
vi.advanceTimersByTime(ms);
17+
return values.splice(0);
18+
};
19+
20+
const sweep = valuesDuring(SHIMMER_SWEEP_MS);
21+
expect(sweep).toHaveLength(SHIMMER_STEPS);
22+
expect(sweep.at(-1)).toBe(1);
23+
expect(sweep).toEqual(Array.from({ length: SHIMMER_STEPS }, (_, i) => (i + 1) / SHIMMER_STEPS));
24+
expect(valuesDuring(SHIMMER_PAUSE_MS)).toEqual([]);
25+
expect(valuesDuring(SHIMMER_SWEEP_MS)).toEqual(sweep);
26+
27+
stop();
28+
expect(vi.getTimerCount()).toBe(0);
29+
expect(valuesDuring((SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS) * 2)).toEqual([]);
30+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const SHIMMER_SWEEP_MS = 1_350;
2+
export const SHIMMER_PAUSE_MS = 1_450;
3+
export const SHIMMER_STEPS = 12;
4+
const SHIMMER_STEP_MS = SHIMMER_SWEEP_MS / SHIMMER_STEPS;
5+
6+
// No progress updates run during the pause. Progress 0 and 1 both sit off the
7+
// content, so the cycle only needs steps 1 through SHIMMER_STEPS.
8+
export function startShimmerSteps(progress: { setValue(value: number): void }): () => void {
9+
let step = 0;
10+
let timer: ReturnType<typeof setTimeout>;
11+
const advance = () => {
12+
step = (step % SHIMMER_STEPS) + 1;
13+
progress.setValue(step / SHIMMER_STEPS);
14+
timer = setTimeout(
15+
advance,
16+
step === SHIMMER_STEPS ? SHIMMER_PAUSE_MS + SHIMMER_STEP_MS : SHIMMER_STEP_MS,
17+
);
18+
};
19+
timer = setTimeout(advance, SHIMMER_STEP_MS);
20+
return () => clearTimeout(timer);
21+
}

0 commit comments

Comments
 (0)