fix: cancel terminal file reveal frames (#3405)

This commit is contained in:
Neil
2026-05-29 18:27:21 -07:00
committed by GitHub
parent c10ad22e2c
commit 2b17c97abe
2 changed files with 58 additions and 11 deletions
@@ -48,6 +48,33 @@ export function getTerminalFileContext(
}
let latestOpenDetectedFilePathRequestId = 0
let pendingEditorRevealFrameIds: number[] = []
function cancelPendingEditorRevealFrames(): void {
if (typeof cancelAnimationFrame === 'function') {
for (const frameId of pendingEditorRevealFrameIds) {
cancelAnimationFrame(frameId)
}
}
pendingEditorRevealFrameIds = []
}
function schedulePendingEditorReveal(callback: () => void): void {
cancelPendingEditorRevealFrames()
const firstFrameId = requestAnimationFrame(() => {
pendingEditorRevealFrameIds = pendingEditorRevealFrameIds.filter(
(frameId) => frameId !== firstFrameId
)
const secondFrameId = requestAnimationFrame(() => {
pendingEditorRevealFrameIds = pendingEditorRevealFrameIds.filter(
(frameId) => frameId !== secondFrameId
)
callback()
})
pendingEditorRevealFrameIds.push(secondFrameId)
})
pendingEditorRevealFrameIds.push(firstFrameId)
}
export function openDetectedFilePath(
filePath: string,
@@ -57,6 +84,7 @@ export function openDetectedFilePath(
): void {
const { runtimeEnvironmentId, worktreeId, worktreePath } = deps
const requestId = ++latestOpenDetectedFilePathRequestId
cancelPendingEditorRevealFrames()
void (async () => {
let statResult
@@ -127,17 +155,15 @@ export function openDetectedFilePath(
if (line !== null) {
const targetColumn = column ?? 1
store.setPendingEditorReveal(null)
requestAnimationFrame(() => {
requestAnimationFrame(() => {
if (requestId !== latestOpenDetectedFilePathRequestId) {
return
}
store.setPendingEditorReveal({
filePath,
line,
column: targetColumn,
matchLength: 0
})
schedulePendingEditorReveal(() => {
if (requestId !== latestOpenDetectedFilePathRequestId) {
return
}
store.setPendingEditorReveal({
filePath,
line,
column: targetColumn,
matchLength: 0
})
})
}
@@ -116,6 +116,9 @@ beforeEach(() => {
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback): number => {
return setTimeout(() => callback(0), 0) as unknown as number
})
vi.stubGlobal('cancelAnimationFrame', (handle: number): void => {
clearTimeout(handle)
})
})
afterEach(() => {
@@ -275,6 +278,24 @@ describe('handleOscLink', () => {
})
})
it('cancels a pending Monaco reveal frame when another file open starts', async () => {
setPlatform('Macintosh')
const cancelAnimationFrame = vi.fn()
vi.stubGlobal(
'requestAnimationFrame',
vi.fn(() => 42)
)
vi.stubGlobal('cancelAnimationFrame', cancelAnimationFrame)
openDetectedFilePath('/tmp/src/main.ts', 42, null, deps)
await flushAsyncWork()
openDetectedFilePath('/tmp/src/other.ts', null, null, deps)
expect(cancelAnimationFrame).toHaveBeenCalledWith(42)
expect(setPendingEditorRevealMock).toHaveBeenCalledWith(null)
})
it('advertises the browser-open behavior in the html hover hint', () => {
setPlatform('Macintosh')
expect(getTerminalHtmlFileOpenHint()).toBe('⌘+click to open in browser')