Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(work): rescan the sidebar tree only on the cache entries it reads (MUL-9375) (#10718)
Bohan Jiang24b80353158c
diff --git a/packages/views/layout/panels/work-tree.in-tree.test.tsx b/packages/views/layout/panels/work-tree.in-tree.test.tsxnew file mode 100644index 0000000000..bbe30ef175--- /dev/null+++ b/packages/views/layout/panels/work-tree.in-tree.test.tsx@@ -0,0 +1,92 @@+import type { ReactNode } from "react";+import { QueryClient, QueryClientProvider, QueryObserver } from "@tanstack/react-query";+import { act, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { issueKeys } from "@multica/core/issues/queries";+import { useSidebarKeptStore } from "@multica/core/issues/stores/sidebar-kept-store";+import type { Issue, ListIssuesResponse } from "@multica/core/types";+import { useIssueInTree } from "./work-tree";++const WS = "ws-1";+const SOURCE = "project:p-1";+const LIST_KEY = [...issueKeys.sidebarAll(WS), SOURCE, {}, 25];++function issue(id: string, identifier: string): Issue {+ return { id, identifier } as Issue;+}++function list(...issues: Issue[]): ListIssuesResponse {+ return { issues, total: issues.length } as ListIssuesResponse;+}++/** A mounted observer on `queryKey`, as a rendered list or detail would hold. */+function observe(qc: QueryClient, queryKey: readonly unknown[]) {+ return new QueryObserver(qc, { queryKey, enabled: false }).subscribe(() => {});+}++function setup(ref: string) {+ const qc = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: Infinity } } });+ const wrapper = ({ children }: { children: ReactNode }) => (+ <QueryClientProvider client={qc}>{children}</QueryClientProvider>+ );+ const hook = renderHook(() => useIssueInTree(WS, ref), { wrapper });+ return { qc, hook };+}++beforeEach(() => {+ useSidebarKeptStore.setState({ held: {} });+});++afterEach(() => {+ vi.restoreAllMocks();+});++describe("useIssueInTree", () => {+ it("finds the issue once an expanded row's list holds it", () => {+ const { qc, hook } = setup("MUL-1");+ expect(hook.result.current.issueId).toBeNull();++ act(() => {+ observe(qc, LIST_KEY);+ qc.setQueryData(LIST_KEY, list(issue("i-1", "MUL-1")));+ });++ expect(hook.result.current).toEqual({ issueId: "i-1", sources: [SOURCE] });+ });++ it("finds an issue a shown list keeps after its query dropped it", () => {+ const { qc, hook } = setup("i-1");+ act(() => {+ observe(qc, LIST_KEY);+ qc.setQueryData(LIST_KEY, list());+ useSidebarKeptStore.setState({ held: { [SOURCE]: { id: "i-1", index: 0 } } });+ });+ expect(hook.result.current.issueId).toBeNull();++ // The kept issue's detail arriving is what lets the answer match it.+ act(() => {+ qc.setQueryData(issueKeys.detail(WS, "i-1"), issue("i-1", "MUL-1"));+ });++ expect(hook.result.current).toEqual({ issueId: "i-1", sources: [SOURCE] });+ });++ // MUL-9375: opening an issue mounts hundreds of observers on unrelated+ // queries, and a rescan of the whole cache for each blocked the main thread.+ it("does not rescan the cache for queries the answer does not read", () => {+ const { qc } = setup("MUL-1");+ const findAll = vi.spyOn(qc.getQueryCache(), "findAll");++ act(() => {+ for (let i = 0; i < 20; i += 1) {+ observe(qc, ["comments", WS, `c-${i}`]);+ qc.setQueryData(["comments", WS, `c-${i}`], { n: i });+ }+ // A detail no list keeps.+ observe(qc, issueKeys.detail(WS, "i-2"));+ qc.setQueryData(issueKeys.detail(WS, "i-2"), issue("i-2", "MUL-2"));+ });++ expect(findAll).not.toHaveBeenCalled();+ });+});diff --git a/packages/views/layout/panels/work-tree.tsx b/packages/views/layout/panels/work-tree.tsxindex 5691503b72..1545930916 100644--- a/packages/views/layout/panels/work-tree.tsx+++ b/packages/views/layout/panels/work-tree.tsx@@ -74,6 +74,22 @@ export interface IssueInTree {const NOT_IN_TREE: IssueInTree = { issueId: null, sources: [] };+function hasPrefix(key: readonly unknown[], prefix: readonly unknown[]): boolean {+ return prefix.every((part, i) => key[i] === part);+}++/**+ * Whether a cache entry can change `useIssueInTree`'s answer: a sidebar list,+ * or the detail of an issue a list keeps.+ */+function readsIntoTree(wsId: string, key: readonly unknown[]): boolean {+ if (hasPrefix(key, issueKeys.sidebarAll(wsId))) return true;+ const detail = issueKeys.detail(wsId, "");+ if (key.length !== detail.length || !hasPrefix(key, detail.slice(0, -1))) return false;+ const id = key[detail.length - 1];+ return Object.values(useSidebarKeptStore.getState().held).some((held) => held.id === id);+}+/*** Where the expanded rows list or keep the issue under `ref` — its UUID, or* an identifier like `MUL-123` matched case-insensitively as the server does.@@ -83,14 +99,20 @@ export function useIssueInTree(wsId: string, ref: string | null): IssueInTree {const cache = qc.getQueryCache();const subscribe = useCallback((onChange: () => void) => {- const unsubscribeCache = cache.subscribe(onChange);+ // Only events on the entries the answer reads. Opening an issue mounts+ // hundreds of observers elsewhere in the cache, and rescanning the+ // whole cache for each of them blocked the main thread for seconds,+ // more the longer the session had filled the cache (MUL-9375).+ const unsubscribeCache = cache.subscribe((event) => {+ if (readsIntoTree(wsId, event.query.queryKey)) onChange();+ });const unsubscribeKept = useSidebarKeptStore.subscribe(onChange);return () => {unsubscribeCache();unsubscribeKept();};},- [cache],+ [cache, wsId],);// The snapshot is a string so an unchanged answer compares equal.const snapshot = useSyncExternalStore(subscribe, () => {