TypeScript
fix(mobile): stop the work-row shimmer from updating every frame (#28530)
shivam006d7e38f048
diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsxindex 4ed6cbef948..9851584d23b 100644--- a/apps/mobile/src/features/threads/thread-work-log.tsx+++ b/apps/mobile/src/features/threads/thread-work-log.tsx@@ -34,7 +34,6 @@ import {import {Animated as NativeAnimated,type ColorValue,- Easing,Pressable,ScrollView,StyleSheet,@@ -105,10 +104,9 @@ import Animated, {import { useAssetUrl } from "../../state/assets";import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";import { useOpenLinkedThread } from "./useOpenLinkedThread";+import { startShimmerSteps } from "./work-shimmer-steps";const SHIMMER_WIDTH = 72;-const SHIMMER_SWEEP_MS = 1_350;-const SHIMMER_PAUSE_MS = 1_450;const SHIMMER_ICON_AND_GAP_WIDTH = 30;export const THREAD_DISCLOSURE_TRANSITION_MS = 180;const WORK_LOG_LAYOUT_TRANSITION = LinearTransition.duration(THREAD_DISCLOSURE_TRANSITION_MS);@@ -269,44 +267,33 @@ export function ShimmeringWorkContent(props: {const appIsActive = useAppActive();const reducedMotion = useReduceMotionEnabled();const screenIsFocused = useIsFocused();- const progress = useAnimatedValue(0);+ // Native from creation, so each setValue updates the view without a React render.+ const progress = useAnimatedValue(0, { useNativeDriver: true });const gradientId = `work-shimmer-${useId().replaceAll(":", "")}`;const contentWidth = Math.min(availableWidth,(props.showIcon ? SHIMMER_ICON_AND_GAP_WIDTH : 0) + Math.ceil(textWidth),);- // 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.- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the native-driver shimmer loop while visible and active, and stops it on cleanup+ // Uses a native Animated value instead of Reanimated, whose per-frame commits+ // starve React's while commit pausing is disabled (package.json). See+ // docs/internals/performance-regressions.md.+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- steps the native shimmer value on timers while visible and active, and stops them on cleanupuseEffect(() => {progress.setValue(0);if (contentWidth <= 0 || reducedMotion || !appIsActive || !screenIsFocused) return;-- const loop = NativeAnimated.loop(- NativeAnimated.timing(progress, {- toValue: 1,- duration: SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS,- easing: Easing.linear,- useNativeDriver: true,- }),- );- loop.start();- return () => loop.stop();+ return startShimmerSteps(progress);}, [appIsActive, contentWidth, progress, reducedMotion, screenIsFocused]);- // 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],+ inputRange: [0, 1],+ outputRange: [-SHIMMER_WIDTH, contentWidth],});const counterSweepTranslateX = progress.interpolate({- inputRange,- outputRange: [SHIMMER_WIDTH, -contentWidth, -contentWidth],+ inputRange: [0, 1],+ outputRange: [SHIMMER_WIDTH, -contentWidth],});return (diff --git a/apps/mobile/src/features/threads/work-shimmer-steps.test.ts b/apps/mobile/src/features/threads/work-shimmer-steps.test.tsnew file mode 100644index 00000000000..9fb7a17ffe8--- /dev/null+++ b/apps/mobile/src/features/threads/work-shimmer-steps.test.ts@@ -0,0 +1,30 @@+import { expect, it, vi } from "vite-plus/test";++import {+ SHIMMER_PAUSE_MS,+ SHIMMER_STEPS,+ SHIMMER_SWEEP_MS,+ startShimmerSteps,+} from "./work-shimmer-steps";++it("steps the sweep, stays quiet through the pause, and stops on cleanup", () => {+ using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };+ vi.useFakeTimers();+ const values: number[] = [];+ const stop = startShimmerSteps({ setValue: (value) => values.push(value) });+ const valuesDuring = (ms: number) => {+ vi.advanceTimersByTime(ms);+ return values.splice(0);+ };++ const sweep = valuesDuring(SHIMMER_SWEEP_MS);+ expect(sweep).toHaveLength(SHIMMER_STEPS);+ expect(sweep.at(-1)).toBe(1);+ expect(sweep).toEqual(Array.from({ length: SHIMMER_STEPS }, (_, i) => (i + 1) / SHIMMER_STEPS));+ expect(valuesDuring(SHIMMER_PAUSE_MS)).toEqual([]);+ expect(valuesDuring(SHIMMER_SWEEP_MS)).toEqual(sweep);++ stop();+ expect(vi.getTimerCount()).toBe(0);+ expect(valuesDuring((SHIMMER_SWEEP_MS + SHIMMER_PAUSE_MS) * 2)).toEqual([]);+});diff --git a/apps/mobile/src/features/threads/work-shimmer-steps.ts b/apps/mobile/src/features/threads/work-shimmer-steps.tsnew file mode 100644index 00000000000..348ca40cccf--- /dev/null+++ b/apps/mobile/src/features/threads/work-shimmer-steps.ts@@ -0,0 +1,21 @@+export const SHIMMER_SWEEP_MS = 1_350;+export const SHIMMER_PAUSE_MS = 1_450;+export const SHIMMER_STEPS = 12;+const SHIMMER_STEP_MS = SHIMMER_SWEEP_MS / SHIMMER_STEPS;++// No progress updates run during the pause. Progress 0 and 1 both sit off the+// content, so the cycle only needs steps 1 through SHIMMER_STEPS.+export function startShimmerSteps(progress: { setValue(value: number): void }): () => void {+ let step = 0;+ let timer: ReturnType<typeof setTimeout>;+ const advance = () => {+ step = (step % SHIMMER_STEPS) + 1;+ progress.setValue(step / SHIMMER_STEPS);+ timer = setTimeout(+ advance,+ step === SHIMMER_STEPS ? SHIMMER_PAUSE_MS + SHIMMER_STEP_MS : SHIMMER_STEP_MS,+ );+ };+ timer = setTimeout(advance, SHIMMER_STEP_MS);+ return () => clearTimeout(timer);+}