fix(mobile): pause hidden home thread list updates (#15705)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5.5 authored and GitHub committed 2026-10-07 22:30:48 -07:00
1 parent eb17e0db2b
commit 65404eb75d
3 files changed
+210 -2

No files matched your search

@@ -9,7 +9,8 @@ import { Platform, useWindowDimensions } from "react-native";
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
import { useProjects, useNavigationThreadShells } from "../../state/entities";
import { useProjects } from "../../state/entities";
import { environmentThreadShells } from "../../state/threads";
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
import { useWorkspaceState } from "../../state/workspace";
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
@@ -22,6 +23,7 @@ import { AndroidHomeFabLayout } from "./AndroidHomeFab";
import { HomeScreen } from "./HomeScreen";
import { HomeHeader } from "./HomeHeader";
import { useHomeListOptions } from "./home-list-options";
import { useAtomValueWhileVisible, useHomeRouteVisible } from "./home-route-visibility";
import { useHomeThreadSelection } from "./home-thread-navigation";
import { buildHomeProjectScopes } from "./homeThreadList";
import { usePendingTaskListActions } from "./usePendingTaskListActions";
@@ -37,7 +39,13 @@ export function HomeRouteScreen() {
const headerWidth = nativePrimaryColumn ? (columnMetrics?.width ?? windowWidth) : windowWidth;
const { layout, panes } = useAdaptiveWorkspaceLayout();
const projects = useProjects();
const threads = useNavigationThreadShells();
// Streaming turns rewrite thread shells many times a second. While a Thread
// covers Home, rebuilding this list is invisible work.
const visible = useHomeRouteVisible();
const threads = useAtomValueWhileVisible(
environmentThreadShells.navigationThreadShellsAtom,
visible,
);
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
const { savedConnectionsById } = useSavedRemoteConnections();
const navigation = useNavigation();
@@ -0,0 +1,143 @@
// @vitest-environment jsdom
import { RegistryContext } from "@effect/atom-react";
import { Atom, AtomRegistry } from "effect/reactivity";
import { act, useEffect } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
type Listener = (event: { readonly data?: { readonly closing: boolean } }) => void;
const harness = vi.hoisted(() => {
const listeners = new Map<string, Set<Listener>>();
const state = { focused: true };
return {
state,
listeners,
navigation: {
isFocused: () => state.focused,
addListener(type: string, listener: Listener) {
let set = listeners.get(type);
if (set === undefined) {
set = new Set();
listeners.set(type, set);
}
set.add(listener);
return () => set.delete(listener);
},
},
emit(type: string, data?: { readonly closing: boolean }) {
for (const listener of listeners.get(type) ?? []) listener({ data });
},
};
});
vi.mock("@react-navigation/native", () => ({
useNavigation: () => harness.navigation,
}));
import { useAtomValueWhileVisible, useHomeRouteVisible } from "./home-route-visibility";
const shellsAtom = Atom.make(0).pipe(Atom.keepAlive);
let registry: AtomRegistry.AtomRegistry;
let root: Root | null = null;
// Value of every committed render, the work a shell update costs on device.
let renders: Array<{ readonly visible: boolean; readonly value: number }> = [];
function Probe() {
const visible = useHomeRouteVisible();
const value = useAtomValueWhileVisible(shellsAtom, visible);
useEffect(() => {
renders.push({ visible, value });
});
return null;
}
function mount() {
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
root = createRoot(document.createElement("div"));
act(() =>
root!.render(
<RegistryContext.Provider value={registry}>
<Probe />
</RegistryContext.Provider>,
),
);
}
function stream(count: number) {
for (let index = 0; index < count; index += 1) {
act(() => registry.update(shellsAtom, (value) => value + 1));
}
}
function pushThread() {
act(() => harness.emit("transitionStart", { closing: true }));
act(() => harness.emit("transitionEnd", { closing: true }));
}
beforeEach(() => {
registry = AtomRegistry.make();
harness.listeners.clear();
harness.state.focused = true;
renders = [];
});
afterEach(() => {
act(() => root?.unmount());
root = null;
registry.dispose();
});
describe("Home route visibility", () => {
it("re-renders for every shell update while Home is visible", () => {
mount();
stream(20);
expect(renders.at(-1)).toEqual({ visible: true, value: 20 });
expect(renders).toHaveLength(21);
});
it("stays live until the covering Thread finishes its push animation", () => {
mount();
act(() => harness.emit("transitionStart", { closing: true }));
stream(3);
expect(renders.at(-1)).toEqual({ visible: true, value: 3 });
});
it("does no work for shell updates while a Thread covers Home", () => {
mount();
stream(2);
pushThread();
const before = renders.length;
stream(600);
expect(renders.length - before).toBe(0);
expect(renders.at(-1)).toEqual({ visible: false, value: 2 });
});
it("shows the freshest list as soon as a back swipe starts revealing Home", () => {
mount();
pushThread();
stream(50);
act(() => harness.emit("transitionStart", { closing: false }));
expect(renders.at(-1)).toEqual({ visible: true, value: 50 });
stream(1);
expect(renders.at(-1)).toEqual({ visible: true, value: 51 });
});
it("refreshes on focus when no appear event arrives", () => {
mount();
pushThread();
stream(5);
act(() => harness.emit("focus"));
expect(renders.at(-1)).toEqual({ visible: true, value: 5 });
});
it("starts hidden when a deep link mounts Home under its Thread", () => {
harness.state.focused = false;
act(() => registry.set(shellsAtom, 7));
mount();
stream(10);
expect(renders).toEqual([{ visible: false, value: 7 }]);
act(() => harness.emit("transitionStart", { closing: false }));
expect(renders.at(-1)).toEqual({ visible: true, value: 17 });
});
});
@@ -0,0 +1,57 @@
import { RegistryContext } from "@effect/atom-react";
import { useNavigation } from "@react-navigation/native";
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { useCallback, useContext, useEffect, useRef, useState, useSyncExternalStore } from "react";
import type { Atom } from "effect/reactivity";
/**
* Whether the Home route can be seen. Home stays mounted under a pushed
* Thread, and native-stack never freezes the route directly below the focused
* one on Fabric, so Home has to stop its own work.
*
* Driven by the native appear/disappear events rather than focus. A form sheet
* over Home blurs it but leaves it on screen, and the presenting screen gets no
* disappear event for it. Home becomes visible as soon as a back swipe or pop
* starts revealing it, so the list is fresh during the transition.
*/
export function useHomeRouteVisible(): boolean {
const navigation =
useNavigation<NativeStackNavigationProp<ReactNavigation.RootParamList, "Home">>();
// A cold deep link mounts Home under its Thread without ever showing it.
const [visible, setVisible] = useState(() => navigation.isFocused());
useEffect(() => {
const show = () => setVisible(true);
const removeTransitionStart = navigation.addListener("transitionStart", ({ data }) => {
if (!data.closing) show();
});
const removeTransitionEnd = navigation.addListener("transitionEnd", ({ data }) => {
if (data.closing) setVisible(false);
});
// Popping a sheet or a deep-linked route above Home has no appear event.
const removeFocus = navigation.addListener("focus", show);
return () => {
removeTransitionStart();
removeTransitionEnd();
removeFocus();
};
}, [navigation]);
return visible;
}
/**
* Like `useAtomValue`, but unsubscribed while `visible` is false: the last
* visible value is held and atom changes do not re-render. Resubscribing reads
* the current value in the same render that turns visibility back on.
*/
export function useAtomValueWhileVisible<A>(atom: Atom.Atom<A>, visible: boolean): A {
const registry = useContext(RegistryContext);
const held = useRef<{ readonly value: A } | null>(null);
const subscribe = useCallback(
(onChange: () => void) => (visible ? registry.subscribe(atom, onChange) : () => undefined),
[registry, atom, visible],
);
return useSyncExternalStore(subscribe, () => {
if (visible || held.current === null) held.current = { value: registry.get(atom) };
return held.current.value;
});
}