perf: Stabilize worktree sidebar activity inputs
Squashed from PR #2611.
This commit is contained in:
@@ -86,6 +86,10 @@ import {
|
||||
setVisibleWorktreeIds,
|
||||
sidebarHasActiveFilters
|
||||
} from './visible-worktrees'
|
||||
import {
|
||||
getVisibleWorktreeBrowserActivityTabs,
|
||||
getVisibleWorktreeTerminalActivityTabs
|
||||
} from './visible-worktree-activity-inputs'
|
||||
import {
|
||||
VIRTUALIZED_SCROLL_ANCHOR_RECORD_EVENT,
|
||||
useVirtualizedScrollAnchor,
|
||||
@@ -136,6 +140,18 @@ const WORKTREE_SIDEBAR_SCROLL_STYLE: React.CSSProperties = {
|
||||
overflowAnchor: 'none'
|
||||
}
|
||||
|
||||
const recordKeyCountCache = new WeakMap<Record<string, unknown>, number>()
|
||||
|
||||
export function countRecordKeysByReference(record: Record<string, unknown>): number {
|
||||
const cached = recordKeyCountCache.get(record)
|
||||
if (cached !== undefined) {
|
||||
return cached
|
||||
}
|
||||
const count = Object.keys(record).length
|
||||
recordKeyCountCache.set(record, count)
|
||||
return count
|
||||
}
|
||||
|
||||
export function shouldAdjustWorktreeSidebarMeasuredRowScroll(args: {
|
||||
isScrolling: boolean
|
||||
now: number
|
||||
@@ -527,6 +543,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
|
||||
const [worktreeDragState, setWorktreeDragState] = useState<WorktreeRowDragState>(
|
||||
WORKTREE_ROW_DRAG_INITIAL_STATE
|
||||
)
|
||||
const [documentVisibilityRevision, setDocumentVisibilityRevision] = useState(0)
|
||||
const worktreeDragSessionRef = useRef<WorktreeDragSession | null>(null)
|
||||
const worktreePointerDragRef = useRef<WorktreePointerDrag | null>(null)
|
||||
const suppressWorktreeClickUntilRef = useRef(0)
|
||||
@@ -541,6 +558,20 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
|
||||
const prVisibleRefreshGeneration = useAppStore((s) => s.prVisibleRefreshGeneration)
|
||||
const settings = useAppStore((s) => s.settings)
|
||||
|
||||
useEffect(
|
||||
() =>
|
||||
installWorktreeVisibleRefreshVisibilityListener(() => {
|
||||
if (document.visibilityState !== 'visible') {
|
||||
// Why: the visible row identity can be unchanged after returning
|
||||
// from a hidden window; reset the key so visible PR/CI rows catch up.
|
||||
lastVisibleRefreshKeyRef.current = '__document_hidden__'
|
||||
return
|
||||
}
|
||||
setDocumentVisibilityRevision((revision) => revision + 1)
|
||||
}),
|
||||
[]
|
||||
)
|
||||
|
||||
// Drag is only meaningful when repo headers are using manual order. The
|
||||
// controller is still constructed for hook order stability when inert.
|
||||
const repoDrag = useRepoHeaderDrag({
|
||||
@@ -901,8 +932,8 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
|
||||
settings
|
||||
])
|
||||
|
||||
const prCacheLen = useAppStore((s) => Object.keys(s.prCache).length)
|
||||
const issueCacheLen = useAppStore((s) => Object.keys(s.issueCache).length)
|
||||
const prCacheLen = useAppStore((s) => countRecordKeysByReference(s.prCache))
|
||||
const issueCacheLen = useAppStore((s) => countRecordKeysByReference(s.issueCache))
|
||||
const renderRowKeySignature = useMemo(
|
||||
() => renderRows.map(getRenderRowKey).join('\n'),
|
||||
[renderRows]
|
||||
@@ -1530,6 +1561,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
|
||||
reportVisibleGitHubPRRefreshCandidates(visibleWorktreeIds, Date.now())
|
||||
}, [
|
||||
cardProps,
|
||||
documentVisibilityRevision,
|
||||
groupBy,
|
||||
renderRows,
|
||||
reportVisibleGitHubPRRefreshCandidates,
|
||||
@@ -2297,6 +2329,11 @@ type WorktreeListProps = {
|
||||
scrollAnchorRef: React.MutableRefObject<VirtualizedScrollAnchor>
|
||||
}
|
||||
|
||||
export function installWorktreeVisibleRefreshVisibilityListener(onChange: () => void): () => void {
|
||||
document.addEventListener('visibilitychange', onChange)
|
||||
return () => document.removeEventListener('visibilitychange', onChange)
|
||||
}
|
||||
|
||||
const WorktreeList = React.memo(function WorktreeList({
|
||||
scrollOffsetRef,
|
||||
scrollAnchorRef
|
||||
@@ -2327,10 +2364,12 @@ const WorktreeList = React.memo(function WorktreeList({
|
||||
|
||||
// Read tabsByWorktree when needed for filtering or sorting
|
||||
const needsActivityMaps = !showSleepingWorkspaces || sortBy === 'smart'
|
||||
const tabsByWorktree = useAppStore((s) => (needsActivityMaps ? s.tabsByWorktree : null))
|
||||
const tabsByWorktree = useAppStore((s) =>
|
||||
needsActivityMaps ? getVisibleWorktreeTerminalActivityTabs(s.tabsByWorktree) : null
|
||||
)
|
||||
const ptyIdsByTabId = useAppStore((s) => (needsActivityMaps ? s.ptyIdsByTabId : null))
|
||||
const browserTabsByWorktree = useAppStore((s) =>
|
||||
!showSleepingWorkspaces ? s.browserTabsByWorktree : null
|
||||
!showSleepingWorkspaces ? getVisibleWorktreeBrowserActivityTabs(s.browserTabsByWorktree) : null
|
||||
)
|
||||
|
||||
const cardProps = useAppStore((s) => s.worktreeCardProperties)
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { BrowserWorkspace, TerminalTab } from '../../../../shared/types'
|
||||
import {
|
||||
getVisibleWorktreeBrowserActivityTabs,
|
||||
getVisibleWorktreeTerminalActivityTabs
|
||||
} from './visible-worktree-activity-inputs'
|
||||
|
||||
function terminalTab(id: string, title: string): TerminalTab {
|
||||
return {
|
||||
id,
|
||||
ptyId: id,
|
||||
worktreeId: 'wt-1',
|
||||
title,
|
||||
customTitle: null,
|
||||
color: null,
|
||||
sortOrder: 0,
|
||||
createdAt: 0
|
||||
}
|
||||
}
|
||||
|
||||
function browserTab(id: string, title: string): BrowserWorkspace {
|
||||
return {
|
||||
id,
|
||||
worktreeId: 'wt-1',
|
||||
activePageId: `${id}-page`,
|
||||
pageIds: [`${id}-page`],
|
||||
url: 'https://example.com',
|
||||
title,
|
||||
loading: false,
|
||||
faviconUrl: null,
|
||||
canGoBack: false,
|
||||
canGoForward: false,
|
||||
loadError: null,
|
||||
createdAt: 0
|
||||
}
|
||||
}
|
||||
|
||||
describe('visible worktree activity inputs', () => {
|
||||
it('preserves terminal activity projection when only tab metadata changes', () => {
|
||||
const first = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'First')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'Renamed')]
|
||||
})
|
||||
|
||||
expect(second).toBe(first)
|
||||
expect(second['wt-1']).toBe(first['wt-1'])
|
||||
})
|
||||
|
||||
it('updates terminal activity projection when tab ids change', () => {
|
||||
const first = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'First')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'First'), terminalTab('tab-2', 'Second')]
|
||||
})
|
||||
|
||||
expect(second).not.toBe(first)
|
||||
expect(second['wt-1']?.map((tab) => tab.id)).toEqual(['tab-1', 'tab-2'])
|
||||
})
|
||||
|
||||
it('reuses unchanged terminal worktree arrays when a sibling worktree changes', () => {
|
||||
const first = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'First')],
|
||||
'wt-2': [terminalTab('tab-2', 'Second')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'Renamed')],
|
||||
'wt-2': [terminalTab('tab-3', 'Third')]
|
||||
})
|
||||
|
||||
expect(second).not.toBe(first)
|
||||
expect(second['wt-1']).toBe(first['wt-1'])
|
||||
expect(second['wt-2']).not.toBe(first['wt-2'])
|
||||
expect(second['wt-2']?.map((tab) => tab.id)).toEqual(['tab-3'])
|
||||
})
|
||||
|
||||
it('drops terminal projections for removed worktrees', () => {
|
||||
const first = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'First')],
|
||||
'wt-2': [terminalTab('tab-2', 'Second')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeTerminalActivityTabs({
|
||||
'wt-1': [terminalTab('tab-1', 'Renamed')]
|
||||
})
|
||||
|
||||
expect(second).not.toBe(first)
|
||||
expect(Object.keys(second)).toEqual(['wt-1'])
|
||||
expect(second['wt-1']).toBe(first['wt-1'])
|
||||
})
|
||||
|
||||
it('preserves browser activity projection when only browser metadata changes', () => {
|
||||
const first = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-1', 'First')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-1', 'Renamed')]
|
||||
})
|
||||
|
||||
expect(second).toBe(first)
|
||||
expect(second['wt-1']).toBe(first['wt-1'])
|
||||
})
|
||||
|
||||
it('updates browser activity projection when browser ids change', () => {
|
||||
const first = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-1', 'First')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-2', 'Second')]
|
||||
})
|
||||
|
||||
expect(second).not.toBe(first)
|
||||
expect(second['wt-1']?.map((tab) => tab.id)).toEqual(['browser-2'])
|
||||
})
|
||||
|
||||
it('drops browser projections for removed worktrees', () => {
|
||||
const first = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-1', 'First')],
|
||||
'wt-2': [browserTab('browser-2', 'Second')]
|
||||
})
|
||||
|
||||
const second = getVisibleWorktreeBrowserActivityTabs({
|
||||
'wt-1': [browserTab('browser-1', 'Renamed')]
|
||||
})
|
||||
|
||||
expect(second).not.toBe(first)
|
||||
expect(Object.keys(second)).toEqual(['wt-1'])
|
||||
expect(second['wt-1']).toBe(first['wt-1'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { BrowserWorkspace, TerminalTab } from '../../../../shared/types'
|
||||
|
||||
type TerminalActivityTab = Pick<TerminalTab, 'id'>
|
||||
type BrowserActivityTab = Pick<BrowserWorkspace, 'id'>
|
||||
|
||||
function haveSameIds<T extends { id: string }>(
|
||||
previous: readonly T[] | undefined,
|
||||
next: readonly { id: string }[]
|
||||
): boolean {
|
||||
if (!previous || previous.length !== next.length) {
|
||||
return false
|
||||
}
|
||||
for (let index = 0; index < next.length; index++) {
|
||||
if (previous[index]?.id !== next[index]?.id) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function projectIdTabs<T extends { id: string }, U extends { id: string }>(
|
||||
tabsByWorktree: Record<string, readonly T[]>,
|
||||
previousProjection: Record<string, U[]> | null
|
||||
): { projection: Record<string, U[]>; unchanged: boolean } {
|
||||
const nextProjection: Record<string, U[]> = {}
|
||||
let unchanged =
|
||||
previousProjection !== null &&
|
||||
Object.keys(previousProjection).length === Object.keys(tabsByWorktree).length
|
||||
|
||||
for (const [worktreeId, tabs] of Object.entries(tabsByWorktree)) {
|
||||
const previousTabs = previousProjection?.[worktreeId]
|
||||
if (haveSameIds(previousTabs, tabs)) {
|
||||
nextProjection[worktreeId] = previousTabs as U[]
|
||||
continue
|
||||
}
|
||||
unchanged = false
|
||||
nextProjection[worktreeId] = tabs.map((tab) => ({ id: tab.id }) as U)
|
||||
}
|
||||
|
||||
return { projection: nextProjection, unchanged }
|
||||
}
|
||||
|
||||
let cachedTerminalSource: Record<string, TerminalTab[]> | null = null
|
||||
let cachedTerminalProjection: Record<string, TerminalActivityTab[]> | null = null
|
||||
|
||||
export function getVisibleWorktreeTerminalActivityTabs(
|
||||
tabsByWorktree: Record<string, TerminalTab[]>
|
||||
): Record<string, TerminalActivityTab[]> {
|
||||
if (cachedTerminalSource === tabsByWorktree && cachedTerminalProjection) {
|
||||
return cachedTerminalProjection
|
||||
}
|
||||
const { projection, unchanged } = projectIdTabs(tabsByWorktree, cachedTerminalProjection)
|
||||
cachedTerminalSource = tabsByWorktree
|
||||
if (unchanged && cachedTerminalProjection) {
|
||||
return cachedTerminalProjection
|
||||
}
|
||||
cachedTerminalProjection = projection
|
||||
return projection
|
||||
}
|
||||
|
||||
let cachedBrowserSource: Record<string, BrowserWorkspace[]> | null = null
|
||||
let cachedBrowserProjection: Record<string, BrowserActivityTab[]> | null = null
|
||||
|
||||
export function getVisibleWorktreeBrowserActivityTabs(
|
||||
browserTabsByWorktree: Record<string, BrowserWorkspace[]>
|
||||
): Record<string, BrowserActivityTab[]> {
|
||||
if (cachedBrowserSource === browserTabsByWorktree && cachedBrowserProjection) {
|
||||
return cachedBrowserProjection
|
||||
}
|
||||
const { projection, unchanged } = projectIdTabs(browserTabsByWorktree, cachedBrowserProjection)
|
||||
cachedBrowserSource = browserTabsByWorktree
|
||||
if (unchanged && cachedBrowserProjection) {
|
||||
return cachedBrowserProjection
|
||||
}
|
||||
cachedBrowserProjection = projection
|
||||
return projection
|
||||
}
|
||||
@@ -84,7 +84,7 @@ export function computeVisibleWorktreeIds(
|
||||
opts: {
|
||||
filterRepoIds: string[]
|
||||
showSleepingWorkspaces: boolean
|
||||
tabsByWorktree: Record<string, TerminalTab[]> | null
|
||||
tabsByWorktree: Record<string, Pick<TerminalTab, 'id'>[]> | null
|
||||
ptyIdsByTabId: Record<string, string[]> | null
|
||||
browserTabsByWorktree?: Record<string, { id: string }[]> | null
|
||||
// Why required: every caller (WorktreeList, getVisibleWorktreeIds
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
countRecordKeysByReference,
|
||||
resolvePendingSidebarReveal,
|
||||
shouldAdjustWorktreeSidebarMeasuredRowScroll
|
||||
} from './WorktreeList'
|
||||
@@ -15,6 +16,21 @@ const makeHeaderRow = (key: string) =>
|
||||
}) as const
|
||||
|
||||
describe('shouldAdjustWorktreeSidebarMeasuredRowScroll', () => {
|
||||
it('counts record keys once per object reference', () => {
|
||||
const keysSpy = vi.spyOn(Object, 'keys')
|
||||
const first = { a: 1, b: 2 }
|
||||
const second = { ...first, c: 3 }
|
||||
|
||||
try {
|
||||
expect(countRecordKeysByReference(first)).toBe(2)
|
||||
expect(countRecordKeysByReference(first)).toBe(2)
|
||||
expect(countRecordKeysByReference(second)).toBe(3)
|
||||
expect(keysSpy).toHaveBeenCalledTimes(2)
|
||||
} finally {
|
||||
keysSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('suppresses measured-row scroll correction while TanStack is scrolling', () => {
|
||||
expect(
|
||||
shouldAdjustWorktreeSidebarMeasuredRowScroll({
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { installWorktreeVisibleRefreshVisibilityListener } from './WorktreeList'
|
||||
|
||||
describe('installWorktreeVisibleRefreshVisibilityListener', () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('subscribes to document visibility changes so visible PR refresh can rerun on return', () => {
|
||||
const listeners = new Map<string, () => void>()
|
||||
const onChange = vi.fn()
|
||||
const addEventListener = vi.fn((event: string, listener: () => void) => {
|
||||
listeners.set(event, listener)
|
||||
})
|
||||
const removeEventListener = vi.fn()
|
||||
|
||||
vi.stubGlobal('document', {
|
||||
addEventListener,
|
||||
removeEventListener
|
||||
})
|
||||
|
||||
const cleanup = installWorktreeVisibleRefreshVisibilityListener(onChange)
|
||||
|
||||
expect(addEventListener).toHaveBeenCalledWith('visibilitychange', onChange)
|
||||
listeners.get('visibilitychange')?.()
|
||||
expect(onChange).toHaveBeenCalledTimes(1)
|
||||
|
||||
cleanup()
|
||||
expect(removeEventListener).toHaveBeenCalledWith('visibilitychange', onChange)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user