merge #1057: perf(sidebar): 页签内容的会话 scope 改为按内容稳定,消除无关重渲染导致的整文件重读
This commit is contained in:
commit
01a6fa573b
16 files changed
+300
-39
No files matched your search
@@ -17,8 +17,9 @@ import { api } from '../src/client/api.ts'
|
||||
import type { Context } from '../src/context-types.ts'
|
||||
import type { SessionScope } from '../src/client/api.ts'
|
||||
import type { SidebarDiffRef } from '../src/client/state.ts'
|
||||
import { t } from '../src/client/locales.ts'
|
||||
|
||||
import { setupReactAct } from './test-utils.ts'
|
||||
import { setupReactAct, renderRoot } from './test-utils.ts'
|
||||
setupReactAct()
|
||||
|
||||
const SCOPE: SessionScope = { sessionId: 's1', cwd: '/p' }
|
||||
@@ -120,6 +121,55 @@ describe('useGitDiffTarget through the inline preview pane (DiffPane)', () => {
|
||||
container.remove()
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* The `content` tab holds bytes from its OWN `fs.read`, not from the patch
|
||||
* the rest of the pane shows. The header's refresh button only bumps the
|
||||
* diff loader's counter, so unless the content effect listens to that
|
||||
* counter the tab keeps serving bytes read before the file changed — and it
|
||||
* has no other reload path (the scope is stable, so re-renders no longer
|
||||
* re-read as a side effect).
|
||||
*/
|
||||
it('re-reads the content tab on the header refresh button', async () => {
|
||||
const worktree: SidebarDiffRef = { kind: 'worktree', path: 'src/a.ts', staged: false }
|
||||
vi.spyOn(api, 'gitDiff').mockResolvedValue({ diff: STAGED_PATCH })
|
||||
const read = vi.spyOn(api, 'fsRead')
|
||||
.mockResolvedValueOnce({ kind: 'text', content: 'first-bytes', size: 11 })
|
||||
.mockResolvedValue({ kind: 'text', content: 'second-bytes', size: 12 })
|
||||
|
||||
// The pane hosts its own tab state only through `onSelectTab`; without it
|
||||
// the strip renders but `onSelect` is a no-op, so the test drives the
|
||||
// selection the way the changes tab does.
|
||||
const mounted = renderRoot(createElement(DiffPane, {
|
||||
target: { kind: 'git', ref: worktree },
|
||||
ctx: {} as Context,
|
||||
scope: SCOPE,
|
||||
height: 300,
|
||||
activeTab: 'content',
|
||||
onSelectTab: () => {},
|
||||
onHeightCommit: () => {},
|
||||
onClose: () => {},
|
||||
onExpand: () => {},
|
||||
}))
|
||||
try {
|
||||
await flushEffects()
|
||||
|
||||
// The content tab loads lazily, only while on screen.
|
||||
expect(mounted.container.textContent).toContain('first-bytes')
|
||||
|
||||
// The refresh button must reload it, since nothing else can. Selected by
|
||||
// the locale-aware label the rest of this suite uses.
|
||||
const refreshButton = mounted.container.querySelector<HTMLButtonElement>(`button[aria-label="${t('refresh')}"]`)
|
||||
expect(refreshButton).not.toBeNull()
|
||||
await act(async () => { refreshButton!.click() })
|
||||
await flushEffects()
|
||||
|
||||
expect(read.mock.calls.length).toBeGreaterThanOrEqual(2)
|
||||
expect(mounted.container.textContent).toContain('second-bytes')
|
||||
} finally {
|
||||
mounted.unmount()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('useGitDiffTarget through the dedicated diff tab (DiffTab)', () => {
|
||||
@@ -132,8 +182,7 @@ describe('useGitDiffTarget through the dedicated diff tab (DiffTab)', () => {
|
||||
try {
|
||||
act(() => {
|
||||
root.render(createElement(DiffTab, {
|
||||
sessionId: 's1',
|
||||
cwd: '/p',
|
||||
scope: SCOPE,
|
||||
diff: { kind: 'commit', hash: 'abc1234', hashFull: 'a'.repeat(40), subject: 'subject' },
|
||||
}))
|
||||
})
|
||||
@@ -156,8 +205,7 @@ describe('useGitDiffTarget through the dedicated diff tab (DiffTab)', () => {
|
||||
try {
|
||||
act(() => {
|
||||
root.render(createElement(DiffTab, {
|
||||
sessionId: 's1',
|
||||
cwd: '/p',
|
||||
scope: SCOPE,
|
||||
diff: { kind: 'worktree', path: 'src/a.ts', staged: false },
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* The `scope` object a tab body receives must be stable across re-renders of
|
||||
* the Sidebar shell.
|
||||
*
|
||||
* Tab bodies key memos and effects off `scope` (DiffPane resolves its media /
|
||||
* html routes from it, TextEditor restores the preview scroll, the side chat
|
||||
* rebuilds its prose-rewrite cache). The cell used to build `{ sessionId, cwd }`
|
||||
* fresh on every render, so every one of those dependencies looked changed
|
||||
* whenever the cell re-rendered. The cell re-renders when a COMPARED memo field
|
||||
* moves (`tab-content-memo.ts`) — a directory toggle reaches every mounted tab
|
||||
* through `expanded`, and `revealed` / `localeRevision` / `tabsVersion` do the
|
||||
* same; a bare poll tick does NOT, because the comparator already absorbs it.
|
||||
* On those re-renders the diff preview re-ran its whole-document rewrite and
|
||||
* its content tab re-issued a whole-file `fs.read`, flashing its loading row.
|
||||
*
|
||||
* The regression: the cell DOES re-render, but the scope it hands down must be
|
||||
* the SAME object, and must only become a new one when the session or cwd
|
||||
* actually changes.
|
||||
*/
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
|
||||
import { setupReactAct, renderRoot } from './test-utils.ts'
|
||||
setupReactAct()
|
||||
|
||||
import { TabContent } from '../src/client/sidebar/TabContent.tsx'
|
||||
import type { SessionScope } from '../src/client/api.ts'
|
||||
import type { Context } from '../src/context-types.ts'
|
||||
import type { SidebarStore, SidebarTab } from '../src/client/state.ts'
|
||||
import type { TabContentMemoKey } from '../src/client/tab-content-memo.ts'
|
||||
|
||||
const TAB: SidebarTab = { id: 'tab:1', type: 'probe', title: 'Probe' }
|
||||
|
||||
/** Every scope object the tab body was rendered with, in order. */
|
||||
const seen: SessionScope[] = []
|
||||
|
||||
function ProbeBody(props: { scope: SessionScope }): null {
|
||||
seen.push(props.scope)
|
||||
return null
|
||||
}
|
||||
|
||||
/** A context whose service resolves exactly one registered tab type. */
|
||||
function makeCtx(): Context {
|
||||
const service = {
|
||||
getTab: (type: string) => (type === TAB.type
|
||||
? { id: TAB.type, title: 'Probe', component: ProbeBody }
|
||||
: undefined),
|
||||
}
|
||||
return { get: (name: string) => (name === 'betterSidebar' ? service : undefined) } as unknown as Context
|
||||
}
|
||||
|
||||
/** Everything the comparator looks at except `tab` / `paneId` (fixed here).
|
||||
* Derived from the real key so a field the comparator gains later is not
|
||||
* silently left uncovered here. */
|
||||
type Fields = Partial<Omit<TabContentMemoKey, 'tab' | 'paneId'>>
|
||||
|
||||
/** Stable array identities, like the store's own `state.expanded` / `revealed`
|
||||
* (a fresh `[]` on every render would itself be a compared change). */
|
||||
const EMPTY_A: string[] = []
|
||||
const EMPTY_B: string[] = []
|
||||
|
||||
function mountCell(): { rerender: (fields?: Fields) => void; unmount: () => void } {
|
||||
const ctx = makeCtx()
|
||||
const store = {} as SidebarStore
|
||||
let current: Fields = { expanded: EMPTY_A, revealed: EMPTY_B }
|
||||
|
||||
const element = (): ReturnType<typeof createElement> => createElement(TabContent, {
|
||||
tab: TAB,
|
||||
paneId: 'pane:a',
|
||||
sessionId: current.sessionId ?? 'session:1',
|
||||
cwd: 'cwd' in current ? current.cwd : '/workspace',
|
||||
// Fixed across every case: `visible` moving is the tab being parked or
|
||||
// shown, which no case here varies (so it is deliberately not in `Fields`).
|
||||
visible: true,
|
||||
expanded: current.expanded ?? EMPTY_A,
|
||||
revealed: current.revealed ?? EMPTY_B,
|
||||
localeRevision: current.localeRevision ?? 'en-US',
|
||||
tabsVersion: current.tabsVersion ?? 0,
|
||||
onToggleDir: () => {},
|
||||
onReferenceFile: () => {},
|
||||
ctx,
|
||||
store,
|
||||
onSubagentJump: () => {},
|
||||
onOpenDiff: () => {},
|
||||
})
|
||||
|
||||
const root = renderRoot(element())
|
||||
return {
|
||||
rerender: (fields = {}) => {
|
||||
current = { ...current, ...fields }
|
||||
root.rerender(element())
|
||||
},
|
||||
unmount: root.unmount,
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(() => { document.body.innerHTML = '' })
|
||||
|
||||
describe('TabContent scope identity', () => {
|
||||
it('keeps the same scope object when only `expanded` changes', () => {
|
||||
seen.length = 0
|
||||
const shell = mountCell()
|
||||
const first = seen.at(-1)
|
||||
expect(first).toBeDefined()
|
||||
|
||||
shell.rerender({ expanded: ['/workspace/src'] })
|
||||
// The cell really did re-render (not a memo skip)...
|
||||
expect(seen.length).toBe(2)
|
||||
// ...and handed down the SAME scope.
|
||||
expect(seen[1]).toBe(first)
|
||||
|
||||
shell.rerender({ expanded: ['/workspace/src', '/workspace/tests'] })
|
||||
expect(seen.length).toBe(3)
|
||||
expect(seen[2]).toBe(first)
|
||||
shell.unmount()
|
||||
})
|
||||
|
||||
/**
|
||||
* The other compared fields reach the body the same way and must not churn
|
||||
* the scope either — these are the triggers a perf investigation should
|
||||
* actually follow (a bare poll tick is absorbed by the comparator and never
|
||||
* gets here at all).
|
||||
*/
|
||||
it.each([
|
||||
['revealed', { revealed: ['/workspace/a.ts'] }],
|
||||
['localeRevision', { localeRevision: 'zh-CN' }],
|
||||
['tabsVersion', { tabsVersion: 1 }],
|
||||
] as Array<[string, Fields]>)('keeps the same scope object when only %s changes', (_label, change) => {
|
||||
seen.length = 0
|
||||
const shell = mountCell()
|
||||
const first = seen.at(-1)
|
||||
|
||||
shell.rerender(change)
|
||||
expect(seen.length).toBe(2)
|
||||
expect(seen[1]).toBe(first)
|
||||
shell.unmount()
|
||||
})
|
||||
|
||||
it('builds a new scope when the session changes', () => {
|
||||
seen.length = 0
|
||||
const shell = mountCell()
|
||||
const first = seen.at(-1)
|
||||
shell.rerender({ sessionId: 'session:2' })
|
||||
expect(seen.at(-1)).not.toBe(first)
|
||||
expect(seen.at(-1)?.sessionId).toBe('session:2')
|
||||
shell.unmount()
|
||||
})
|
||||
|
||||
it('builds a new scope when the cwd changes', () => {
|
||||
seen.length = 0
|
||||
const shell = mountCell()
|
||||
const first = seen.at(-1)
|
||||
shell.rerender({ cwd: '/other' })
|
||||
expect(seen.at(-1)).not.toBe(first)
|
||||
expect(seen.at(-1)?.cwd).toBe('/other')
|
||||
shell.unmount()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user