From d5a835e8701de7157c4d5c2df1ea37f33f79b6ee Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 29 May 2026 18:54:43 -0700 Subject: [PATCH] fix: cancel editor reveal frames (#3412) --- src/renderer/src/store/slices/editor.test.ts | 47 ++++++++++++++++++++ src/renderer/src/store/slices/editor.ts | 35 ++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/store/slices/editor.test.ts b/src/renderer/src/store/slices/editor.test.ts index ba1008bf8..e652b0f26 100644 --- a/src/renderer/src/store/slices/editor.test.ts +++ b/src/renderer/src/store/slices/editor.test.ts @@ -2012,6 +2012,7 @@ describe('createEditorSlice activateMarkdownLink', () => { }) afterEach(() => { + vi.unstubAllGlobals() vi.useRealTimers() }) @@ -2207,6 +2208,52 @@ describe('createEditorSlice activateMarkdownLink', () => { }) }) + it('cancels superseded line-anchor reveal frames', async () => { + const store = createEditorStore() + pathExistsMock.mockResolvedValue(true) + let nextFrameId = 1 + const pendingFrames = new Map() + const canceledFrameIds = new Set() + vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => { + const frameId = nextFrameId++ + pendingFrames.set(frameId, callback) + return frameId + }) + vi.stubGlobal('cancelAnimationFrame', (frameId: number) => { + canceledFrameIds.add(frameId) + pendingFrames.delete(frameId) + }) + + await store.getState().activateMarkdownLink('./first.md#L3', { + sourceFilePath: '/repo/docs/note.md', + worktreeId: 'wt-1', + worktreeRoot: '/repo' + }) + await store.getState().activateMarkdownLink('./second.md#L9', { + sourceFilePath: '/repo/docs/note.md', + worktreeId: 'wt-1', + worktreeRoot: '/repo' + }) + + expect(canceledFrameIds).toContain(1) + while (pendingFrames.size > 0) { + const nextPendingFrame = pendingFrames.entries().next() + if (nextPendingFrame.done) { + break + } + const [frameId, callback] = nextPendingFrame.value + pendingFrames.delete(frameId) + callback(0) + } + expect(store.getState().pendingEditorReveal).toEqual({ + filePath: '/repo/docs/second.md', + fileId: '/repo/docs/second.md', + line: 9, + column: 1, + matchLength: 0 + }) + }) + it('reveals active-runtime markdown line anchors on the owner-qualified tab id', async () => { const store = createEditorStore() pathExistsMock.mockResolvedValue(true) diff --git a/src/renderer/src/store/slices/editor.ts b/src/renderer/src/store/slices/editor.ts index d162e124b..9fbf6a752 100644 --- a/src/renderer/src/store/slices/editor.ts +++ b/src/renderer/src/store/slices/editor.ts @@ -205,6 +205,36 @@ export type PendingEditorReveal = { matchLength: number } +const pendingEditorLineRevealFrameIds = new Set() + +function cancelPendingEditorLineRevealFrames(): void { + if (typeof cancelAnimationFrame === 'function') { + for (const frameId of pendingEditorLineRevealFrameIds) { + cancelAnimationFrame(frameId) + } + } + pendingEditorLineRevealFrameIds.clear() +} + +function trackEditorLineRevealFrameId(frameId: number): void { + pendingEditorLineRevealFrameIds.add(frameId) +} + +function requestTrackedEditorLineRevealFrame(callback: FrameRequestCallback): void { + let completed = false + let frameId: number | undefined + frameId = requestAnimationFrame((timestamp) => { + completed = true + if (frameId !== undefined) { + pendingEditorLineRevealFrameIds.delete(frameId) + } + callback(timestamp) + }) + if (!completed) { + trackEditorLineRevealFrameId(frameId) + } +} + function scheduleEditorLineReveal( get: () => AppState, filePath: string, @@ -214,9 +244,10 @@ function scheduleEditorLineReveal( ): void { // Why: openFile can replace a preview and remount Monaco asynchronously; the // reveal must land after that remount or the old editor can clear it. + cancelPendingEditorLineRevealFrames() get().setPendingEditorReveal(null) - requestAnimationFrame(() => { - requestAnimationFrame(() => { + requestTrackedEditorLineRevealFrame(() => { + requestTrackedEditorLineRevealFrame(() => { get().setPendingEditorReveal({ filePath, fileId,